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