28
28
import org .bson .conversions .Bson ;
29
29
import org .slf4j .Logger ;
30
30
import org .slf4j .LoggerFactory ;
31
+
31
32
import org .springframework .beans .BeansException ;
32
33
import org .springframework .context .ApplicationContext ;
33
34
import org .springframework .context .ApplicationContextAware ;
46
47
import org .springframework .data .geo .GeoResult ;
47
48
import org .springframework .data .geo .GeoResults ;
48
49
import org .springframework .data .geo .Metric ;
50
+ import org .springframework .data .mapping .MappingException ;
49
51
import org .springframework .data .mapping .callback .EntityCallbacks ;
50
52
import org .springframework .data .mapping .context .MappingContext ;
51
53
import org .springframework .data .mongodb .MongoDatabaseFactory ;
102
104
import org .springframework .data .projection .SpelAwareProxyProjectionFactory ;
103
105
import org .springframework .data .util .CloseableIterator ;
104
106
import org .springframework .data .util .Optionals ;
105
- import org .springframework .jca .cci .core .ConnectionCallback ;
106
107
import org .springframework .lang .Nullable ;
107
108
import org .springframework .util .Assert ;
108
109
import org .springframework .util .ClassUtils ;
@@ -972,7 +973,7 @@ public <T> GeoResults<T> geoNear(NearQuery near, Class<?> domainType, String col
972
973
for (Document element : results ) {
973
974
974
975
GeoResult <T > geoResult = callback .doWith (element );
975
- aggregate = aggregate .add (new BigDecimal (geoResult .getDistance ().getValue ()));
976
+ aggregate = aggregate .add (BigDecimal . valueOf (geoResult .getDistance ().getValue ()));
976
977
result .add (geoResult );
977
978
}
978
979
@@ -2751,25 +2752,24 @@ private MongoCollection<Document> getAndPrepareCollection(MongoDatabase db, Stri
2751
2752
* Internal method using callbacks to do queries against the datastore that requires reading a single object from a
2752
2753
* collection of objects. It will take the following steps
2753
2754
* <ol>
2754
- * <li>Execute the given {@link ConnectionCallback } for a {@link Document}.</li>
2755
+ * <li>Execute the given {@link CollectionCallback } for a {@link Document}.</li>
2755
2756
* <li>Apply the given {@link DocumentCallback} to each of the {@link Document}s to obtain the result.</li>
2756
2757
* <ol>
2757
2758
*
2758
2759
* @param <T>
2759
2760
* @param collectionCallback the callback to retrieve the {@link Document} with
2760
- * @param objectCallback the {@link DocumentCallback} to transform {@link Document}s into the actual domain type
2761
+ * @param documentCallback the {@link DocumentCallback} to transform {@link Document}s into the actual domain type
2761
2762
* @param collectionName the collection to be queried
2762
2763
* @return
2763
2764
*/
2764
2765
@ Nullable
2765
2766
private <T > T executeFindOneInternal (CollectionCallback <Document > collectionCallback ,
2766
- DocumentCallback <T > objectCallback , String collectionName ) {
2767
+ DocumentCallback <T > documentCallback , String collectionName ) {
2767
2768
2768
2769
try {
2769
2770
2770
- T result = objectCallback
2771
- .doWith (collectionCallback .doInCollection (getAndPrepareCollection (doGetDatabase (), collectionName )));
2772
- return result ;
2771
+ Document document = collectionCallback .doInCollection (getAndPrepareCollection (doGetDatabase (), collectionName ));
2772
+ return document != null ? documentCallback .doWith (document ) : null ;
2773
2773
} catch (RuntimeException e ) {
2774
2774
throw potentiallyConvertRuntimeException (e , exceptionTranslator );
2775
2775
}
@@ -2779,7 +2779,7 @@ private <T> T executeFindOneInternal(CollectionCallback<Document> collectionCall
2779
2779
* Internal method using callback to do queries against the datastore that requires reading a collection of objects.
2780
2780
* It will take the following steps
2781
2781
* <ol>
2782
- * <li>Execute the given {@link ConnectionCallback } for a {@link FindIterable}.</li>
2782
+ * <li>Execute the given {@link CollectionCallback } for a {@link FindIterable}.</li>
2783
2783
* <li>Prepare that {@link FindIterable} with the given {@link CursorPreparer} (will be skipped if
2784
2784
* {@link CursorPreparer} is {@literal null}</li>
2785
2785
* <li>Iterate over the {@link FindIterable} and applies the given {@link DocumentCallback} to each of the
@@ -2789,36 +2789,27 @@ private <T> T executeFindOneInternal(CollectionCallback<Document> collectionCall
2789
2789
* @param <T>
2790
2790
* @param collectionCallback the callback to retrieve the {@link FindIterable} with
2791
2791
* @param preparer the {@link CursorPreparer} to potentially modify the {@link FindIterable} before iterating over it
2792
- * @param objectCallback the {@link DocumentCallback} to transform {@link Document}s into the actual domain type
2792
+ * @param documentCallback the {@link DocumentCallback} to transform {@link Document}s into the actual domain type
2793
2793
* @param collectionName the collection to be queried
2794
2794
* @return
2795
2795
*/
2796
2796
private <T > List <T > executeFindMultiInternal (CollectionCallback <FindIterable <Document >> collectionCallback ,
2797
- CursorPreparer preparer , DocumentCallback <T > objectCallback , String collectionName ) {
2797
+ CursorPreparer preparer , DocumentCallback <T > documentCallback , String collectionName ) {
2798
2798
2799
2799
try {
2800
2800
2801
- MongoCursor <Document > cursor = null ;
2802
-
2803
- try {
2804
-
2805
- cursor = preparer
2806
- .initiateFind (getAndPrepareCollection (doGetDatabase (), collectionName ), collectionCallback ::doInCollection )
2807
- .iterator ();
2801
+ try (MongoCursor <Document > cursor = preparer
2802
+ .initiateFind (getAndPrepareCollection (doGetDatabase (), collectionName ), collectionCallback ::doInCollection )
2803
+ .iterator ()) {
2808
2804
2809
2805
List <T > result = new ArrayList <>();
2810
2806
2811
2807
while (cursor .hasNext ()) {
2812
2808
Document object = cursor .next ();
2813
- result .add (objectCallback .doWith (object ));
2809
+ result .add (documentCallback .doWith (object ));
2814
2810
}
2815
2811
2816
2812
return result ;
2817
- } finally {
2818
-
2819
- if (cursor != null ) {
2820
- cursor .close ();
2821
- }
2822
2813
}
2823
2814
} catch (RuntimeException e ) {
2824
2815
throw potentiallyConvertRuntimeException (e , exceptionTranslator );
@@ -2828,24 +2819,13 @@ private <T> List<T> executeFindMultiInternal(CollectionCallback<FindIterable<Doc
2828
2819
private void executeQueryInternal (CollectionCallback <FindIterable <Document >> collectionCallback ,
2829
2820
CursorPreparer preparer , DocumentCallbackHandler callbackHandler , String collectionName ) {
2830
2821
2831
- try {
2832
-
2833
- MongoCursor <Document > cursor = null ;
2834
-
2835
- try {
2836
-
2837
- cursor = preparer
2838
- .initiateFind (getAndPrepareCollection (doGetDatabase (), collectionName ), collectionCallback ::doInCollection )
2839
- .iterator ();
2822
+ try (MongoCursor <Document > cursor = preparer
2823
+ .initiateFind (getAndPrepareCollection (doGetDatabase (), collectionName ), collectionCallback ::doInCollection )
2824
+ .iterator ()) {
2840
2825
2841
2826
while (cursor .hasNext ()) {
2842
2827
callbackHandler .processDocument (cursor .next ());
2843
2828
}
2844
- } finally {
2845
- if (cursor != null ) {
2846
- cursor .close ();
2847
- }
2848
- }
2849
2829
} catch (RuntimeException e ) {
2850
2830
throw potentiallyConvertRuntimeException (e , exceptionTranslator );
2851
2831
}
@@ -3143,8 +3123,7 @@ public Document doInCollection(MongoCollection<Document> collection) throws Mong
3143
3123
3144
3124
interface DocumentCallback <T > {
3145
3125
3146
- @ Nullable
3147
- T doWith (@ Nullable Document object );
3126
+ T doWith (Document object );
3148
3127
}
3149
3128
3150
3129
/**
@@ -3168,22 +3147,19 @@ private class ReadDocumentCallback<T> implements DocumentCallback<T> {
3168
3147
this .collectionName = collectionName ;
3169
3148
}
3170
3149
3171
- @ Nullable
3172
- public T doWith (@ Nullable Document document ) {
3173
-
3174
- T source = null ;
3150
+ public T doWith (Document document ) {
3175
3151
3176
- if (document != null ) {
3177
3152
maybeEmitEvent (new AfterLoadEvent <>(document , type , collectionName ));
3178
- source = reader .read (type , document );
3179
- }
3153
+ T entity = reader .read (type , document );
3180
3154
3181
- if (source != null ) {
3182
- maybeEmitEvent (new AfterConvertEvent <>(document , source , collectionName ));
3183
- source = maybeCallAfterConvert (source , document , collectionName );
3184
- }
3155
+ if (entity == null ) {
3156
+ throw new MappingException (String .format ("EntityReader %s returned null" , reader ));
3157
+ }
3185
3158
3186
- return source ;
3159
+ maybeEmitEvent (new AfterConvertEvent <>(document , entity , collectionName ));
3160
+ entity = maybeCallAfterConvert (entity , document , collectionName );
3161
+
3162
+ return entity ;
3187
3163
}
3188
3164
}
3189
3165
@@ -3216,8 +3192,7 @@ private class ProjectingReadCallback<S, T> implements DocumentCallback<T> {
3216
3192
* @see org.springframework.data.mongodb.core.MongoTemplate.DocumentCallback#doWith(org.bson.Document)
3217
3193
*/
3218
3194
@ SuppressWarnings ("unchecked" )
3219
- @ Nullable
3220
- public T doWith (@ Nullable Document document ) {
3195
+ public T doWith (Document document ) {
3221
3196
3222
3197
if (document == null ) {
3223
3198
return null ;
@@ -3228,15 +3203,16 @@ public T doWith(@Nullable Document document) {
3228
3203
3229
3204
maybeEmitEvent (new AfterLoadEvent <>(document , targetType , collectionName ));
3230
3205
3231
- Object source = reader .read (typeToRead , document );
3232
- Object result = targetType .isInterface () ? projectionFactory .createProjection (targetType , source ) : source ;
3206
+ Object entity = reader .read (typeToRead , document );
3233
3207
3234
- if (result != null ) {
3235
- maybeEmitEvent (new AfterConvertEvent <>(document , result , collectionName ));
3236
- result = maybeCallAfterConvert (result , document , collectionName );
3208
+ if (entity == null ) {
3209
+ throw new MappingException (String .format ("EntityReader %s returned null" , reader ));
3237
3210
}
3238
3211
3239
- return (T ) result ;
3212
+ Object result = targetType .isInterface () ? projectionFactory .createProjection (targetType , entity ) : entity ;
3213
+
3214
+ maybeEmitEvent (new AfterConvertEvent <>(document , result , collectionName ));
3215
+ return (T ) maybeCallAfterConvert (result , document , collectionName );
3240
3216
}
3241
3217
}
3242
3218
@@ -3373,8 +3349,7 @@ static class GeoNearResultDocumentCallback<T> implements DocumentCallback<GeoRes
3373
3349
this .metric = metric ;
3374
3350
}
3375
3351
3376
- @ Nullable
3377
- public GeoResult <T > doWith (@ Nullable Document object ) {
3352
+ public GeoResult <T > doWith (Document object ) {
3378
3353
3379
3354
double distance = Double .NaN ;
3380
3355
if (object .containsKey (distanceField )) {
@@ -3401,10 +3376,6 @@ static class CloseableIterableCursorAdapter<T> implements CloseableIterator<T> {
3401
3376
3402
3377
/**
3403
3378
* Creates a new {@link CloseableIterableCursorAdapter} backed by the given {@link MongoCollection}.
3404
- *
3405
- * @param cursor
3406
- * @param exceptionTranslator
3407
- * @param objectReadCallback
3408
3379
*/
3409
3380
CloseableIterableCursorAdapter (MongoIterable <Document > cursor , PersistenceExceptionTranslator exceptionTranslator ,
3410
3381
DocumentCallback <T > objectReadCallback ) {
@@ -3448,8 +3419,7 @@ public T next() {
3448
3419
3449
3420
try {
3450
3421
Document item = cursor .next ();
3451
- T converted = objectReadCallback .doWith (item );
3452
- return converted ;
3422
+ return objectReadCallback .doWith (item );
3453
3423
} catch (RuntimeException ex ) {
3454
3424
throw potentiallyConvertRuntimeException (ex , exceptionTranslator );
3455
3425
}
0 commit comments