Skip to content

Commit f78b506

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

File tree

7 files changed

+306
-379
lines changed

7 files changed

+306
-379
lines changed

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

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2015 by the original author(s).
2+
* Copyright 2011-2016 by the original author(s).
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -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,39 @@ 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+
if (readRef != null) {
1215+
maybeEmitEvent(new AfterLoadEvent<T>(readRef, (Class<T>) rawType, collectionName));
1216+
}
1217+
1218+
final T target = (T) read(type, readRef, path);
1219+
1220+
if (target != null) {
1221+
maybeEmitEvent(new AfterConvertEvent<T>(readRef, target, collectionName));
1222+
}
1223+
1224+
return target;
1225+
}
1226+
1227+
private void maybeEmitEvent(MongoMappingEvent<?> event) {
1228+
1229+
if (canPublishEvent()) {
1230+
this.applicationContext.publishEvent(event);
1231+
}
12291232
}
12301233

12311234
private boolean canPublishEvent() {

0 commit comments

Comments
 (0)