1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.common.validator.old; |
17 | |
|
18 | |
import java.util.Date; |
19 | |
import java.util.List; |
20 | |
|
21 | |
import org.kuali.student.core.dictionary.old.dto.Field; |
22 | |
import org.kuali.student.core.dictionary.old.dto.ObjectStructure; |
23 | |
import org.kuali.student.core.dictionary.old.dto.State; |
24 | |
import org.kuali.student.core.dictionary.old.dto.Type; |
25 | |
|
26 | 0 | public class ValidatorUtils { |
27 | |
|
28 | |
public static boolean compareValues(Object value1, Object value2, |
29 | |
String dataType, String operator, DateParser dateParser) { |
30 | |
|
31 | 0 | boolean result = false; |
32 | 0 | Integer compareResult = null; |
33 | |
|
34 | |
|
35 | 0 | if (null != dataType) { |
36 | 0 | if ("string".equalsIgnoreCase(dataType)) { |
37 | 0 | String v1 = getString(value1); |
38 | 0 | String v2 = getString(value2); |
39 | 0 | compareResult = v1.compareTo(v2); |
40 | 0 | } else if ("integer".equalsIgnoreCase(dataType)) { |
41 | 0 | Integer v1 = getInteger(value1); |
42 | 0 | Integer v2 = getInteger(value2); |
43 | 0 | compareResult = v1.compareTo(v2); |
44 | 0 | } else if ("long".equalsIgnoreCase(dataType)) { |
45 | 0 | Long v1 = getLong(value1); |
46 | 0 | Long v2 = getLong(value2); |
47 | 0 | compareResult = v1.compareTo(v2); |
48 | 0 | } else if ("double".equalsIgnoreCase(dataType)) { |
49 | 0 | Double v1 = getDouble(value1); |
50 | 0 | Double v2 = getDouble(value2); |
51 | 0 | compareResult = v1.compareTo(v2); |
52 | 0 | } else if ("float".equalsIgnoreCase(dataType)) { |
53 | 0 | Float v1 = getFloat(value1); |
54 | 0 | Float v2 = getFloat(value2); |
55 | 0 | compareResult = v1.compareTo(v2); |
56 | 0 | } else if ("boolean".equalsIgnoreCase(dataType)) { |
57 | 0 | Boolean v1 = getBoolean(value1); |
58 | 0 | Boolean v2 = getBoolean(value2); |
59 | 0 | compareResult = v1.compareTo(v2); |
60 | 0 | } else if ("date".equalsIgnoreCase(dataType)) { |
61 | 0 | Date v1 = getDate(value1, dateParser); |
62 | 0 | Date v2 = getDate(value2, dateParser); |
63 | 0 | compareResult = v1.compareTo(v2); |
64 | |
} |
65 | |
} |
66 | |
|
67 | 0 | if (null != compareResult) { |
68 | 0 | if (("equals".equalsIgnoreCase(operator) |
69 | |
|| "greater_than_equal".equalsIgnoreCase(operator) || "less_than_equal" |
70 | |
.equalsIgnoreCase(operator)) |
71 | |
&& 0 == compareResult) { |
72 | 0 | result = true; |
73 | |
} |
74 | |
|
75 | 0 | if ("greater_than".equalsIgnoreCase(operator) && 1 == compareResult) { |
76 | 0 | result = true; |
77 | |
} |
78 | |
|
79 | 0 | if ("less_than".equalsIgnoreCase(operator) && -1 == compareResult) { |
80 | 0 | result = true; |
81 | |
} |
82 | |
} |
83 | |
|
84 | 0 | return result; |
85 | |
} |
86 | |
|
87 | |
protected static Integer getInteger(Object o) { |
88 | 0 | if (o == null) |
89 | 0 | return null; |
90 | |
|
91 | 0 | Integer result = null; |
92 | 0 | if (o instanceof Integer) |
93 | 0 | return (Integer) o; |
94 | 0 | if (o instanceof Number) |
95 | 0 | return ((Number) o).intValue(); |
96 | 0 | String s = o.toString(); |
97 | 0 | if (s != null && s.trim().length() > 0) { |
98 | 0 | result = Integer.valueOf(s.trim()); |
99 | |
} |
100 | 0 | return result; |
101 | |
} |
102 | |
|
103 | |
protected static Long getLong(Object o) { |
104 | 0 | if (o == null) |
105 | 0 | return null; |
106 | |
|
107 | 0 | Long result = null; |
108 | 0 | if (o instanceof Long) |
109 | 0 | return (Long) o; |
110 | 0 | if (o instanceof Number) |
111 | 0 | return ((Number) o).longValue(); |
112 | 0 | String s = o.toString(); |
113 | 0 | if (s != null && s.trim().length() > 0) { |
114 | 0 | result = Long.valueOf(s.trim()); |
115 | |
} |
116 | 0 | return result; |
117 | |
} |
118 | |
|
119 | |
protected static Float getFloat(Object o) { |
120 | 0 | if (o == null) |
121 | 0 | return null; |
122 | |
|
123 | 0 | Float result = null; |
124 | 0 | if (o instanceof Float) |
125 | 0 | return (Float) o; |
126 | 0 | if (o instanceof Number) |
127 | 0 | return ((Number) o).floatValue(); |
128 | 0 | String s = o.toString(); |
129 | 0 | if (s != null && s.trim().length() > 0) { |
130 | 0 | result = Float.valueOf(s.trim()); |
131 | |
} |
132 | 0 | return result; |
133 | |
} |
134 | |
|
135 | |
protected static Double getDouble(Object o) { |
136 | 2 | if (o == null) |
137 | 0 | return null; |
138 | |
|
139 | 2 | Double result = null; |
140 | 2 | if (o instanceof Double) |
141 | 0 | return (Double) o; |
142 | 2 | if (o instanceof Number) |
143 | 0 | return ((Number) o).doubleValue(); |
144 | 2 | String s = o.toString(); |
145 | 2 | if (s != null && s.trim().length() > 0) { |
146 | 2 | result = Double.valueOf(s.trim()); |
147 | |
} |
148 | 2 | return result; |
149 | |
} |
150 | |
|
151 | |
protected static Date getDate(Object o, DateParser dateParser) { |
152 | 6 | if (o == null) |
153 | 3 | return null; |
154 | |
|
155 | 3 | Date result = null; |
156 | 3 | if (o instanceof Date) |
157 | 0 | return (Date) o; |
158 | 3 | String s = o.toString(); |
159 | 3 | if (s != null && s.trim().length() > 0) { |
160 | 3 | result = dateParser.parseDate(s.trim()); |
161 | |
} |
162 | 3 | return result; |
163 | |
} |
164 | |
|
165 | |
protected static String getString(Object o) { |
166 | 9 | if (o == null) |
167 | 0 | return null; |
168 | |
|
169 | 9 | if (o instanceof String) |
170 | 9 | return (String) o; |
171 | 0 | return o.toString(); |
172 | |
} |
173 | |
|
174 | |
private static Boolean getBoolean(Object o) { |
175 | 0 | if (o == null) |
176 | 0 | return null; |
177 | |
|
178 | 0 | Boolean result = null; |
179 | 0 | if (o instanceof Boolean) |
180 | 0 | return (Boolean) o; |
181 | 0 | String s = o.toString(); |
182 | 0 | if (s != null && s.trim().length() > 0) { |
183 | 0 | result = Boolean.parseBoolean(s.trim()); |
184 | |
} |
185 | 0 | return result; |
186 | |
} |
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
protected static Field getField(String key, ObjectStructure objStructure, |
199 | |
String type, String state) { |
200 | 0 | List<Type> typeList = objStructure.getType(); |
201 | |
|
202 | 0 | for (Type t : typeList) { |
203 | 0 | if (t.getKey().equalsIgnoreCase(type)) { |
204 | 0 | for (State s : t.getState()) { |
205 | 0 | if (s.getKey().equalsIgnoreCase(state)) { |
206 | 0 | for (Field f : s.getField()) { |
207 | 0 | if (f.getKey().equals(key)) { |
208 | 0 | return f; |
209 | |
} |
210 | |
} |
211 | |
} |
212 | |
} |
213 | |
} |
214 | |
} |
215 | |
|
216 | 0 | return null; |
217 | |
} |
218 | |
|
219 | |
} |
220 | |
|