|  1 |     | 
     | 
  |  2 |     | 
     | 
  |  3 |     | 
     | 
  |  4 |     | 
     | 
  |  5 |     | 
     | 
  |  6 |     | 
     | 
  |  7 |     | 
     | 
  |  8 |     | 
     | 
  |  9 |     | 
     | 
  |  10 |     | 
     | 
  |  11 |     | 
     | 
  |  12 |     | 
     | 
  |  13 |     | 
     | 
  |  14 |     | 
     | 
  |  15 |     | 
     | 
  |  16 |     | 
   package org.kuali.rice.kns.datadictionary.validation.processor;  | 
  |  17 |     | 
     | 
  |  18 |     | 
   import java.math.BigDecimal;  | 
  |  19 |     | 
   import java.util.Date;  | 
  |  20 |     | 
     | 
  |  21 |     | 
   import org.kuali.rice.core.util.RiceKeyConstants;  | 
  |  22 |     | 
   import org.kuali.rice.kns.datadictionary.exception.AttributeValidationException;  | 
  |  23 |     | 
   import org.kuali.rice.kns.datadictionary.validation.AttributeValueReader;  | 
  |  24 |     | 
   import org.kuali.rice.kns.datadictionary.validation.DataType;  | 
  |  25 |     | 
   import org.kuali.rice.kns.datadictionary.validation.ValidationUtils;  | 
  |  26 |     | 
   import org.kuali.rice.kns.datadictionary.validation.ValidationUtils.Result;  | 
  |  27 |     | 
   import org.kuali.rice.kns.datadictionary.validation.capability.RangeConstrainable;  | 
  |  28 |     | 
   import org.kuali.rice.kns.datadictionary.validation.constraint.Constraint;  | 
  |  29 |     | 
   import org.kuali.rice.kns.datadictionary.validation.constraint.RangeConstraint;  | 
  |  30 |     | 
   import org.kuali.rice.kns.datadictionary.validation.result.ConstraintValidationResult;  | 
  |  31 |     | 
   import org.kuali.rice.kns.datadictionary.validation.result.DictionaryValidationResult;  | 
  |  32 |     | 
   import org.kuali.rice.kns.datadictionary.validation.result.ProcessorResult;  | 
  |  33 |     | 
     | 
  |  34 |     | 
     | 
  |  35 |     | 
     | 
  |  36 |     | 
     | 
  |  37 |     | 
     | 
  |  38 |     | 
     | 
  |  39 |     | 
     | 
  |  40 |     | 
     | 
  |  41 |    0 |    public class RangeConstraintProcessor extends MandatoryElementConstraintProcessor<RangeConstraint> { | 
  |  42 |     | 
     | 
  |  43 |     | 
           private static final String CONSTRAINT_NAME = "range constraint";  | 
  |  44 |     | 
     | 
  |  45 |     | 
             | 
  |  46 |     | 
     | 
  |  47 |     | 
     | 
  |  48 |     | 
           @Override  | 
  |  49 |     | 
           public ProcessorResult process(DictionaryValidationResult result, Object value, RangeConstraint constraint, AttributeValueReader attributeValueReader) throws AttributeValidationException { | 
  |  50 |     | 
                     | 
  |  51 |     | 
                     | 
  |  52 |    0 |                    return new ProcessorResult(processSingleRangeConstraint(result, value, constraint, attributeValueReader));  | 
  |  53 |     | 
           }  | 
  |  54 |     | 
             | 
  |  55 |     | 
           @Override   | 
  |  56 |     | 
           public String getName() { | 
  |  57 |    0 |                    return CONSTRAINT_NAME;  | 
  |  58 |     | 
           }  | 
  |  59 |     | 
     | 
  |  60 |     | 
             | 
  |  61 |     | 
     | 
  |  62 |     | 
     | 
  |  63 |     | 
           @Override  | 
  |  64 |     | 
           public Class<? extends Constraint> getConstraintType() { | 
  |  65 |    0 |                    return RangeConstraint.class;  | 
  |  66 |     | 
           }  | 
  |  67 |     | 
             | 
  |  68 |     | 
           protected ConstraintValidationResult processSingleRangeConstraint(DictionaryValidationResult result, Object value, RangeConstraint constraint, AttributeValueReader attributeValueReader) throws AttributeValidationException { | 
  |  69 |     | 
                     | 
  |  70 |    0 |                    if (ValidationUtils.isNullOrEmpty(value))  | 
  |  71 |    0 |                            return result.addSkipped(attributeValueReader, CONSTRAINT_NAME);  | 
  |  72 |     | 
                     | 
  |  73 |     | 
             | 
  |  74 |     | 
                     | 
  |  75 |    0 |                    DataType dataType = constraint.getDataType();  | 
  |  76 |    0 |                    Object typedValue = value;  | 
  |  77 |     | 
     | 
  |  78 |    0 |                    if (dataType != null) { | 
  |  79 |    0 |                            typedValue = ValidationUtils.convertToDataType(value, dataType, dateTimeService);  | 
  |  80 |     | 
                   }          | 
  |  81 |     | 
     | 
  |  82 |     | 
                     | 
  |  83 |    0 |                    if (typedValue instanceof Date)  | 
  |  84 |    0 |                            return validateRange(result, (Date)typedValue, constraint, attributeValueReader);  | 
  |  85 |    0 |                    else if (typedValue instanceof Number)  | 
  |  86 |    0 |                            return validateRange(result, (Number)typedValue, constraint, attributeValueReader);  | 
  |  87 |     | 
                     | 
  |  88 |    0 |                    return result.addSkipped(attributeValueReader, CONSTRAINT_NAME);  | 
  |  89 |     | 
           }  | 
  |  90 |     | 
             | 
  |  91 |     | 
           protected ConstraintValidationResult validateRange(DictionaryValidationResult result, Date value, RangeConstraint constraint, AttributeValueReader attributeValueReader) throws IllegalArgumentException {         | 
  |  92 |     | 
     | 
  |  93 |    0 |                    Date date = value != null ? ValidationUtils.getDate(value, dateTimeService) : null;  | 
  |  94 |     | 
     | 
  |  95 |    0 |            String inclusiveMaxText = constraint.getInclusiveMax();  | 
  |  96 |    0 |            String exclusiveMinText = constraint.getExclusiveMin();  | 
  |  97 |     | 
     | 
  |  98 |    0 |            Date inclusiveMax = inclusiveMaxText != null ? ValidationUtils.getDate(inclusiveMaxText, dateTimeService) : null;  | 
  |  99 |    0 |            Date exclusiveMin = exclusiveMinText != null ? ValidationUtils.getDate(exclusiveMinText, dateTimeService) : null;  | 
  |  100 |     | 
             | 
  |  101 |    0 |                    return isInRange(result, date, inclusiveMax, inclusiveMaxText, exclusiveMin, exclusiveMinText, attributeValueReader);  | 
  |  102 |     | 
           }  | 
  |  103 |     | 
             | 
  |  104 |     | 
           protected ConstraintValidationResult validateRange(DictionaryValidationResult result, Number value, RangeConstraint constraint, AttributeValueReader attributeValueReader) throws IllegalArgumentException { | 
  |  105 |     | 
     | 
  |  106 |     | 
                     | 
  |  107 |     | 
             | 
  |  108 |    0 |            BigDecimal number = value != null ? new BigDecimal(value.toString()) : null;  | 
  |  109 |     | 
     | 
  |  110 |    0 |            String inclusiveMaxText = constraint.getInclusiveMax();  | 
  |  111 |    0 |            String exclusiveMinText = constraint.getExclusiveMin();  | 
  |  112 |     | 
             | 
  |  113 |    0 |            BigDecimal inclusiveMax = inclusiveMaxText != null ? new BigDecimal(inclusiveMaxText) : null;  | 
  |  114 |    0 |            BigDecimal exclusiveMin = exclusiveMinText != null ? new BigDecimal(exclusiveMinText) : null;  | 
  |  115 |     | 
             | 
  |  116 |    0 |                    return isInRange(result, number, inclusiveMax, inclusiveMaxText, exclusiveMin, exclusiveMinText, attributeValueReader);  | 
  |  117 |     | 
           }  | 
  |  118 |     | 
     | 
  |  119 |     | 
           private <T> ConstraintValidationResult isInRange(DictionaryValidationResult result, T value, Comparable<T> inclusiveMax, String inclusiveMaxText, Comparable<T> exclusiveMin, String exclusiveMinText, AttributeValueReader attributeValueReader) { | 
  |  120 |     | 
             | 
  |  121 |    0 |            Result lessThanMax = ValidationUtils.isLessThanOrEqual(value, inclusiveMax);   | 
  |  122 |     | 
             | 
  |  123 |    0 |            Result greaterThanMin = ValidationUtils.isGreaterThan(value, exclusiveMin);   | 
  |  124 |     | 
               | 
  |  125 |     | 
             | 
  |  126 |    0 |            if (lessThanMax != Result.INVALID && greaterThanMin != Result.INVALID) {  | 
  |  127 |     | 
                     | 
  |  128 |    0 |                    if (lessThanMax == Result.UNDEFINED && greaterThanMin == Result.UNDEFINED)  | 
  |  129 |    0 |                            return result.addNoConstraint(attributeValueReader, CONSTRAINT_NAME);  | 
  |  130 |     | 
                     | 
  |  131 |     | 
                     | 
  |  132 |    0 |                    return result.addSuccess(attributeValueReader, CONSTRAINT_NAME);  | 
  |  133 |     | 
           }  | 
  |  134 |     | 
             | 
  |  135 |     | 
             | 
  |  136 |    0 |            if (lessThanMax != Result.UNDEFINED && greaterThanMin != Result.UNDEFINED)   | 
  |  137 |    0 |                    return result.addError(attributeValueReader, CONSTRAINT_NAME, RiceKeyConstants.ERROR_OUT_OF_RANGE, exclusiveMinText, inclusiveMaxText);  | 
  |  138 |     | 
             | 
  |  139 |    0 |            else if (lessThanMax == Result.INVALID)  | 
  |  140 |    0 |                    return result.addError(attributeValueReader, CONSTRAINT_NAME, RiceKeyConstants.ERROR_INCLUSIVE_MAX, inclusiveMaxText);  | 
  |  141 |     | 
             | 
  |  142 |     | 
           else   | 
  |  143 |    0 |                    return result.addError(attributeValueReader, CONSTRAINT_NAME, RiceKeyConstants.ERROR_EXCLUSIVE_MIN, exclusiveMinText);  | 
  |  144 |     | 
           }  | 
  |  145 |     | 
             | 
  |  146 |     | 
   }  |