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.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 }