| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.krad.datadictionary.validation; |
| 17 | |
|
| 18 | |
import java.math.BigDecimal; |
| 19 | |
import java.text.ParseException; |
| 20 | |
import java.util.Collection; |
| 21 | |
import java.util.Date; |
| 22 | |
|
| 23 | |
import org.apache.commons.lang.StringUtils; |
| 24 | |
import org.kuali.rice.core.api.datetime.DateTimeService; |
| 25 | |
import org.kuali.rice.krad.datadictionary.exception.AttributeValidationException; |
| 26 | |
import org.kuali.rice.krad.util.ObjectUtils; |
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | 0 | public class ValidationUtils { |
| 34 | |
|
| 35 | |
public static String buildPath(String attributePath, String attributeName) { |
| 36 | 0 | if (StringUtils.isNotBlank(attributeName)) { |
| 37 | 0 | if (StringUtils.isNotBlank(attributePath)) |
| 38 | 0 | return new StringBuilder(attributePath).append(".").append(attributeName).toString(); |
| 39 | |
|
| 40 | 0 | return attributeName; |
| 41 | |
} |
| 42 | 0 | return attributePath; |
| 43 | |
} |
| 44 | |
|
| 45 | |
public static boolean compareValues(Object value1, Object value2, |
| 46 | |
DataType dataType, String operator, boolean isCaseSensitive, DateTimeService dateTimeService) { |
| 47 | |
|
| 48 | 0 | boolean result = false; |
| 49 | 0 | Integer compareResult = null; |
| 50 | |
|
| 51 | 0 | if("has_value".equalsIgnoreCase(operator)){ |
| 52 | 0 | if(value1==null){ |
| 53 | 0 | return "false".equals(value2.toString().toLowerCase()); |
| 54 | |
} |
| 55 | 0 | if(value1 instanceof Collection && ((Collection<?>) value1).isEmpty()){ |
| 56 | 0 | return "false".equals(value2.toString().toLowerCase()); |
| 57 | |
} |
| 58 | 0 | return "true".equals(value2.toString().toLowerCase()); |
| 59 | |
} |
| 60 | |
|
| 61 | 0 | if (null != dataType) { |
| 62 | 0 | if (DataType.STRING.equals(dataType)) { |
| 63 | 0 | String v1 = getString(value1); |
| 64 | 0 | String v2 = getString(value2); |
| 65 | |
|
| 66 | 0 | if(!isCaseSensitive) { |
| 67 | 0 | v1 = v1.toUpperCase(); |
| 68 | 0 | v2 = v2.toUpperCase(); |
| 69 | |
} |
| 70 | |
|
| 71 | 0 | compareResult = v1.compareTo(v2); |
| 72 | 0 | } else if (DataType.INTEGER.equals(dataType)) { |
| 73 | 0 | Integer v1 = getInteger(value1); |
| 74 | 0 | Integer v2 = getInteger(value2); |
| 75 | 0 | compareResult = v1.compareTo(v2); |
| 76 | 0 | } else if (DataType.LONG.equals(dataType)) { |
| 77 | 0 | Long v1 = getLong(value1); |
| 78 | 0 | Long v2 = getLong(value2); |
| 79 | 0 | compareResult = v1.compareTo(v2); |
| 80 | 0 | } else if (DataType.DOUBLE.equals(dataType)) { |
| 81 | 0 | Double v1 = getDouble(value1); |
| 82 | 0 | Double v2 = getDouble(value2); |
| 83 | 0 | compareResult = v1.compareTo(v2); |
| 84 | 0 | } else if (DataType.FLOAT.equals(dataType)) { |
| 85 | 0 | Float v1 = getFloat(value1); |
| 86 | 0 | Float v2 = getFloat(value2); |
| 87 | 0 | compareResult = v1.compareTo(v2); |
| 88 | 0 | } else if (DataType.BOOLEAN.equals(dataType)) { |
| 89 | 0 | Boolean v1 = getBoolean(value1); |
| 90 | 0 | Boolean v2 = getBoolean(value2); |
| 91 | 0 | compareResult = v1.compareTo(v2); |
| 92 | 0 | } else if (DataType.DATE.equals(dataType)) { |
| 93 | 0 | Date v1 = getDate(value1, dateTimeService); |
| 94 | 0 | Date v2 = getDate(value2, dateTimeService); |
| 95 | 0 | compareResult = v1.compareTo(v2); |
| 96 | |
} |
| 97 | |
} |
| 98 | |
|
| 99 | 0 | if (null != compareResult) { |
| 100 | 0 | if (("equals".equalsIgnoreCase(operator) |
| 101 | |
|| "greater_than_equal".equalsIgnoreCase(operator) || "less_than_equal" |
| 102 | |
.equalsIgnoreCase(operator)) |
| 103 | |
&& 0 == compareResult) { |
| 104 | 0 | result = true; |
| 105 | |
} |
| 106 | |
|
| 107 | 0 | if (("not_equal".equalsIgnoreCase (operator) |
| 108 | |
|| "greater_than".equalsIgnoreCase(operator)) && compareResult >= 1) { |
| 109 | 0 | result = true; |
| 110 | |
} |
| 111 | |
|
| 112 | 0 | if (("not_equal".equalsIgnoreCase (operator) |
| 113 | |
|| "less_than".equalsIgnoreCase(operator)) && compareResult <= -1) { |
| 114 | 0 | result = true; |
| 115 | |
} |
| 116 | |
} |
| 117 | |
|
| 118 | 0 | return result; |
| 119 | |
} |
| 120 | |
|
| 121 | |
public static Integer getInteger(Object o) { |
| 122 | 0 | Integer result = null; |
| 123 | 0 | if (o instanceof Integer) |
| 124 | 0 | return (Integer) o; |
| 125 | 0 | if (o == null) |
| 126 | 0 | return null; |
| 127 | 0 | if (o instanceof Number) |
| 128 | 0 | return ((Number) o).intValue(); |
| 129 | 0 | String s = o.toString(); |
| 130 | 0 | if (s != null && s.trim().length() > 0) { |
| 131 | 0 | result = Integer.valueOf(s.trim()); |
| 132 | |
} |
| 133 | 0 | return result; |
| 134 | |
} |
| 135 | |
|
| 136 | |
public static Long getLong(Object o) { |
| 137 | 0 | Long result = null; |
| 138 | 0 | if (o instanceof Long) |
| 139 | 0 | return (Long) o; |
| 140 | 0 | if (o == null) |
| 141 | 0 | return null; |
| 142 | 0 | if (o instanceof Number) |
| 143 | 0 | return ((Number) o).longValue(); |
| 144 | 0 | String s = o.toString(); |
| 145 | 0 | if (s != null && s.trim().length() > 0) { |
| 146 | 0 | result = Long.valueOf(s.trim()); |
| 147 | |
} |
| 148 | 0 | return result; |
| 149 | |
} |
| 150 | |
|
| 151 | |
public static Float getFloat(Object o) { |
| 152 | 0 | Float result = null; |
| 153 | 0 | if (o instanceof Float) |
| 154 | 0 | return (Float) o; |
| 155 | 0 | if (o == null) |
| 156 | 0 | return null; |
| 157 | 0 | if (o instanceof Number) |
| 158 | 0 | return ((Number) o).floatValue(); |
| 159 | 0 | String s = o.toString(); |
| 160 | 0 | if (s != null && s.trim().length() > 0) { |
| 161 | 0 | result = Float.valueOf(s.trim()); |
| 162 | |
} |
| 163 | 0 | return result; |
| 164 | |
} |
| 165 | |
|
| 166 | |
public static Double getDouble(Object o) { |
| 167 | 0 | Double result = null; |
| 168 | 0 | if (o instanceof BigDecimal) |
| 169 | 0 | return ((BigDecimal) o).doubleValue(); |
| 170 | 0 | if (o instanceof Double) |
| 171 | 0 | return (Double) o; |
| 172 | 0 | if (o == null) |
| 173 | 0 | return null; |
| 174 | 0 | if (o instanceof Number) |
| 175 | 0 | return ((Number) o).doubleValue(); |
| 176 | 0 | String s = o.toString(); |
| 177 | 0 | if (s != null && s.trim().length() > 0) { |
| 178 | 0 | result = Double.valueOf(s.trim()); |
| 179 | |
} |
| 180 | 0 | return result; |
| 181 | |
} |
| 182 | |
|
| 183 | |
public static Date getDate(Object o, DateTimeService dateTimeService) throws IllegalArgumentException { |
| 184 | 0 | Date result = null; |
| 185 | 0 | if (o instanceof Date) |
| 186 | 0 | return (Date) 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 | |
try { |
| 192 | 0 | result = dateTimeService.convertToDate(s.trim()); |
| 193 | 0 | } catch (ParseException e) { |
| 194 | 0 | throw new IllegalArgumentException(e); |
| 195 | 0 | } |
| 196 | |
} |
| 197 | 0 | return result; |
| 198 | |
} |
| 199 | |
|
| 200 | |
public static String getString(Object o) { |
| 201 | 0 | if (o instanceof String) |
| 202 | 0 | return (String) o; |
| 203 | 0 | if (o == null) |
| 204 | 0 | return null; |
| 205 | 0 | return o.toString(); |
| 206 | |
} |
| 207 | |
|
| 208 | |
public static Boolean getBoolean(Object o) { |
| 209 | 0 | Boolean result = null; |
| 210 | 0 | if (o instanceof Boolean) |
| 211 | 0 | return (Boolean) o; |
| 212 | 0 | if (o == null) |
| 213 | 0 | return null; |
| 214 | 0 | String s = o.toString(); |
| 215 | 0 | if (s != null && s.trim().length() > 0) { |
| 216 | 0 | result = Boolean.parseBoolean(s.trim()); |
| 217 | |
} |
| 218 | 0 | return result; |
| 219 | |
} |
| 220 | |
|
| 221 | |
|
| 222 | |
public static boolean hasText(String string) { |
| 223 | |
|
| 224 | 0 | if (string == null || string.length() < 1) { |
| 225 | 0 | return false; |
| 226 | |
} |
| 227 | 0 | int stringLength = string.length(); |
| 228 | |
|
| 229 | 0 | for (int i = 0; i < stringLength; i++) { |
| 230 | 0 | char currentChar = string.charAt(i); |
| 231 | 0 | if (' ' != currentChar || '\t' != currentChar || '\n' != currentChar) { |
| 232 | 0 | return true; |
| 233 | |
} |
| 234 | |
} |
| 235 | |
|
| 236 | 0 | return false; |
| 237 | |
} |
| 238 | |
|
| 239 | |
public static boolean isNullOrEmpty(Object value) { |
| 240 | 0 | return ObjectUtils.isNull(value) || (value instanceof String && StringUtils.isBlank(((String) value).trim())); |
| 241 | |
} |
| 242 | |
|
| 243 | |
|
| 244 | 0 | public static enum Result { VALID, INVALID, UNDEFINED }; |
| 245 | |
|
| 246 | |
public static Object convertToDataType(Object value, DataType dataType, DateTimeService dateTimeService) throws AttributeValidationException { |
| 247 | 0 | Object returnValue = value; |
| 248 | |
|
| 249 | 0 | if (null == value) |
| 250 | 0 | return null; |
| 251 | |
|
| 252 | 0 | switch (dataType) { |
| 253 | |
case BOOLEAN: |
| 254 | 0 | if (! (value instanceof Boolean)) { |
| 255 | 0 | returnValue = Boolean.valueOf(value.toString()); |
| 256 | |
|
| 257 | |
|
| 258 | 0 | if (!value.toString().equalsIgnoreCase("TRUE") && !value.toString().equalsIgnoreCase("FALSE")) |
| 259 | 0 | throw new AttributeValidationException("Value " + value.toString() + " is not a boolean!"); |
| 260 | |
} |
| 261 | |
break; |
| 262 | |
case INTEGER: |
| 263 | 0 | if (! (value instanceof Number)) { |
| 264 | 0 | returnValue = Integer.valueOf(value.toString()); |
| 265 | |
} |
| 266 | |
break; |
| 267 | |
case LONG: |
| 268 | 0 | if (! (value instanceof Number)) { |
| 269 | 0 | returnValue = Long.valueOf(value.toString()); |
| 270 | |
} |
| 271 | |
break; |
| 272 | |
case DOUBLE: |
| 273 | 0 | if (! (value instanceof Number)) { |
| 274 | 0 | returnValue = Double.valueOf(value.toString()); |
| 275 | |
} |
| 276 | 0 | if (((Double)returnValue).isNaN()) |
| 277 | 0 | throw new AttributeValidationException("Infinite Double values are not valid!"); |
| 278 | 0 | if (((Double)returnValue).isInfinite()) |
| 279 | 0 | throw new AttributeValidationException("Infinite Double values are not valid!"); |
| 280 | |
break; |
| 281 | |
case FLOAT: |
| 282 | 0 | if (! (value instanceof Number)) { |
| 283 | 0 | returnValue = Float.valueOf(value.toString()); |
| 284 | |
} |
| 285 | 0 | if (((Float)returnValue).isNaN()) |
| 286 | 0 | throw new AttributeValidationException("NaN Float values are not valid!"); |
| 287 | 0 | if (((Float)returnValue).isInfinite()) |
| 288 | 0 | throw new AttributeValidationException("Infinite Float values are not valid!"); |
| 289 | |
break; |
| 290 | |
case TRUNCATED_DATE: |
| 291 | |
case DATE: |
| 292 | 0 | if (! (value instanceof Date)) { |
| 293 | |
try { |
| 294 | 0 | returnValue = dateTimeService.convertToDate(value.toString()); |
| 295 | 0 | } catch (ParseException pe) { |
| 296 | 0 | throw new AttributeValidationException("Value " + value.toString() + " is not a date!"); |
| 297 | 0 | } |
| 298 | |
} |
| 299 | |
break; |
| 300 | |
case STRING: |
| 301 | |
case COMPLEX: |
| 302 | |
break; |
| 303 | |
} |
| 304 | |
|
| 305 | 0 | return returnValue; |
| 306 | |
} |
| 307 | |
|
| 308 | |
public static <T> Result isGreaterThan(T value, Comparable<T> limit) { |
| 309 | 0 | return limit == null ? Result.UNDEFINED : ( limit.compareTo(value) < 0 ? Result.VALID : Result.INVALID ); |
| 310 | |
} |
| 311 | |
|
| 312 | |
public static <T> Result isGreaterThanOrEqual(T value, Comparable<T> limit) { |
| 313 | 0 | return limit == null ? Result.UNDEFINED : ( limit.compareTo(value) <= 0 ? Result.VALID : Result.INVALID ); |
| 314 | |
} |
| 315 | |
|
| 316 | |
public static <T> Result isLessThan(T value, Comparable<T> limit) { |
| 317 | 0 | return limit == null ? Result.UNDEFINED : ( limit.compareTo(value) > 0 ? Result.VALID : Result.INVALID ); |
| 318 | |
} |
| 319 | |
|
| 320 | |
public static <T> Result isLessThanOrEqual(T value, Comparable<T> limit) { |
| 321 | 0 | return limit == null ? Result.UNDEFINED : ( limit.compareTo(value) >= 0 ? Result.VALID : Result.INVALID ); |
| 322 | |
} |
| 323 | |
|
| 324 | |
|
| 325 | |
public static String[] getPathTokens(String fieldPath) { |
| 326 | 0 | return (fieldPath != null && fieldPath.contains(".") ? fieldPath.split("\\.") : new String[]{fieldPath}); |
| 327 | |
} |
| 328 | |
|
| 329 | |
} |
| 330 | |
|