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: #405.
This commit is contained in:
committed by
Oliver Gierke
parent
31d4434562
commit
4b59736f82
@@ -20,6 +20,8 @@ import java.util.Map.Entry;
|
|||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
import org.bson.conversions.Bson;
|
import org.bson.conversions.Bson;
|
||||||
import org.springframework.core.convert.converter.Converter;
|
import org.springframework.core.convert.converter.Converter;
|
||||||
|
import org.springframework.data.domain.Sort;
|
||||||
|
import org.springframework.data.domain.Sort.Order;
|
||||||
import org.springframework.data.mapping.Association;
|
import org.springframework.data.mapping.Association;
|
||||||
import org.springframework.data.mapping.context.MappingContext;
|
import org.springframework.data.mapping.context.MappingContext;
|
||||||
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
|
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
|
||||||
@@ -69,7 +71,7 @@ public class UpdateMapper extends QueryMapper {
|
|||||||
for (String s : document.keySet()) {
|
for (String s : document.keySet()) {
|
||||||
if (s.startsWith("$")) {
|
if (s.startsWith("$")) {
|
||||||
|
|
||||||
if(s.equals("$set")){
|
if (s.equals("$set")) {
|
||||||
set = document.get(s, Document.class);
|
set = document.get(s, Document.class);
|
||||||
}
|
}
|
||||||
hasOperators = true;
|
hasOperators = true;
|
||||||
@@ -99,6 +101,7 @@ public class UpdateMapper extends QueryMapper {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns {@literal true} if the given {@link Document} is an update object that uses update operators.
|
* Returns {@literal true} if the given {@link Document} is an update object that uses update operators.
|
||||||
|
*
|
||||||
* @param updateObj
|
* @param updateObj
|
||||||
* @return {@literal true} if the given {@link Document} is an update object.
|
* @return {@literal true} if the given {@link Document} is an update object.
|
||||||
*/
|
*/
|
||||||
@@ -194,11 +197,23 @@ public class UpdateMapper extends QueryMapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Document getMappedValue(Field field, Modifier modifier) {
|
private Document getMappedValue(Field field, Modifier modifier) {
|
||||||
|
return new Document(modifier.getKey(), getMappedModifier(field, modifier));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Object getMappedModifier(Field field, Modifier modifier) {
|
||||||
|
|
||||||
|
Object value = modifier.getValue();
|
||||||
|
|
||||||
|
if (value instanceof Sort) {
|
||||||
|
|
||||||
|
Document sortObject = getSortObject((Sort) value);
|
||||||
|
return field == null || field.getPropertyEntity() == null ? sortObject
|
||||||
|
: getMappedSort(sortObject, field.getPropertyEntity());
|
||||||
|
}
|
||||||
|
|
||||||
TypeInformation<?> typeHint = field == null ? ClassTypeInformation.OBJECT : field.getTypeHint();
|
TypeInformation<?> typeHint = field == null ? ClassTypeInformation.OBJECT : field.getTypeHint();
|
||||||
|
|
||||||
Object value = converter.convertToMongoType(modifier.getValue(), typeHint);
|
return converter.convertToMongoType(value, typeHint);
|
||||||
return new Document(modifier.getKey(), value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private TypeInformation<?> getTypeHintForEntity(Object source, MongoPersistentEntity<?> entity) {
|
private TypeInformation<?> getTypeHintForEntity(Object source, MongoPersistentEntity<?> entity) {
|
||||||
@@ -229,6 +244,17 @@ public class UpdateMapper extends QueryMapper {
|
|||||||
: new MetadataBackedUpdateField(entity, key, mappingContext);
|
: new MetadataBackedUpdateField(entity, key, mappingContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Document getSortObject(Sort sort) {
|
||||||
|
|
||||||
|
Document document = new Document();
|
||||||
|
|
||||||
|
for (Order order : sort) {
|
||||||
|
document.put(order.getProperty(), order.isAscending() ? 1 : -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return document;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link MetadataBackedField} that handles {@literal $} paths inside a field key. We clean up an update key
|
* {@link MetadataBackedField} that handles {@literal $} paths inside a field key. We clean up an update key
|
||||||
* containing a {@literal $} before handing it to the super class to make sure property lookups and transformations
|
* containing a {@literal $} before handing it to the super class to make sure property lookups and transformations
|
||||||
|
|||||||
@@ -667,33 +667,42 @@ public class Update {
|
|||||||
* Implementation of {@link Modifier} representing {@code $sort}.
|
* Implementation of {@link Modifier} representing {@code $sort}.
|
||||||
*
|
*
|
||||||
* @author Pavel Vodrazka
|
* @author Pavel Vodrazka
|
||||||
|
* @author Mark Paluch
|
||||||
* @since 1.10
|
* @since 1.10
|
||||||
*/
|
*/
|
||||||
private static class SortModifier implements Modifier {
|
private static class SortModifier implements Modifier {
|
||||||
|
|
||||||
private final Object sort;
|
private final Object sort;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new {@link SortModifier} instance given {@link Direction}.
|
||||||
|
*
|
||||||
|
* @param direction must not be {@literal null}.
|
||||||
|
*/
|
||||||
public SortModifier(Direction direction) {
|
public SortModifier(Direction direction) {
|
||||||
|
|
||||||
|
Assert.notNull(direction, "Direction must not be null!");
|
||||||
this.sort = direction.isAscending() ? 1 : -1;
|
this.sort = direction.isAscending() ? 1 : -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new {@link SortModifier} instance given {@link Sort}.
|
||||||
|
*
|
||||||
|
* @param sort must not be {@literal null}.
|
||||||
|
*/
|
||||||
public SortModifier(Sort sort) {
|
public SortModifier(Sort sort) {
|
||||||
this.sort = createDBObject(sort);
|
|
||||||
}
|
|
||||||
|
|
||||||
private Document createDBObject(Sort sort) {
|
Assert.notNull(sort, "Sort must not be null!");
|
||||||
|
|
||||||
Document obj = new Document();
|
|
||||||
|
|
||||||
for (Order order : sort) {
|
for (Order order : sort) {
|
||||||
|
|
||||||
if (order.isIgnoreCase()) {
|
if (order.isIgnoreCase()) {
|
||||||
throw new IllegalArgumentException(String.format("Given sort contained an Order for %s with ignore case! "
|
throw new IllegalArgumentException(String.format("Given sort contained an Order for %s with ignore case! "
|
||||||
+ "MongoDB does not support sorting ignoring case currently!", order.getProperty()));
|
+ "MongoDB does not support sorting ignoring case currently!", order.getProperty()));
|
||||||
}
|
}
|
||||||
obj.put(order.getProperty(), order.isAscending() ? 1 : -1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return obj;
|
this.sort = sort;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -780,14 +789,14 @@ public class Update {
|
|||||||
* Propagates {@code $sort} to {@code $push}. {@code $sort} requires the {@code $each} operator. Forces document
|
* Propagates {@code $sort} to {@code $push}. {@code $sort} requires the {@code $each} operator. Forces document
|
||||||
* elements to be sorted in given {@literal order}.
|
* elements to be sorted in given {@literal order}.
|
||||||
*
|
*
|
||||||
* @param order must not be {@literal null}.
|
* @param sort must not be {@literal null}.
|
||||||
* @return never {@literal null}.
|
* @return never {@literal null}.
|
||||||
* @since 1.10
|
* @since 1.10
|
||||||
*/
|
*/
|
||||||
public PushOperatorBuilder sort(Sort order) {
|
public PushOperatorBuilder sort(Sort sort) {
|
||||||
|
|
||||||
Assert.notNull(order, "Order must not be 'null'.");
|
Assert.notNull(sort, "Sort must not be 'null'.");
|
||||||
this.modifiers.addModifier(new SortModifier(order));
|
this.modifiers.addModifier(new SortModifier(sort));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -420,7 +420,8 @@ public class UpdateMapperUnitTests {
|
|||||||
|
|
||||||
Update update = new Update().push("scores").sort(Direction.DESC).each(42, 23, 68);
|
Update update = new Update().push("scores").sort(Direction.DESC).each(42, 23, 68);
|
||||||
|
|
||||||
Document mappedObject = mapper.getMappedObject(update.getUpdateObject(), context.getPersistentEntity(Object.class));
|
Document mappedObject = mapper.getMappedObject(update.getUpdateObject(),
|
||||||
|
context.getPersistentEntity(ParentClass.class));
|
||||||
|
|
||||||
Document push = getAsDocument(mappedObject, "$push");
|
Document push = getAsDocument(mappedObject, "$push");
|
||||||
Document key = getAsDocument(push, "scores");
|
Document key = getAsDocument(push, "scores");
|
||||||
@@ -436,17 +437,18 @@ public class UpdateMapperUnitTests {
|
|||||||
@Test
|
@Test
|
||||||
public void updatePushEachWithDocumentSortShouldRenderCorrectly() {
|
public void updatePushEachWithDocumentSortShouldRenderCorrectly() {
|
||||||
|
|
||||||
Update update = new Update().push("names")
|
Update update = new Update().push("list")
|
||||||
.sort(new Sort(new Order(Direction.ASC, "last"), new Order(Direction.ASC, "first")))
|
.sort(new Sort(new Order(Direction.ASC, "value"), new Order(Direction.ASC, "field")))
|
||||||
.each(Collections.emptyList());
|
.each(Collections.emptyList());
|
||||||
|
|
||||||
Document mappedObject = mapper.getMappedObject(update.getUpdateObject(), context.getPersistentEntity(Object.class));
|
Document mappedObject = mapper.getMappedObject(update.getUpdateObject(),
|
||||||
|
context.getPersistentEntity(EntityWithList.class));
|
||||||
|
|
||||||
Document push = getAsDocument(mappedObject, "$push");
|
Document push = getAsDocument(mappedObject, "$push");
|
||||||
Document key = getAsDocument(push, "names");
|
Document key = getAsDocument(push, "list");
|
||||||
|
|
||||||
assertThat(key.containsKey("$sort"), is(true));
|
assertThat(key.containsKey("$sort"), is(true));
|
||||||
assertThat((Document) key.get("$sort"), equalTo(new Document("last", 1).append("first", 1)));
|
assertThat((Document) key.get("$sort"), equalTo(new Document("renamed-value", 1).append("field", 1)));
|
||||||
assertThat(key.containsKey("$each"), is(true));
|
assertThat(key.containsKey("$each"), is(true));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1317,9 +1319,14 @@ public class UpdateMapperUnitTests {
|
|||||||
NestedDocument concreteValue;
|
NestedDocument concreteValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static class EntityWithList {
|
||||||
|
List<EntityWithAliasedObject> list;
|
||||||
|
}
|
||||||
|
|
||||||
static class EntityWithAliasedObject {
|
static class EntityWithAliasedObject {
|
||||||
|
|
||||||
@Field("renamed-value") Object value;
|
@Field("renamed-value") Object value;
|
||||||
|
Object field;
|
||||||
}
|
}
|
||||||
|
|
||||||
static class EntityWithObjectMap {
|
static class EntityWithObjectMap {
|
||||||
|
|||||||
Reference in New Issue
Block a user