Skip to content

Commit f5ba89e

Browse files
committed
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.
1 parent bab94e2 commit f5ba89e

File tree

3 files changed

+71
-27
lines changed

3 files changed

+71
-27
lines changed

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

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2015 the original author or authors.
2+
* Copyright 2013-2016 the original author or authors.
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.
@@ -18,6 +18,8 @@
1818
import java.util.Map.Entry;
1919

2020
import org.springframework.core.convert.converter.Converter;
21+
import org.springframework.data.domain.Sort;
22+
import org.springframework.data.domain.Sort.Order;
2123
import org.springframework.data.mapping.Association;
2224
import org.springframework.data.mapping.context.MappingContext;
2325
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
@@ -37,6 +39,7 @@
3739
* @author Thomas Darimont
3840
* @author Oliver Gierke
3941
* @author Christoph Strobl
42+
* @author Mark Paluch
4043
*/
4144
public class UpdateMapper extends QueryMapper {
4245

@@ -130,11 +133,23 @@ private boolean isQuery(Object value) {
130133
}
131134

132135
private DBObject getMappedValue(Field field, Modifier modifier) {
136+
return new BasicDBObject(modifier.getKey(), getMappedModifier(field, modifier));
137+
}
138+
139+
private Object getMappedModifier(Field field, Modifier modifier) {
140+
141+
Object value = modifier.getValue();
142+
143+
if (value instanceof Sort) {
144+
145+
DBObject sortObject = getSortObject((Sort) value);
146+
return field == null || field.getPropertyEntity() == null ? sortObject
147+
: getMappedSort(sortObject, field.getPropertyEntity());
148+
}
133149

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

136-
Object value = converter.convertToMongoType(modifier.getValue(), typeHint);
137-
return new BasicDBObject(modifier.getKey(), value);
152+
return converter.convertToMongoType(value, typeHint);
138153
}
139154

140155
private TypeInformation<?> getTypeHintForEntity(Object source, MongoPersistentEntity<?> entity) {
@@ -153,6 +168,17 @@ private TypeInformation<?> getTypeHintForEntity(Object source, MongoPersistentEn
153168
return NESTED_DOCUMENT;
154169
}
155170

171+
public DBObject getSortObject(Sort sort) {
172+
173+
DBObject dbo = new BasicDBObject();
174+
175+
for (Order order : sort) {
176+
dbo.put(order.getProperty(), order.isAscending() ? 1 : -1);
177+
}
178+
179+
return dbo;
180+
}
181+
156182
/*
157183
* (non-Javadoc)
158184
* @see org.springframework.data.mongodb.core.convert.QueryMapper#createPropertyField(org.springframework.data.mongodb.core.mapping.MongoPersistentEntity, java.lang.String, org.springframework.data.mapping.context.MappingContext)

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

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -664,38 +664,47 @@ public Object getValue() {
664664
return this.count;
665665
}
666666
}
667-
667+
668668
/**
669669
* Implementation of {@link Modifier} representing {@code $sort}.
670670
*
671671
* @author Pavel Vodrazka
672+
* @author Mark Paluch
672673
* @since 1.10
673674
*/
674675
private static class SortModifier implements Modifier {
675676

676677
private final Object sort;
677678

679+
/**
680+
* Creates a new {@link SortModifier} instance given {@link Direction}.
681+
*
682+
* @param direction must not be {@literal null}.
683+
*/
678684
public SortModifier(Direction direction) {
685+
686+
Assert.notNull(direction, "Direction must not be null!");
679687
this.sort = direction.isAscending() ? 1 : -1;
680688
}
681689

690+
/**
691+
* Creates a new {@link SortModifier} instance given {@link Sort}.
692+
*
693+
* @param sort must not be {@literal null}.
694+
*/
682695
public SortModifier(Sort sort) {
683-
this.sort = createDBObject(sort);
684-
}
685696

686-
private DBObject createDBObject(Sort sort) {
687-
688-
DBObject obj = new BasicDBObject();
697+
Assert.notNull(sort, "Sort must not be null!");
689698

690699
for (Order order : sort) {
700+
691701
if (order.isIgnoreCase()) {
692702
throw new IllegalArgumentException(String.format("Given sort contained an Order for %s with ignore case! "
693703
+ "MongoDB does not support sorting ignoring case currently!", order.getProperty()));
694704
}
695-
obj.put(order.getProperty(), order.isAscending() ? 1 : -1);
696705
}
697706

698-
return obj;
707+
this.sort = sort;
699708
}
700709

701710
/*
@@ -714,7 +723,7 @@ public String getKey() {
714723
@Override
715724
public Object getValue() {
716725
return this.sort;
717-
}
726+
}
718727
}
719728

720729
/**
@@ -764,8 +773,8 @@ public PushOperatorBuilder slice(int count) {
764773
}
765774

766775
/**
767-
* Propagates {@code $sort} to {@code $push}. {@code $sort} requires the {@code $each} operator.
768-
* Forces elements to be sorted by values in given {@literal direction}.
776+
* Propagates {@code $sort} to {@code $push}. {@code $sort} requires the {@code $each} operator. Forces elements to
777+
* be sorted by values in given {@literal direction}.
769778
*
770779
* @param direction must not be {@literal null}.
771780
* @return never {@literal null}.
@@ -779,17 +788,17 @@ public PushOperatorBuilder sort(Direction direction) {
779788
}
780789

781790
/**
782-
* Propagates {@code $sort} to {@code $push}. {@code $sort} requires the {@code $each} operator.
783-
* Forces document elements to be sorted in given {@literal order}.
791+
* Propagates {@code $sort} to {@code $push}. {@code $sort} requires the {@code $each} operator. Forces document
792+
* elements to be sorted in given {@literal order}.
784793
*
785-
* @param order must not be {@literal null}.
794+
* @param sort must not be {@literal null}.
786795
* @return never {@literal null}.
787796
* @since 1.10
788797
*/
789-
public PushOperatorBuilder sort(Sort order) {
798+
public PushOperatorBuilder sort(Sort sort) {
790799

791-
Assert.notNull(order, "Order must not be 'null'.");
792-
this.modifiers.addModifier(new SortModifier(order));
800+
Assert.notNull(sort, "Sort must not be 'null'.");
801+
this.modifiers.addModifier(new SortModifier(sort));
793802
return this;
794803
}
795804

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,8 @@ public void updatePushEachWithValueSortShouldRenderCorrectly() {
429429

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

432-
DBObject mappedObject = mapper.getMappedObject(update.getUpdateObject(), context.getPersistentEntity(Object.class));
432+
DBObject mappedObject = mapper.getMappedObject(update.getUpdateObject(),
433+
context.getPersistentEntity(ParentClass.class));
433434

434435
DBObject push = getAsDBObject(mappedObject, "$push");
435436
DBObject key = getAsDBObject(push, "scores");
@@ -445,16 +446,19 @@ public void updatePushEachWithValueSortShouldRenderCorrectly() {
445446
@Test
446447
public void updatePushEachWithDocumentSortShouldRenderCorrectly() {
447448

448-
Update update = new Update().push("names").sort(new Sort(new Order(Direction.ASC, "last"), new Order(Direction.ASC, "first")))
449+
Update update = new Update().push("list")
450+
.sort(new Sort(new Order(Direction.ASC, "value"), new Order(Direction.ASC, "field")))
449451
.each(Collections.emptyList());
450452

451-
DBObject mappedObject = mapper.getMappedObject(update.getUpdateObject(), context.getPersistentEntity(Object.class));
453+
DBObject mappedObject = mapper.getMappedObject(update.getUpdateObject(),
454+
context.getPersistentEntity(EntityWithList.class));
452455

453456
DBObject push = getAsDBObject(mappedObject, "$push");
454-
DBObject key = getAsDBObject(push, "names");
457+
DBObject key = getAsDBObject(push, "list");
455458

456459
assertThat(key.containsField("$sort"), is(true));
457-
assertThat((DBObject) key.get("$sort"), equalTo(new BasicDBObjectBuilder().add("last", 1).add("first", 1).get()));
460+
assertThat((DBObject) key.get("$sort"),
461+
equalTo(new BasicDBObjectBuilder().add("renamed-value", 1).add("field", 1).get()));
458462
assertThat(key.containsField("$each"), is(true));
459463
}
460464

@@ -464,8 +468,8 @@ public void updatePushEachWithDocumentSortShouldRenderCorrectly() {
464468
@Test
465469
public void updatePushEachWithSortShouldRenderCorrectlyWhenUsingMultiplePush() {
466470

467-
Update update = new Update().push("authors").sort(Direction.ASC).each("Harry")
468-
.push("chapters").sort(new Sort(Direction.ASC, "order")).each(Collections.emptyList());
471+
Update update = new Update().push("authors").sort(Direction.ASC).each("Harry").push("chapters")
472+
.sort(new Sort(Direction.ASC, "order")).each(Collections.emptyList());
469473

470474
DBObject mappedObject = mapper.getMappedObject(update.getUpdateObject(), context.getPersistentEntity(Object.class));
471475

@@ -1278,9 +1282,14 @@ static class EntityWithObject {
12781282
NestedDocument concreteValue;
12791283
}
12801284

1285+
static class EntityWithList {
1286+
List<EntityWithAliasedObject> list;
1287+
}
1288+
12811289
static class EntityWithAliasedObject {
12821290

12831291
@Field("renamed-value") Object value;
1292+
Object field;
12841293
}
12851294

12861295
static class EntityWithObjectMap {

0 commit comments

Comments
 (0)