| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ValidatorUtils |
|
| 8.5;8.5 |
| 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.Date; | |
| 19 | ||
| 20 | import org.kuali.student.core.dictionary.dto.DataType; | |
| 21 | import org.kuali.student.core.dictionary.dto.FieldDefinition; | |
| 22 | import org.kuali.student.core.dictionary.dto.ObjectStructureDefinition; | |
| 23 | ||
| 24 | 0 | public class ValidatorUtils { |
| 25 | ||
| 26 | public static boolean compareValues(Object value1, Object value2, | |
| 27 | DataType dataType, String operator, boolean isCaseSensitive, DateParser dateParser) { | |
| 28 | ||
| 29 | 9 | boolean result = false; |
| 30 | 9 | Integer compareResult = null; |
| 31 | ||
| 32 | // Convert objects into appropriate data types | |
| 33 | 9 | if (null != dataType) { |
| 34 | 0 | if (DataType.STRING.equals(dataType)) { |
| 35 | 0 | String v1 = getString(value1); |
| 36 | 0 | String v2 = getString(value2); |
| 37 | ||
| 38 | 0 | if(!isCaseSensitive) { |
| 39 | 0 | v1 = v1.toUpperCase(); |
| 40 | 0 | v2 = v2.toUpperCase(); |
| 41 | } | |
| 42 | ||
| 43 | 0 | compareResult = v1.compareTo(v2); |
| 44 | 0 | } else if (DataType.INTEGER.equals(dataType)) { |
| 45 | 0 | Integer v1 = getInteger(value1); |
| 46 | 0 | Integer v2 = getInteger(value2); |
| 47 | 0 | compareResult = v1.compareTo(v2); |
| 48 | 0 | } else if (DataType.LONG.equals(dataType)) { |
| 49 | 0 | Long v1 = getLong(value1); |
| 50 | 0 | Long v2 = getLong(value2); |
| 51 | 0 | compareResult = v1.compareTo(v2); |
| 52 | 0 | } else if (DataType.DOUBLE.equals(dataType)) { |
| 53 | 0 | Double v1 = getDouble(value1); |
| 54 | 0 | Double v2 = getDouble(value2); |
| 55 | 0 | compareResult = v1.compareTo(v2); |
| 56 | 0 | } else if (DataType.FLOAT.equals(dataType)) { |
| 57 | 0 | Float v1 = getFloat(value1); |
| 58 | 0 | Float v2 = getFloat(value2); |
| 59 | 0 | compareResult = v1.compareTo(v2); |
| 60 | 0 | } else if (DataType.BOOLEAN.equals(dataType)) { |
| 61 | 0 | Boolean v1 = getBoolean(value1); |
| 62 | 0 | Boolean v2 = getBoolean(value2); |
| 63 | 0 | compareResult = v1.compareTo(v2); |
| 64 | 0 | } else if (DataType.DATE.equals(dataType)) { |
| 65 | 0 | Date v1 = getDate(value1, dateParser); |
| 66 | 0 | Date v2 = getDate(value2, dateParser); |
| 67 | 0 | compareResult = v1.compareTo(v2); |
| 68 | } | |
| 69 | } | |
| 70 | ||
| 71 | 9 | if (null != compareResult) { |
| 72 | 0 | if (("equals".equalsIgnoreCase(operator) |
| 73 | || "greater_than_equal".equalsIgnoreCase(operator) || "less_than_equal" | |
| 74 | .equalsIgnoreCase(operator)) | |
| 75 | && 0 == compareResult) { | |
| 76 | 0 | result = true; |
| 77 | } | |
| 78 | ||
| 79 | 0 | if (("not_equal".equalsIgnoreCase (operator) |
| 80 | || "greater_than".equalsIgnoreCase(operator)) && compareResult >= 1) { | |
| 81 | 0 | result = true; |
| 82 | } | |
| 83 | ||
| 84 | 0 | if (("not_equal".equalsIgnoreCase (operator) |
| 85 | || "less_than".equalsIgnoreCase(operator)) && compareResult <= -1) { | |
| 86 | 0 | result = true; |
| 87 | } | |
| 88 | } | |
| 89 | ||
| 90 | 9 | return result; |
| 91 | } | |
| 92 | ||
| 93 | public static Integer getInteger(Object o) { | |
| 94 | 0 | Integer result = null; |
| 95 | 0 | if (o instanceof Integer) |
| 96 | 0 | return (Integer) o; |
| 97 | 0 | if (o == null) |
| 98 | 0 | return null; |
| 99 | 0 | if (o instanceof Number) |
| 100 | 0 | return ((Number) o).intValue(); |
| 101 | 0 | String s = o.toString(); |
| 102 | 0 | if (s != null && s.trim().length() > 0) { |
| 103 | 0 | result = Integer.valueOf(s.trim()); |
| 104 | } | |
| 105 | 0 | return result; |
| 106 | } | |
| 107 | ||
| 108 | public static Long getLong(Object o) { | |
| 109 | 0 | Long result = null; |
| 110 | 0 | if (o instanceof Long) |
| 111 | 0 | return (Long) o; |
| 112 | 0 | if (o == null) |
| 113 | 0 | return null; |
| 114 | 0 | if (o instanceof Number) |
| 115 | 0 | return ((Number) o).longValue(); |
| 116 | 0 | String s = o.toString(); |
| 117 | 0 | if (s != null && s.trim().length() > 0) { |
| 118 | 0 | result = Long.valueOf(s.trim()); |
| 119 | } | |
| 120 | 0 | return result; |
| 121 | } | |
| 122 | ||
| 123 | public static Float getFloat(Object o) { | |
| 124 | 0 | Float result = null; |
| 125 | 0 | if (o instanceof Float) |
| 126 | 0 | return (Float) o; |
| 127 | 0 | if (o == null) |
| 128 | 0 | return null; |
| 129 | 0 | if (o instanceof Number) |
| 130 | 0 | return ((Number) o).floatValue(); |
| 131 | 0 | String s = o.toString(); |
| 132 | 0 | if (s != null && s.trim().length() > 0) { |
| 133 | 0 | result = Float.valueOf(s.trim()); |
| 134 | } | |
| 135 | 0 | return result; |
| 136 | } | |
| 137 | ||
| 138 | public static Double getDouble(Object o) { | |
| 139 | 2 | Double result = null; |
| 140 | 2 | if (o instanceof Double) |
| 141 | 0 | return (Double) o; |
| 142 | 2 | if (o == null) |
| 143 | 0 | return null; |
| 144 | 2 | if (o instanceof Number) |
| 145 | 0 | return ((Number) o).doubleValue(); |
| 146 | 2 | String s = o.toString(); |
| 147 | 2 | if (s != null && s.trim().length() > 0) { |
| 148 | 2 | result = Double.valueOf(s.trim()); |
| 149 | } | |
| 150 | 2 | return result; |
| 151 | } | |
| 152 | ||
| 153 | public static Date getDate(Object o, DateParser dateParser) { | |
| 154 | 10 | Date result = null; |
| 155 | 10 | if (o instanceof Date) |
| 156 | 0 | return (Date) o; |
| 157 | 10 | if (o == null) |
| 158 | 5 | return null; |
| 159 | 5 | String s = o.toString(); |
| 160 | 5 | if (s != null && s.trim().length() > 0) { |
| 161 | 5 | result = dateParser.parseDate(s.trim()); |
| 162 | } | |
| 163 | 5 | return result; |
| 164 | } | |
| 165 | ||
| 166 | public static String getString(Object o) { | |
| 167 | 15 | if (o instanceof String) |
| 168 | 15 | return (String) o; |
| 169 | 0 | if (o == null) |
| 170 | 0 | return null; |
| 171 | 0 | return o.toString(); |
| 172 | } | |
| 173 | ||
| 174 | public static Boolean getBoolean(Object o) { | |
| 175 | 0 | Boolean result = null; |
| 176 | 0 | if (o instanceof Boolean) |
| 177 | 0 | return (Boolean) o; |
| 178 | 0 | if (o == null) |
| 179 | 0 | return null; |
| 180 | 0 | String s = o.toString(); |
| 181 | 0 | if (s != null && s.trim().length() > 0) { |
| 182 | 0 | result = Boolean.parseBoolean(s.trim()); |
| 183 | } | |
| 184 | 0 | return result; |
| 185 | } | |
| 186 | ||
| 187 | /** | |
| 188 | * Traverses the dictionary ObjectStructure to find the field with the match | |
| 189 | * key, type and state | |
| 190 | * The key has to relative to the current object structure that is being traversed. | |
| 191 | * example: current object structure is ActivityInfo and if we want to lookup | |
| 192 | * the academicSubjectorgId, then <property name="fieldPath" value="academicSubjectOrgs.orgId"/> | |
| 193 | * The current object structure starts from the field on which the constraint is applied on. | |
| 194 | * If we want to address fields outside of this object structure we ll need to pass in the | |
| 195 | * dictionary context. | |
| 196 | * @param key | |
| 197 | * @param type | |
| 198 | * @param state | |
| 199 | * @param objStructure | |
| 200 | * @return | |
| 201 | */ | |
| 202 | public static FieldDefinition getField(String key, ObjectStructureDefinition objStructure) { | |
| 203 | 13 | String[] lookupPathTokens = getPathTokens(key); |
| 204 | 26 | for(int i = 0; i < lookupPathTokens.length; i++) { |
| 205 | 13 | for (FieldDefinition f : objStructure.getAttributes()) { |
| 206 | 58 | if (f.getName().equals(lookupPathTokens[i])) { |
| 207 | 0 | if(i==lookupPathTokens.length-1){ |
| 208 | 0 | return f; |
| 209 | } | |
| 210 | else{ | |
| 211 | 0 | objStructure = f.getDataObjectStructure(); |
| 212 | 0 | break; |
| 213 | } | |
| 214 | ||
| 215 | } | |
| 216 | } | |
| 217 | } | |
| 218 | 13 | return null; |
| 219 | } | |
| 220 | ||
| 221 | private static String[] getPathTokens(String fieldPath) { | |
| 222 | 13 | return (fieldPath != null && fieldPath.contains(".") ? fieldPath.split("\\.") : new String[]{fieldPath}); |
| 223 | } | |
| 224 | ||
| 225 | } | |
| 226 |