|
6 | 6 | import java.util.Locale;
|
7 | 7 |
|
8 | 8 | import net.sf.mmm.util.exception.api.IllegalCaseException;
|
9 |
| -import net.sf.mmm.util.search.base.AbstractSearchCriteria; |
10 | 9 |
|
11 | 10 | import org.slf4j.Logger;
|
12 | 11 | import org.slf4j.LoggerFactory;
|
| 12 | +import org.springframework.data.domain.Page; |
| 13 | +import org.springframework.data.domain.PageImpl; |
| 14 | +import org.springframework.data.domain.Pageable; |
| 15 | +import org.springframework.data.domain.Sort; |
| 16 | +import org.springframework.data.domain.Sort.Direction; |
| 17 | +import org.springframework.data.domain.Sort.Order; |
13 | 18 | import org.springframework.util.StringUtils;
|
14 | 19 |
|
15 | 20 | import com.querydsl.core.JoinExpression;
|
|
26 | 31 | import io.oasp.module.basic.common.api.query.LikePatternSyntax;
|
27 | 32 | import io.oasp.module.basic.common.api.query.StringSearchConfigTo;
|
28 | 33 | import io.oasp.module.basic.common.api.query.StringSearchOperator;
|
29 |
| -import io.oasp.module.jpa.common.api.to.OrderByTo; |
30 |
| -import io.oasp.module.jpa.common.api.to.OrderDirection; |
31 |
| -import io.oasp.module.jpa.common.api.to.PaginatedListTo; |
32 |
| -import io.oasp.module.jpa.common.api.to.PaginationResultTo; |
33 |
| -import io.oasp.module.jpa.common.api.to.PaginationTo; |
34 |
| -import io.oasp.module.jpa.common.api.to.SearchCriteriaTo; |
35 | 34 |
|
36 | 35 | /**
|
37 |
| - * Class with utility methods for dealing with QueryDSL. Either extend this class or use {@link QueryDslUtil#get()}. |
| 36 | + * Class with utility methods for dealing with queries. Either extend this class or use {@link QueryUtil#get()}. |
38 | 37 | *
|
39 | 38 | * @since 3.0.0
|
40 | 39 | */
|
41 |
| -public class QueryDslHelper { |
| 40 | +public class QueryHelper { |
42 | 41 |
|
43 |
| - private static final Logger LOG = LoggerFactory.getLogger(QueryDslHelper.class); |
| 42 | + private static final Logger LOG = LoggerFactory.getLogger(QueryHelper.class); |
44 | 43 |
|
45 | 44 | /** JPA query property to configure the timeout in milliseconds. */
|
46 | 45 | protected static final String QUERY_PROPERTY_TIMEOUT = "javax.persistence.query.timeout";
|
47 | 46 |
|
48 |
| - /** |
49 |
| - * Returns a paginated list of entities according to the supplied {@link SearchCriteriaTo criteria}. |
50 |
| - * <p> |
51 |
| - * Applies {@code limit} and {@code offset} values to the supplied {@code query} according to the supplied |
52 |
| - * {@link PaginationTo pagination} information inside {@code criteria}. |
53 |
| - * <p> |
54 |
| - * If a {@link PaginationTo#isTotal() total count} of available entities is requested, will also execute a second |
55 |
| - * query, without pagination parameters applied, to obtain said count. |
56 |
| - * <p> |
57 |
| - * Will install a query timeout if {@link SearchCriteriaTo#getSearchTimeout()} is not null. |
58 |
| - * |
59 |
| - * @param <E> generic type of the entity. |
60 |
| - * @param criteria contains information about the requested page. |
61 |
| - * @param query is a query which is preconfigured with the desired conditions for the search. |
62 |
| - * @param applySortOrder - {@code true} to automatically {@link #applySortOrder(List, JPAQuery) apply} the |
63 |
| - * {@link SearchCriteriaTo#getSort() sort order} from the given {@link SearchCriteriaTo}, {@code false} |
64 |
| - * otherwise (to apply manually for complex queries). |
65 |
| - * @return a paginated list. |
66 |
| - */ |
67 |
| - protected <E> PaginatedListTo<E> findPaginatedGeneric(SearchCriteriaTo criteria, JPAQuery<E> query, |
68 |
| - boolean applySortOrder) { |
69 |
| - |
70 |
| - applyTimeout(query, criteria.getSearchTimeout()); |
71 |
| - |
72 |
| - PaginationTo pagination = criteria.getPagination(); |
73 |
| - PaginationResultTo paginationResult = createPaginationResult(pagination, query); |
74 |
| - applyPagination(pagination, query); |
75 |
| - |
76 |
| - if (applySortOrder) { |
77 |
| - applySortOrder(criteria.getSort(), query); |
78 |
| - } |
79 |
| - |
80 |
| - List<E> paginatedList = query.fetch(); |
81 |
| - |
82 |
| - return new PaginatedListTo<>(paginatedList, paginationResult); |
83 |
| - } |
84 |
| - |
85 |
| - /** |
86 |
| - * @param sort the {@link SearchCriteriaTo#getSort() sort order}. |
87 |
| - * @param query the {@link JPAQuery} to modify. |
88 |
| - */ |
89 |
| - @SuppressWarnings("rawtypes") |
90 |
| - protected void applySortOrder(List<OrderByTo> sort, JPAQuery<?> query) { |
91 |
| - |
92 |
| - if ((sort == null) || sort.isEmpty()) { |
93 |
| - return; |
94 |
| - } |
95 |
| - PathBuilder<?> alias = findAlias(query); |
96 |
| - for (OrderByTo orderBy : sort) { |
97 |
| - String name = orderBy.getName(); |
98 |
| - ComparablePath<Comparable> path = alias.getComparable(name, Comparable.class); |
99 |
| - OrderSpecifier<Comparable> orderSpecifier; |
100 |
| - if (orderBy.getDirection() == OrderDirection.ASC) { |
101 |
| - orderSpecifier = path.asc(); |
102 |
| - } else { |
103 |
| - orderSpecifier = path.desc(); |
104 |
| - } |
105 |
| - query.orderBy(orderSpecifier); |
106 |
| - } |
107 |
| - } |
108 |
| - |
109 |
| - private <E> PathBuilder<E> findAlias(JPAQuery<E> query) { |
110 |
| - |
111 |
| - String alias = null; |
112 |
| - List<JoinExpression> joins = query.getMetadata().getJoins(); |
113 |
| - if ((joins != null) && !joins.isEmpty()) { |
114 |
| - JoinExpression join = joins.get(0); |
115 |
| - Expression<?> target = join.getTarget(); |
116 |
| - if (target instanceof EntityPath) { |
117 |
| - alias = target.toString(); // no safe API |
118 |
| - } |
119 |
| - } |
120 |
| - Class<E> type = query.getType(); |
121 |
| - if (alias == null) { |
122 |
| - // should actually never happen, but fallback is provided as buest guess |
123 |
| - alias = StringUtils.uncapitalize(type.getSimpleName()); |
124 |
| - } |
125 |
| - return new PathBuilder<>(type, alias); |
126 |
| - } |
127 |
| - |
128 |
| - /** |
129 |
| - * Creates a {@link PaginationResultTo pagination result} for the given {@code pagination} and {@code query}. |
130 |
| - * <p> |
131 |
| - * Needs to be called before pagination is applied to the {@code query}. |
132 |
| - * |
133 |
| - * @param pagination contains information about the requested page. |
134 |
| - * @param query is a query preconfigured with the desired conditions for the search. |
135 |
| - * @return information about the applied pagination. |
136 |
| - */ |
137 |
| - protected PaginationResultTo createPaginationResult(PaginationTo pagination, JPAQuery<?> query) { |
138 |
| - |
139 |
| - Long total = calculateTotalBeforePagination(pagination, query); |
140 |
| - return new PaginationResultTo(pagination, total); |
141 |
| - } |
142 |
| - |
143 |
| - /** |
144 |
| - * Calculates the total number of entities the given {@link JPAQuery query} would return without pagination applied. |
145 |
| - * <p> |
146 |
| - * Needs to be called before pagination is applied to the {@code query}. |
147 |
| - * |
148 |
| - * @param pagination is the pagination information as requested by the client. |
149 |
| - * @param query is the {@link JPAQuery query} for which to calculate the total. |
150 |
| - * @return the total count, or {@literal null} if {@link PaginationTo#isTotal()} is {@literal false}. |
151 |
| - */ |
152 |
| - protected Long calculateTotalBeforePagination(PaginationTo pagination, JPAQuery<?> query) { |
153 |
| - |
154 |
| - Long total = null; |
155 |
| - if (pagination.isTotal()) { |
156 |
| - total = query.clone().fetchCount(); |
157 |
| - } |
158 |
| - return total; |
159 |
| - } |
160 |
| - |
161 |
| - /** |
162 |
| - * Applies the {@link PaginationTo pagination criteria} to the given {@link JPAQuery}. |
163 |
| - * |
164 |
| - * @param pagination is the {@link PaginationTo pagination criteria} to apply. |
165 |
| - * @param query is the {@link JPAQuery} to apply to. |
166 |
| - */ |
167 |
| - protected void applyPagination(PaginationTo pagination, JPAQuery<?> query) { |
168 |
| - |
169 |
| - if (pagination == PaginationTo.NO_PAGINATION) { |
170 |
| - return; |
171 |
| - } |
172 |
| - |
173 |
| - Integer limit = pagination.getSize(); |
174 |
| - if (limit != null) { |
175 |
| - query.limit(limit); |
176 |
| - |
177 |
| - int page = pagination.getPage(); |
178 |
| - if (page > 0) { |
179 |
| - query.offset((page - 1) * limit); |
180 |
| - } |
181 |
| - } |
182 |
| - } |
183 |
| - |
184 |
| - /** |
185 |
| - * Applies the meta-data of the given {@link AbstractSearchCriteria search criteria} to the given {@link JPAQuery}. |
186 |
| - * |
187 |
| - * @param criteria is the {@link AbstractSearchCriteria search criteria} to apply. |
188 |
| - * @param query is the {@link JPAQuery} to apply to. |
189 |
| - */ |
190 |
| - protected void applyCriteria(AbstractSearchCriteria criteria, JPAQuery<?> query) { |
191 |
| - |
192 |
| - Integer limit = criteria.getMaximumHitCount(); |
193 |
| - if (limit != null) { |
194 |
| - query.limit(limit); |
195 |
| - } |
196 |
| - int offset = criteria.getHitOffset(); |
197 |
| - if (offset > 0) { |
198 |
| - query.offset(offset); |
199 |
| - } |
200 |
| - Long timeout = criteria.getSearchTimeout(); |
201 |
| - applyTimeout(query, timeout); |
202 |
| - } |
203 |
| - |
204 | 47 | /**
|
205 | 48 | * @param query the {@link JPAQuery} to modify.
|
206 | 49 | * @param timeout the timeout in milliseconds.
|
@@ -509,4 +352,78 @@ protected <T> void whereIn(JPAQuery<?> query, SimpleExpression<T> expression, Co
|
509 | 352 | }
|
510 | 353 | }
|
511 | 354 |
|
| 355 | + /** |
| 356 | + * Returns a {@link Page} of entities according to the supplied {@link Pageable} and {@link JPAQuery}. |
| 357 | + * |
| 358 | + * @param <E> generic type of the entity. |
| 359 | + * @param pageable contains information about the requested page and sorting. |
| 360 | + * @param query is a query which is pre-configured with the desired conditions for the search. |
| 361 | + * @param determineTotal - {@code true} to determine the {@link Page#getTotalElements() total number of hits}, |
| 362 | + * {@code false} otherwise. |
| 363 | + * @return a paginated list. |
| 364 | + */ |
| 365 | + protected <E> Page<E> findPaginatedGeneric(Pageable pageable, JPAQuery<E> query, boolean determineTotal) { |
| 366 | + |
| 367 | + long total = -1; |
| 368 | + if (determineTotal) { |
| 369 | + total = query.clone().fetchCount(); |
| 370 | + } |
| 371 | + int offset = 0; |
| 372 | + if (pageable != null) { |
| 373 | + offset = pageable.getOffset(); |
| 374 | + query.offset(offset); |
| 375 | + query.limit(pageable.getPageSize()); |
| 376 | + applySort(query, pageable.getSort()); |
| 377 | + } |
| 378 | + List<E> hits = query.fetch(); |
| 379 | + if (total == -1) { |
| 380 | + total = offset + hits.size(); |
| 381 | + } |
| 382 | + return new PageImpl<>(hits, pageable, total); |
| 383 | + } |
| 384 | + |
| 385 | + /** |
| 386 | + * @param query the {@link JPAQuery} to apply the {@link Sort} to. |
| 387 | + * @param sort the {@link Sort} to apply as ORDER BY to the given {@link JPAQuery}. |
| 388 | + */ |
| 389 | + @SuppressWarnings("rawtypes") |
| 390 | + protected void applySort(JPAQuery<?> query, Sort sort) { |
| 391 | + |
| 392 | + if (sort == null) { |
| 393 | + return; |
| 394 | + } |
| 395 | + PathBuilder<?> alias = findAlias(query); |
| 396 | + for (Order order : sort) { |
| 397 | + String property = order.getProperty(); |
| 398 | + Direction direction = order.getDirection(); |
| 399 | + ComparablePath<Comparable> path = alias.getComparable(property, Comparable.class); |
| 400 | + OrderSpecifier<Comparable> orderSpecifier; |
| 401 | + if (direction == Direction.ASC) { |
| 402 | + orderSpecifier = path.asc(); |
| 403 | + } else { |
| 404 | + orderSpecifier = path.desc(); |
| 405 | + } |
| 406 | + query.orderBy(orderSpecifier); |
| 407 | + } |
| 408 | + } |
| 409 | + |
| 410 | + private <E> PathBuilder<E> findAlias(JPAQuery<E> query) { |
| 411 | + |
| 412 | + String alias = null; |
| 413 | + List<JoinExpression> joins = query.getMetadata().getJoins(); |
| 414 | + if ((joins != null) && !joins.isEmpty()) { |
| 415 | + JoinExpression join = joins.get(0); |
| 416 | + Expression<?> target = join.getTarget(); |
| 417 | + if (target instanceof EntityPath) { |
| 418 | + alias = target.toString(); // no safe API |
| 419 | + } |
| 420 | + } |
| 421 | + Class<E> type = query.getType(); |
| 422 | + if (alias == null) { |
| 423 | + // should actually never happen, but fallback is provided as buest guess |
| 424 | + alias = StringUtils.uncapitalize(type.getSimpleName()); |
| 425 | + } |
| 426 | + return new PathBuilder<>(type, alias); |
| 427 | + } |
| 428 | + |
512 | 429 | }
|
0 commit comments