20
20
21
21
package com .arangodb .springframework .core .convert .resolver ;
22
22
23
- import java .util .Arrays ;
24
- import java .util .Map ;
23
+ import java .util .*;
25
24
import java .util .stream .Collectors ;
26
25
27
26
import org .springframework .data .util .TypeInformation ;
@@ -46,48 +45,65 @@ public RelationsResolver(final ArangoOperations template) {
46
45
}
47
46
48
47
@ Override
49
- public Object resolveOne (final String id , final TypeInformation <?> type , final Relations annotation ) {
50
- return annotation .lazy () ? proxy (id , type , annotation , (i , t , a ) -> _resolveOne (i , t , a ))
51
- : _resolveOne (id , type , annotation );
48
+ public Object resolveOne (final String id , final TypeInformation <?> type , final Collection < TypeInformation <?>> traversedTypes , final Relations annotation ) {
49
+ return annotation .lazy () ? proxy (id , type , annotation , (i , t , a ) -> _resolveOne (i , t , traversedTypes , a ))
50
+ : _resolveOne (id , type , traversedTypes , annotation );
52
51
}
53
52
54
53
@ Override
55
- public Object resolveMultiple (final String id , final TypeInformation <?> type , final Relations annotation ) {
56
- return annotation .lazy () ? proxy (id , type , annotation , (i , t , a ) -> _resolveMultiple (i , t , a ))
57
- : _resolveMultiple (id , type , annotation );
54
+ public Object resolveMultiple (final String id , final TypeInformation <?> type , final Collection < TypeInformation <?>> traversedTypes , final Relations annotation ) {
55
+ return annotation .lazy () ? proxy (id , type , annotation , (i , t , a ) -> _resolveMultiple (i , t , traversedTypes , a ))
56
+ : _resolveMultiple (id , type , traversedTypes , annotation );
58
57
}
59
58
60
- private Object _resolveOne (final String id , final TypeInformation <?> type , final Relations annotation ) {
61
- return _resolve (id , type .getType (), annotation , true ).first ();
59
+ private Object _resolveOne (final String id , final TypeInformation <?> type , final Collection <TypeInformation <?>> traversedTypes , final Relations annotation ) {
60
+ Collection <Class <?>> rawTypes = new ArrayList <>();
61
+ for (TypeInformation <?> it : traversedTypes ) {
62
+ rawTypes .add (it .getType ());
63
+ }
64
+ return _resolve (id , type .getType (), rawTypes , annotation , true ).first ();
62
65
}
63
66
64
- private Object _resolveMultiple (final String id , final TypeInformation <?> type , final Relations annotation ) {
65
- return _resolve (id , getNonNullComponentType (type ).getType (), annotation , false ).asListRemaining ();
67
+ private Object _resolveMultiple (final String id , final TypeInformation <?> type , final Collection <TypeInformation <?>> traversedTypes , final Relations annotation ) {
68
+ Collection <Class <?>> rawTypes = new ArrayList <>();
69
+ for (TypeInformation <?> it : traversedTypes ) {
70
+ rawTypes .add (it .getType ());
71
+ }
72
+ return _resolve (id , getNonNullComponentType (type ).getType (), rawTypes , annotation , false ).asListRemaining ();
66
73
}
67
74
68
75
private ArangoCursor <?> _resolve (
69
76
final String id ,
70
77
final Class <?> type ,
78
+ final Collection <Class <?>> traversedTypes ,
71
79
final Relations annotation ,
72
80
final boolean limit ) {
73
81
74
82
final String edges = Arrays .stream (annotation .edges ()).map (e -> template .collection (e ).name ())
75
83
.collect (Collectors .joining ("," ));
76
84
85
+ List <Class <?>> allTraversedTypes = new ArrayList <>();
86
+ allTraversedTypes .add (type );
87
+ allTraversedTypes .addAll (traversedTypes );
88
+
89
+ MapBuilder bindVars = new MapBuilder ().put ("start" , id );
90
+ StringBuilder withClause = new StringBuilder ("WITH " );
91
+ for (int i = 0 ; i < allTraversedTypes .size (); i ++) {
92
+ bindVars .put ("@with" + i , allTraversedTypes .get (i ));
93
+ if (i > 0 ) withClause .append (", " );
94
+ withClause .append ("@@with" ).append (i );
95
+ }
96
+
77
97
final String query = String .format (
78
- "WITH @@vertex FOR v IN %d .. %d %s @start %s OPTIONS {bfs: true, uniqueVertices: \" global\" } %s RETURN v" , //
79
- Math .max (1 , annotation .minDepth ()), //
80
- Math .max (1 , annotation .maxDepth ()), //
81
- annotation .direction (), //
82
- edges , //
83
- limit ? "LIMIT 1" : "" );
84
-
85
- final Map <String , Object > bindVars = new MapBuilder ()//
86
- .put ("start" , id ) //
87
- .put ("@vertex" , type ) //
88
- .get ();
89
-
90
- return template .query (query , bindVars , type );
98
+ "%s FOR v IN %d .. %d %s @start %s OPTIONS {bfs: true, uniqueVertices: \" global\" } %s RETURN v" , //
99
+ withClause , //
100
+ Math .max (1 , annotation .minDepth ()), //
101
+ Math .max (1 , annotation .maxDepth ()), //
102
+ annotation .direction (), //
103
+ edges , //
104
+ limit ? "LIMIT 1" : "" );
105
+
106
+ return template .query (query , bindVars .get (), type );
91
107
}
92
108
93
109
}
0 commit comments