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:
Mark Paluch
2016-12-02 17:29:23 +01:00
committed by Oliver Gierke
parent 7f39c42eb7
commit c6a4e7166c
3 changed files with 71 additions and 27 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2015 the original author or authors.
* Copyright 2013-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,6 +18,8 @@ package org.springframework.data.mongodb.core.convert;
import java.util.Map.Entry;
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.context.MappingContext;
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
@@ -37,6 +39,7 @@ import com.mongodb.DBObject;
* @author Thomas Darimont
* @author Oliver Gierke
* @author Christoph Strobl
* @author Mark Paluch
*/
public class UpdateMapper extends QueryMapper {
@@ -130,11 +133,23 @@ public class UpdateMapper extends QueryMapper {
}
private DBObject getMappedValue(Field field, Modifier modifier) {
return new BasicDBObject(modifier.getKey(), getMappedModifier(field, modifier));
}
private Object getMappedModifier(Field field, Modifier modifier) {
Object value = modifier.getValue();
if (value instanceof Sort) {
DBObject sortObject = getSortObject((Sort) value);
return field == null || field.getPropertyEntity() == null ? sortObject
: getMappedSort(sortObject, field.getPropertyEntity());
}
TypeInformation<?> typeHint = field == null ? ClassTypeInformation.OBJECT : field.getTypeHint();
Object value = converter.convertToMongoType(modifier.getValue(), typeHint);
return new BasicDBObject(modifier.getKey(), value);
return converter.convertToMongoType(value, typeHint);
}
private TypeInformation<?> getTypeHintForEntity(Object source, MongoPersistentEntity<?> entity) {
@@ -153,6 +168,17 @@ public class UpdateMapper extends QueryMapper {
return NESTED_DOCUMENT;
}
public DBObject getSortObject(Sort sort) {
DBObject dbo = new BasicDBObject();
for (Order order : sort) {
dbo.put(order.getProperty(), order.isAscending() ? 1 : -1);
}
return dbo;
}
/*
* (non-Javadoc)
* @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)

View File

@@ -664,38 +664,47 @@ public class Update {
return this.count;
}
}
/**
* Implementation of {@link Modifier} representing {@code $sort}.
*
* @author Pavel Vodrazka
* @author Mark Paluch
* @since 1.10
*/
private static class SortModifier implements Modifier {
private final Object sort;
/**
* Creates a new {@link SortModifier} instance given {@link Direction}.
*
* @param direction must not be {@literal null}.
*/
public SortModifier(Direction direction) {
Assert.notNull(direction, "Direction must not be null!");
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) {
this.sort = createDBObject(sort);
}
private DBObject createDBObject(Sort sort) {
DBObject obj = new BasicDBObject();
Assert.notNull(sort, "Sort must not be null!");
for (Order order : sort) {
if (order.isIgnoreCase()) {
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()));
}
obj.put(order.getProperty(), order.isAscending() ? 1 : -1);
}
return obj;
this.sort = sort;
}
/*
@@ -714,7 +723,7 @@ public class Update {
@Override
public Object getValue() {
return this.sort;
}
}
}
/**
@@ -764,8 +773,8 @@ public class Update {
}
/**
* Propagates {@code $sort} to {@code $push}. {@code $sort} requires the {@code $each} operator.
* Forces elements to be sorted by values in given {@literal direction}.
* Propagates {@code $sort} to {@code $push}. {@code $sort} requires the {@code $each} operator. Forces elements to
* be sorted by values in given {@literal direction}.
*
* @param direction must not be {@literal null}.
* @return never {@literal null}.
@@ -779,17 +788,17 @@ public class Update {
}
/**
* Propagates {@code $sort} to {@code $push}. {@code $sort} requires the {@code $each} operator.
* Forces document elements to be sorted in given {@literal order}.
* Propagates {@code $sort} to {@code $push}. {@code $sort} requires the {@code $each} operator. Forces document
* 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}.
* @since 1.10
*/
public PushOperatorBuilder sort(Sort order) {
public PushOperatorBuilder sort(Sort sort) {
Assert.notNull(order, "Order must not be 'null'.");
this.modifiers.addModifier(new SortModifier(order));
Assert.notNull(sort, "Sort must not be 'null'.");
this.modifiers.addModifier(new SortModifier(sort));
return this;
}

View File

@@ -429,7 +429,8 @@ public class UpdateMapperUnitTests {
Update update = new Update().push("scores").sort(Direction.DESC).each(42, 23, 68);
DBObject mappedObject = mapper.getMappedObject(update.getUpdateObject(), context.getPersistentEntity(Object.class));
DBObject mappedObject = mapper.getMappedObject(update.getUpdateObject(),
context.getPersistentEntity(ParentClass.class));
DBObject push = getAsDBObject(mappedObject, "$push");
DBObject key = getAsDBObject(push, "scores");
@@ -445,16 +446,19 @@ public class UpdateMapperUnitTests {
@Test
public void updatePushEachWithDocumentSortShouldRenderCorrectly() {
Update update = new Update().push("names").sort(new Sort(new Order(Direction.ASC, "last"), new Order(Direction.ASC, "first")))
Update update = new Update().push("list")
.sort(new Sort(new Order(Direction.ASC, "value"), new Order(Direction.ASC, "field")))
.each(Collections.emptyList());
DBObject mappedObject = mapper.getMappedObject(update.getUpdateObject(), context.getPersistentEntity(Object.class));
DBObject mappedObject = mapper.getMappedObject(update.getUpdateObject(),
context.getPersistentEntity(EntityWithList.class));
DBObject push = getAsDBObject(mappedObject, "$push");
DBObject key = getAsDBObject(push, "names");
DBObject key = getAsDBObject(push, "list");
assertThat(key.containsField("$sort"), is(true));
assertThat((DBObject) key.get("$sort"), equalTo(new BasicDBObjectBuilder().add("last", 1).add("first", 1).get()));
assertThat((DBObject) key.get("$sort"),
equalTo(new BasicDBObjectBuilder().add("renamed-value", 1).add("field", 1).get()));
assertThat(key.containsField("$each"), is(true));
}
@@ -464,8 +468,8 @@ public class UpdateMapperUnitTests {
@Test
public void updatePushEachWithSortShouldRenderCorrectlyWhenUsingMultiplePush() {
Update update = new Update().push("authors").sort(Direction.ASC).each("Harry")
.push("chapters").sort(new Sort(Direction.ASC, "order")).each(Collections.emptyList());
Update update = new Update().push("authors").sort(Direction.ASC).each("Harry").push("chapters")
.sort(new Sort(Direction.ASC, "order")).each(Collections.emptyList());
DBObject mappedObject = mapper.getMappedObject(update.getUpdateObject(), context.getPersistentEntity(Object.class));
@@ -1278,9 +1282,14 @@ public class UpdateMapperUnitTests {
NestedDocument concreteValue;
}
static class EntityWithList {
List<EntityWithAliasedObject> list;
}
static class EntityWithAliasedObject {
@Field("renamed-value") Object value;
Object field;
}
static class EntityWithObjectMap {