Biblioteca Java - Blame information for rev 18
Subversion Repositories:
(root)/Frameworks and Technologies/Neo4J Samples/Neo4JTutorial/src/main/java/com/linkscreens/graphsin/repository/MediaItemNode.java
Rev | Author | Line No. | Line |
---|---|---|---|
18 | mihai | 1 | package com.linkscreens.graphsin.repository; |
2 | |||
3 | import java.util.Collections; | ||
4 | import java.util.Date; | ||
5 | |||
6 | import org.neo4j.graphdb.Direction; | ||
7 | import org.neo4j.graphdb.GraphDatabaseService; | ||
8 | import org.neo4j.graphdb.Node; | ||
9 | import org.neo4j.graphdb.Path; | ||
10 | import org.neo4j.graphdb.Relationship; | ||
11 | import org.neo4j.graphdb.traversal.TraversalDescription; | ||
12 | import org.neo4j.helpers.collection.IterableWrapper; | ||
13 | |||
14 | public class MediaItemNode { | ||
15 | public static final String DESCRIPTION = "description"; | ||
16 | public static final String ID = "id"; | ||
17 | |||
18 | private final Node underlyingNode; | ||
19 | |||
20 | public MediaItemNode( Node album) { | ||
21 | this.underlyingNode = album; | ||
22 | } | ||
23 | |||
24 | private GraphDatabaseService graphDb() | ||
25 | { | ||
26 | return underlyingNode.getGraphDatabase(); | ||
27 | } | ||
28 | |||
29 | protected Node getUnderlyingNode() | ||
30 | { | ||
31 | return underlyingNode; | ||
32 | } | ||
33 | |||
34 | public String getId(){ | ||
35 | return (String)underlyingNode.getProperty( ID ); | ||
36 | } | ||
37 | |||
38 | public String getDescription() | ||
39 | { | ||
40 | return (String)underlyingNode.getProperty( DESCRIPTION ); | ||
41 | } | ||
42 | |||
43 | @Override | ||
44 | public int hashCode() | ||
45 | { | ||
46 | return underlyingNode.hashCode(); | ||
47 | } | ||
48 | |||
49 | @Override | ||
50 | public boolean equals( Object o ) | ||
51 | { | ||
52 | return o instanceof MediaItemNode && | ||
53 | underlyingNode.equals( ( (MediaItemNode)o ).getUnderlyingNode() ); | ||
54 | } | ||
55 | |||
56 | @Override | ||
57 | public String toString() | ||
58 | { | ||
59 | return "MediaItem[" + getDescription() + "]"; | ||
60 | } | ||
61 | |||
62 | //managing statuses | ||
63 | |||
64 | private Node createNewCommentNode( String text ) | ||
65 | { | ||
66 | Node com = graphDb().createNode(); | ||
67 | com.setProperty( StatusUpdateNode.TEXT, text ); | ||
68 | com.setProperty( StatusUpdateNode.DATE, new Date().getTime() ); | ||
69 | return com; | ||
70 | } | ||
71 | |||
72 | public Iterable<CommentNode> getComments() | ||
73 | { | ||
74 | Relationship firstStatus = underlyingNode.getSingleRelationship( | ||
75 | RelTypes.COMMENT, Direction.OUTGOING ); | ||
76 | |||
77 | if ( firstStatus == null ) | ||
78 | { | ||
79 | return Collections.emptyList(); //don't have any comment | ||
80 | } | ||
81 | |||
82 | // START SNIPPET: getStatusTraversal | ||
83 | TraversalDescription traversal = graphDb().traversalDescription() | ||
84 | .depthFirst() | ||
85 | .relationships( RelTypes.NEXT_COMMENT ); | ||
86 | // END SNIPPET: getStatusTraversal | ||
87 | |||
88 | |||
89 | return new IterableWrapper<CommentNode, Path>( | ||
90 | traversal.traverse( firstStatus.getEndNode() ) ) | ||
91 | { | ||
92 | @Override | ||
93 | protected CommentNode underlyingObjectToObject( Path path ) | ||
94 | { | ||
95 | return new CommentNode( path.endNode() ); | ||
96 | } | ||
97 | }; | ||
98 | } | ||
99 | |||
100 | public void addStatus( String text ) | ||
101 | { | ||
102 | CommentNode oldStatus; | ||
103 | if ( getComments().iterator().hasNext() ) | ||
104 | { | ||
105 | oldStatus = getComments().iterator().next(); | ||
106 | } else | ||
107 | { | ||
108 | oldStatus = null; | ||
109 | } | ||
110 | |||
111 | Node newStatus = createNewCommentNode( text ); | ||
112 | |||
113 | if ( oldStatus != null ) | ||
114 | { | ||
115 | underlyingNode.getSingleRelationship( RelTypes.COMMENT, Direction.OUTGOING ).delete(); | ||
116 | newStatus.createRelationshipTo( oldStatus.getUnderlyingNode(), RelTypes.NEXT_COMMENT ); | ||
117 | } | ||
118 | |||
119 | underlyingNode.createRelationshipTo( newStatus, RelTypes.COMMENT ); | ||
120 | } | ||
121 | |||
122 | |||
123 | |||
124 | |||
125 | |||
126 | } |