Skip to content

Commit 7b8dade

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

File tree

7 files changed

+305
-378
lines changed

7 files changed

+305
-378
lines changed

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

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import org.springframework.beans.BeansException;
3131
import org.springframework.context.ApplicationContext;
3232
import org.springframework.context.ApplicationContextAware;
33-
import org.springframework.context.ApplicationEventPublisher;
3433
import org.springframework.core.CollectionFactory;
3534
import org.springframework.core.convert.ConversionException;
3635
import org.springframework.core.convert.ConversionService;
@@ -57,6 +56,7 @@
5756
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
5857
import org.springframework.data.mongodb.core.mapping.event.AfterConvertEvent;
5958
import org.springframework.data.mongodb.core.mapping.event.AfterLoadEvent;
59+
import org.springframework.data.mongodb.core.mapping.event.MongoMappingEvent;
6060
import org.springframework.data.util.ClassTypeInformation;
6161
import org.springframework.data.util.TypeInformation;
6262
import org.springframework.expression.spel.standard.SpelExpressionParser;
@@ -872,7 +872,7 @@ protected DBRef createDBRef(Object target, MongoPersistentProperty property) {
872872
@Override
873873
public Object getValueInternal(MongoPersistentProperty prop, DBObject dbo, SpELExpressionEvaluator evaluator,
874874
ObjectPath path) {
875-
return new MongoDbPropertyValueProvider(dbo, evaluator, path, false).getPropertyValue(prop);
875+
return new MongoDbPropertyValueProvider(dbo, evaluator, path).getPropertyValue(prop);
876876
}
877877

878878
/**
@@ -1116,7 +1116,6 @@ private class MongoDbPropertyValueProvider implements PropertyValueProvider<Mong
11161116
private final DBObjectAccessor source;
11171117
private final SpELExpressionEvaluator evaluator;
11181118
private final ObjectPath path;
1119-
private final boolean ignoreLazyDBRefProperties;
11201119

11211120
/**
11221121
* Creates a new {@link MongoDbPropertyValueProvider} for the given source, {@link SpELExpressionEvaluator} and
@@ -1127,18 +1126,13 @@ private class MongoDbPropertyValueProvider implements PropertyValueProvider<Mong
11271126
* @param path can be {@literal null}.
11281127
*/
11291128
public MongoDbPropertyValueProvider(DBObject source, SpELExpressionEvaluator evaluator, ObjectPath path) {
1130-
this(source, evaluator, path, true); // ignoring by default
1131-
}
11321129

1133-
MongoDbPropertyValueProvider(DBObject source, SpELExpressionEvaluator evaluator, ObjectPath path,
1134-
boolean ignoreLazyDBRefProperties) {
11351130
Assert.notNull(source);
11361131
Assert.notNull(evaluator);
11371132

11381133
this.source = new DBObjectAccessor(source);
11391134
this.evaluator = evaluator;
11401135
this.path = path;
1141-
this.ignoreLazyDBRefProperties = ignoreLazyDBRefProperties;
11421136
}
11431137

11441138
/*
@@ -1153,12 +1147,6 @@ public <T> T getPropertyValue(MongoPersistentProperty property) {
11531147
if (value == null) {
11541148
return null;
11551149
}
1156-
if (this.ignoreLazyDBRefProperties && property.isDbReference() && property.getDBRef().lazy()) { // lazy DBRef,
1157-
// BasicDBList are
1158-
// resolved later
1159-
// by default
1160-
return null;
1161-
}
11621150

11631151
return readValue(value, property.getTypeInformation(), path);
11641152
}
@@ -1220,24 +1208,39 @@ private <T> T readValue(Object value, TypeInformation<?> type, ObjectPath path)
12201208

12211209
@SuppressWarnings("unchecked")
12221210
private <T> T potentiallyReadOrResolveDbRef(DBRef dbref, TypeInformation<?> type, ObjectPath path, Class<?> rawType) {
1211+
12231212
if (rawType.equals(DBRef.class)) {
12241213
return (T) dbref;
12251214
}
1215+
12261216
Object object = dbref == null ? null : path.getPathItem(dbref.getId(), dbref.getCollectionName());
12271217
return (T) (object != null ? object : readAndConvertDBRef(dbref, type, path, rawType));
12281218
}
12291219

1230-
private <T> T readAndConvertDBRef(DBRef dbref, TypeInformation<?> type, ObjectPath path, Class<?> rawType) {
1231-
DBObject readRef = readRef(dbref);
1220+
@SuppressWarnings("unchecked")
1221+
private <T> T readAndConvertDBRef(DBRef dbref, TypeInformation<?> type, ObjectPath path, final Class<?> rawType) {
1222+
1223+
final DBObject readRef = readRef(dbref);
12321224
final String collectionName = dbref.getCollectionName();
1233-
if (canPublishEvent())
1234-
((ApplicationEventPublisher) this.applicationContext)
1235-
.publishEvent(new AfterLoadEvent<T>(readRef, (Class<T>) rawType, collectionName));
1236-
T t = (T) read(type, readRef, path);
1237-
if (canPublishEvent())
1238-
((ApplicationEventPublisher) this.applicationContext)
1239-
.publishEvent(new AfterConvertEvent<T>(readRef, t, collectionName));
1240-
return t;
1225+
1226+
if (readRef != null) {
1227+
maybeEmitEvent(new AfterLoadEvent<T>(readRef, (Class<T>) rawType, collectionName));
1228+
}
1229+
1230+
final T target = (T) read(type, readRef, path);
1231+
1232+
if (target != null) {
1233+
maybeEmitEvent(new AfterConvertEvent<T>(readRef, target, collectionName));
1234+
}
1235+
1236+
return target;
1237+
}
1238+
1239+
private void maybeEmitEvent(MongoMappingEvent<?> event) {
1240+
1241+
if (canPublishEvent()) {
1242+
this.applicationContext.publishEvent(event);
1243+
}
12411244
}
12421245

12431246
private boolean canPublishEvent() {

0 commit comments

Comments
 (0)