Skip to content

Commit 0cf810a

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

File tree

7 files changed

+265
-350
lines changed

7 files changed

+265
-350
lines changed

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

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import org.springframework.beans.BeansException;
3333
import org.springframework.context.ApplicationContext;
3434
import org.springframework.context.ApplicationContextAware;
35-
import org.springframework.context.ApplicationEventPublisher;
3635
import org.springframework.core.CollectionFactory;
3736
import org.springframework.core.convert.ConversionException;
3837
import org.springframework.core.convert.ConversionService;
@@ -59,6 +58,7 @@
5958
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
6059
import org.springframework.data.mongodb.core.mapping.event.AfterConvertEvent;
6160
import org.springframework.data.mongodb.core.mapping.event.AfterLoadEvent;
61+
import org.springframework.data.mongodb.core.mapping.event.MongoMappingEvent;
6262
import org.springframework.data.util.ClassTypeInformation;
6363
import org.springframework.data.util.TypeInformation;
6464
import org.springframework.expression.spel.standard.SpelExpressionParser;
@@ -892,7 +892,7 @@ protected DBRef createDBRef(Object target, MongoPersistentProperty property) {
892892
@Override
893893
public Object getValueInternal(MongoPersistentProperty prop, Bson dbo, SpELExpressionEvaluator evaluator,
894894
ObjectPath path) {
895-
return new MongoDbPropertyValueProvider(dbo, evaluator, path, false).getPropertyValue(prop);
895+
return new MongoDbPropertyValueProvider(dbo, evaluator, path).getPropertyValue(prop);
896896
}
897897

898898
/**
@@ -1218,7 +1218,6 @@ private class MongoDbPropertyValueProvider implements PropertyValueProvider<Mong
12181218
private final DocumentAccessor source;
12191219
private final SpELExpressionEvaluator evaluator;
12201220
private final ObjectPath path;
1221-
private final boolean ignoreLazyDBRefProperties;
12221221

12231222
/**
12241223
* Creates a new {@link MongoDbPropertyValueProvider} for the given source, {@link SpELExpressionEvaluator} and
@@ -1229,18 +1228,13 @@ private class MongoDbPropertyValueProvider implements PropertyValueProvider<Mong
12291228
* @param path can be {@literal null}.
12301229
*/
12311230
public MongoDbPropertyValueProvider(Bson source, SpELExpressionEvaluator evaluator, ObjectPath path) {
1232-
this(source, evaluator, path, true); // ignoring by default
1233-
}
12341231

1235-
MongoDbPropertyValueProvider(Bson source, SpELExpressionEvaluator evaluator, ObjectPath path,
1236-
boolean ignoreLazyDBRefProperties) {
12371232
Assert.notNull(source);
12381233
Assert.notNull(evaluator);
12391234

12401235
this.source = new DocumentAccessor(source);
12411236
this.evaluator = evaluator;
12421237
this.path = path;
1243-
this.ignoreLazyDBRefProperties = ignoreLazyDBRefProperties;
12441238
}
12451239

12461240
/*
@@ -1255,12 +1249,6 @@ public <T> T getPropertyValue(MongoPersistentProperty property) {
12551249
if (value == null) {
12561250
return null;
12571251
}
1258-
if (this.ignoreLazyDBRefProperties && property.isDbReference() && property.getDBRef().lazy()) { // lazy DBRef,
1259-
// BasicDBList are
1260-
// resolved later
1261-
// by default
1262-
return null;
1263-
}
12641252

12651253
return readValue(value, property.getTypeInformation(), path);
12661254
}
@@ -1324,24 +1312,39 @@ private <T> T readValue(Object value, TypeInformation<?> type, ObjectPath path)
13241312

13251313
@SuppressWarnings("unchecked")
13261314
private <T> T potentiallyReadOrResolveDbRef(DBRef dbref, TypeInformation<?> type, ObjectPath path, Class<?> rawType) {
1315+
13271316
if (rawType.equals(DBRef.class)) {
13281317
return (T) dbref;
13291318
}
1319+
13301320
Object object = dbref == null ? null : path.getPathItem(dbref.getId(), dbref.getCollectionName());
13311321
return (T) (object != null ? object : readAndConvertDBRef(dbref, type, path, rawType));
13321322
}
13331323

13341324
private <T> T readAndConvertDBRef(DBRef dbref, TypeInformation<?> type, ObjectPath path, Class<?> rawType) {
1325+
13351326
Document readRef = readRef(dbref);
1327+
13361328
final String collectionName = dbref.getCollectionName();
1337-
if (canPublishEvent())
1338-
((ApplicationEventPublisher) this.applicationContext)
1339-
.publishEvent(new AfterLoadEvent<T>(readRef, (Class<T>) rawType, collectionName));
1340-
T t = (T) read(type, readRef, path);
1341-
if (canPublishEvent())
1342-
((ApplicationEventPublisher) this.applicationContext)
1343-
.publishEvent(new AfterConvertEvent<T>(readRef, t, collectionName));
1344-
return t;
1329+
1330+
if (readRef != null) {
1331+
maybeEmitEvent(new AfterLoadEvent<T>(readRef, (Class<T>) rawType, collectionName));
1332+
}
1333+
1334+
final T target = (T) read(type, readRef, path);
1335+
1336+
if (target != null) {
1337+
maybeEmitEvent(new AfterConvertEvent<T>(readRef, target, collectionName));
1338+
}
1339+
1340+
return target;
1341+
}
1342+
1343+
private void maybeEmitEvent(MongoMappingEvent<?> event) {
1344+
1345+
if (canPublishEvent()) {
1346+
this.applicationContext.publishEvent(event);
1347+
}
13451348
}
13461349

13471350
private boolean canPublishEvent() {

0 commit comments

Comments
 (0)