View Javadoc

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