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.domain;
    21
    22  import java.util.Arrays;
    23  import java.util.Collection;
    24  import java.util.Map;
    25
    26  import org.neo4j.graphdb.DynamicRelationshipType;
    27  import org.neo4j.graphdb.RelationshipType;
    28  import org.neo4j.graphdb.RelationshipExpander;
    29  import org.neo4j.graphdb.Expander;
    30  import org.neo4j.kernel.Traversal;
    31
    32  // import org.neo4j.graphdb.Traverser.Order;
    33  // import org.neo4j.graphdb.traversal.TraversalDescription;
    34  // import org.neo4j.graphdb.traversal.Uniqueness;
    35  // import org.neo4j.kernel.Traversal;
    36
    37  public class RelationshipExpanderBuilder
    38  {
    39
    40      @SuppressWarnings("unchecked")
    41      public static RelationshipExpander describeRelationships(
    42              Map<String, Object> description )
    43      {
    44      	Expander expander = Traversal.emptyExpander();
    45
    46          Object relationshipsDescription = description.get( "relationships" );
    47          if ( relationshipsDescription != null )
    48          {
    49              Collection<Object> pairDescriptions;
    50              if ( relationshipsDescription instanceof Collection )
    51              {
    52                  pairDescriptions = (Collection<Object>) relationshipsDescription;
    53              }
    54              else
    55              {
    56                  pairDescriptions = Arrays.asList( relationshipsDescription );
    57              }
    58
    59              for ( Object pairDescription : pairDescriptions )
    60              {
    61                  Map map = (Map) pairDescription;
    62                  String name = (String) map.get( "type" );
    63                  RelationshipType type = DynamicRelationshipType.withName( name );
    64                  String directionName = (String) map.get( "direction" );
    65                  expander = ( directionName == null ) ? expander.add( type ) :
    66                  	expander.add( type, stringToEnum( directionName,
    67                          RelationshipDirection.class, true ).internal );
    68              }
    69          }
    70          return expander;
    71      }
    72
    73
    74      // TODO Refactor - same method exists in TraversalDescriptionBuilder
    75      private static <T extends Enum<T>> T stringToEnum( String name, Class<T> enumClass,
    76              boolean fuzzyMatch )
    77      {
    78          if ( name == null )
    79          {
    80              return null;
    81          }
    82
    83  //        name = enumifyName( name );
    84          for ( T candidate : enumClass.getEnumConstants() )
    85          {
    86              if ( candidate.name().equals( name ) )
    87              {
    88                  return candidate;
    89              }
    90          }
    91          if ( fuzzyMatch )
    92          {
    93              for ( T candidate : enumClass.getEnumConstants() )
    94              {
    95                  if ( candidate.name().startsWith( name ) )
    96                  {
    97                      return candidate;
    98                  }
    99              }
    100         }
    101         throw new RuntimeException( "Unregognized " + enumClass.getSimpleName() + " '" +
    102                 name + "'" );
    103     }
    104
    105 }