1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.neo4j.server.rest.repr;
21
22 import org.neo4j.graphdb.Node;
23 import org.neo4j.helpers.collection.IterableWrapper;
24
25 public final class NodeRepresentation extends ObjectRepresentation implements
26 ExtensibleRepresentation, EntityRepresentation
27 {
28 private final Node node;
29
30 public NodeRepresentation( Node node )
31 {
32 super( RepresentationType.NODE );
33 this.node = node;
34 }
35
36 @Override
37 public String getIdentity()
38 {
39 return Long.toString( node.getId() );
40 }
41
42 @Mapping( "self" )
43 public ValueRepresentation selfUri()
44 {
45 return ValueRepresentation.uri( path( "" ) );
46 }
47
48 public long getId()
49 {
50 return node.getId();
51 }
52
53 private String path( String path )
54 {
55 return "node/" + node.getId() + path;
56 }
57
58 static String path( Node node )
59 {
60 return "node/" + node.getId();
61 }
62
63 @Mapping( "create_relationship" )
64 public ValueRepresentation relationshipCreationUri()
65 {
66 return ValueRepresentation.uri( path( "/relationships" ) );
67 }
68
69 @Mapping( "all_relationships" )
70 public ValueRepresentation allRelationshipsUri()
71 {
72 return ValueRepresentation.uri( path( "/relationships/all" ) );
73 }
74
75 @Mapping( "incoming_relationships" )
76 public ValueRepresentation incomingRelationshipsUri()
77 {
78 return ValueRepresentation.uri( path( "/relationships/in" ) );
79 }
80
81 @Mapping( "outgoing_relationships" )
82 public ValueRepresentation outgoingRelationshipsUri()
83 {
84 return ValueRepresentation.uri( path( "/relationships/out" ) );
85 }
86
87 @Mapping( "all_typed_relationships" )
88 public ValueRepresentation allTypedRelationshipsUriTemplate()
89 {
90 return ValueRepresentation.template( path( "/relationships/all/{-list|&|types}" ) );
91 }
92
93 @Mapping( "incoming_typed_relationships" )
94 public ValueRepresentation incomingTypedRelationshipsUriTemplate()
95 {
96 return ValueRepresentation.template( path( "/relationships/in/{-list|&|types}" ) );
97 }
98
99 @Mapping( "outgoing_typed_relationships" )
100 public ValueRepresentation outgoingTypedRelationshipsUriTemplate()
101 {
102 return ValueRepresentation.template( path( "/relationships/out/{-list|&|types}" ) );
103 }
104
105 @Mapping( "properties" )
106 public ValueRepresentation propertiesUri()
107 {
108 return ValueRepresentation.uri( path( "/properties" ) );
109 }
110
111 @Mapping( "property" )
112 public ValueRepresentation propertyUriTemplate()
113 {
114 return ValueRepresentation.template( path( "/properties/{key}" ) );
115 }
116
117 @Mapping( "traverse" )
118 public ValueRepresentation traverseUriTemplate()
119 {
120 return ValueRepresentation.template( path( "/traverse/{returnType}" ) );
121 }
122
123 @Override
124 void extraData( MappingSerializer serializer )
125 {
126 MappingWriter writer = serializer.writer;
127 MappingWriter properties = writer.newMapping( RepresentationType.PROPERTIES, "data" );
128 new PropertiesRepresentation( node ).serialize( properties );
129 if ( writer.isInteractive() )
130 {
131 serializer.putList(
132 "relationship_types",
133 ListRepresentation.relationshipTypes( node.getGraphDatabase().getRelationshipTypes() ) );
134 }
135 properties.done();
136 }
137
138 public static ListRepresentation list( Iterable<Node> nodes )
139 {
140 return new ListRepresentation( RepresentationType.NODE,
141 new IterableWrapper<Representation, Node>( nodes )
142 {
143 @Override
144 protected Representation underlyingObjectToObject( Node node )
145 {
146 return new NodeRepresentation( node );
147 }
148 } );
149 }
150 }