DATACMNS-525 - Introduce infrastructure for KeyValue repositories.

Added basic infrastructure for repositories on top of key-value data stores. For more details see the original pull request.

Original pull request: #95.
This commit is contained in:
Christoph Strobl
2014-09-22 15:29:52 +02:00
committed by Oliver Gierke
parent e1b38faee9
commit e2358f3bf8
66 changed files with 9455 additions and 18 deletions

View File

@@ -15,10 +15,26 @@
*/
package org.springframework.data.querydsl;
import java.util.ArrayList;
import java.util.List;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.mapping.PropertyPath;
import org.springframework.util.Assert;
import com.mysema.query.support.Expressions;
import com.mysema.query.types.Expression;
import com.mysema.query.types.OrderSpecifier;
import com.mysema.query.types.OrderSpecifier.NullHandling;
import com.mysema.query.types.Path;
import com.mysema.query.types.path.PathBuilder;
/**
* Utility class for Querydsl.
*
* @author Oliver Gierke
* @author Christoph Strobl
*/
public abstract class QueryDslUtils {
@@ -28,4 +44,96 @@ public abstract class QueryDslUtils {
private QueryDslUtils() {
}
/**
* Transforms a plain {@link Order} into a QueryDsl specific {@link OrderSpecifier}.
*
* @param sort
* @param builder must not be {@literal null}.
* @return empty {@code OrderSpecifier<?>[]} when sort is {@literal null}.
*/
public static OrderSpecifier<?>[] toOrderSpecifier(Sort sort, PathBuilder<?> builder) {
Assert.notNull(builder, "Builder must not be 'null'.");
if (sort == null) {
return new OrderSpecifier<?>[0];
}
List<OrderSpecifier<?>> specifiers = null;
if (sort instanceof QSort) {
specifiers = ((QSort) sort).getOrderSpecifiers();
} else {
specifiers = new ArrayList<OrderSpecifier<?>>();
for (Order order : sort) {
specifiers.add(toOrderSpecifier(order, builder));
}
}
return specifiers.toArray(new OrderSpecifier<?>[specifiers.size()]);
}
@SuppressWarnings({ "rawtypes", "unchecked" })
private static OrderSpecifier<?> toOrderSpecifier(Order order, PathBuilder<?> builder) {
return new OrderSpecifier(order.isAscending() ? com.mysema.query.types.Order.ASC
: com.mysema.query.types.Order.DESC, buildOrderPropertyPathFrom(order, builder),
toQueryDslNullHandling(order.getNullHandling()));
}
/**
* Creates an {@link Expression} for the given {@link Order} property.
*
* @param order must not be {@literal null}.
* @param builder must not be {@literal null}.
* @return
*/
private static Expression<?> buildOrderPropertyPathFrom(Order order, PathBuilder<?> builder) {
Assert.notNull(order, "Order must not be null!");
Assert.notNull(builder, "Builder must not be null!");
PropertyPath path = PropertyPath.from(order.getProperty(), builder.getType());
Expression<?> sortPropertyExpression = builder;
while (path != null) {
if (!path.hasNext() && order.isIgnoreCase()) {
// if order is ignore-case we have to treat the last path segment as a String.
sortPropertyExpression = Expressions.stringPath((Path<?>) sortPropertyExpression, path.getSegment()).lower();
} else {
sortPropertyExpression = Expressions.path(path.getType(), (Path<?>) sortPropertyExpression, path.getSegment());
}
path = path.next();
}
return sortPropertyExpression;
}
/**
* Converts the given {@link org.springframework.data.domain.Sort.NullHandling} to the appropriate Querydsl
* {@link NullHandling}.
*
* @param nullHandling must not be {@literal null}.
* @return
*/
private static NullHandling toQueryDslNullHandling(org.springframework.data.domain.Sort.NullHandling nullHandling) {
Assert.notNull(nullHandling, "NullHandling must not be null!");
switch (nullHandling) {
case NULLS_FIRST:
return NullHandling.NullsFirst;
case NULLS_LAST:
return NullHandling.NullsLast;
case NATIVE:
default:
return NullHandling.Default;
}
}
}