1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.datadictionary.util; |
17 | |
|
18 | |
import java.sql.Timestamp; |
19 | |
import java.text.DateFormat; |
20 | |
import java.text.ParseException; |
21 | |
import java.text.SimpleDateFormat; |
22 | |
import java.util.ArrayList; |
23 | |
import java.util.Date; |
24 | |
import java.util.List; |
25 | |
|
26 | |
import org.kuali.rice.kns.datadictionary.validation.DataType; |
27 | |
import org.kuali.student.common.infc.Comparison; |
28 | |
import org.kuali.student.common.infc.Criteria; |
29 | |
import org.kuali.student.datadictionary.infc.AttributeDefinitionInfc; |
30 | |
import org.kuali.student.datadictionary.infc.DictionaryEntry; |
31 | |
import org.kuali.student.r2.common.exceptions.InvalidParameterException; |
32 | |
import org.kuali.student.r2.common.exceptions.OperationFailedException; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
public class CriteriaValidatorParser { |
51 | |
|
52 | 0 | public enum Operator { |
53 | |
|
54 | 0 | EQ, IN, GT, LT, NEQ, GTE, LTE, LIKE, BETWEEN; |
55 | |
} |
56 | |
private Criteria criteria; |
57 | |
private DictionaryEntry dictionaryEntry; |
58 | |
private transient List<Object> parsedValues; |
59 | |
private transient List<Operator> parsedOperators; |
60 | |
|
61 | 3 | public CriteriaValidatorParser() { |
62 | 3 | } |
63 | |
|
64 | |
public Criteria getCriteria() { |
65 | 0 | return criteria; |
66 | |
} |
67 | |
|
68 | |
public void setCriteria(Criteria criteria) { |
69 | 3 | this.criteria = criteria; |
70 | 3 | } |
71 | |
|
72 | |
public DictionaryEntry getDictionaryEntry() { |
73 | 0 | return dictionaryEntry; |
74 | |
} |
75 | |
|
76 | |
public void setDictionaryEntry(DictionaryEntry dictionaryEntry) { |
77 | 3 | this.dictionaryEntry = dictionaryEntry; |
78 | 3 | } |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
public List<Operator> getParsedOperators() { |
85 | 3 | return parsedOperators; |
86 | |
} |
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
public List<Object> getParsedValues() { |
98 | 3 | return parsedValues; |
99 | |
} |
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
public void validate() |
112 | |
throws InvalidParameterException, |
113 | |
OperationFailedException { |
114 | 3 | parsedValues = new ArrayList<Object>(); |
115 | 3 | parsedOperators = new ArrayList <Operator> (); |
116 | 3 | if (this.criteria.getComparisons() == null) { |
117 | 0 | throw new InvalidParameterException ("Comparisons list is null -- to select all specify an empty list"); |
118 | |
} |
119 | 3 | int i = 0; |
120 | 3 | for (Comparison comparison : this.criteria.getComparisons()) { |
121 | 0 | this.validate(i, comparison); |
122 | 0 | i++; |
123 | |
} |
124 | 3 | } |
125 | |
|
126 | |
private void validate(int i, Comparison comparison) |
127 | |
throws InvalidParameterException, |
128 | |
OperationFailedException { |
129 | 0 | String fieldKey = comparison.getFieldKey(); |
130 | 0 | String operator = comparison.getOperator(); |
131 | 0 | List<String> values = comparison.getValues(); |
132 | 0 | AttributeDefinitionInfc ad = this.getAttributeDefinition(fieldKey); |
133 | 0 | if (ad == null) { |
134 | 0 | throw new InvalidParameterException("The " + i + "th comparison's field key " + fieldKey + " is not defined in the dictionary"); |
135 | |
} |
136 | 0 | if (operator == null) { |
137 | 0 | throw new InvalidParameterException("The " + i + "th comparison's operator is null"); |
138 | |
} |
139 | 0 | if (operator.equals("=")) { |
140 | 0 | this.parsedOperators.add(Operator.EQ); |
141 | 0 | } else if (operator.equals("<")) { |
142 | 0 | this.parsedOperators.add(Operator.LT); |
143 | 0 | } else if (operator.equals(">")) { |
144 | 0 | this.parsedOperators.add(Operator.GT); |
145 | 0 | } else if (operator.equals("!=")) { |
146 | 0 | this.parsedOperators.add(Operator.NEQ); |
147 | 0 | } else if (operator.equals("<=")) { |
148 | 0 | this.parsedOperators.add(Operator.LTE); |
149 | 0 | } else if (operator.equals(">=")) { |
150 | 0 | this.parsedOperators.add(Operator.GTE); |
151 | 0 | } else if (operator.equals("in")) { |
152 | 0 | this.parsedOperators.add(Operator.IN); |
153 | 0 | } else if (operator.equals("between")) { |
154 | 0 | this.parsedOperators.add(Operator.BETWEEN); |
155 | 0 | } else if (operator.equals("like")) { |
156 | 0 | this.parsedOperators.add(Operator.LIKE); |
157 | 0 | if (ad.getDataType().equals (DataType.STRING)) { |
158 | 0 | throw new InvalidParameterException("The " + i + "th comparison's operator is LIKE which can only be applied to strings, " + ad.getDataType() + " is invalid."); |
159 | |
} |
160 | |
} else { |
161 | 0 | throw new InvalidParameterException("The " + i + "th comparison's operator, " + operator + ", is invalid."); |
162 | |
} |
163 | 0 | if (values == null) { |
164 | 0 | throw new InvalidParameterException("The " + i + "th comparison's values list is required and cannot be null"); |
165 | |
} |
166 | 0 | if (values.isEmpty()) { |
167 | 0 | throw new InvalidParameterException("The " + i + "th comparison's values list is required and cannot be an empty list"); |
168 | |
} |
169 | 0 | if (values.get(0) == null) { |
170 | 0 | if (!operator.equals("=") && !operator.equals("!=")) { |
171 | 0 | throw new InvalidParameterException("The " + i + "th comparison's value is null but the operator " + operator + " is a comparison operator that does not apply"); |
172 | |
} |
173 | 0 | return; |
174 | |
} |
175 | 0 | if (operator.equals("between")) { |
176 | 0 | if (values.size() != 2) { |
177 | 0 | throw new InvalidParameterException("The " + i + "th comparison is a between operator which requires two values, found " + values.size()); |
178 | |
} |
179 | 0 | if (values.get(0) == null) { |
180 | 0 | throw new InvalidParameterException("The " + i + "th comparison is a between operator but the first value is null"); |
181 | |
} |
182 | 0 | if (values.get(1) == null) { |
183 | 0 | throw new InvalidParameterException("The " + i + "th comparison is a between operator but the second value is null"); |
184 | |
} |
185 | 0 | } else if (values.size() > 1) { |
186 | 0 | if (!operator.equals("in")) { |
187 | 0 | throw new InvalidParameterException("The " + i + "th comparison's value is a list but the operator " + operator + " is a comparison operator that does not apply"); |
188 | |
} |
189 | |
} |
190 | 0 | switch (ad.getDataType()) { |
191 | |
case STRING: |
192 | 0 | break; |
193 | |
case DATE: |
194 | |
case TRUNCATED_DATE: |
195 | 0 | break; |
196 | |
case BOOLEAN: |
197 | 0 | if (! operator.equals("=") && !operator.equals("!=")) { |
198 | 0 | throw new InvalidParameterException("The " + i + "th comparison's operator " + operator + " is a comparison operator that does not apply to the field's boolean data type"); |
199 | |
} |
200 | |
case INTEGER: |
201 | |
case FLOAT: |
202 | |
case DOUBLE: |
203 | |
case LONG: |
204 | 0 | break; |
205 | |
case COMPLEX: |
206 | 0 | if (! operator.equals("=") && !operator.equals("!=")) { |
207 | 0 | throw new InvalidParameterException("The " + i + "th comparison's operator " + operator + " is a comparison operator that does not apply to the field's complex data type"); |
208 | |
} |
209 | 0 | if (values.get(0) == null) { |
210 | 0 | throw new InvalidParameterException("The " + i + "th comparison's value is not null but attribute type is complex. Complex can only be checked to see if it is null or not null"); |
211 | |
} |
212 | |
} |
213 | 0 | parsedValues.add(parseValues(i, ad.getDataType(), comparison.getValues(), comparison.isIgnoreCase())); |
214 | 0 | } |
215 | |
|
216 | |
private Object parseValues(int i, DataType dataType, List<String> values, boolean ignoreCase) |
217 | |
throws InvalidParameterException { |
218 | 0 | if (values.size() == 1) { |
219 | 0 | return parseValue(i, dataType, values.get(0), ignoreCase); |
220 | |
} |
221 | 0 | List<Object> list = new ArrayList<Object>(); |
222 | 0 | for (String value : values) { |
223 | 0 | list.add(parseValue(i, dataType, value, ignoreCase)); |
224 | |
} |
225 | 0 | return list; |
226 | |
} |
227 | |
|
228 | |
private Object parseValue(int i, DataType dataType, String value, boolean ignoreCase) |
229 | |
throws InvalidParameterException { |
230 | 0 | if (value == null) { |
231 | 0 | return null; |
232 | |
} |
233 | 0 | switch (dataType) { |
234 | |
case STRING: |
235 | 0 | return parseString(i, value, ignoreCase); |
236 | |
case DATE: |
237 | 0 | return parseDateTime(i, value); |
238 | |
case TRUNCATED_DATE: |
239 | 0 | return parseDate(i, value); |
240 | |
case BOOLEAN: |
241 | 0 | return parseBoolean(i, value); |
242 | |
case INTEGER: |
243 | 0 | return parseInteger(i, value); |
244 | |
case FLOAT: |
245 | 0 | return parseFloat(i, value); |
246 | |
case DOUBLE: |
247 | 0 | return parseDouble(i, value); |
248 | |
case LONG: |
249 | 0 | return parseLong(i, value); |
250 | |
case COMPLEX: |
251 | 0 | throw new InvalidParameterException("The " + i + "th comparison's value is not null but attribute type is complex. Complex can only be checked to see if it is null or not null"); |
252 | |
default: |
253 | 0 | throw new IllegalArgumentException("Unknown/unhandled datatype " + dataType); |
254 | |
} |
255 | |
} |
256 | |
|
257 | |
private String parseString(int i, String cv, boolean ignoreCase) throws InvalidParameterException { |
258 | 0 | if (cv == null) { |
259 | 0 | return null; |
260 | |
} |
261 | 0 | if (ignoreCase) { |
262 | 0 | return cv.toLowerCase(); |
263 | |
} |
264 | 0 | return cv; |
265 | |
} |
266 | |
|
267 | |
private Timestamp parseDateTime(int i, String cv) throws InvalidParameterException { |
268 | 0 | DateFormat df = new SimpleDateFormat("yyyy-MM-dd-hh.mm.ss"); |
269 | |
try { |
270 | 0 | return new Timestamp(df.parse(cv).getTime()); |
271 | 0 | } catch (ParseException ex) { |
272 | 0 | throw new InvalidParameterException("The " + i + "th comparison's value " + cv + " cannot be parsed as a dateTime"); |
273 | |
} |
274 | |
} |
275 | |
|
276 | |
private Date parseDate(int i, String cv) throws InvalidParameterException { |
277 | 0 | DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); |
278 | |
try { |
279 | 0 | return df.parse(cv); |
280 | 0 | } catch (ParseException ex) { |
281 | 0 | throw new InvalidParameterException("The " + i + "th comparison's value " + cv + " cannot be parsed as a date"); |
282 | |
} |
283 | |
} |
284 | |
|
285 | |
private Integer parseInteger(int i, String cv) throws InvalidParameterException { |
286 | |
try { |
287 | 0 | return Integer.parseInt(cv); |
288 | 0 | } catch (NumberFormatException ex) { |
289 | 0 | throw new InvalidParameterException("The " + i + "th comparison's value " + cv + " cannot be parsed as an integer"); |
290 | |
} |
291 | |
} |
292 | |
|
293 | |
private Long parseLong(int i, String cv) throws InvalidParameterException { |
294 | |
try { |
295 | 0 | return Long.parseLong(cv); |
296 | 0 | } catch (NumberFormatException ex) { |
297 | 0 | throw new InvalidParameterException("The " + i + "th comparison's value " + cv + " cannot be parsed as a Long"); |
298 | |
} |
299 | |
} |
300 | |
|
301 | |
private Boolean parseBoolean(int i, String cv) throws InvalidParameterException { |
302 | 0 | if (cv.equalsIgnoreCase("true")) { |
303 | 0 | return Boolean.TRUE; |
304 | |
} |
305 | 0 | if (cv.equalsIgnoreCase("false")) { |
306 | 0 | return Boolean.FALSE; |
307 | |
} |
308 | 0 | throw new InvalidParameterException("The " + i + "th comparison's value " + cv + " cannot be parsed as a Boolean"); |
309 | |
|
310 | |
} |
311 | |
|
312 | |
private Float parseFloat(int i, String cv) throws InvalidParameterException { |
313 | |
try { |
314 | 0 | return Float.parseFloat(cv); |
315 | 0 | } catch (NumberFormatException ex) { |
316 | 0 | throw new InvalidParameterException("The " + i + "th comparison's value " + cv + " cannot be parsed as an float"); |
317 | |
} |
318 | |
} |
319 | |
|
320 | |
private Double parseDouble(int i, String cv) throws InvalidParameterException { |
321 | |
try { |
322 | 0 | return Double.parseDouble(cv); |
323 | 0 | } catch (NumberFormatException ex) { |
324 | 0 | throw new InvalidParameterException("The " + i + "th comparison's value " + cv + " cannot be parsed as an double"); |
325 | |
} |
326 | |
} |
327 | |
|
328 | |
private String initLower(String str) { |
329 | 0 | if (str == null) { |
330 | 0 | return null; |
331 | |
} |
332 | 0 | if (str.length() == 0) { |
333 | 0 | return str; |
334 | |
} |
335 | 0 | if (str.length() == 1) { |
336 | 0 | return str.toLowerCase(); |
337 | |
} |
338 | 0 | return str.substring(0, 1).toLowerCase() + str.substring(1); |
339 | |
} |
340 | |
|
341 | |
private boolean calcIsList(AttributeDefinitionInfc ad) { |
342 | 0 | if (ad.getMaxOccurs() == null) { |
343 | 0 | return false; |
344 | |
} |
345 | 0 | if (ad.getMaxOccurs() <= 1) { |
346 | 0 | return false; |
347 | |
} |
348 | 0 | return true; |
349 | |
|
350 | |
} |
351 | |
|
352 | |
private AttributeDefinitionInfc getAttributeDefinition(String fk) |
353 | |
throws InvalidParameterException, |
354 | |
OperationFailedException { |
355 | 0 | for (AttributeDefinitionInfc ad : this.dictionaryEntry.getAttributes()) { |
356 | 0 | if (ad.getName().equals(fk)) { |
357 | 0 | return ad; |
358 | |
} |
359 | |
} |
360 | 0 | return null; |
361 | |
} |
362 | |
|
363 | |
} |
364 | |
|