28
28
import java .util .ArrayList ;
29
29
import java .util .Collection ;
30
30
import java .util .Date ;
31
- import java .util .EnumSet ;
31
+ import java .util .HashMap ;
32
+ import java .util .Map ;
32
33
import java .util .Set ;
33
34
import java .util .UUID ;
34
35
73
74
*
74
75
*/
75
76
class JpaPredicateBuilder {
77
+
78
+ public static final Map <Class <?>, Class <?>> WRAPPERS_TO_PRIMITIVES = new HashMap <Class <?>, Class <?>>();
79
+ public static final Map <Class <?>, Class <?>> PRIMITIVES_TO_WRAPPERS = new HashMap <Class <?>, Class <?>>();
80
+
81
+ static {
82
+ PRIMITIVES_TO_WRAPPERS .put (boolean .class , Boolean .class );
83
+ PRIMITIVES_TO_WRAPPERS .put (byte .class , Byte .class );
84
+ PRIMITIVES_TO_WRAPPERS .put (char .class , Character .class );
85
+ PRIMITIVES_TO_WRAPPERS .put (double .class , Double .class );
86
+ PRIMITIVES_TO_WRAPPERS .put (float .class , Float .class );
87
+ PRIMITIVES_TO_WRAPPERS .put (int .class , Integer .class );
88
+ PRIMITIVES_TO_WRAPPERS .put (long .class , Long .class );
89
+ PRIMITIVES_TO_WRAPPERS .put (short .class , Short .class );
90
+ PRIMITIVES_TO_WRAPPERS .put (void .class , Void .class );
91
+
92
+ WRAPPERS_TO_PRIMITIVES .put (Boolean .class , boolean .class );
93
+ WRAPPERS_TO_PRIMITIVES .put (Byte .class , byte .class );
94
+ WRAPPERS_TO_PRIMITIVES .put (Character .class , char .class );
95
+ WRAPPERS_TO_PRIMITIVES .put (Double .class , double .class );
96
+ WRAPPERS_TO_PRIMITIVES .put (Float .class , float .class );
97
+ WRAPPERS_TO_PRIMITIVES .put (Integer .class , int .class );
98
+ WRAPPERS_TO_PRIMITIVES .put (Long .class , long .class );
99
+ WRAPPERS_TO_PRIMITIVES .put (Short .class , short .class );
100
+ WRAPPERS_TO_PRIMITIVES .put (Void .class , void .class );
101
+ }
102
+
76
103
77
104
private final CriteriaBuilder cb ;
78
105
79
- private final EnumSet <Logical > globalOptions ;
80
-
81
106
/**
82
- * Field name can be prepended with (comma separated list of local options)
83
- * if field is prepended with (), even without options, any global options
84
- * avoided and defaults are used.Defaults: for numbers and booleas -
85
- * equality; for strings case insensitive beginning matches; for dates
86
- * greater or equal;
87
- *
88
- * @param cb
89
- * @param globalOptions
107
+ * Create JpaPredicateBuilder instance with CriteriaBuilder
108
+ *
109
+ * @param cb criteria builder instance
90
110
*/
91
- public JpaPredicateBuilder (CriteriaBuilder cb , EnumSet < Logical > globalOptions ) {
111
+ public JpaPredicateBuilder (CriteriaBuilder cb ) {
92
112
this .cb = cb ;
93
- this .globalOptions = globalOptions ;
94
113
}
95
114
96
115
protected Predicate addOrNull (Path <?> root , Predicate p ) {
@@ -109,48 +128,43 @@ protected Predicate getStringPredicate(Path<String> root, PredicateFilter filter
109
128
Predicate arrayValuePredicate = mayBeArrayValuePredicate (root , filter );
110
129
111
130
if (arrayValuePredicate == null ) {
131
+ // CASE sensitive by default
112
132
String compareValue = filter .getValue ().toString ();
113
133
Expression <String > fieldValue = root ;
114
134
115
- if (filter .getCriterias ().contains (PredicateFilter .Criteria .IN )) {
116
- CriteriaBuilder .In <Object > in = cb .in (root );
117
- return in .value (compareValue );
118
- }
119
- if (filter .getCriterias ().contains (PredicateFilter .Criteria .NIN )) {
120
- return cb .not (root .in (compareValue ));
121
- }
122
-
123
- if (filter .getCriterias ().contains (PredicateFilter .Criteria .CASE )) {
124
- fieldValue = root ;
125
- }
126
- else if (filter .getCriterias ().contains (PredicateFilter .Criteria .LOWER )) {
135
+ if (filter .getCriterias ().contains (PredicateFilter .Criteria .LOWER )) {
127
136
fieldValue = cb .lower (root );
128
- compareValue = compareValue .toLowerCase ();
129
137
}
130
138
else if (filter .getCriterias ().contains (PredicateFilter .Criteria .UPPER )) {
131
139
fieldValue = cb .upper (root );
132
- compareValue = compareValue .toUpperCase ();
133
140
}
134
-
135
- if (filter .getCriterias ().contains (PredicateFilter .Criteria .EQ )) {
141
+
142
+ if (filter .getCriterias ().contains (PredicateFilter .Criteria .IN )) {
143
+ return cb .in (fieldValue ).value (compareValue );
144
+ }
145
+ else if (filter .getCriterias ().contains (PredicateFilter .Criteria .NIN )) {
146
+ return cb .not (fieldValue .in (compareValue ));
147
+ }
148
+ else if (filter .getCriterias ().contains (PredicateFilter .Criteria .EQ )) {
136
149
return cb .equal (fieldValue , compareValue );
137
150
}
138
151
else if (filter .getCriterias ().contains (PredicateFilter .Criteria .NE )) {
139
152
return cb .notEqual (fieldValue , compareValue );
140
153
}
141
- else if (filter .getCriterias ().contains (PredicateFilter .Criteria .LIKE )) {
154
+
155
+ if (filter .getCriterias ().contains (PredicateFilter .Criteria .LIKE )) {
142
156
compareValue = "%" + compareValue + "%" ;
143
157
}
144
158
else if (filter .getCriterias ().contains (PredicateFilter .Criteria .ENDS )) {
145
159
compareValue = "%" + compareValue ;
146
160
}
147
- else if (filter .getCriterias ().contains (PredicateFilter .Criteria .EXACT )) {
148
- // do nothing
161
+ else if (filter .getCriterias ().contains (PredicateFilter .Criteria .STARTS )) {
162
+ compareValue = compareValue + "%" ;
149
163
}
150
- // STARTS or empty (default)
151
164
else {
152
- compareValue = compareValue + "%" ;
165
+ // do nothing
153
166
}
167
+
154
168
return cb .like (fieldValue , compareValue );
155
169
}
156
170
@@ -668,7 +682,7 @@ private Predicate getTypedPredicate(From<?,?> from, Path<?> field, PredicateFilt
668
682
PredicateFilter predicateFilter = new PredicateFilter (filter .getField (), value , criterias );
669
683
670
684
if (type .isPrimitive ())
671
- type = JpaQueryBuilder . PRIMITIVES_TO_WRAPPERS .get (type );
685
+ type = PRIMITIVES_TO_WRAPPERS .get (type );
672
686
if (type .equals (String .class )) {
673
687
return getStringPredicate ((Path <String >)field , filter );
674
688
}
0 commit comments