1
2
3
4
5
6
7
8
9
10
11
12
13
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 public class ValidatorUtils {
29
30 public static boolean compareValues(Object value1, Object value2,
31 DataType dataType, String operator, boolean isCaseSensitive, DateParser dateParser) {
32
33 boolean result = false;
34 Integer compareResult = null;
35 if("has_value".equalsIgnoreCase(operator)){
36 if(value1==null){
37 return "false".equals(value2.toString().toLowerCase());
38 }
39 if(value1 instanceof Collection && ((Collection<?>) value1).isEmpty()){
40 return "false".equals(value2.toString().toLowerCase());
41 }
42 return "true".equals(value2.toString().toLowerCase());
43 }
44
45 if (null != dataType) {
46 if (DataType.STRING.equals(dataType)) {
47 String v1 = getString(value1);
48 String v2 = getString(value2);
49
50 if(!isCaseSensitive) {
51 v1 = v1.toUpperCase();
52 v2 = v2.toUpperCase();
53 }
54
55 compareResult = v1.compareTo(v2);
56 } else if (DataType.INTEGER.equals(dataType)) {
57 Integer v1 = getInteger(value1);
58 Integer v2 = getInteger(value2);
59 compareResult = v1.compareTo(v2);
60 } else if (DataType.LONG.equals(dataType)) {
61 Long v1 = getLong(value1);
62 Long v2 = getLong(value2);
63 compareResult = v1.compareTo(v2);
64 } else if (DataType.DOUBLE.equals(dataType)) {
65 Double v1 = getDouble(value1);
66 Double v2 = getDouble(value2);
67 compareResult = v1.compareTo(v2);
68 } else if (DataType.FLOAT.equals(dataType)) {
69 Float v1 = getFloat(value1);
70 Float v2 = getFloat(value2);
71 compareResult = v1.compareTo(v2);
72 } else if (DataType.BOOLEAN.equals(dataType)) {
73 Boolean v1 = getBoolean(value1);
74 Boolean v2 = getBoolean(value2);
75 compareResult = v1.compareTo(v2);
76 } else if (DataType.DATE.equals(dataType)) {
77 Date v1 = getDate(value1, dateParser);
78 Date v2 = getDate(value2, dateParser);
79 compareResult = v1.compareTo(v2);
80 }
81 }
82
83 if (null != compareResult) {
84 if (("equals".equalsIgnoreCase(operator)
85 || "greater_than_equal".equalsIgnoreCase(operator) || "less_than_equal"
86 .equalsIgnoreCase(operator))
87 && 0 == compareResult) {
88 result = true;
89 }
90
91 if (("not_equal".equalsIgnoreCase (operator)
92 || "greater_than".equalsIgnoreCase(operator) || "greater_than_equal".equalsIgnoreCase(operator)) && compareResult >= 1) {
93 result = true;
94 }
95
96 if (("not_equal".equalsIgnoreCase (operator)
97 || "less_than".equalsIgnoreCase(operator)|| "less_than_equal".equalsIgnoreCase(operator)) && compareResult <= -1) {
98 result = true;
99 }
100 }
101
102 return result;
103 }
104
105 public static Integer getInteger(Object o) {
106 Integer result = null;
107 if (o instanceof Integer)
108 return (Integer) o;
109 if (o == null)
110 return null;
111 if (o instanceof Number)
112 return ((Number) o).intValue();
113 String s = o.toString();
114 if (s != null && s.trim().length() > 0) {
115 result = Integer.valueOf(s.trim());
116 }
117 return result;
118 }
119
120 public static Long getLong(Object o) {
121 Long result = null;
122 if (o instanceof Long)
123 return (Long) o;
124 if (o == null)
125 return null;
126 if (o instanceof Number)
127 return ((Number) o).longValue();
128 String s = o.toString();
129 if (s != null && s.trim().length() > 0) {
130 result = Long.valueOf(s.trim());
131 }
132 return result;
133 }
134
135 public static Float getFloat(Object o) {
136 Float result = null;
137 if (o instanceof Float)
138 return (Float) o;
139 if (o == null)
140 return null;
141 if (o instanceof Number)
142 return ((Number) o).floatValue();
143 String s = o.toString();
144 if (s != null && s.trim().length() > 0) {
145 result = Float.valueOf(s.trim());
146 }
147 return result;
148 }
149
150 public static Double getDouble(Object o) {
151 Double result = null;
152 if (o instanceof Double)
153 return (Double) o;
154 if (o == null)
155 return null;
156 if (o instanceof Number)
157 return ((Number) o).doubleValue();
158 String s = o.toString();
159 if (s != null && s.trim().length() > 0) {
160 result = Double.valueOf(s.trim());
161 }
162 return result;
163 }
164
165 public static Date getDate(Object o, DateParser dateParser) {
166 Date result = null;
167 if (o instanceof Date)
168 return (Date) o;
169 if (o == null)
170 return null;
171 String s = o.toString();
172 if (s != null && s.trim().length() > 0) {
173 result = dateParser.parseDate(s.trim());
174 }
175 return result;
176 }
177
178 public static String getString(Object o) {
179 if (o instanceof String)
180 return (String) o;
181 if (o == null)
182 return null;
183 return o.toString();
184 }
185
186 public static Boolean getBoolean(Object o) {
187 Boolean result = null;
188 if (o instanceof Boolean)
189 return (Boolean) o;
190 if (o == null)
191 return null;
192 String s = o.toString();
193 if (s != null && s.trim().length() > 0) {
194 result = Boolean.parseBoolean(s.trim());
195 }
196 return result;
197 }
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214 public static FieldDefinition getField(String key, ObjectStructureDefinition objStructure) {
215 String[] lookupPathTokens = getPathTokens(key);
216 for(int i = 0; i < lookupPathTokens.length; i++) {
217 for (FieldDefinition f : objStructure.getAttributes()) {
218 if (f.getName().equals(lookupPathTokens[i])) {
219 if(i==lookupPathTokens.length-1){
220 return f;
221 }
222 else{
223 objStructure = f.getDataObjectStructure();
224 break;
225 }
226
227 }
228 }
229 }
230 return null;
231 }
232
233 private static String[] getPathTokens(String fieldPath) {
234 return (fieldPath != null && fieldPath.contains(".") ? fieldPath.split("\\.") : new String[]{fieldPath});
235 }
236
237
238
239
240
241
242
243 public static boolean hasErrors(List<ValidationResultInfo> validationResults){
244 if (validationResults !=null){
245 for (ValidationResultInfo vr:validationResults){
246 if (vr.getErrorLevel() == ErrorLevel.ERROR){
247 return true;
248 }
249 }
250 }
251
252 return false;
253 }
254
255
256
257
258
259
260
261 public static boolean hasWarnings(List<ValidationResultInfo> validationResults){
262 if (validationResults !=null){
263 for (ValidationResultInfo vr:validationResults){
264 if (vr.getErrorLevel() == ErrorLevel.WARN){
265 return true;
266 }
267 }
268 }
269
270 return false;
271 }
272
273 }
274