Skip to content

Commit 4b59736

Browse files
mp911deodrotbohm
authored andcommitted
DATAMONGO-1141 - Polishing.
Add property to field name mapping for Sort orders by moving Sort mapping to UpdateMapper. Fix typo. Add JavaDoc. Reformat code. Remove trailing whitespaces. Original pull request: spring-projects#405.
1 parent 31d4434 commit 4b59736

File tree

3 files changed

+62
-20
lines changed

3 files changed

+62
-20
lines changed

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

+29-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import org.bson.Document;
2121
import org.bson.conversions.Bson;
2222
import org.springframework.core.convert.converter.Converter;
23+
import org.springframework.data.domain.Sort;
24+
import org.springframework.data.domain.Sort.Order;
2325
import org.springframework.data.mapping.Association;
2426
import org.springframework.data.mapping.context.MappingContext;
2527
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
@@ -69,7 +71,7 @@ public Document getMappedObject(Bson query, MongoPersistentEntity<?> entity) {
6971
for (String s : document.keySet()) {
7072
if (s.startsWith("$")) {
7173

72-
if(s.equals("$set")){
74+
if (s.equals("$set")) {
7375
set = document.get(s, Document.class);
7476
}
7577
hasOperators = true;
@@ -99,6 +101,7 @@ public Document getMappedObject(Bson query, MongoPersistentEntity<?> entity) {
99101

100102
/**
101103
* Returns {@literal true} if the given {@link Document} is an update object that uses update operators.
104+
*
102105
* @param updateObj
103106
* @return {@literal true} if the given {@link Document} is an update object.
104107
*/
@@ -194,11 +197,23 @@ private boolean isQuery(Object value) {
194197
}
195198

196199
private Document getMappedValue(Field field, Modifier modifier) {
200+
return new Document(modifier.getKey(), getMappedModifier(field, modifier));
201+
}
202+
203+
private Object getMappedModifier(Field field, Modifier modifier) {
204+
205+
Object value = modifier.getValue();
206+
207+
if (value instanceof Sort) {
208+
209+
Document sortObject = getSortObject((Sort) value);
210+
return field == null || field.getPropertyEntity() == null ? sortObject
211+
: getMappedSort(sortObject, field.getPropertyEntity());
212+
}
197213

198214
TypeInformation<?> typeHint = field == null ? ClassTypeInformation.OBJECT : field.getTypeHint();
199215

200-
Object value = converter.convertToMongoType(modifier.getValue(), typeHint);
201-
return new Document(modifier.getKey(), value);
216+
return converter.convertToMongoType(value, typeHint);
202217
}
203218

204219
private TypeInformation<?> getTypeHintForEntity(Object source, MongoPersistentEntity<?> entity) {
@@ -229,6 +244,17 @@ protected Field createPropertyField(MongoPersistentEntity<?> entity, String key,
229244
: new MetadataBackedUpdateField(entity, key, mappingContext);
230245
}
231246

247+
private static Document getSortObject(Sort sort) {
248+
249+
Document document = new Document();
250+
251+
for (Order order : sort) {
252+
document.put(order.getProperty(), order.isAscending() ? 1 : -1);
253+
}
254+
255+
return document;
256+
}
257+
232258
/**
233259
* {@link MetadataBackedField} that handles {@literal $} paths inside a field key. We clean up an update key
234260
* containing a {@literal $} before handing it to the super class to make sure property lookups and transformations

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

+20-11
Original file line numberDiff line numberDiff line change
@@ -667,33 +667,42 @@ public Object getValue() {
667667
* Implementation of {@link Modifier} representing {@code $sort}.
668668
*
669669
* @author Pavel Vodrazka
670+
* @author Mark Paluch
670671
* @since 1.10
671672
*/
672673
private static class SortModifier implements Modifier {
673674

674675
private final Object sort;
675676

677+
/**
678+
* Creates a new {@link SortModifier} instance given {@link Direction}.
679+
*
680+
* @param direction must not be {@literal null}.
681+
*/
676682
public SortModifier(Direction direction) {
683+
684+
Assert.notNull(direction, "Direction must not be null!");
677685
this.sort = direction.isAscending() ? 1 : -1;
678686
}
679687

688+
/**
689+
* Creates a new {@link SortModifier} instance given {@link Sort}.
690+
*
691+
* @param sort must not be {@literal null}.
692+
*/
680693
public SortModifier(Sort sort) {
681-
this.sort = createDBObject(sort);
682-
}
683-
684-
private Document createDBObject(Sort sort) {
685694

686-
Document obj = new Document();
695+
Assert.notNull(sort, "Sort must not be null!");
687696

688697
for (Order order : sort) {
698+
689699
if (order.isIgnoreCase()) {
690700
throw new IllegalArgumentException(String.format("Given sort contained an Order for %s with ignore case! "
691701
+ "MongoDB does not support sorting ignoring case currently!", order.getProperty()));
692702
}
693-
obj.put(order.getProperty(), order.isAscending() ? 1 : -1);
694703
}
695704

696-
return obj;
705+
this.sort = sort;
697706
}
698707

699708
/*
@@ -780,14 +789,14 @@ public PushOperatorBuilder sort(Direction direction) {
780789
* Propagates {@code $sort} to {@code $push}. {@code $sort} requires the {@code $each} operator. Forces document
781790
* elements to be sorted in given {@literal order}.
782791
*
783-
* @param order must not be {@literal null}.
792+
* @param sort must not be {@literal null}.
784793
* @return never {@literal null}.
785794
* @since 1.10
786795
*/
787-
public PushOperatorBuilder sort(Sort order) {
796+
public PushOperatorBuilder sort(Sort sort) {
788797

789-
Assert.notNull(order, "Order must not be 'null'.");
790-
this.modifiers.addModifier(new SortModifier(order));
798+
Assert.notNull(sort, "Sort must not be 'null'.");
799+
this.modifiers.addModifier(new SortModifier(sort));
791800
return this;
792801
}
793802

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/UpdateMapperUnitTests.java

+13-6
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,8 @@ public void updatePushEachWithValueSortShouldRenderCorrectly() {
420420

421421
Update update = new Update().push("scores").sort(Direction.DESC).each(42, 23, 68);
422422

423-
Document mappedObject = mapper.getMappedObject(update.getUpdateObject(), context.getPersistentEntity(Object.class));
423+
Document mappedObject = mapper.getMappedObject(update.getUpdateObject(),
424+
context.getPersistentEntity(ParentClass.class));
424425

425426
Document push = getAsDocument(mappedObject, "$push");
426427
Document key = getAsDocument(push, "scores");
@@ -436,17 +437,18 @@ public void updatePushEachWithValueSortShouldRenderCorrectly() {
436437
@Test
437438
public void updatePushEachWithDocumentSortShouldRenderCorrectly() {
438439

439-
Update update = new Update().push("names")
440-
.sort(new Sort(new Order(Direction.ASC, "last"), new Order(Direction.ASC, "first")))
440+
Update update = new Update().push("list")
441+
.sort(new Sort(new Order(Direction.ASC, "value"), new Order(Direction.ASC, "field")))
441442
.each(Collections.emptyList());
442443

443-
Document mappedObject = mapper.getMappedObject(update.getUpdateObject(), context.getPersistentEntity(Object.class));
444+
Document mappedObject = mapper.getMappedObject(update.getUpdateObject(),
445+
context.getPersistentEntity(EntityWithList.class));
444446

445447
Document push = getAsDocument(mappedObject, "$push");
446-
Document key = getAsDocument(push, "names");
448+
Document key = getAsDocument(push, "list");
447449

448450
assertThat(key.containsKey("$sort"), is(true));
449-
assertThat((Document) key.get("$sort"), equalTo(new Document("last", 1).append("first", 1)));
451+
assertThat((Document) key.get("$sort"), equalTo(new Document("renamed-value", 1).append("field", 1)));
450452
assertThat(key.containsKey("$each"), is(true));
451453
}
452454

@@ -1317,9 +1319,14 @@ static class EntityWithObject {
13171319
NestedDocument concreteValue;
13181320
}
13191321

1322+
static class EntityWithList {
1323+
List<EntityWithAliasedObject> list;
1324+
}
1325+
13201326
static class EntityWithAliasedObject {
13211327

13221328
@Field("renamed-value") Object value;
1329+
Object field;
13231330
}
13241331

13251332
static class EntityWithObjectMap {

0 commit comments

Comments
 (0)