1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.krad.datadictionary.validation; |
17 | |
|
18 | |
import org.apache.commons.lang.StringUtils; |
19 | |
import org.kuali.rice.core.api.datetime.DateTimeService; |
20 | |
import org.kuali.rice.krad.datadictionary.exception.AttributeValidationException; |
21 | |
|
22 | |
import java.math.BigDecimal; |
23 | |
import java.text.ParseException; |
24 | |
import java.util.Collection; |
25 | |
import java.util.Date; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | 0 | public class ValidationUtils { |
33 | |
|
34 | |
public static String buildPath(String attributePath, String attributeName) { |
35 | 24 | if (StringUtils.isNotBlank(attributeName)) { |
36 | 24 | if (StringUtils.isNotBlank(attributePath)) |
37 | 0 | return new StringBuilder(attributePath).append(".").append(attributeName).toString(); |
38 | |
|
39 | 24 | return attributeName; |
40 | |
} |
41 | 0 | return attributePath; |
42 | |
} |
43 | |
|
44 | |
public static boolean compareValues(Object value1, Object value2, |
45 | |
DataType dataType, String operator, boolean isCaseSensitive, DateTimeService dateTimeService) { |
46 | |
|
47 | 2 | boolean result = false; |
48 | 2 | Integer compareResult = null; |
49 | |
|
50 | 2 | if("has_value".equalsIgnoreCase(operator)){ |
51 | 0 | if(value1==null){ |
52 | 0 | return "false".equals(value2.toString().toLowerCase()); |
53 | |
} |
54 | 0 | if(value1 instanceof Collection && ((Collection<?>) value1).isEmpty()){ |
55 | 0 | return "false".equals(value2.toString().toLowerCase()); |
56 | |
} |
57 | 0 | return "true".equals(value2.toString().toLowerCase()); |
58 | |
} |
59 | |
|
60 | 2 | if (null != dataType) { |
61 | 2 | if (DataType.STRING.equals(dataType)) { |
62 | 2 | String v1 = getString(value1); |
63 | 2 | String v2 = getString(value2); |
64 | |
|
65 | 2 | if(!isCaseSensitive) { |
66 | 2 | v1 = v1.toUpperCase(); |
67 | 2 | v2 = v2.toUpperCase(); |
68 | |
} |
69 | |
|
70 | 2 | compareResult = v1.compareTo(v2); |
71 | 2 | } else if (DataType.INTEGER.equals(dataType)) { |
72 | 0 | Integer v1 = getInteger(value1); |
73 | 0 | Integer v2 = getInteger(value2); |
74 | 0 | compareResult = v1.compareTo(v2); |
75 | 0 | } else if (DataType.LONG.equals(dataType)) { |
76 | 0 | Long v1 = getLong(value1); |
77 | 0 | Long v2 = getLong(value2); |
78 | 0 | compareResult = v1.compareTo(v2); |
79 | 0 | } else if (DataType.DOUBLE.equals(dataType)) { |
80 | 0 | Double v1 = getDouble(value1); |
81 | 0 | Double v2 = getDouble(value2); |
82 | 0 | compareResult = v1.compareTo(v2); |
83 | 0 | } else if (DataType.FLOAT.equals(dataType)) { |
84 | 0 | Float v1 = getFloat(value1); |
85 | 0 | Float v2 = getFloat(value2); |
86 | 0 | compareResult = v1.compareTo(v2); |
87 | 0 | } else if (DataType.BOOLEAN.equals(dataType)) { |
88 | 0 | Boolean v1 = getBoolean(value1); |
89 | 0 | Boolean v2 = getBoolean(value2); |
90 | 0 | compareResult = v1.compareTo(v2); |
91 | 0 | } else if (DataType.DATE.equals(dataType)) { |
92 | 0 | Date v1 = getDate(value1, dateTimeService); |
93 | 0 | Date v2 = getDate(value2, dateTimeService); |
94 | 0 | compareResult = v1.compareTo(v2); |
95 | |
} |
96 | |
} |
97 | |
|
98 | 2 | if (null != compareResult) { |
99 | 2 | if (("equals".equalsIgnoreCase(operator) |
100 | |
|| "greater_than_equal".equalsIgnoreCase(operator) || "less_than_equal" |
101 | |
.equalsIgnoreCase(operator)) |
102 | |
&& 0 == compareResult) { |
103 | 1 | result = true; |
104 | |
} |
105 | |
|
106 | 2 | if (("not_equal".equalsIgnoreCase (operator) |
107 | |
|| "greater_than".equalsIgnoreCase(operator)) && compareResult >= 1) { |
108 | 0 | result = true; |
109 | |
} |
110 | |
|
111 | 2 | if (("not_equal".equalsIgnoreCase (operator) |
112 | |
|| "less_than".equalsIgnoreCase(operator)) && compareResult <= -1) { |
113 | 0 | result = true; |
114 | |
} |
115 | |
} |
116 | |
|
117 | 2 | return result; |
118 | |
} |
119 | |
|
120 | |
public static Integer getInteger(Object o) { |
121 | 0 | Integer result = null; |
122 | 0 | if (o instanceof Integer) |
123 | 0 | return (Integer) o; |
124 | 0 | if (o == null) |
125 | 0 | return null; |
126 | 0 | if (o instanceof Number) |
127 | 0 | return ((Number) o).intValue(); |
128 | 0 | String s = o.toString(); |
129 | 0 | if (s != null && s.trim().length() > 0) { |
130 | 0 | result = Integer.valueOf(s.trim()); |
131 | |
} |
132 | 0 | return result; |
133 | |
} |
134 | |
|
135 | |
public static Long getLong(Object o) { |
136 | 0 | Long result = null; |
137 | 0 | if (o instanceof Long) |
138 | 0 | return (Long) o; |
139 | 0 | if (o == null) |
140 | 0 | return null; |
141 | 0 | if (o instanceof Number) |
142 | 0 | return ((Number) o).longValue(); |
143 | 0 | String s = o.toString(); |
144 | 0 | if (s != null && s.trim().length() > 0) { |
145 | 0 | result = Long.valueOf(s.trim()); |
146 | |
} |
147 | 0 | return result; |
148 | |
} |
149 | |
|
150 | |
public static Float getFloat(Object o) { |
151 | 0 | Float result = null; |
152 | 0 | if (o instanceof Float) |
153 | 0 | return (Float) o; |
154 | 0 | if (o == null) |
155 | 0 | return null; |
156 | 0 | if (o instanceof Number) |
157 | 0 | return ((Number) o).floatValue(); |
158 | 0 | String s = o.toString(); |
159 | 0 | if (s != null && s.trim().length() > 0) { |
160 | 0 | result = Float.valueOf(s.trim()); |
161 | |
} |
162 | 0 | return result; |
163 | |
} |
164 | |
|
165 | |
public static Double getDouble(Object o) { |
166 | 0 | Double result = null; |
167 | 0 | if (o instanceof BigDecimal) |
168 | 0 | return ((BigDecimal) o).doubleValue(); |
169 | 0 | if (o instanceof Double) |
170 | 0 | return (Double) o; |
171 | 0 | if (o == null) |
172 | 0 | return null; |
173 | 0 | if (o instanceof Number) |
174 | 0 | return ((Number) o).doubleValue(); |
175 | 0 | String s = o.toString(); |
176 | 0 | if (s != null && s.trim().length() > 0) { |
177 | 0 | result = Double.valueOf(s.trim()); |
178 | |
} |
179 | 0 | return result; |
180 | |
} |
181 | |
|
182 | |
public static Date getDate(Object o, DateTimeService dateTimeService) throws IllegalArgumentException { |
183 | 0 | Date result = null; |
184 | 0 | if (o instanceof Date) |
185 | 0 | return (Date) o; |
186 | 0 | if (o == null) |
187 | 0 | return null; |
188 | 0 | String s = o.toString(); |
189 | 0 | if (s != null && s.trim().length() > 0) { |
190 | |
try { |
191 | 0 | result = dateTimeService.convertToDate(s.trim()); |
192 | 0 | } catch (ParseException e) { |
193 | 0 | throw new IllegalArgumentException(e); |
194 | 0 | } |
195 | |
} |
196 | 0 | return result; |
197 | |
} |
198 | |
|
199 | |
public static String getString(Object o) { |
200 | 8 | if (o instanceof String) |
201 | 8 | return (String) o; |
202 | 0 | if (o == null) |
203 | 0 | return null; |
204 | 0 | return o.toString(); |
205 | |
} |
206 | |
|
207 | |
public static Boolean getBoolean(Object o) { |
208 | 0 | Boolean result = null; |
209 | 0 | if (o instanceof Boolean) |
210 | 0 | return (Boolean) o; |
211 | 0 | if (o == null) |
212 | 0 | return null; |
213 | 0 | String s = o.toString(); |
214 | 0 | if (s != null && s.trim().length() > 0) { |
215 | 0 | result = Boolean.parseBoolean(s.trim()); |
216 | |
} |
217 | 0 | return result; |
218 | |
} |
219 | |
|
220 | |
|
221 | |
public static boolean hasText(String string) { |
222 | |
|
223 | 18 | if (string == null || string.length() < 1) { |
224 | 13 | return false; |
225 | |
} |
226 | 5 | int stringLength = string.length(); |
227 | |
|
228 | 5 | for (int i = 0; i < stringLength; i++) { |
229 | 5 | char currentChar = string.charAt(i); |
230 | 5 | if (' ' != currentChar || '\t' != currentChar || '\n' != currentChar) { |
231 | 5 | return true; |
232 | |
} |
233 | |
} |
234 | |
|
235 | 0 | return false; |
236 | |
} |
237 | |
|
238 | |
public static boolean isNullOrEmpty(Object value) { |
239 | 42 | return value == null || (value instanceof String && StringUtils.isBlank(((String) value).trim())); |
240 | |
} |
241 | |
|
242 | |
|
243 | 4 | public static enum Result { VALID, INVALID, UNDEFINED }; |
244 | |
|
245 | |
public static Object convertToDataType(Object value, DataType dataType, DateTimeService dateTimeService) throws AttributeValidationException { |
246 | 12 | Object returnValue = value; |
247 | |
|
248 | 12 | if (null == value) |
249 | 0 | return null; |
250 | |
|
251 | 12 | switch (dataType) { |
252 | |
case BOOLEAN: |
253 | 0 | if (! (value instanceof Boolean)) { |
254 | 0 | returnValue = Boolean.valueOf(value.toString()); |
255 | |
|
256 | |
|
257 | 0 | if (!value.toString().equalsIgnoreCase("TRUE") && !value.toString().equalsIgnoreCase("FALSE")) |
258 | 0 | throw new AttributeValidationException("Value " + value.toString() + " is not a boolean!"); |
259 | |
} |
260 | |
break; |
261 | |
case INTEGER: |
262 | 0 | if (! (value instanceof Number)) { |
263 | 0 | returnValue = Integer.valueOf(value.toString()); |
264 | |
} |
265 | |
break; |
266 | |
case LONG: |
267 | 6 | if (! (value instanceof Number)) { |
268 | 6 | returnValue = Long.valueOf(value.toString()); |
269 | |
} |
270 | |
break; |
271 | |
case DOUBLE: |
272 | 0 | if (! (value instanceof Number)) { |
273 | 0 | returnValue = Double.valueOf(value.toString()); |
274 | |
} |
275 | 0 | if (((Double)returnValue).isNaN()) |
276 | 0 | throw new AttributeValidationException("Infinite Double values are not valid!"); |
277 | 0 | if (((Double)returnValue).isInfinite()) |
278 | 0 | throw new AttributeValidationException("Infinite Double values are not valid!"); |
279 | |
break; |
280 | |
case FLOAT: |
281 | 0 | if (! (value instanceof Number)) { |
282 | 0 | returnValue = Float.valueOf(value.toString()); |
283 | |
} |
284 | 0 | if (((Float)returnValue).isNaN()) |
285 | 0 | throw new AttributeValidationException("NaN Float values are not valid!"); |
286 | 0 | if (((Float)returnValue).isInfinite()) |
287 | 0 | throw new AttributeValidationException("Infinite Float values are not valid!"); |
288 | |
break; |
289 | |
case TRUNCATED_DATE: |
290 | |
case DATE: |
291 | 0 | if (! (value instanceof Date)) { |
292 | |
try { |
293 | 0 | returnValue = dateTimeService.convertToDate(value.toString()); |
294 | 0 | } catch (ParseException pe) { |
295 | 0 | throw new AttributeValidationException("Value " + value.toString() + " is not a date!"); |
296 | 0 | } |
297 | |
} |
298 | |
break; |
299 | |
case STRING: |
300 | |
case COMPLEX: |
301 | |
break; |
302 | |
} |
303 | |
|
304 | 12 | return returnValue; |
305 | |
} |
306 | |
|
307 | |
public static <T> Result isGreaterThan(T value, Comparable<T> limit) { |
308 | 6 | return limit == null ? Result.UNDEFINED : ( limit.compareTo(value) < 0 ? Result.VALID : Result.INVALID ); |
309 | |
} |
310 | |
|
311 | |
public static <T> Result isGreaterThanOrEqual(T value, Comparable<T> limit) { |
312 | 12 | return limit == null ? Result.UNDEFINED : ( limit.compareTo(value) <= 0 ? Result.VALID : Result.INVALID ); |
313 | |
} |
314 | |
|
315 | |
public static <T> Result isLessThan(T value, Comparable<T> limit) { |
316 | 0 | return limit == null ? Result.UNDEFINED : ( limit.compareTo(value) > 0 ? Result.VALID : Result.INVALID ); |
317 | |
} |
318 | |
|
319 | |
public static <T> Result isLessThanOrEqual(T value, Comparable<T> limit) { |
320 | 18 | return limit == null ? Result.UNDEFINED : ( limit.compareTo(value) >= 0 ? Result.VALID : Result.INVALID ); |
321 | |
} |
322 | |
|
323 | |
|
324 | |
public static String[] getPathTokens(String fieldPath) { |
325 | 0 | return (fieldPath != null && fieldPath.contains(".") ? fieldPath.split("\\.") : new String[]{fieldPath}); |
326 | |
} |
327 | |
|
328 | |
} |
329 | |
|