-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathSearchParameters.java
774 lines (636 loc) · 20.8 KB
/
SearchParameters.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
/*
* Copyright 2015 JAXIO http://www.jaxio.com
*
* 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 com.jaxio.jpa.querybyexample;
import com.google.common.base.Function;
import org.apache.commons.lang.builder.ToStringBuilder;
import javax.persistence.metamodel.Attribute;
import javax.persistence.metamodel.SingularAttribute;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Lists.transform;
import static com.google.common.collect.Maps.newHashMap;
import static com.google.common.collect.Sets.newHashSet;
import static com.jaxio.jpa.querybyexample.PropertySelector.newPropertySelector;
import static org.apache.commons.lang.StringUtils.isNotBlank;
/*
* The SearchParameters is used to pass search parameters to the DAO layer.
* <p>
* Its usage keeps 'find' method signatures in the DAO/Service layer simple.
* <p>
* A SearchParameters helps you drive your search in the following areas:
* <ul>
* <li>Configure the search mode (EQUALS, LIKE, ...)</li>
* <li>Pagination: it allows you to limit your search results to a specific range.</li>
* <li>Allow you to specify ORDER BY and ASC/DESC</li>
* <li>Enable/disable case sensitivity</li>
* <li>Enable/disable 2d level cache</li>
* <li>LIKE search against all string values: simply set the searchPattern property</li>
* <li>Named query: if you set a named query it will be executed. Named queries can be defined in annotation or src/main/resources/META-INF/orm.xml</li>
* <li>FullTextSearch: simply set the term property (requires Hibernate Search)</li>
* </ul>
* <p>
* Note : All requests are limited to a maximum number of elements to prevent resource exhaustion.
*
* @see SearchMode
* @see OrderBy
* @see Range
* @see PropertySelector
*/
public class SearchParameters implements Serializable {
private static final long serialVersionUID = 1L;
private SearchMode searchMode = SearchMode.EQUALS;
private boolean andMode = true;
// named query related
private String namedQuery;
private Map<String, Object> parameters = newHashMap();
private Set<OrderBy> orders = newHashSet();
// technical parameters
private boolean caseSensitive = true;
// pagination
private int maxResults = -1;
private int first = 0;
private int pageSize = 0;
// fetches
private Set<PathHolder> fetches = newHashSet();
// ranges
private List<Range<?, ?>> ranges = newArrayList();
// property selectors
private List<PropertySelector<?, ?>> properties = newArrayList();
// pattern to match against all strings.
private String searchPattern;
// hibernate search terms
private List<TermSelector> terms = newArrayList();
private Float searchSimilarity = 0.5f;
private LuceneQueryBuilder luceneQueryBuilder = new DefaultLuceneQueryBuilder();
// Warn: before enabling cache for queries,
// check this: https://hibernate.atlassian.net/browse/HHH-1523
private Boolean cacheable = false;
private String cacheRegion;
// extra parameters
private Map<String, Object> extraParameters = newHashMap();
private boolean useAndInXToMany = true;
private boolean useDistinct = false;
// -----------------------------------
// SearchMode
// -----------------------------------
/*
* Fluently set the @{link SearchMode}. It defaults to EQUALS.
*
* @param searchMode searchmode
* @see SearchMode#EQUALS
*/
public void setSearchMode(SearchMode searchMode) {
this.searchMode = checkNotNull(searchMode);
}
/*
* Return the @{link SearchMode}. It defaults to EQUALS.
*
* @see SearchMode#EQUALS
*/
public SearchMode getSearchMode() {
return searchMode;
}
public boolean is(SearchMode searchMode) {
return getSearchMode() == searchMode;
}
/*
* Fluently set the @{link SearchMode}. It defaults to EQUALS.
*
* @param searchMode searchmode
* @see SearchMode#EQUALS
*/
public SearchParameters searchMode(SearchMode searchMode) {
setSearchMode(searchMode);
return this;
}
/*
* Use the EQUALS @{link SearchMode}.
*
* @param searchMode searchmode
* @see SearchMode#EQUALS
*/
public SearchParameters equals() {
return searchMode(SearchMode.EQUALS);
}
/*
* Use the ANYWHERE @{link SearchMode}.
*
* @param searchMode searchmode
* @see SearchMode#ANYWHERE
*/
public SearchParameters anywhere() {
return searchMode(SearchMode.ANYWHERE);
}
/*
* Use the STARTING_LIKE @{link SearchMode}.
*
* @see SearchMode#STARTING_LIKE
*/
public SearchParameters startingLike() {
return searchMode(SearchMode.STARTING_LIKE);
}
/*
* Use the LIKE @{link SearchMode}.
*
* @see SearchMode#LIKE
*/
public SearchParameters like() {
return searchMode(SearchMode.LIKE);
}
/*
* Use the ENDING_LIKE @{link SearchMode}.
*
* @see SearchMode#ENDING_LIKE
*/
public SearchParameters endingLike() {
return searchMode(SearchMode.ENDING_LIKE);
}
// -----------------------------------
// Predicate mode
// -----------------------------------
public void setAndMode(boolean andMode) {
this.andMode = andMode;
}
public boolean isAndMode() {
return andMode;
}
/*
* use <code>and</code> to build the final predicate
*/
public SearchParameters andMode() {
setAndMode(true);
return this;
}
/*
* use <code>or</code> to build the final predicate
*/
public SearchParameters orMode() {
setAndMode(false);
return this;
}
// -----------------------------------
// Named query support
// -----------------------------------
/*
* Returns true if a named query has been set, false otherwise. When it returns true, the DAO layer will call the namedQuery.
*/
public boolean hasNamedQuery() {
return isNotBlank(namedQuery);
}
/*
* Set the named query to be used by the DAO layer. Null by default.
*/
public void setNamedQuery(String namedQuery) {
this.namedQuery = namedQuery;
}
/*
* Return the name of the named query to be used by the DAO layer.
*/
public String getNamedQuery() {
return namedQuery;
}
/*
* Set the parameters for the named query.
*/
public void setNamedQueryParameters(Map<String, Object> parameters) {
this.parameters = checkNotNull(parameters);
}
/*
* Set the parameters for the named query.
*/
public void addNamedQueryParameter(String name, Object value) {
parameters.put(checkNotNull(name), checkNotNull(value));
}
/*
* The parameters associated with the named query, if any.
*/
public Map<String, Object> getNamedQueryParameters() {
return parameters;
}
/*
* Return the value of the given parameter name.
*/
public Object getNamedQueryParameter(String parameterName) {
return parameters.get(checkNotNull(parameterName));
}
/*
* Fluently set the named query to be used by the DAO layer. Null by default.
*/
public SearchParameters namedQuery(String namedQuery) {
setNamedQuery(namedQuery);
return this;
}
/*
* Fluently set the parameters for the named query.
*/
public SearchParameters namedQueryParameters(Map<String, Object> parameters) {
setNamedQueryParameters(parameters);
return this;
}
/*
* Fluently set the parameter for the named query.
*/
public SearchParameters namedQueryParameter(String name, Object value) {
addNamedQueryParameter(name, value);
return this;
}
// -----------------------------------
// Search pattern support
// -----------------------------------
/*
* When it returns true, it indicates to the DAO layer to use the given searchPattern on all string properties.
*/
public boolean hasSearchPattern() {
return isNotBlank(searchPattern);
}
/*
* Set the pattern which may contains wildcards (ex: <code>e%r%ka</code> ).
* <p>
* The given searchPattern is used by the DAO layer on all string properties. Null by default.
*/
public void setSearchPattern(String searchPattern) {
this.searchPattern = searchPattern;
}
public SearchParameters searchPattern(String searchPattern) {
setSearchPattern(searchPattern);
return this;
}
public String getSearchPattern() {
return searchPattern;
}
// -----------------------------------
// Terms support (hibernate search)
// -----------------------------------
public void addTerm(TermSelector term) {
terms.add(checkNotNull(term));
}
public List<TermSelector> getTerms() {
return terms;
}
public boolean hasTerms() {
return !terms.isEmpty();
}
public Float getSearchSimilarity() {
return searchSimilarity;
}
public void setSearchSimilarity(Float searchSimilarity) {
this.searchSimilarity = searchSimilarity;
}
public LuceneQueryBuilder getLuceneQueryBuilder() {
return luceneQueryBuilder;
}
public void setLuceneQueryBuilder(LuceneQueryBuilder luceneQueryBuilder) {
this.luceneQueryBuilder = checkNotNull(luceneQueryBuilder);
}
public SearchParameters term(TermSelector... terms) {
for (TermSelector term : checkNotNull(terms)) {
addTerm(term);
}
return this;
}
public SearchParameters term(String... selected) {
return term(new TermSelector().selected(selected));
}
public SearchParameters term(SingularAttribute<?, ?> attribute, String... selected) {
return term(new TermSelector(attribute).selected(selected));
}
/*
* Specify the similarity for the indexed properties, {@link #searchSimilarity} is between 0f and 1f
*/
public SearchParameters searchSimilarity(Float searchSimilarity) {
setSearchSimilarity(searchSimilarity);
return this;
}
public SearchParameters luceneQueryBuilder(LuceneQueryBuilder luceneQueryBuilder) {
setLuceneQueryBuilder(luceneQueryBuilder);
return this;
}
// -----------------------------------
// Case sensitiveness support
// -----------------------------------
/*
* Set the case sensitiveness. Defaults to false.
*
* @param caseSensitive caseSensitive
*/
public void setCaseSensitive(boolean caseSensitive) {
this.caseSensitive = caseSensitive;
}
public boolean isCaseSensitive() {
return caseSensitive;
}
public boolean isCaseInsensitive() {
return !caseSensitive;
}
/*
* Fluently set the case sensitiveness. Defaults to false.
*
* @param caseSensitive caseSensitive
*/
public SearchParameters caseSensitive(boolean caseSensitive) {
setCaseSensitive(caseSensitive);
return this;
}
/*
* Fluently set the case sensitiveness to true.
*/
public SearchParameters caseSensitive() {
return caseSensitive(true);
}
/*
* Fluently set the case sensitiveness to false.
*/
public SearchParameters caseInsensitive() {
return caseSensitive(false);
}
// -----------------------------------
// Order by support
// -----------------------------------
public List<OrderBy> getOrders() {
return newArrayList(orders);
}
public void addOrderBy(OrderBy orderBy) {
if (!orders.add(checkNotNull(orderBy))) {
throw new IllegalArgumentException("Duplicate orderBy: '" + orderBy + "'.");
}
}
public boolean hasOrders() {
return !orders.isEmpty();
}
public SearchParameters orderBy(OrderBy... orderBys) {
for (OrderBy orderBy : checkNotNull(orderBys)) {
addOrderBy(orderBy);
}
return this;
}
public SearchParameters asc(Attribute<?, ?>... attributes) {
return orderBy(new OrderBy(OrderByDirection.ASC, attributes));
}
public SearchParameters desc(Attribute<?, ?>... attributes) {
return orderBy(new OrderBy(OrderByDirection.DESC, attributes));
}
public SearchParameters orderBy(OrderByDirection orderByDirection, Attribute<?, ?>... attributes) {
return orderBy(new OrderBy(orderByDirection, attributes));
}
public SearchParameters asc(String property, Class<?> from) {
return orderBy(new OrderBy(OrderByDirection.ASC, property, from));
}
public SearchParameters desc(String property, Class<?> from) {
return orderBy(new OrderBy(OrderByDirection.DESC, property, from));
}
public SearchParameters orderBy(OrderByDirection orderByDirection, String property, Class<?> from) {
return orderBy(new OrderBy(orderByDirection, property, from));
}
// -----------------------------------
// Search by range support
// -----------------------------------
public List<Range<?, ?>> getRanges() {
return ranges;
}
public void addRange(Range<?, ?> range) {
ranges.add(checkNotNull(range));
}
public boolean hasRanges() {
return !ranges.isEmpty();
}
public SearchParameters range(Range<?, ?>... ranges) {
for (Range<?, ?> range : checkNotNull(ranges)) {
addRange(range);
}
return this;
}
public <D extends Comparable<? super D>> SearchParameters range(D from, D to, Attribute<?, ?>... attributes) {
return range(new Range<D, D>(from, to, attributes));
}
// -----------------------------------
// Search by property selector support
// -----------------------------------
public List<PropertySelector<?, ?>> getProperties() {
return properties;
}
public void addProperty(PropertySelector<?, ?> propertySelector) {
properties.add(checkNotNull(propertySelector));
}
public boolean hasProperties() {
return !properties.isEmpty();
}
public SearchParameters property(PropertySelector<?, ?>... propertySelectors) {
for (PropertySelector<?, ?> propertySelector : checkNotNull(propertySelectors)) {
addProperty(propertySelector);
}
return this;
}
public <F> SearchParameters property(Attribute<?, ?> fields, F... selected) {
return property(newPropertySelector(fields).selected(selected));
}
// -----------------------------------
// Pagination support
// -----------------------------------
/*
* Set the maximum number of results to retrieve. Pass -1 for no limits.
*/
public void setMaxResults(int maxResults) {
this.maxResults = maxResults;
}
public int getMaxResults() {
return maxResults;
}
/*
* Set the position of the first result to retrieve.
*
* @param first position of the first result, numbered from 0
*/
public void setFirst(int first) {
this.first = first;
}
public int getFirst() {
return first;
}
/*
* Set the page size, that is the maximum number of result to retrieve.
*/
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPageSize() {
return pageSize;
}
public SearchParameters maxResults(int maxResults) {
setMaxResults(maxResults);
return this;
}
public SearchParameters noLimit() {
setMaxResults(-1);
return this;
}
public SearchParameters limitBroadSearch() {
setMaxResults(500);
return this;
}
public SearchParameters first(int first) {
setFirst(first);
return this;
}
public SearchParameters pageSize(int pageSize) {
setPageSize(pageSize);
return this;
}
// -----------------------------------------
// Fetch associated entity using a LEFT Join
// -----------------------------------------
/*
* Returns the attributes (x-to-one association) which must be fetched with a left join.
*/
public List<List<Attribute<?, ?>>> getFetches() {
return transform(newArrayList(fetches), new Function<PathHolder, List<Attribute<?, ?>>>() {
public List<Attribute<?, ?>> apply(PathHolder input) {
return input.getAttributes();
}
});
}
public boolean hasFetches() {
return !fetches.isEmpty();
}
/*
* The given attribute (x-to-one association) will be fetched with a left join.
*/
public void addFetch(Attribute<?, ?>... attributes) {
addFetch(newArrayList(attributes));
}
public void addFetch(List<Attribute<?, ?>> attributes) {
fetches.add(new PathHolder(attributes));
}
/*
* Fluently set the fetch attribute
*/
public SearchParameters fetch(Attribute<?, ?>... attributes) {
fetch(newArrayList(attributes));
return this;
}
/*
* Fluently set the fetch attribute
*/
public SearchParameters fetch(List<Attribute<?, ?>> attributes) {
addFetch(attributes);
return this;
}
// -----------------------------------
// Caching support
// -----------------------------------
/*
* Default to false. Please read https://hibernate.atlassian.net/browse/HHH-1523 before using cache.
*/
public void setCacheable(boolean cacheable) {
this.cacheable = cacheable;
}
public boolean isCacheable() {
return cacheable;
}
public boolean hasCacheRegion() {
return isNotBlank(cacheRegion);
}
public void setCacheRegion(String cacheRegion) {
this.cacheRegion = cacheRegion;
}
public String getCacheRegion() {
return cacheRegion;
}
public SearchParameters cacheable(boolean cacheable) {
setCacheable(cacheable);
return this;
}
public SearchParameters enableCache() {
setCacheable(true);
return this;
}
public SearchParameters disableCache() {
setCacheable(false);
return this;
}
public SearchParameters cacheRegion(String cacheRegion) {
setCacheRegion(checkNotNull(cacheRegion));
return this;
}
// -----------------------------------
// Extra parameters
// -----------------------------------
/*
* Set additionnal parameters.
*/
public void setExtraParameters(Map<String, Object> extraParameters) {
this.extraParameters = extraParameters;
}
public Map<String, Object> getExtraParameters() {
return extraParameters;
}
/*
* add additionnal parameter.
*/
public SearchParameters addExtraParameter(String key, Object o) {
extraParameters.put(checkNotNull(key), o);
return this;
}
/*
* get additionnal parameter.
*/
@SuppressWarnings("unchecked")
public <T> T getExtraParameter(String key) {
return (T) extraParameters.get(key);
}
// -----------------------------------
// Use and in XToMany Search
// -----------------------------------
public void setUseAndInXToMany(boolean useAndInXToMany) {
this.useAndInXToMany = useAndInXToMany;
}
public boolean getUseAndInXToMany() {
return useAndInXToMany;
}
public SearchParameters useOrInXToMany() {
return useAndInXToMany(false);
}
public SearchParameters useAndInXToMany() {
return useAndInXToMany(true);
}
public SearchParameters useAndInXToMany(boolean xToManyAndMode) {
setUseAndInXToMany(xToManyAndMode);
return this;
}
// -----------------------------------
// Distinct
// -----------------------------------
public void setDistinct(boolean useDistinct) {
this.useDistinct = useDistinct;
}
public boolean getDistinct() {
return useDistinct;
}
public SearchParameters distinct(boolean useDistinct) {
setDistinct(useDistinct);
return this;
}
public SearchParameters distinct() {
return distinct(true);
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}