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