Clover Coverage Report - Kuali Student 1.2-M2-SNAPSHOT (Aggregated)
Coverage timestamp: Fri Apr 22 2011 04:03:20 EST
../../../../../img/srcFileCovDistChart5.png 45% of files have more coverage
135   274   77   11.25
96   204   0.57   12
12     6.42  
1    
 
  ValidatorUtils       Line # 28 135 0% 77 139 42.8% 0.42798355
 
  (92)
 
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    public class ValidatorUtils {
29   
 
30  396 toggle public static boolean compareValues(Object value1, Object value2,
31    DataType dataType, String operator, boolean isCaseSensitive, DateParser dateParser) {
32   
33  396 boolean result = false;
34  396 Integer compareResult = null;
35  396 if("has_value".equalsIgnoreCase(operator)){
36  6 if(value1==null){
37  0 return "false".equals(value2.toString().toLowerCase());
38    }
39  6 if(value1 instanceof Collection && ((Collection<?>) value1).isEmpty()){
40  0 return "false".equals(value2.toString().toLowerCase());
41    }
42  6 return "true".equals(value2.toString().toLowerCase());
43    }
44    // Convert objects into appropriate data types
45  390 if (null != dataType) {
46  378 if (DataType.STRING.equals(dataType)) {
47  378 String v1 = getString(value1);
48  378 String v2 = getString(value2);
49   
50  378 if(!isCaseSensitive) {
51  378 v1 = v1.toUpperCase();
52  378 v2 = v2.toUpperCase();
53    }
54   
55  378 compareResult = v1.compareTo(v2);
56  0 } 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  390 if (null != compareResult) {
84  378 if (("equals".equalsIgnoreCase(operator)
85    || "greater_than_equal".equalsIgnoreCase(operator) || "less_than_equal"
86    .equalsIgnoreCase(operator))
87    && 0 == compareResult) {
88  6 result = true;
89    }
90   
91  378 if (("not_equal".equalsIgnoreCase (operator)
92    || "greater_than".equalsIgnoreCase(operator) || "greater_than_equal".equalsIgnoreCase(operator)) && compareResult >= 1) {
93  0 result = true;
94    }
95   
96  378 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  390 return result;
103    }
104   
 
105  106 toggle public static Integer getInteger(Object o) {
106  106 Integer result = null;
107  106 if (o instanceof Integer)
108  0 return (Integer) o;
109  106 if (o == null)
110  106 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  62 toggle public static Long getLong(Object o) {
121  62 Long result = null;
122  62 if (o instanceof Long)
123  0 return (Long) o;
124  62 if (o == null)
125  62 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  0 toggle 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  2 toggle 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  1026 toggle public static Date getDate(Object o, DateParser dateParser) {
166  1026 Date result = null;
167  1026 if (o instanceof Date)
168  0 return (Date) o;
169  1026 if (o == null)
170  1021 return null;
171  5 String s = o.toString();
172  5 if (s != null && s.trim().length() > 0) {
173  5 result = dateParser.parseDate(s.trim());
174    }
175  5 return result;
176    }
177   
 
178  2205 toggle public static String getString(Object o) {
179  2205 if (o instanceof String)
180  2204 return (String) o;
181  1 if (o == null)
182  0 return null;
183  1 return o.toString();
184    }
185   
 
186  0 toggle 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  159 toggle public static FieldDefinition getField(String key, ObjectStructureDefinition objStructure) {
215  159 String[] lookupPathTokens = getPathTokens(key);
216  173 for(int i = 0; i < lookupPathTokens.length; i++) {
217  159 for (FieldDefinition f : objStructure.getAttributes()) {
218  4477 if (f.getName().equals(lookupPathTokens[i])) {
219  145 if(i==lookupPathTokens.length-1){
220  145 return f;
221    }
222    else{
223  0 objStructure = f.getDataObjectStructure();
224  0 break;
225    }
226   
227    }
228    }
229    }
230  14 return null;
231    }
232   
 
233  159 toggle private static String[] getPathTokens(String fieldPath) {
234  159 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  45 toggle public static boolean hasErrors(List<ValidationResultInfo> validationResults){
244  45 if (validationResults !=null){
245  45 for (ValidationResultInfo vr:validationResults){
246  0 if (vr.getErrorLevel() == ErrorLevel.ERROR){
247  0 return true;
248    }
249    }
250    }
251   
252  45 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  0 toggle 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