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.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
33
34
35
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
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
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 }