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.Relationship;
23 import org.neo4j.helpers.collection.IterableWrapper;
24
25 public final class RelationshipRepresentation extends ObjectRepresentation implements
26 ExtensibleRepresentation, EntityRepresentation
27 {
28 private final Relationship rel;
29
30 public RelationshipRepresentation( Relationship rel )
31 {
32 super( RepresentationType.RELATIONSHIP );
33 this.rel = rel;
34 }
35
36 @Override
37 public String getIdentity()
38 {
39 return Long.toString( rel.getId() );
40 }
41
42 public long getId()
43 {
44 return rel.getId();
45 }
46
47 @Mapping( "self" )
48 public ValueRepresentation selfUri()
49 {
50 return ValueRepresentation.uri( path( "" ) );
51 }
52
53 private String path( String path )
54 {
55 return "relationship/" + rel.getId() + path;
56 }
57
58 static String path( Relationship rel )
59 {
60 return "relationship/" + rel.getId();
61 }
62
63 @Mapping( "type" )
64 public ValueRepresentation getType()
65 {
66 return ValueRepresentation.relationshipType( rel.getType() );
67 }
68
69 @Mapping( "start" )
70 public ValueRepresentation startNodeUri()
71 {
72 return ValueRepresentation.uri( NodeRepresentation.path( rel.getStartNode() ) );
73 }
74
75 @Mapping( "end" )
76 public ValueRepresentation endNodeUri()
77 {
78 return ValueRepresentation.uri( NodeRepresentation.path( rel.getEndNode() ) );
79 }
80
81 @Mapping( "properties" )
82 public ValueRepresentation propertiesUri()
83 {
84 return ValueRepresentation.uri( path( "/properties" ) );
85 }
86
87 @Mapping( "property" )
88 public ValueRepresentation propertyUriTemplate()
89 {
90 return ValueRepresentation.template( path( "/properties/{key}" ) );
91 }
92
93 @Override
94 void extraData( MappingSerializer serializer )
95 {
96 MappingWriter properties = serializer.writer.newMapping( RepresentationType.PROPERTIES, "data" );
97 new PropertiesRepresentation( rel ).serialize( properties );
98 properties.done();
99 }
100
101 public static ListRepresentation list( Iterable<Relationship> relationships )
102 {
103 return new ListRepresentation( RepresentationType.RELATIONSHIP,
104 new IterableWrapper<Representation, Relationship>( relationships )
105 {
106 @Override
107 protected Representation underlyingObjectToObject( Relationship relationship )
108 {
109 return new RelationshipRepresentation( relationship );
110 }
111 } );
112 }
113 }