/
HibernateCriteriaBuilder.java
1476 lines (1321 loc) · 56.2 KB
/
HibernateCriteriaBuilder.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
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2004-2005 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 grails.orm;
import groovy.lang.*;
import org.codehaus.groovy.grails.commons.*;
import org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsHibernateUtil;
import org.hibernate.*;
import org.hibernate.criterion.*;
import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.metadata.ClassMetadata;
import org.hibernate.transform.ResultTransformer;
import org.hibernate.type.AssociationType;
import org.hibernate.type.Type;
import org.springframework.beans.BeanUtils;
import org.springframework.orm.hibernate3.SessionHolder;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import java.beans.PropertyDescriptor;
import java.util.*;
/**
* <p>Wraps the Hibernate Criteria API in a builder. The builder can be retrieved through the "createCriteria()" dynamic static
* method of Grails domain classes (Example in Groovy):
*
* <pre>
* def c = Account.createCriteria()
* def results = c {
* projections {
* groupProperty("branch")
* }
* like("holderFirstName", "Fred%")
* and {
* between("balance", 500, 1000)
* eq("branch", "London")
* }
* maxResults(10)
* order("holderLastName", "desc")
* }
* </pre>
*
* <p>The builder can also be instantiated standalone with a SessionFactory and persistent Class instance:
*
* <pre>
* new HibernateCriteriaBuilder(clazz, sessionFactory).list {
* eq("firstName", "Fred")
* }
* </pre>
*
* @author Graeme Rocher
*/
public class HibernateCriteriaBuilder extends GroovyObjectSupport {
public static final String AND = "and"; // builder
public static final String IS_NULL = "isNull"; // builder
public static final String IS_NOT_NULL = "isNotNull"; // builder
public static final String NOT = "not";// builder
public static final String OR = "or"; // builder
public static final String ID_EQUALS = "idEq"; // builder
public static final String IS_EMPTY = "isEmpty"; //builder
public static final String IS_NOT_EMPTY = "isNotEmpty"; //builder
public static final String RLIKE = "rlike";//method
public static final String BETWEEN = "between";//method
public static final String EQUALS = "eq";//method
public static final String EQUALS_PROPERTY = "eqProperty";//method
public static final String GREATER_THAN = "gt";//method
public static final String GREATER_THAN_PROPERTY = "gtProperty";//method
public static final String GREATER_THAN_OR_EQUAL = "ge";//method
public static final String GREATER_THAN_OR_EQUAL_PROPERTY = "geProperty";//method
public static final String ILIKE = "ilike";//method
public static final String IN = "in";//method
public static final String LESS_THAN = "lt"; //method
public static final String LESS_THAN_PROPERTY = "ltProperty";//method
public static final String LESS_THAN_OR_EQUAL = "le";//method
public static final String LESS_THAN_OR_EQUAL_PROPERTY = "leProperty";//method
public static final String LIKE = "like";//method
public static final String NOT_EQUAL = "ne";//method
public static final String NOT_EQUAL_PROPERTY = "neProperty";//method
public static final String SIZE_EQUALS = "sizeEq"; //method
public static final String ORDER_DESCENDING = "desc";
public static final String ORDER_ASCENDING = "asc";
private static final String ROOT_DO_CALL = "doCall";
private static final String ROOT_CALL = "call";
private static final String LIST_CALL = "list";
private static final String LIST_DISTINCT_CALL = "listDistinct";
private static final String COUNT_CALL = "count";
private static final String GET_CALL = "get";
private static final String SCROLL_CALL = "scroll";
private static final String SET_RESULT_TRANSFORMER_CALL = "setResultTransformer";
private static final String PROJECTIONS = "projections";
private SessionFactory sessionFactory;
private Session hibernateSession;
private Class<?> targetClass;
private Criteria criteria;
private MetaClass criteriaMetaClass;
private boolean uniqueResult = false;
private List<LogicalExpression> logicalExpressionStack = new ArrayList<LogicalExpression>();
private List<String> associationStack = new ArrayList<String>();
private boolean participate;
private boolean scroll;
private boolean count;
private ProjectionList projectionList = Projections.projectionList();
private List<String> aliasStack = new ArrayList<String>();
private List<Criteria> aliasInstanceStack = new ArrayList<Criteria>();
private Map<String, String> aliasMap = new HashMap<String, String>();
private static final String ALIAS = "_alias";
private ResultTransformer resultTransformer;
private int aliasCount;
private boolean paginationEnabledList = false;
private List<Order> orderEntries;
private GrailsApplication grailsApplication;
@SuppressWarnings("rawtypes")
public HibernateCriteriaBuilder(Class targetClass, SessionFactory sessionFactory) {
this.targetClass = targetClass;
this.sessionFactory = sessionFactory;
}
@SuppressWarnings("rawtypes")
public HibernateCriteriaBuilder(Class targetClass, SessionFactory sessionFactory, boolean uniqueResult) {
this.targetClass = targetClass;
this.sessionFactory = sessionFactory;
this.uniqueResult = uniqueResult;
}
public void setGrailsApplication(GrailsApplication grailsApplication) {
this.grailsApplication = grailsApplication;
}
/**
* Returns the criteria instance
* @return The criteria instance
*/
public Criteria getInstance() {
return criteria;
}
/**
* Set whether a unique result should be returned
* @param uniqueResult True if a unique result should be returned
*/
public void setUniqueResult(boolean uniqueResult) {
this.uniqueResult = uniqueResult;
}
/**
* A projection that selects a property name
* @param propertyName The name of the property
*/
public void property(String propertyName) {
property(propertyName, null);
}
/**
* A projection that selects a property name
* @param propertyName The name of the property
* @param alias The alias to use
*/
public void property(String propertyName, String alias) {
final PropertyProjection propertyProjection = Projections.property(calculatePropertyName(propertyName));
addProjectionToList(propertyProjection, alias);
}
/**
* Adds a projection to the projectList for the given alias
*
* @param propertyProjection The projection
* @param alias The alias
*/
protected void addProjectionToList(Projection propertyProjection, String alias) {
if (alias != null) {
projectionList.add(propertyProjection,alias);
}
else {
projectionList.add(propertyProjection);
}
}
/**
* A projection that selects a distince property name
* @param propertyName The property name
*/
public void distinct(String propertyName) {
distinct(propertyName, null);
}
/**
* A projection that selects a distince property name
* @param propertyName The property name
* @param alias The alias to use
*/
public void distinct(String propertyName, String alias) {
final Projection proj = Projections.distinct(Projections.property(calculatePropertyName(propertyName)));
addProjectionToList(proj,alias);
}
/**
* A distinct projection that takes a list
*
* @param propertyNames The list of distince property names
*/
@SuppressWarnings("rawtypes")
public void distinct(Collection propertyNames) {
distinct(propertyNames, null);
}
/**
* A distinct projection that takes a list
*
* @param propertyNames The list of distince property names
* @param alias The alias to use
*/
@SuppressWarnings("rawtypes")
public void distinct(Collection propertyNames, String alias) {
ProjectionList list = Projections.projectionList();
for (Object o : propertyNames) {
list.add(Projections.property(calculatePropertyName(o.toString())));
}
final Projection proj = Projections.distinct(list);
addProjectionToList(proj, alias);
}
/**
* Adds a projection that allows the criteria to return the property average value
*
* @param propertyName The name of the property
*/
public void avg(String propertyName) {
avg(propertyName, null);
}
/**
* Adds a projection that allows the criteria to return the property average value
*
* @param propertyName The name of the property
* @param alias The alias to use
*/
public void avg(String propertyName, String alias) {
final AggregateProjection aggregateProjection = Projections.avg(calculatePropertyName(propertyName));
addProjectionToList(aggregateProjection, alias);
}
/**
* Use a join query
*
* @param associationPath The path of the association
*/
public void join(String associationPath) {
criteria.setFetchMode(calculatePropertyName(associationPath), FetchMode.JOIN);
}
/**
* Whether a pessimistic lock should be obtained.
*
* @param shouldLock True if it should
*/
public void lock(boolean shouldLock) {
String lastAlias = getLastAlias();
if (shouldLock) {
if (lastAlias != null) {
criteria.setLockMode(lastAlias, LockMode.PESSIMISTIC_WRITE);
}
else {
criteria.setLockMode(LockMode.PESSIMISTIC_WRITE);
}
}
else {
if (lastAlias != null) {
criteria.setLockMode(lastAlias, LockMode.NONE);
}
else {
criteria.setLockMode(LockMode.NONE);
}
}
}
/**
* Use a select query
*
* @param associationPath The path of the association
*/
public void select(String associationPath) {
criteria.setFetchMode(calculatePropertyName(associationPath), FetchMode.SELECT);
}
/**
* Whether to use the query cache
* @param shouldCache True if the query should be cached
*/
public void cache(boolean shouldCache) {
criteria.setCacheable(shouldCache);
}
/**
* Calculates the property name including any alias paths
*
* @param propertyName The property name
* @return The calculated property name
*/
private String calculatePropertyName(String propertyName) {
String lastAlias = getLastAlias();
if (lastAlias != null) {
return lastAlias +'.'+propertyName;
}
return propertyName;
}
private String getLastAlias() {
if (aliasStack.size() > 0) {
return aliasStack.get(aliasStack.size() - 1).toString();
}
return null;
}
public Class<?> getTargetClass() {
return targetClass;
}
/**
* Calculates the property value, converting GStrings if necessary
*
* @param propertyValue The property value
* @return The calculated property value
*/
private Object calculatePropertyValue(Object propertyValue) {
if (propertyValue instanceof CharSequence) {
return propertyValue.toString();
}
return propertyValue;
}
/**
* Adds a projection that allows the criteria to return the property count
*
* @param propertyName The name of the property
*/
public void count(String propertyName) {
count(propertyName, null);
}
/**
* Adds a projection that allows the criteria to return the property count
*
* @param propertyName The name of the property
* @param alias The alias to use
*/
public void count(String propertyName, String alias) {
final CountProjection proj = Projections.count(calculatePropertyName(propertyName));
addProjectionToList(proj, alias);
}
/**
* Adds a projection that allows the criteria to return the distinct property count
*
* @param propertyName The name of the property
*/
public void countDistinct(String propertyName) {
countDistinct(propertyName, null);
}
/**
* Adds a projection that allows the criteria to return the distinct property count
*
* @param propertyName The name of the property
* @param alias The alias to use
*/
public void countDistinct(String propertyName, String alias) {
final CountProjection proj = Projections.countDistinct(calculatePropertyName(propertyName));
addProjectionToList(proj, alias);
}
/**
* Adds a projection that allows the criteria's result to be grouped by a property
*
* @param propertyName The name of the property
*/
public void groupProperty(String propertyName) {
groupProperty(propertyName, null);
}
/**
* Adds a projection that allows the criteria's result to be grouped by a property
*
* @param propertyName The name of the property
* @param alias The alias to use
*/
public void groupProperty(String propertyName, String alias) {
final PropertyProjection proj = Projections.groupProperty(calculatePropertyName(propertyName));
addProjectionToList(proj, alias);
}
/**
* Adds a projection that allows the criteria to retrieve a maximum property value
*
* @param propertyName The name of the property
*/
public void max(String propertyName) {
max(propertyName, null);
}
/**
* Adds a projection that allows the criteria to retrieve a maximum property value
*
* @param propertyName The name of the property
* @param alias The alias to use
*/
public void max(String propertyName, String alias) {
final AggregateProjection proj = Projections.max(calculatePropertyName(propertyName));
addProjectionToList(proj, alias);
}
/**
* Adds a projection that allows the criteria to retrieve a minimum property value
*
* @param propertyName The name of the property
*/
public void min(String propertyName) {
min(propertyName, null);
}
/**
* Adds a projection that allows the criteria to retrieve a minimum property value
*
* @param alias The alias to use
*/
public void min(String propertyName, String alias) {
final AggregateProjection aggregateProjection = Projections.min(calculatePropertyName(propertyName));
addProjectionToList(aggregateProjection, alias);
}
/**
* Adds a projection that allows the criteria to return the row count
*
*/
public void rowCount() {
rowCount(null);
}
/**
* Adds a projection that allows the criteria to return the row count
*
* @param alias The alias to use
*/
public void rowCount(String alias) {
final Projection proj = Projections.rowCount();
addProjectionToList(proj, alias);
}
/**
* Adds a projection that allows the criteria to retrieve the sum of the results of a property
*
* @param propertyName The name of the property
*/
public void sum(String propertyName) {
sum(propertyName, null);
}
/**
* Adds a projection that allows the criteria to retrieve the sum of the results of a property
*
* @param propertyName The name of the property
* @param alias The alias to use
*/
public void sum(String propertyName, String alias) {
final AggregateProjection proj = Projections.sum(calculatePropertyName(propertyName));
addProjectionToList(proj, alias);
}
/**
* Sets the fetch mode of an associated path
*
* @param associationPath The name of the associated path
* @param fetchMode The fetch mode to set
*/
public void fetchMode(String associationPath, FetchMode fetchMode) {
if (criteria != null) {
criteria.setFetchMode(associationPath, fetchMode);
}
}
/**
* Sets the resultTransformer.
* @param transformer The result transformer to use.
*/
public void resultTransformer(ResultTransformer transformer) {
if (criteria == null) {
throwRuntimeException(new IllegalArgumentException("Call to [resultTransformer] not supported here"));
}
resultTransformer = transformer;
}
/**
* Creates a Criterion that compares to class properties for equality
* @param propertyName The first property name
* @param otherPropertyName The second property name
* @return A Criterion instance
*/
public Object eqProperty(String propertyName, String otherPropertyName) {
if (!validateSimpleExpression()) {
throwRuntimeException(new IllegalArgumentException("Call to [eqProperty] with propertyName [" +
propertyName + "] and other property name [" + otherPropertyName + "] not allowed here."));
}
propertyName = calculatePropertyName(propertyName);
otherPropertyName = calculatePropertyName(otherPropertyName);
return addToCriteria(Restrictions.eqProperty(propertyName, otherPropertyName));
}
/**
* Creates a Criterion that compares to class properties for !equality
* @param propertyName The first property name
* @param otherPropertyName The second property name
* @return A Criterion instance
*/
public Object neProperty(String propertyName, String otherPropertyName) {
if (!validateSimpleExpression()) {
throwRuntimeException(new IllegalArgumentException("Call to [neProperty] with propertyName [" +
propertyName + "] and other property name [" + otherPropertyName + "] not allowed here."));
}
propertyName = calculatePropertyName(propertyName);
otherPropertyName = calculatePropertyName(otherPropertyName);
return addToCriteria(Restrictions.neProperty(propertyName, otherPropertyName));
}
/**
* Creates a Criterion that tests if the first property is greater than the second property
* @param propertyName The first property name
* @param otherPropertyName The second property name
* @return A Criterion instance
*/
public Object gtProperty(String propertyName, String otherPropertyName) {
if (!validateSimpleExpression()) {
throwRuntimeException(new IllegalArgumentException("Call to [gtProperty] with propertyName [" +
propertyName + "] and other property name [" + otherPropertyName + "] not allowed here."));
}
propertyName = calculatePropertyName(propertyName);
otherPropertyName = calculatePropertyName(otherPropertyName);
return addToCriteria(Restrictions.gtProperty(propertyName, otherPropertyName));
}
/**
* Creates a Criterion that tests if the first property is greater than or equal to the second property
* @param propertyName The first property name
* @param otherPropertyName The second property name
* @return A Criterion instance
*/
public Object geProperty(String propertyName, String otherPropertyName) {
if (!validateSimpleExpression()) {
throwRuntimeException(new IllegalArgumentException("Call to [geProperty] with propertyName [" +
propertyName + "] and other property name [" + otherPropertyName + "] not allowed here."));
}
propertyName = calculatePropertyName(propertyName);
otherPropertyName = calculatePropertyName(otherPropertyName);
return addToCriteria(Restrictions.geProperty(propertyName, otherPropertyName));
}
/**
* Creates a Criterion that tests if the first property is less than the second property
* @param propertyName The first property name
* @param otherPropertyName The second property name
* @return A Criterion instance
*/
public Object ltProperty(String propertyName, String otherPropertyName) {
if (!validateSimpleExpression()) {
throwRuntimeException(new IllegalArgumentException("Call to [ltProperty] with propertyName [" +
propertyName + "] and other property name [" + otherPropertyName + "] not allowed here."));
}
propertyName = calculatePropertyName(propertyName);
otherPropertyName = calculatePropertyName(otherPropertyName);
return addToCriteria(Restrictions.ltProperty(propertyName, otherPropertyName));
}
/**
* Creates a Criterion that tests if the first property is less than or equal to the second property
* @param propertyName The first property name
* @param otherPropertyName The second property name
* @return A Criterion instance
*/
public Object leProperty(String propertyName, String otherPropertyName) {
if (!validateSimpleExpression()) {
throwRuntimeException(new IllegalArgumentException("Call to [leProperty] with propertyName [" +
propertyName + "] and other property name [" + otherPropertyName + "] not allowed here."));
}
propertyName = calculatePropertyName(propertyName);
otherPropertyName = calculatePropertyName(otherPropertyName);
return addToCriteria(Restrictions.leProperty(propertyName, otherPropertyName));
}
/**
* Creates a "greater than" Criterion based on the specified property name and value
* @param propertyName The property name
* @param propertyValue The property value
* @return A Criterion instance
*/
public Object gt(String propertyName, Object propertyValue) {
if (!validateSimpleExpression()) {
throwRuntimeException(new IllegalArgumentException("Call to [gt] with propertyName [" +
propertyName + "] and value [" + propertyValue + "] not allowed here."));
}
propertyName = calculatePropertyName(propertyName);
propertyValue = calculatePropertyValue(propertyValue);
return addToCriteria(Restrictions.gt(propertyName, propertyValue));
}
/**
* Creates a "greater than or equal to" Criterion based on the specified property name and value
* @param propertyName The property name
* @param propertyValue The property value
* @return A Criterion instance
*/
public Object ge(String propertyName, Object propertyValue) {
if (!validateSimpleExpression()) {
throwRuntimeException(new IllegalArgumentException("Call to [ge] with propertyName [" +
propertyName + "] and value [" + propertyValue + "] not allowed here."));
}
propertyName = calculatePropertyName(propertyName);
propertyValue = calculatePropertyValue(propertyValue);
return addToCriteria(Restrictions.ge(propertyName, propertyValue));
}
/**
* Creates a "less than" Criterion based on the specified property name and value
* @param propertyName The property name
* @param propertyValue The property value
* @return A Criterion instance
*/
public Object lt(String propertyName, Object propertyValue) {
if (!validateSimpleExpression()) {
throwRuntimeException(new IllegalArgumentException("Call to [lt] with propertyName [" +
propertyName + "] and value [" + propertyValue + "] not allowed here."));
}
propertyName = calculatePropertyName(propertyName);
propertyValue = calculatePropertyValue(propertyValue);
return addToCriteria(Restrictions.lt(propertyName, propertyValue));
}
/**
* Creates a "less than or equal to" Criterion based on the specified property name and value
* @param propertyName The property name
* @param propertyValue The property value
* @return A Criterion instance
*/
public Object le(String propertyName, Object propertyValue) {
if (!validateSimpleExpression()) {
throwRuntimeException(new IllegalArgumentException("Call to [le] with propertyName [" +
propertyName + "] and value [" + propertyValue + "] not allowed here."));
}
propertyName = calculatePropertyName(propertyName);
propertyValue = calculatePropertyValue(propertyValue);
return addToCriteria(Restrictions.le(propertyName, propertyValue));
}
/**
* Creates an "equals" Criterion based on the specified property name and value. Case-sensitive.
* @param propertyName The property name
* @param propertyValue The property value
*
* @return A Criterion instance
*/
public Object eq(String propertyName, Object propertyValue) {
return eq(propertyName, propertyValue, Collections.emptyMap());
}
/**
* Groovy moves the map to the first parameter if using the idiomatic form, e.g.
* <code>eq 'firstName', 'Fred', ignoreCase: true</code>.
* @param params optional map with customization parameters; currently only 'ignoreCase' is supported.
* @param propertyName
* @param propertyValue
* @return A Criterion instance
*/
@SuppressWarnings("rawtypes")
public Object eq(Map params, String propertyName, Object propertyValue) {
return eq(propertyName, propertyValue, params);
}
/**
* Creates an "equals" Criterion based on the specified property name and value.
* Supports case-insensitive search if the <code>params</code> map contains <code>true</code>
* under the 'ignoreCase' key.
* @param propertyName The property name
* @param propertyValue The property value
* @param params optional map with customization parameters; currently only 'ignoreCase' is supported.
*
* @return A Criterion instance
*/
@SuppressWarnings("rawtypes")
public Object eq(String propertyName, Object propertyValue, Map params) {
if (!validateSimpleExpression()) {
throwRuntimeException(new IllegalArgumentException("Call to [eq] with propertyName [" +
propertyName + "] and value [" + propertyValue + "] not allowed here."));
}
propertyName = calculatePropertyName(propertyName);
propertyValue = calculatePropertyValue(propertyValue);
SimpleExpression eq = Restrictions.eq(propertyName, propertyValue);
if (params != null) {
Object ignoreCase = params.get("ignoreCase");
if (ignoreCase instanceof Boolean && (Boolean)ignoreCase) {
eq = eq.ignoreCase();
}
}
return addToCriteria(eq);
}
/**
* Applies a sql restriction to the results to allow something like:
<pre>
def results = Person.withCriteria {
sqlRestriction "char_length(first_name) <= 4"
}
</pre>
*
* @param sqlRestriction the sql restriction
* @return a Criterion instance
*/
public Object sqlRestriction(String sqlRestriction) {
if (!validateSimpleExpression()) {
throwRuntimeException(new IllegalArgumentException("Call to [sqlRestriction] with value [" +
sqlRestriction + "] not allowed here."));
}
return addToCriteria(Restrictions.sqlRestriction(sqlRestriction));
}
/**
* Creates a Criterion with from the specified property name and "like" expression
* @param propertyName The property name
* @param propertyValue The like value
*
* @return A Criterion instance
*/
public Object like(String propertyName, Object propertyValue) {
if (!validateSimpleExpression()) {
throwRuntimeException(new IllegalArgumentException("Call to [like] with propertyName [" +
propertyName + "] and value [" + propertyValue + "] not allowed here."));
}
propertyName = calculatePropertyName(propertyName);
propertyValue = calculatePropertyValue(propertyValue);
return addToCriteria(Restrictions.like(propertyName, propertyValue));
}
/**
* Creates a Criterion with from the specified property name and "rlike" (a regular expression version of "like") expression
* @param propertyName The property name
* @param propertyValue The ilike value
*
* @return A Criterion instance
*/
public Object rlike(String propertyName, Object propertyValue) {
if (!validateSimpleExpression()) {
throwRuntimeException(new IllegalArgumentException("Call to [rlike] with propertyName [" +
propertyName + "] and value [" + propertyValue + "] not allowed here."));
}
propertyName = calculatePropertyName(propertyName);
propertyValue = calculatePropertyValue(propertyValue);
return addToCriteria(new RlikeExpression(propertyName, propertyValue));
}
/**
* Creates a Criterion with from the specified property name and "ilike" (a case sensitive version of "like") expression
* @param propertyName The property name
* @param propertyValue The ilike value
*
* @return A Criterion instance
*/
public Object ilike(String propertyName, Object propertyValue) {
if (!validateSimpleExpression()) {
throwRuntimeException(new IllegalArgumentException("Call to [ilike] with propertyName [" +
propertyName + "] and value [" + propertyValue + "] not allowed here."));
}
propertyName = calculatePropertyName(propertyName);
propertyValue = calculatePropertyValue(propertyValue);
return addToCriteria(Restrictions.ilike(propertyName, propertyValue));
}
/**
* Applys a "in" contrain on the specified property
* @param propertyName The property name
* @param values A collection of values
*
* @return A Criterion instance
*/
@SuppressWarnings("rawtypes")
public Object in(String propertyName, Collection values) {
if (!validateSimpleExpression()) {
throwRuntimeException(new IllegalArgumentException("Call to [in] with propertyName [" +
propertyName + "] and values [" + values + "] not allowed here."));
}
propertyName = calculatePropertyName(propertyName);
return addToCriteria(Restrictions.in(propertyName, values));
}
/**
* Delegates to in as in is a Groovy keyword
*/
@SuppressWarnings("rawtypes")
public Object inList(String propertyName, Collection values) {
return in(propertyName, values);
}
/**
* Delegates to in as in is a Groovy keyword
*/
public Object inList(String propertyName, Object[] values) {
return in(propertyName, values);
}
/**
* Applys a "in" contrain on the specified property
* @param propertyName The property name
* @param values A collection of values
*
* @return A Criterion instance
*/
public Object in(String propertyName, Object[] values) {
if (!validateSimpleExpression()) {
throwRuntimeException(new IllegalArgumentException("Call to [in] with propertyName [" +
propertyName + "] and values [" + values + "] not allowed here."));
}
propertyName = calculatePropertyName(propertyName);
return addToCriteria(Restrictions.in(propertyName, values));
}
/**
* Orders by the specified property name (defaults to ascending)
*
* @param propertyName The property name to order by
* @return A Order instance
*/
public Object order(String propertyName) {
if (criteria == null) {
throwRuntimeException(new IllegalArgumentException("Call to [order] with propertyName [" +
propertyName + "]not allowed here."));
}
propertyName = calculatePropertyName(propertyName);
Order o = Order.asc(propertyName);
if (paginationEnabledList) {
orderEntries.add(o);
}
else {
criteria.addOrder(o);
}
return o;
}
/**
* Orders by the specified property name and direction
*
* @param propertyName The property name to order by
* @param direction Either "asc" for ascending or "desc" for descending
*
* @return A Order instance
*/
public Object order(String propertyName, String direction) {
if (criteria == null) {
throwRuntimeException(new IllegalArgumentException("Call to [order] with propertyName [" +
propertyName + "]not allowed here."));
}
propertyName = calculatePropertyName(propertyName);
Order o;
if (direction.equals(ORDER_DESCENDING)) {
o = Order.desc(propertyName);
}
else {
o = Order.asc(propertyName);
}
if (paginationEnabledList) {
orderEntries.add(o);
}
else {
criteria.addOrder(o);
}
return o;
}
/**
* Creates a Criterion that contrains a collection property by size
*
* @param propertyName The property name
* @param size The size to constrain by
*
* @return A Criterion instance
*/
public Object sizeEq(String propertyName, int size) {
if (!validateSimpleExpression()) {
throwRuntimeException(new IllegalArgumentException("Call to [sizeEq] with propertyName [" +
propertyName + "] and size [" + size + "] not allowed here."));
}
propertyName = calculatePropertyName(propertyName);
return addToCriteria(Restrictions.sizeEq(propertyName, size));
}
/**
* Creates a Criterion that contrains a collection property to be greater than the given size
*
* @param propertyName The property name
* @param size The size to constrain by
*
* @return A Criterion instance
*/
public Object sizeGt(String propertyName, int size) {
if (!validateSimpleExpression()) {
throwRuntimeException(new IllegalArgumentException("Call to [sizeGt] with propertyName [" +
propertyName + "] and size [" + size + "] not allowed here."));
}
propertyName = calculatePropertyName(propertyName);
return addToCriteria(Restrictions.sizeGt(propertyName, size));
}
/**
* Creates a Criterion that contrains a collection property to be greater than or equal to the given size
*
* @param propertyName The property name
* @param size The size to constrain by
*
* @return A Criterion instance
*/
public Object sizeGe(String propertyName, int size) {
if (!validateSimpleExpression()) {
throwRuntimeException(new IllegalArgumentException("Call to [sizeGe] with propertyName [" +
propertyName + "] and size [" + size + "] not allowed here."));
}
propertyName = calculatePropertyName(propertyName);
return addToCriteria(Restrictions.sizeGe(propertyName, size));
}
/**
* Creates a Criterion that contrains a collection property to be less than or equal to the given size
*
* @param propertyName The property name
* @param size The size to constrain by
*
* @return A Criterion instance
*/
public Object sizeLe(String propertyName, int size) {
if (!validateSimpleExpression()) {
throwRuntimeException(new IllegalArgumentException("Call to [sizeLe] with propertyName [" +
propertyName + "] and size [" + size + "] not allowed here."));
}
propertyName = calculatePropertyName(propertyName);
return addToCriteria(Restrictions.sizeLe(propertyName, size));
}
/**
* Creates a Criterion that contrains a collection property to be less than to the given size
*
* @param propertyName The property name
* @param size The size to constrain by
*
* @return A Criterion instance
*/
public Object sizeLt(String propertyName, int size) {
if (!validateSimpleExpression()) {
throwRuntimeException(new IllegalArgumentException("Call to [sizeLt] with propertyName [" +
propertyName + "] and size [" + size + "] not allowed here."));
}
propertyName = calculatePropertyName(propertyName);
return addToCriteria(Restrictions.sizeLt(propertyName, size));
}
/**
* Creates a Criterion that contrains a collection property to be not equal to the given size
*
* @param propertyName The property name