View Javadoc

1   /**
2    * Copyright (c) 2002-2011 "Neo Technology,"
3    * Network Engine for Objects in Lund AB [http://neotechnology.com]
4    *
5    * This file is part of Neo4j.
6    *
7    * Neo4j is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation, either version 3 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
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 }