24
24
import java .util .Map .Entry ;
25
25
import java .util .Optional ;
26
26
import java .util .Set ;
27
- import java .util .function .BiFunction ;
28
27
import java .util .function .Supplier ;
29
28
import java .util .stream .Collectors ;
30
29
34
33
import org .springframework .data .mapping .model .SpELContext ;
35
34
import org .springframework .data .mongodb .core .convert .ReferenceLoader .DocumentReferenceQuery ;
36
35
import org .springframework .data .mongodb .core .convert .ReferenceResolver .LookupFunction ;
37
- import org .springframework .data .mongodb .core .convert .ReferenceResolver .ReferenceContext ;
36
+ import org .springframework .data .mongodb .core .convert .ReferenceResolver .ReferenceCollection ;
38
37
import org .springframework .data .mongodb .core .convert .ReferenceResolver .ResultConversionFunction ;
39
38
import org .springframework .data .mongodb .core .mapping .DocumentReference ;
40
39
import org .springframework .data .mongodb .core .mapping .MongoPersistentEntity ;
43
42
import org .springframework .data .mongodb .util .json .ParameterBindingContext ;
44
43
import org .springframework .data .mongodb .util .json .ParameterBindingDocumentCodec ;
45
44
import org .springframework .data .mongodb .util .json .ValueProvider ;
46
- import org .springframework .data .util .ClassTypeInformation ;
47
45
import org .springframework .data .util .Lazy ;
48
46
import org .springframework .data .util .Streamable ;
49
- import org .springframework .data .util .TypeInformation ;
50
47
import org .springframework .expression .EvaluationContext ;
51
48
import org .springframework .lang .Nullable ;
52
49
import org .springframework .util .StringUtils ;
59
56
*/
60
57
public class ReferenceReader {
61
58
62
- private final ParameterBindingDocumentCodec codec ;
63
-
64
59
private final Lazy <MappingContext <? extends MongoPersistentEntity <?>, MongoPersistentProperty >> mappingContext ;
65
60
private final Supplier <SpELContext > spelContextSupplier ;
61
+ private final ParameterBindingDocumentCodec codec ;
66
62
67
63
public ReferenceReader (MappingContext <? extends MongoPersistentEntity <?>, MongoPersistentProperty > mappingContext ,
68
64
Supplier <SpELContext > spelContextSupplier ) {
@@ -79,18 +75,17 @@ public ReferenceReader(
79
75
this .codec = new ParameterBindingDocumentCodec ();
80
76
}
81
77
82
- Object readReference (MongoPersistentProperty property , Object value ,
83
- LookupFunction lookupFunction ,
78
+ Object readReference (MongoPersistentProperty property , Object value , LookupFunction lookupFunction ,
84
79
ResultConversionFunction resultConversionFunction ) {
85
80
86
81
SpELContext spELContext = spelContextSupplier .get ();
87
82
88
83
DocumentReferenceQuery filter = computeFilter (property , value , spELContext );
89
- ReferenceContext referenceContext = computeReferenceContext (property , value , spELContext );
84
+ ReferenceCollection referenceCollection = computeReferenceContext (property , value , spELContext );
90
85
91
- Iterable <Document > result = lookupFunction .apply (filter , referenceContext );
86
+ Iterable <Document > result = lookupFunction .apply (filter , referenceCollection );
92
87
93
- if (!result .iterator ().hasNext ()) {
88
+ if (!result .iterator ().hasNext ()) {
94
89
return null ;
95
90
}
96
91
@@ -99,18 +94,17 @@ Object readReference(MongoPersistentProperty property, Object value,
99
94
}
100
95
101
96
return resultConversionFunction .apply (result .iterator ().next (), property );
102
-
103
97
}
104
98
105
- private ReferenceContext computeReferenceContext (MongoPersistentProperty property , Object value ,
99
+ private ReferenceCollection computeReferenceContext (MongoPersistentProperty property , Object value ,
106
100
SpELContext spELContext ) {
107
101
108
102
if (value instanceof Iterable ) {
109
103
value = ((Iterable <?>) value ).iterator ().next ();
110
104
}
111
105
112
106
if (value instanceof DBRef ) {
113
- return ReferenceContext .fromDBRef ((DBRef ) value );
107
+ return ReferenceCollection .fromDBRef ((DBRef ) value );
114
108
}
115
109
116
110
if (value instanceof Document ) {
@@ -127,10 +121,10 @@ private ReferenceContext computeReferenceContext(MongoPersistentProperty propert
127
121
String targetCollection = parseValueOrGet (documentReference .collection (), bindingContext ,
128
122
() -> ref .get ("collection" ,
129
123
mappingContext .get ().getPersistentEntity (property .getAssociationTargetType ()).getCollection ()));
130
- return new ReferenceContext (targetDatabase , targetCollection );
124
+ return new ReferenceCollection (targetDatabase , targetCollection );
131
125
}
132
126
133
- return new ReferenceContext (ref .getString ("db" ), ref .get ("collection" ,
127
+ return new ReferenceCollection (ref .getString ("db" ), ref .get ("collection" ,
134
128
mappingContext .get ().getPersistentEntity (property .getAssociationTargetType ()).getCollection ()));
135
129
}
136
130
@@ -143,10 +137,10 @@ private ReferenceContext computeReferenceContext(MongoPersistentProperty propert
143
137
String targetCollection = parseValueOrGet (documentReference .collection (), bindingContext ,
144
138
() -> mappingContext .get ().getPersistentEntity (property .getAssociationTargetType ()).getCollection ());
145
139
146
- return new ReferenceContext (targetDatabase , targetCollection );
140
+ return new ReferenceCollection (targetDatabase , targetCollection );
147
141
}
148
142
149
- return new ReferenceContext (null ,
143
+ return new ReferenceCollection (null ,
150
144
mappingContext .get ().getPersistentEntity (property .getAssociationTargetType ()).getCollection ());
151
145
}
152
146
@@ -252,15 +246,15 @@ public Iterable<Document> apply(MongoCollection<Document> collection) {
252
246
253
247
static class MapDocumentReferenceQuery implements DocumentReferenceQuery {
254
248
255
- Document filter ;
256
- Document sort ;
257
- Map <Object , Document > filterOrderMap ;
249
+ private final Document filter ;
250
+ private final Document sort ;
251
+ private final Map <Object , Document > filterOrderMap ;
258
252
259
253
public MapDocumentReferenceQuery (Document filter , Document sort , Map <Object , Document > filterOrderMap ) {
260
254
261
255
this .filter = filter ;
262
- this .filterOrderMap = filterOrderMap ;
263
256
this .sort = sort ;
257
+ this .filterOrderMap = filterOrderMap ;
264
258
}
265
259
266
260
@ Override
@@ -274,47 +268,46 @@ public Bson getSort() {
274
268
}
275
269
276
270
@ Override
277
- public Iterable <Document > restoreOrder (Iterable <Document > stream ) {
271
+ public Iterable <Document > restoreOrder (Iterable <Document > documents ) {
278
272
279
273
Map <String , Object > targetMap = new LinkedHashMap <>();
280
- List <Document > collected = stream instanceof List ? (List <Document >) stream : Streamable .of (stream ).toList ();
274
+ List <Document > collected = documents instanceof List ? (List <Document >) documents
275
+ : Streamable .of (documents ).toList ();
281
276
282
277
for (Entry <Object , Document > filterMapping : filterOrderMap .entrySet ()) {
283
278
284
- String key = filterMapping .getKey ().toString ();
285
- Optional <Document > first = collected .stream ().filter (it -> {
286
-
287
- boolean found = it .entrySet ().containsAll (filterMapping .getValue ().entrySet ());
288
- return found ;
289
- }).findFirst ();
279
+ Optional <Document > first = collected .stream ()
280
+ .filter (it -> it .entrySet ().containsAll (filterMapping .getValue ().entrySet ())).findFirst ();
290
281
291
- targetMap .put (key , first .orElse (null ));
282
+ targetMap .put (filterMapping . getKey (). toString () , first .orElse (null ));
292
283
}
293
284
return Collections .singleton (new Document (targetMap ));
294
285
}
295
286
}
296
287
297
288
static class ListDocumentReferenceQuery implements DocumentReferenceQuery {
298
289
299
- Document filter ;
300
- Document sort ;
290
+ private final Document filter ;
291
+ private final Document sort ;
301
292
302
293
public ListDocumentReferenceQuery (Document filter , Document sort ) {
294
+
303
295
this .filter = filter ;
304
296
this .sort = sort ;
305
297
}
306
298
307
299
@ Override
308
- public Iterable <Document > restoreOrder (Iterable <Document > stream ) {
300
+ public Iterable <Document > restoreOrder (Iterable <Document > documents ) {
309
301
310
302
if (filter .containsKey ("$or" )) {
311
303
List <Document > ors = filter .get ("$or" , List .class );
312
- List <Document > target = stream instanceof List ? (List <Document >) stream : Streamable .of (stream ).toList ();
304
+ List <Document > target = documents instanceof List ? (List <Document >) documents
305
+ : Streamable .of (documents ).toList ();
313
306
return target .stream ().sorted ((o1 , o2 ) -> compareAgainstReferenceIndex (ors , o1 , o2 ))
314
307
.collect (Collectors .toList ());
315
308
}
316
309
317
- return stream ;
310
+ return documents ;
318
311
}
319
312
320
313
public Document getFilter () {
@@ -340,7 +333,5 @@ int compareAgainstReferenceIndex(List<Document> referenceList, Document document
340
333
}
341
334
return referenceList .size ();
342
335
}
343
-
344
336
}
345
-
346
337
}
0 commit comments