29
29
import org .springframework .beans .BeansException ;
30
30
import org .springframework .context .ApplicationContext ;
31
31
import org .springframework .context .ApplicationContextAware ;
32
- import org .springframework .context .ApplicationEventPublisher ;
33
32
import org .springframework .core .CollectionFactory ;
34
33
import org .springframework .core .convert .ConversionException ;
35
34
import org .springframework .core .convert .ConversionService ;
56
55
import org .springframework .data .mongodb .core .mapping .MongoPersistentProperty ;
57
56
import org .springframework .data .mongodb .core .mapping .event .AfterConvertEvent ;
58
57
import org .springframework .data .mongodb .core .mapping .event .AfterLoadEvent ;
58
+ import org .springframework .data .mongodb .core .mapping .event .MongoMappingEvent ;
59
59
import org .springframework .data .util .ClassTypeInformation ;
60
60
import org .springframework .data .util .TypeInformation ;
61
61
import org .springframework .expression .spel .standard .SpelExpressionParser ;
@@ -871,7 +871,7 @@ protected DBRef createDBRef(Object target, MongoPersistentProperty property) {
871
871
@ Override
872
872
public Object getValueInternal (MongoPersistentProperty prop , DBObject dbo , SpELExpressionEvaluator evaluator ,
873
873
ObjectPath path ) {
874
- return new MongoDbPropertyValueProvider (dbo , evaluator , path , false ).getPropertyValue (prop );
874
+ return new MongoDbPropertyValueProvider (dbo , evaluator , path ).getPropertyValue (prop );
875
875
}
876
876
877
877
/**
@@ -1104,7 +1104,6 @@ private class MongoDbPropertyValueProvider implements PropertyValueProvider<Mong
1104
1104
private final DBObjectAccessor source ;
1105
1105
private final SpELExpressionEvaluator evaluator ;
1106
1106
private final ObjectPath path ;
1107
- private final boolean ignoreLazyDBRefProperties ;
1108
1107
1109
1108
/**
1110
1109
* Creates a new {@link MongoDbPropertyValueProvider} for the given source, {@link SpELExpressionEvaluator} and
@@ -1115,18 +1114,13 @@ private class MongoDbPropertyValueProvider implements PropertyValueProvider<Mong
1115
1114
* @param path can be {@literal null}.
1116
1115
*/
1117
1116
public MongoDbPropertyValueProvider (DBObject source , SpELExpressionEvaluator evaluator , ObjectPath path ) {
1118
- this (source , evaluator , path , true ); // ignoring by default
1119
- }
1120
1117
1121
- MongoDbPropertyValueProvider (DBObject source , SpELExpressionEvaluator evaluator , ObjectPath path ,
1122
- boolean ignoreLazyDBRefProperties ) {
1123
1118
Assert .notNull (source );
1124
1119
Assert .notNull (evaluator );
1125
1120
1126
1121
this .source = new DBObjectAccessor (source );
1127
1122
this .evaluator = evaluator ;
1128
1123
this .path = path ;
1129
- this .ignoreLazyDBRefProperties = ignoreLazyDBRefProperties ;
1130
1124
}
1131
1125
1132
1126
/*
@@ -1141,12 +1135,6 @@ public <T> T getPropertyValue(MongoPersistentProperty property) {
1141
1135
if (value == null ) {
1142
1136
return null ;
1143
1137
}
1144
- if (this .ignoreLazyDBRefProperties && property .isDbReference () && property .getDBRef ().lazy ()) { // lazy DBRef,
1145
- // BasicDBList are
1146
- // resolved later
1147
- // by default
1148
- return null ;
1149
- }
1150
1138
1151
1139
return readValue (value , property .getTypeInformation (), path );
1152
1140
}
@@ -1208,24 +1196,47 @@ private <T> T readValue(Object value, TypeInformation<?> type, ObjectPath path)
1208
1196
1209
1197
@ SuppressWarnings ("unchecked" )
1210
1198
private <T > T potentiallyReadOrResolveDbRef (DBRef dbref , TypeInformation <?> type , ObjectPath path , Class <?> rawType ) {
1199
+
1211
1200
if (rawType .equals (DBRef .class )) {
1212
1201
return (T ) dbref ;
1213
1202
}
1203
+
1214
1204
Object object = dbref == null ? null : path .getPathItem (dbref .getId (), dbref .getCollectionName ());
1215
1205
return (T ) (object != null ? object : readAndConvertDBRef (dbref , type , path , rawType ));
1216
1206
}
1217
1207
1218
- private <T > T readAndConvertDBRef (DBRef dbref , TypeInformation <?> type , ObjectPath path , Class <?> rawType ) {
1219
- DBObject readRef = readRef (dbref );
1208
+ @ SuppressWarnings ("unchecked" )
1209
+ private <T > T readAndConvertDBRef (DBRef dbref , TypeInformation <?> type , ObjectPath path , final Class <?> rawType ) {
1210
+
1211
+ final DBObject readRef = readRef (dbref );
1220
1212
final String collectionName = dbref .getCollectionName ();
1221
- if (canPublishEvent ())
1222
- ((ApplicationEventPublisher ) this .applicationContext )
1223
- .publishEvent (new AfterLoadEvent <T >(readRef , (Class <T >) rawType , collectionName ));
1224
- T t = (T ) read (type , readRef , path );
1225
- if (canPublishEvent ())
1226
- ((ApplicationEventPublisher ) this .applicationContext )
1227
- .publishEvent (new AfterConvertEvent <T >(readRef , t , collectionName ));
1228
- return t ;
1213
+
1214
+ maybeEmitEvent (new MappingEventSupplier <T >() {
1215
+
1216
+ @ Override
1217
+ public MongoMappingEvent <T > get () {
1218
+ return (MongoMappingEvent <T >) new AfterLoadEvent <T >(readRef , (Class <T >) rawType , collectionName );
1219
+ }
1220
+ });
1221
+
1222
+ final T target = (T ) read (type , readRef , path );
1223
+
1224
+ maybeEmitEvent (new MappingEventSupplier <T >() {
1225
+
1226
+ @ Override
1227
+ public MongoMappingEvent <T > get () {
1228
+ return (MongoMappingEvent <T >) new AfterConvertEvent <T >(readRef , target , collectionName );
1229
+ }
1230
+ });
1231
+
1232
+ return target ;
1233
+ }
1234
+
1235
+ private <T > void maybeEmitEvent (MappingEventSupplier <T > event ) {
1236
+
1237
+ if (canPublishEvent ()) {
1238
+ this .applicationContext .publishEvent (event .get ());
1239
+ }
1229
1240
}
1230
1241
1231
1242
private boolean canPublishEvent () {
@@ -1252,4 +1263,14 @@ DBObject readRef(DBRef ref) {
1252
1263
static class NestedDocument {
1253
1264
1254
1265
}
1266
+
1267
+ /**
1268
+ * @author Christoph Strobl
1269
+ * @param <T>
1270
+ * @since 1.10
1271
+ */
1272
+ private static interface MappingEventSupplier <T > {
1273
+ MongoMappingEvent <T > get ();
1274
+ }
1275
+
1255
1276
}
0 commit comments