Skip to content

Commit 9d0c8ec

Browse files
committed
DATAMONGO-1245 - Polishing.
Adapt to API changes in Spring Data Commons. Related tickets: DATACMNS-810. Original pull request: #341.
1 parent 5a78d99 commit 9d0c8ec

File tree

8 files changed

+174
-202
lines changed

8 files changed

+174
-202
lines changed

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

-19
Original file line numberDiff line numberDiff line change
@@ -636,25 +636,6 @@ public <T> T findById(Object id, Class<T> entityClass, String collectionName) {
636636
return doFindOne(collectionName, new BasicDBObject(idKey, id), null, entityClass);
637637
}
638638

639-
/*
640-
* (non-Javadoc)
641-
* @see org.springframework.data.mongodb.core.MongoOperations#findByExample(java.lang.Object)
642-
*/
643-
public <S extends T, T> List<T> findByExample(S sample) {
644-
return findByExample(Example.of(sample));
645-
}
646-
647-
/*
648-
* (non-Javadoc)
649-
* @see org.springframework.data.mongodb.core.MongoOperations#findByExample(org.springframework.data.domain.Example)
650-
*/
651-
@SuppressWarnings("unchecked")
652-
public <S extends T, T> List<T> findByExample(Example<S> sample) {
653-
654-
Assert.notNull(sample, "Sample object must not be null!");
655-
return (List<T>) find(new Query(new Criteria().alike(sample)), sample.getResultType());
656-
}
657-
658639
public <T> GeoResults<T> geoNear(NearQuery near, Class<T> entityClass) {
659640
return geoNear(near, entityClass, determineCollectionName(entityClass));
660641
}

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

+38-23
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818
import java.util.ArrayList;
1919
import java.util.Arrays;
20-
import java.util.Collections;
2120
import java.util.HashMap;
21+
import java.util.HashSet;
2222
import java.util.Iterator;
2323
import java.util.List;
2424
import java.util.Map;
@@ -27,20 +27,20 @@
2727
import java.util.Stack;
2828
import java.util.regex.Pattern;
2929

30-
import org.bson.BasicBSONObject;
3130
import org.springframework.data.domain.Example;
32-
import org.springframework.data.domain.ExampleSpec;
33-
import org.springframework.data.domain.ExampleSpec.NullHandler;
34-
import org.springframework.data.domain.ExampleSpec.PropertyValueTransformer;
35-
import org.springframework.data.domain.ExampleSpec.StringMatcher;
31+
import org.springframework.data.domain.ExampleMatcher.NullHandler;
32+
import org.springframework.data.domain.ExampleMatcher.PropertyValueTransformer;
33+
import org.springframework.data.domain.ExampleMatcher.StringMatcher;
3634
import org.springframework.data.mapping.PropertyHandler;
3735
import org.springframework.data.mapping.context.MappingContext;
3836
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
3937
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
4038
import org.springframework.data.mongodb.core.query.MongoRegexCreator;
4139
import org.springframework.data.mongodb.core.query.SerializationUtils;
42-
import org.springframework.data.repository.core.support.ExampleSpecAccessor;
40+
import org.springframework.data.repository.core.support.ExampleMatcherAccessor;
4341
import org.springframework.data.repository.query.parser.Part.Type;
42+
import org.springframework.data.util.TypeInformation;
43+
import org.springframework.util.Assert;
4444
import org.springframework.util.ObjectUtils;
4545
import org.springframework.util.StringUtils;
4646

@@ -74,43 +74,59 @@ public MongoExampleMapper(MongoConverter converter) {
7474
* Returns the given {@link Example} as {@link DBObject} holding matching values extracted from
7575
* {@link Example#getProbe()}.
7676
*
77-
* @param example
77+
* @param example must not be {@literal null}.
7878
* @return
79-
* @since 1.8
8079
*/
8180
public DBObject getMappedExample(Example<?> example) {
81+
82+
Assert.notNull(example, "Example must not be null!");
83+
8284
return getMappedExample(example, mappingContext.getPersistentEntity(example.getProbeType()));
8385
}
8486

8587
/**
8688
* Returns the given {@link Example} as {@link DBObject} holding matching values extracted from
8789
* {@link Example#getProbe()}.
8890
*
89-
* @param example
90-
* @param entity
91+
* @param example must not be {@literal null}.
92+
* @param entity must not be {@literal null}.
9193
* @return
92-
* @since 1.8
9394
*/
95+
@SuppressWarnings({ "unchecked", "rawtypes" })
9496
public DBObject getMappedExample(Example<?> example, MongoPersistentEntity<?> entity) {
9597

98+
Assert.notNull(example, "Example must not be null!");
99+
Assert.notNull(entity, "MongoPersistentEntity must not be null!");
100+
96101
DBObject reference = (DBObject) converter.convertToMongoType(example.getProbe());
97102

98103
if (entity.hasIdProperty() && entity.getIdentifierAccessor(example.getProbe()).getIdentifier() == null) {
99104
reference.removeField(entity.getIdProperty().getFieldName());
100105
}
101106

102-
ExampleSpecAccessor exampleSpecAccessor = new ExampleSpecAccessor(example.getExampleSpec());
107+
ExampleMatcherAccessor matcherAccessor = new ExampleMatcherAccessor(example.getMatcher());
103108

104-
applyPropertySpecs("", reference, example.getProbeType(), exampleSpecAccessor);
109+
applyPropertySpecs("", reference, example.getProbeType(), matcherAccessor);
105110

106-
if (exampleSpecAccessor.isTyped()) {
107-
this.converter.getTypeMapper().writeTypeRestrictions(reference, (Set) Collections.singleton(example.getResultType()));
108-
}
111+
this.converter.getTypeMapper().writeTypeRestrictions(reference, getTypesToMatch(example));
109112

110-
return ObjectUtils.nullSafeEquals(NullHandler.INCLUDE, exampleSpecAccessor.getNullHandler()) ? reference
113+
return ObjectUtils.nullSafeEquals(NullHandler.INCLUDE, matcherAccessor.getNullHandler()) ? reference
111114
: new BasicDBObject(SerializationUtils.flattenMap(reference));
112115
}
113116

117+
private Set<Class<?>> getTypesToMatch(Example<?> example) {
118+
119+
Set<Class<?>> types = new HashSet<Class<?>>();
120+
121+
for (TypeInformation<?> reference : mappingContext.getManagedTypes()) {
122+
if (example.getProbeType().isAssignableFrom(reference.getType())) {
123+
types.add(reference.getType());
124+
}
125+
}
126+
127+
return types;
128+
}
129+
114130
private String getMappedPropertyPath(String path, Class<?> probeType) {
115131

116132
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(probeType);
@@ -159,7 +175,7 @@ public void doWithPersistentProperty(MongoPersistentProperty property) {
159175
}
160176

161177
private void applyPropertySpecs(String path, DBObject source, Class<?> probeType,
162-
ExampleSpecAccessor exampleSpecAccessor) {
178+
ExampleMatcherAccessor exampleSpecAccessor) {
163179

164180
if (!(source instanceof BasicDBObject)) {
165181
return;
@@ -172,12 +188,12 @@ private void applyPropertySpecs(String path, DBObject source, Class<?> probeType
172188
Map.Entry<String, Object> entry = iter.next();
173189
String propertyPath = StringUtils.hasText(path) ? path + "." + entry.getKey() : entry.getKey();
174190
String mappedPropertyPath = getMappedPropertyPath(propertyPath, probeType);
175-
176-
if(isEmptyIdProperty(entry)) {
191+
192+
if (isEmptyIdProperty(entry)) {
177193
iter.remove();
178194
continue;
179195
}
180-
196+
181197
if (exampleSpecAccessor.isIgnoredPath(propertyPath) || exampleSpecAccessor.isIgnoredPath(mappedPropertyPath)) {
182198
iter.remove();
183199
continue;
@@ -243,5 +259,4 @@ private void applyStringMatcher(Map.Entry<String, Object> entry, StringMatcher s
243259
dbo.put("$options", "i");
244260
}
245261
}
246-
247262
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepository.java

+9-17
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import org.springframework.data.domain.PageImpl;
3131
import org.springframework.data.domain.Pageable;
3232
import org.springframework.data.domain.Sort;
33-
import org.springframework.data.domain.TypedExampleSpec;
3433
import org.springframework.data.mongodb.core.MongoOperations;
3534
import org.springframework.data.mongodb.core.MongoTemplate;
3635
import org.springframework.data.mongodb.core.query.Criteria;
@@ -273,12 +272,14 @@ public <S extends T> Page<S> findAll(Example<S> example, Pageable pageable) {
273272

274273
Query q = new Query(new Criteria().alike(example)).with(pageable);
275274

276-
long count = mongoOperations.count(q, getResultType(example), entityInformation.getCollectionName());
275+
long count = mongoOperations.count(q, example.getProbeType(), entityInformation.getCollectionName());
276+
277277
if (count == 0) {
278278
return new PageImpl<S>(Collections.<S> emptyList());
279279
}
280-
return new PageImpl<S>(mongoOperations.find(q, getResultType(example), entityInformation.getCollectionName()), pageable,
281-
count);
280+
281+
return new PageImpl<S>(mongoOperations.find(q, example.getProbeType(), entityInformation.getCollectionName()),
282+
pageable, count);
282283
}
283284

284285
/*
@@ -296,7 +297,7 @@ public <S extends T> List<S> findAll(Example<S> example, Sort sort) {
296297
q.with(sort);
297298
}
298299

299-
return mongoOperations.find(q, getResultType(example), entityInformation.getCollectionName());
300+
return mongoOperations.find(q, example.getProbeType(), entityInformation.getCollectionName());
300301
}
301302

302303
/*
@@ -318,7 +319,7 @@ public <S extends T> S findOne(Example<S> example) {
318319
Assert.notNull(example, "Sample must not be null!");
319320

320321
Query q = new Query(new Criteria().alike(example));
321-
return mongoOperations.findOne(q, getResultType(example), entityInformation.getCollectionName());
322+
return mongoOperations.findOne(q, example.getProbeType(), entityInformation.getCollectionName());
322323
}
323324

324325
/*
@@ -331,7 +332,7 @@ public <S extends T> long count(Example<S> example) {
331332
Assert.notNull(example, "Sample must not be null!");
332333

333334
Query q = new Query(new Criteria().alike(example));
334-
return mongoOperations.count(q, getResultType(example), entityInformation.getCollectionName());
335+
return mongoOperations.count(q, example.getProbeType(), entityInformation.getCollectionName());
335336
}
336337

337338
/*
@@ -344,16 +345,7 @@ public <S extends T> boolean exists(Example<S> example) {
344345
Assert.notNull(example, "Sample must not be null!");
345346

346347
Query q = new Query(new Criteria().alike(example));
347-
return mongoOperations.exists(q, getResultType(example), entityInformation.getCollectionName());
348-
}
349-
350-
private <S extends T> Class<S> getResultType(Example<S> example) {
351-
352-
if (example.getExampleSpec() instanceof TypedExampleSpec<?>) {
353-
return example.getResultType();
354-
}
355-
356-
return (Class<S>) entityInformation.getJavaType();
348+
return mongoOperations.exists(q, example.getProbeType(), entityInformation.getCollectionName());
357349
}
358350

359351
private List<T> findAll(Query query) {

0 commit comments

Comments
 (0)