From 5448b3e63bfc770552dc874b154594eebf6e823b Mon Sep 17 00:00:00 2001 From: Thomas Darimont Date: Thu, 27 Nov 2014 12:21:21 +0100 Subject: [PATCH] DATACMNS-604 - Move QueryDsl helper methods from QueryDslUtils to KV package. Moved to new class KeyValueQueryDslUtils in order to avoid class loading issues. Original pull request: #107. --- .../support/KeyValueQueryDslUtils.java | 135 ++++++++++++++++++ .../support/QueryDslKeyValueRepository.java | 2 +- .../data/querydsl/QueryDslUtils.java | 108 -------------- .../support}/QueryDslUtilsUnitTests.java | 18 +-- 4 files changed, 146 insertions(+), 117 deletions(-) create mode 100644 src/main/java/org/springframework/data/keyvalue/repository/support/KeyValueQueryDslUtils.java rename src/test/java/org/springframework/data/{querydsl => keyvalue/repository/support}/QueryDslUtilsUnitTests.java (83%) diff --git a/src/main/java/org/springframework/data/keyvalue/repository/support/KeyValueQueryDslUtils.java b/src/main/java/org/springframework/data/keyvalue/repository/support/KeyValueQueryDslUtils.java new file mode 100644 index 000000000..9619a7c37 --- /dev/null +++ b/src/main/java/org/springframework/data/keyvalue/repository/support/KeyValueQueryDslUtils.java @@ -0,0 +1,135 @@ +/* + * Copyright 2014 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.keyvalue.repository.support; + +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.data.querydsl.QSort; +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; + +/** + * @author Christoph Strobl + * @author Thomas Darimont + */ +abstract class KeyValueQueryDslUtils { + + private KeyValueQueryDslUtils() { + // prevent instantiation + } + + /** + * 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> specifiers = null; + + if (sort instanceof QSort) { + specifiers = ((QSort) sort).getOrderSpecifiers(); + } else { + + specifiers = new ArrayList>(); + 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; + } + } +} diff --git a/src/main/java/org/springframework/data/keyvalue/repository/support/QueryDslKeyValueRepository.java b/src/main/java/org/springframework/data/keyvalue/repository/support/QueryDslKeyValueRepository.java index aa853891f..7622eabea 100644 --- a/src/main/java/org/springframework/data/keyvalue/repository/support/QueryDslKeyValueRepository.java +++ b/src/main/java/org/springframework/data/keyvalue/repository/support/QueryDslKeyValueRepository.java @@ -15,7 +15,7 @@ */ package org.springframework.data.keyvalue.repository.support; -import static org.springframework.data.querydsl.QueryDslUtils.*; +import static org.springframework.data.keyvalue.repository.support.KeyValueQueryDslUtils.*; import java.io.Serializable; diff --git a/src/main/java/org/springframework/data/querydsl/QueryDslUtils.java b/src/main/java/org/springframework/data/querydsl/QueryDslUtils.java index db88ca067..1376d7c31 100644 --- a/src/main/java/org/springframework/data/querydsl/QueryDslUtils.java +++ b/src/main/java/org/springframework/data/querydsl/QueryDslUtils.java @@ -15,26 +15,10 @@ */ 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 { @@ -44,96 +28,4 @@ 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> specifiers = null; - - if (sort instanceof QSort) { - specifiers = ((QSort) sort).getOrderSpecifiers(); - } else { - - specifiers = new ArrayList>(); - 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; - } - } } diff --git a/src/test/java/org/springframework/data/querydsl/QueryDslUtilsUnitTests.java b/src/test/java/org/springframework/data/keyvalue/repository/support/QueryDslUtilsUnitTests.java similarity index 83% rename from src/test/java/org/springframework/data/querydsl/QueryDslUtilsUnitTests.java rename to src/test/java/org/springframework/data/keyvalue/repository/support/QueryDslUtilsUnitTests.java index 62f431a63..478e4534f 100644 --- a/src/test/java/org/springframework/data/querydsl/QueryDslUtilsUnitTests.java +++ b/src/test/java/org/springframework/data/keyvalue/repository/support/QueryDslUtilsUnitTests.java @@ -13,10 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.data.querydsl; +package org.springframework.data.keyvalue.repository.support; import static org.hamcrest.collection.IsArrayWithSize.*; import static org.junit.Assert.*; +import static org.springframework.data.keyvalue.repository.support.KeyValueQueryDslUtils.*; import org.hamcrest.collection.IsArrayContainingInOrder; import org.junit.Before; @@ -26,6 +27,7 @@ import org.springframework.data.domain.Sort.Direction; import org.springframework.data.domain.Sort.NullHandling; import org.springframework.data.keyvalue.Person; import org.springframework.data.keyvalue.QPerson; +import org.springframework.data.querydsl.SimpleEntityPathResolver; import com.mysema.query.types.EntityPath; import com.mysema.query.types.OrderSpecifier; @@ -33,6 +35,7 @@ import com.mysema.query.types.path.PathBuilder; /** * @author Christoph Strobl + * @author Thomas Darimont */ public class QueryDslUtilsUnitTests { @@ -51,7 +54,7 @@ public class QueryDslUtilsUnitTests { */ @Test(expected = IllegalArgumentException.class) public void toOrderSpecifierThrowsExceptioOnNullPathBuilder() { - QueryDslUtils.toOrderSpecifier(new Sort("firstname"), null); + toOrderSpecifier(new Sort("firstname"), null); } /** @@ -59,7 +62,7 @@ public class QueryDslUtilsUnitTests { */ @Test public void toOrderSpecifierReturnsEmptyArrayWhenSortIsNull() { - assertThat(QueryDslUtils.toOrderSpecifier(null, builder), arrayWithSize(0)); + assertThat(toOrderSpecifier(null, builder), arrayWithSize(0)); } /** @@ -70,7 +73,7 @@ public class QueryDslUtilsUnitTests { Sort sort = new Sort(Direction.ASC, "firstname"); - OrderSpecifier[] specifiers = QueryDslUtils.toOrderSpecifier(sort, builder); + OrderSpecifier[] specifiers = toOrderSpecifier(sort, builder); assertThat(specifiers, IsArrayContainingInOrder.> arrayContaining(QPerson.person.firstname.asc())); } @@ -83,7 +86,7 @@ public class QueryDslUtilsUnitTests { Sort sort = new Sort(Direction.DESC, "firstname"); - OrderSpecifier[] specifiers = QueryDslUtils.toOrderSpecifier(sort, builder); + OrderSpecifier[] specifiers = toOrderSpecifier(sort, builder); assertThat(specifiers, IsArrayContainingInOrder.> arrayContaining(QPerson.person.firstname.desc())); @@ -97,7 +100,7 @@ public class QueryDslUtilsUnitTests { Sort sort = new Sort(Direction.DESC, "firstname").and(new Sort(Direction.ASC, "age")); - OrderSpecifier[] specifiers = QueryDslUtils.toOrderSpecifier(sort, builder); + OrderSpecifier[] specifiers = toOrderSpecifier(sort, builder); assertThat(specifiers, IsArrayContainingInOrder.> arrayContaining( QPerson.person.firstname.desc(), QPerson.person.age.asc())); @@ -111,10 +114,9 @@ public class QueryDslUtilsUnitTests { Sort sort = new Sort(new Sort.Order(Direction.DESC, "firstname", NullHandling.NULLS_LAST)); - OrderSpecifier[] specifiers = QueryDslUtils.toOrderSpecifier(sort, builder); + OrderSpecifier[] specifiers = toOrderSpecifier(sort, builder); assertThat(specifiers, IsArrayContainingInOrder.> arrayContaining(QPerson.person.firstname.desc().nullsLast())); } - }