Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ValidatorUtils |
|
| 8.666666666666666;8.667 |
1 | /** | |
2 | * Copyright 2010 The Kuali Foundation Licensed under the | |
3 | * Educational Community License, Version 2.0 (the "License"); you may | |
4 | * not use this file except in compliance with the License. You may | |
5 | * obtain a copy of the License at | |
6 | * | |
7 | * http://www.osedu.org/licenses/ECL-2.0 | |
8 | * | |
9 | * Unless required by applicable law or agreed to in writing, | |
10 | * software distributed under the License is distributed on an "AS IS" | |
11 | * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | |
12 | * or implied. See the License for the specific language governing | |
13 | * permissions and limitations under the License. | |
14 | */ | |
15 | ||
16 | package org.kuali.student.common.validator; | |
17 | ||
18 | import java.util.Collection; | |
19 | import java.util.Date; | |
20 | import java.util.List; | |
21 | ||
22 | import org.kuali.student.common.dictionary.dto.DataType; | |
23 | import org.kuali.student.common.dictionary.dto.FieldDefinition; | |
24 | import org.kuali.student.common.dictionary.dto.ObjectStructureDefinition; | |
25 | import org.kuali.student.common.validation.dto.ValidationResultInfo; | |
26 | import org.kuali.student.common.validation.dto.ValidationResultInfo.ErrorLevel; | |
27 | ||
28 | 0 | public class ValidatorUtils { |
29 | ||
30 | public static boolean compareValues(Object value1, Object value2, | |
31 | DataType dataType, String operator, boolean isCaseSensitive, DateParser dateParser) { | |
32 | ||
33 | 21 | boolean result = false; |
34 | 21 | Integer compareResult = null; |
35 | 21 | if("has_value".equalsIgnoreCase(operator)){ |
36 | 0 | if(value1==null){ |
37 | 0 | return "false".equals(value2.toString().toLowerCase()); |
38 | } | |
39 | 0 | if(value1 instanceof Collection && ((Collection<?>) value1).isEmpty()){ |
40 | 0 | return "false".equals(value2.toString().toLowerCase()); |
41 | } | |
42 | 0 | return "true".equals(value2.toString().toLowerCase()); |
43 | } | |
44 | // Convert objects into appropriate data types | |
45 | 21 | if (null != dataType) { |
46 | 7 | if (DataType.STRING.equals(dataType)) { |
47 | 7 | String v1 = getString(value1); |
48 | 7 | String v2 = getString(value2); |
49 | ||
50 | 7 | if(!isCaseSensitive) { |
51 | 7 | v1 = v1.toUpperCase(); |
52 | 7 | v2 = v2.toUpperCase(); |
53 | } | |
54 | ||
55 | 7 | compareResult = v1.compareTo(v2); |
56 | 7 | } else if (DataType.INTEGER.equals(dataType)) { |
57 | 0 | Integer v1 = getInteger(value1); |
58 | 0 | Integer v2 = getInteger(value2); |
59 | 0 | compareResult = v1.compareTo(v2); |
60 | 0 | } else if (DataType.LONG.equals(dataType)) { |
61 | 0 | Long v1 = getLong(value1); |
62 | 0 | Long v2 = getLong(value2); |
63 | 0 | compareResult = v1.compareTo(v2); |
64 | 0 | } else if (DataType.DOUBLE.equals(dataType)) { |
65 | 0 | Double v1 = getDouble(value1); |
66 | 0 | Double v2 = getDouble(value2); |
67 | 0 | compareResult = v1.compareTo(v2); |
68 | 0 | } else if (DataType.FLOAT.equals(dataType)) { |
69 | 0 | Float v1 = getFloat(value1); |
70 | 0 | Float v2 = getFloat(value2); |
71 | 0 | compareResult = v1.compareTo(v2); |
72 | 0 | } else if (DataType.BOOLEAN.equals(dataType)) { |
73 | 0 | Boolean v1 = getBoolean(value1); |
74 | 0 | Boolean v2 = getBoolean(value2); |
75 | 0 | compareResult = v1.compareTo(v2); |
76 | 0 | } else if (DataType.DATE.equals(dataType)) { |
77 | 0 | Date v1 = getDate(value1, dateParser); |
78 | 0 | Date v2 = getDate(value2, dateParser); |
79 | 0 | compareResult = v1.compareTo(v2); |
80 | } | |
81 | } | |
82 | ||
83 | 21 | if (null != compareResult) { |
84 | 7 | if (("equals".equalsIgnoreCase(operator) |
85 | || "greater_than_equal".equalsIgnoreCase(operator) || "less_than_equal" | |
86 | .equalsIgnoreCase(operator)) | |
87 | && 0 == compareResult) { | |
88 | 3 | result = true; |
89 | } | |
90 | ||
91 | 7 | if (("not_equal".equalsIgnoreCase (operator) |
92 | || "greater_than".equalsIgnoreCase(operator) || "greater_than_equal".equalsIgnoreCase(operator)) && compareResult >= 1) { | |
93 | 0 | result = true; |
94 | } | |
95 | ||
96 | 7 | if (("not_equal".equalsIgnoreCase (operator) |
97 | || "less_than".equalsIgnoreCase(operator)|| "less_than_equal".equalsIgnoreCase(operator)) && compareResult <= -1) { | |
98 | 0 | result = true; |
99 | } | |
100 | } | |
101 | ||
102 | 21 | return result; |
103 | } | |
104 | ||
105 | public static Integer getInteger(Object o) { | |
106 | 0 | Integer result = null; |
107 | 0 | if (o instanceof Integer) |
108 | 0 | return (Integer) o; |
109 | 0 | if (o == null) |
110 | 0 | return null; |
111 | 0 | if (o instanceof Number) |
112 | 0 | return ((Number) o).intValue(); |
113 | 0 | String s = o.toString(); |
114 | 0 | if (s != null && s.trim().length() > 0) { |
115 | 0 | result = Integer.valueOf(s.trim()); |
116 | } | |
117 | 0 | return result; |
118 | } | |
119 | ||
120 | public static Long getLong(Object o) { | |
121 | 0 | Long result = null; |
122 | 0 | if (o instanceof Long) |
123 | 0 | return (Long) o; |
124 | 0 | if (o == null) |
125 | 0 | return null; |
126 | 0 | if (o instanceof Number) |
127 | 0 | return ((Number) o).longValue(); |
128 | 0 | String s = o.toString(); |
129 | 0 | if (s != null && s.trim().length() > 0) { |
130 | 0 | result = Long.valueOf(s.trim()); |
131 | } | |
132 | 0 | return result; |
133 | } | |
134 | ||
135 | public static Float getFloat(Object o) { | |
136 | 0 | Float result = null; |
137 | 0 | if (o instanceof Float) |
138 | 0 | return (Float) o; |
139 | 0 | if (o == null) |
140 | 0 | return null; |
141 | 0 | if (o instanceof Number) |
142 | 0 | return ((Number) o).floatValue(); |
143 | 0 | String s = o.toString(); |
144 | 0 | if (s != null && s.trim().length() > 0) { |
145 | 0 | result = Float.valueOf(s.trim()); |
146 | } | |
147 | 0 | return result; |
148 | } | |
149 | ||
150 | public static Double getDouble(Object o) { | |
151 | 2 | Double result = null; |
152 | 2 | if (o instanceof Double) |
153 | 0 | return (Double) o; |
154 | 2 | if (o == null) |
155 | 0 | return null; |
156 | 2 | if (o instanceof Number) |
157 | 0 | return ((Number) o).doubleValue(); |
158 | 2 | String s = o.toString(); |
159 | 2 | if (s != null && s.trim().length() > 0) { |
160 | 2 | result = Double.valueOf(s.trim()); |
161 | } | |
162 | 2 | return result; |
163 | } | |
164 | ||
165 | public static Date getDate(Object o, DateParser dateParser) { | |
166 | 14 | Date result = null; |
167 | 14 | if (o instanceof Date) |
168 | 0 | return (Date) o; |
169 | 14 | if (o == null) |
170 | 7 | return null; |
171 | 7 | String s = o.toString(); |
172 | 7 | if (s != null && s.trim().length() > 0) { |
173 | 7 | result = dateParser.parseDate(s.trim()); |
174 | } | |
175 | 7 | return result; |
176 | } | |
177 | ||
178 | public static String getString(Object o) { | |
179 | 35 | if (o instanceof String) |
180 | 35 | return (String) o; |
181 | 0 | if (o == null) |
182 | 0 | return null; |
183 | 0 | return o.toString(); |
184 | } | |
185 | ||
186 | public static Boolean getBoolean(Object o) { | |
187 | 0 | Boolean result = null; |
188 | 0 | if (o instanceof Boolean) |
189 | 0 | return (Boolean) o; |
190 | 0 | if (o == null) |
191 | 0 | return null; |
192 | 0 | String s = o.toString(); |
193 | 0 | if (s != null && s.trim().length() > 0) { |
194 | 0 | result = Boolean.parseBoolean(s.trim()); |
195 | } | |
196 | 0 | return result; |
197 | } | |
198 | ||
199 | /** | |
200 | * Traverses the dictionary ObjectStructure to find the field with the match | |
201 | * key, type and state | |
202 | * The key has to relative to the current object structure that is being traversed. | |
203 | * example: current object structure is ActivityInfo and if we want to lookup | |
204 | * the academicSubjectorgId, then <property name="fieldPath" value="academicSubjectOrgs.orgId"/> | |
205 | * The current object structure starts from the field on which the constraint is applied on. | |
206 | * If we want to address fields outside of this object structure we ll need to pass in the | |
207 | * dictionary context. | |
208 | * @param key | |
209 | * @param type | |
210 | * @param state | |
211 | * @param objStructure | |
212 | * @return | |
213 | */ | |
214 | public static FieldDefinition getField(String key, ObjectStructureDefinition objStructure) { | |
215 | 28 | String[] lookupPathTokens = getPathTokens(key); |
216 | 45 | for(int i = 0; i < lookupPathTokens.length; i++) { |
217 | 28 | for (FieldDefinition f : objStructure.getAttributes()) { |
218 | 120 | if (f.getName().equals(lookupPathTokens[i])) { |
219 | 11 | if(i==lookupPathTokens.length-1){ |
220 | 11 | return f; |
221 | } | |
222 | else{ | |
223 | 0 | objStructure = f.getDataObjectStructure(); |
224 | 0 | break; |
225 | } | |
226 | ||
227 | } | |
228 | } | |
229 | } | |
230 | 17 | return null; |
231 | } | |
232 | ||
233 | private static String[] getPathTokens(String fieldPath) { | |
234 | 28 | return (fieldPath != null && fieldPath.contains(".") ? fieldPath.split("\\.") : new String[]{fieldPath}); |
235 | } | |
236 | ||
237 | /** | |
238 | * Inspect the validation result to determine if there are any errors. | |
239 | * | |
240 | * @param validationResults | |
241 | * @return true if at least one validation result is an error. | |
242 | */ | |
243 | public static boolean hasErrors(List<ValidationResultInfo> validationResults){ | |
244 | 0 | if (validationResults !=null){ |
245 | 0 | for (ValidationResultInfo vr:validationResults){ |
246 | 0 | if (vr.getErrorLevel() == ErrorLevel.ERROR){ |
247 | 0 | return true; |
248 | } | |
249 | } | |
250 | } | |
251 | ||
252 | 0 | return false; |
253 | } | |
254 | ||
255 | /** | |
256 | * Inspect the validation result to determine if there are any errors. | |
257 | * | |
258 | * @param validationResults | |
259 | * @return true if at least one validation result is an error. | |
260 | */ | |
261 | public static boolean hasWarnings(List<ValidationResultInfo> validationResults){ | |
262 | 0 | if (validationResults !=null){ |
263 | 0 | for (ValidationResultInfo vr:validationResults){ |
264 | 0 | if (vr.getErrorLevel() == ErrorLevel.WARN){ |
265 | 0 | return true; |
266 | } | |
267 | } | |
268 | } | |
269 | ||
270 | 0 | return false; |
271 | } | |
272 | ||
273 | } | |
274 |