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.graphdb.Path;
    24  import org.neo4j.graphdb.Relationship;
    25  import org.neo4j.helpers.collection.IterableWrapper;
    26
    27  public class PathRepresentation<P extends Path> extends ObjectRepresentation //implements ExtensibleRepresentation
    28  {
    29      private final P path;
    30
    31      public PathRepresentation( P path )
    32      {
    33          super( RepresentationType.PATH );
    34          this.path = path;
    35      }
    36
    37      /*
    38      @Override
    39      public String getIdentity()
    40      {
    41          StringBuilder result = new StringBuilder( Long.toString( path.startNode().getId() ) );
    42          String sep = "+";
    43          for ( Relationship rel : path.relationships() )
    44          {
    45              result.append( sep ).append( Long.toString( rel.getId() ) );
    46              sep = "-";
    47          }
    48          result.append( "+" ).append( Long.toString( path.endNode().getId() ) );
    49          return result.toString();
    50      }
    51      */
    52
    53      protected P getPath()
    54      {
    55          return path;
    56      }
    57
    58      @Mapping( "start" )
    59      public ValueRepresentation startNode()
    60      {
    61          return ValueRepresentation.uri( NodeRepresentation.path( path.startNode() ) );
    62      }
    63
    64      @Mapping( "end" )
    65      public ValueRepresentation endNode()
    66      {
    67          return ValueRepresentation.uri( NodeRepresentation.path( path.endNode() ) );
    68      }
    69
    70      @Mapping( "length" )
    71      public ValueRepresentation length()
    72      {
    73          return ValueRepresentation.number( path.length() );
    74      }
    75
    76      @Mapping( "nodes" )
    77      public ListRepresentation nodes()
    78      {
    79          return new ListRepresentation( RepresentationType.NODE, new IterableWrapper<Representation, Node>(
    80                  path.nodes() )
    81          {
    82              @Override
    83              protected Representation underlyingObjectToObject( Node node )
    84              {
    85                  return ValueRepresentation.uri( NodeRepresentation.path( node ) );
    86              }
    87          } );
    88      }
    89
    90      @Mapping( "relationships" )
    91      public ListRepresentation relationships()
    92      {
    93          return new ListRepresentation( RepresentationType.RELATIONSHIP,
    94                  new IterableWrapper<Representation, Relationship>( path.relationships() )
    95                  {
    96                      @Override
    97                      protected Representation underlyingObjectToObject( Relationship node )
    98                      {
    99                          return ValueRepresentation.uri( RelationshipRepresentation.path( node ) );
    100                     }
    101                 } );
    102     }
    103 }