Skip to content

Commit bd2ae34

Browse files
DATAMONGO-1271 - Polishing.
Removed non Java 6 language features, reworked and added a few tests. Original Pull Request: #322
1 parent ad6ecc2 commit bd2ae34

File tree

7 files changed

+325
-378
lines changed

7 files changed

+325
-378
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import org.springframework.beans.BeansException;
3030
import org.springframework.context.ApplicationContext;
3131
import org.springframework.context.ApplicationContextAware;
32-
import org.springframework.context.ApplicationEventPublisher;
3332
import org.springframework.core.CollectionFactory;
3433
import org.springframework.core.convert.ConversionException;
3534
import org.springframework.core.convert.ConversionService;
@@ -56,6 +55,7 @@
5655
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
5756
import org.springframework.data.mongodb.core.mapping.event.AfterConvertEvent;
5857
import org.springframework.data.mongodb.core.mapping.event.AfterLoadEvent;
58+
import org.springframework.data.mongodb.core.mapping.event.MongoMappingEvent;
5959
import org.springframework.data.util.ClassTypeInformation;
6060
import org.springframework.data.util.TypeInformation;
6161
import org.springframework.expression.spel.standard.SpelExpressionParser;
@@ -871,7 +871,7 @@ protected DBRef createDBRef(Object target, MongoPersistentProperty property) {
871871
@Override
872872
public Object getValueInternal(MongoPersistentProperty prop, DBObject dbo, SpELExpressionEvaluator evaluator,
873873
ObjectPath path) {
874-
return new MongoDbPropertyValueProvider(dbo, evaluator, path, false).getPropertyValue(prop);
874+
return new MongoDbPropertyValueProvider(dbo, evaluator, path).getPropertyValue(prop);
875875
}
876876

877877
/**
@@ -1104,7 +1104,6 @@ private class MongoDbPropertyValueProvider implements PropertyValueProvider<Mong
11041104
private final DBObjectAccessor source;
11051105
private final SpELExpressionEvaluator evaluator;
11061106
private final ObjectPath path;
1107-
private final boolean ignoreLazyDBRefProperties;
11081107

11091108
/**
11101109
* Creates a new {@link MongoDbPropertyValueProvider} for the given source, {@link SpELExpressionEvaluator} and
@@ -1115,18 +1114,13 @@ private class MongoDbPropertyValueProvider implements PropertyValueProvider<Mong
11151114
* @param path can be {@literal null}.
11161115
*/
11171116
public MongoDbPropertyValueProvider(DBObject source, SpELExpressionEvaluator evaluator, ObjectPath path) {
1118-
this(source, evaluator, path, true); // ignoring by default
1119-
}
11201117

1121-
MongoDbPropertyValueProvider(DBObject source, SpELExpressionEvaluator evaluator, ObjectPath path,
1122-
boolean ignoreLazyDBRefProperties) {
11231118
Assert.notNull(source);
11241119
Assert.notNull(evaluator);
11251120

11261121
this.source = new DBObjectAccessor(source);
11271122
this.evaluator = evaluator;
11281123
this.path = path;
1129-
this.ignoreLazyDBRefProperties = ignoreLazyDBRefProperties;
11301124
}
11311125

11321126
/*
@@ -1141,12 +1135,6 @@ public <T> T getPropertyValue(MongoPersistentProperty property) {
11411135
if (value == null) {
11421136
return null;
11431137
}
1144-
if (this.ignoreLazyDBRefProperties && property.isDbReference() && property.getDBRef().lazy()) { // lazy DBRef,
1145-
// BasicDBList are
1146-
// resolved later
1147-
// by default
1148-
return null;
1149-
}
11501138

11511139
return readValue(value, property.getTypeInformation(), path);
11521140
}
@@ -1208,24 +1196,47 @@ private <T> T readValue(Object value, TypeInformation<?> type, ObjectPath path)
12081196

12091197
@SuppressWarnings("unchecked")
12101198
private <T> T potentiallyReadOrResolveDbRef(DBRef dbref, TypeInformation<?> type, ObjectPath path, Class<?> rawType) {
1199+
12111200
if (rawType.equals(DBRef.class)) {
12121201
return (T) dbref;
12131202
}
1203+
12141204
Object object = dbref == null ? null : path.getPathItem(dbref.getId(), dbref.getCollectionName());
12151205
return (T) (object != null ? object : readAndConvertDBRef(dbref, type, path, rawType));
12161206
}
12171207

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);
12201212
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+
}
12291240
}
12301241

12311242
private boolean canPublishEvent() {
@@ -1252,4 +1263,14 @@ DBObject readRef(DBRef ref) {
12521263
static class NestedDocument {
12531264

12541265
}
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+
12551276
}

0 commit comments

Comments
 (0)