|  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 org.kuali.rice.core.util.RiceKeyConstants;  | 
  |  19 |     | 
   import org.kuali.rice.kns.datadictionary.exception.AttributeValidationException;  | 
  |  20 |     | 
   import org.kuali.rice.kns.datadictionary.validation.AttributeValueReader;  | 
  |  21 |     | 
   import org.kuali.rice.kns.datadictionary.validation.DataType;  | 
  |  22 |     | 
   import org.kuali.rice.kns.datadictionary.validation.ValidationUtils;  | 
  |  23 |     | 
   import org.kuali.rice.kns.datadictionary.validation.ValidationUtils.Result;  | 
  |  24 |     | 
   import org.kuali.rice.kns.datadictionary.validation.constraint.Constraint;  | 
  |  25 |     | 
   import org.kuali.rice.kns.datadictionary.validation.constraint.LengthConstraint;  | 
  |  26 |     | 
   import org.kuali.rice.kns.datadictionary.validation.result.ConstraintValidationResult;  | 
  |  27 |     | 
   import org.kuali.rice.kns.datadictionary.validation.result.DictionaryValidationResult;  | 
  |  28 |     | 
   import org.kuali.rice.kns.datadictionary.validation.result.ProcessorResult;  | 
  |  29 |     | 
     | 
  |  30 |     | 
     | 
  |  31 |     | 
     | 
  |  32 |     | 
     | 
  |  33 |     | 
     | 
  |  34 |    0 |    public class LengthConstraintProcessor extends MandatoryElementConstraintProcessor<LengthConstraint> { | 
  |  35 |     | 
     | 
  |  36 |     | 
           private static final String CONSTRAINT_NAME = "length constraint";  | 
  |  37 |     | 
             | 
  |  38 |     | 
             | 
  |  39 |     | 
     | 
  |  40 |     | 
     | 
  |  41 |     | 
           @Override  | 
  |  42 |     | 
           public ProcessorResult process(DictionaryValidationResult result, Object value, LengthConstraint constraint, AttributeValueReader attributeValueReader) throws AttributeValidationException { | 
  |  43 |     | 
     | 
  |  44 |     | 
                     | 
  |  45 |     | 
                     | 
  |  46 |     | 
                     | 
  |  47 |    0 |                    return new ProcessorResult(processSingleLengthConstraint(result, value, constraint, attributeValueReader));  | 
  |  48 |     | 
           }  | 
  |  49 |     | 
     | 
  |  50 |     | 
           @Override   | 
  |  51 |     | 
           public String getName() { | 
  |  52 |    0 |                    return CONSTRAINT_NAME;  | 
  |  53 |     | 
           }  | 
  |  54 |     | 
             | 
  |  55 |     | 
             | 
  |  56 |     | 
     | 
  |  57 |     | 
     | 
  |  58 |     | 
           @Override  | 
  |  59 |     | 
           public Class<? extends Constraint> getConstraintType() { | 
  |  60 |    0 |                    return LengthConstraint.class;  | 
  |  61 |     | 
           }  | 
  |  62 |     | 
             | 
  |  63 |     | 
           protected ConstraintValidationResult processSingleLengthConstraint(DictionaryValidationResult result, Object value, LengthConstraint constraint, AttributeValueReader attributeValueReader) throws AttributeValidationException { | 
  |  64 |     | 
                     | 
  |  65 |    0 |                    if (ValidationUtils.isNullOrEmpty(value))  | 
  |  66 |    0 |                            return result.addSkipped(attributeValueReader, CONSTRAINT_NAME);  | 
  |  67 |     | 
     | 
  |  68 |    0 |                    DataType dataType = constraint.getDataType();  | 
  |  69 |    0 |                    Object typedValue = value;  | 
  |  70 |     | 
     | 
  |  71 |    0 |                    if (dataType != null) { | 
  |  72 |    0 |                            typedValue = ValidationUtils.convertToDataType(value, dataType, dateTimeService);  | 
  |  73 |     | 
                   }          | 
  |  74 |     | 
     | 
  |  75 |     | 
                     | 
  |  76 |    0 |                    if (typedValue instanceof String) { | 
  |  77 |    0 |                            return validateLength(result, (String)typedValue, constraint, attributeValueReader);  | 
  |  78 |     | 
                   }   | 
  |  79 |     | 
                     | 
  |  80 |    0 |                    return result.addSkipped(attributeValueReader, CONSTRAINT_NAME);  | 
  |  81 |     | 
           }  | 
  |  82 |     | 
             | 
  |  83 |     | 
             | 
  |  84 |     | 
           protected ConstraintValidationResult validateLength(DictionaryValidationResult result, String value, LengthConstraint constraint, AttributeValueReader attributeValueReader) throws IllegalArgumentException { | 
  |  85 |    0 |                    Integer valueLength = Integer.valueOf(value.length());  | 
  |  86 |     | 
                     | 
  |  87 |    0 |                    Integer maxLength = constraint.getMaxLength();  | 
  |  88 |    0 |                    Integer minLength = constraint.getMinLength();  | 
  |  89 |     | 
                     | 
  |  90 |    0 |                    Result lessThanMax = ValidationUtils.isLessThanOrEqual(valueLength, maxLength);  | 
  |  91 |    0 |                    Result greaterThanMin = ValidationUtils.isGreaterThanOrEqual(valueLength, minLength);  | 
  |  92 |     | 
                     | 
  |  93 |     | 
             | 
  |  94 |    0 |            if (lessThanMax != Result.INVALID && greaterThanMin != Result.INVALID) {  | 
  |  95 |     | 
                     | 
  |  96 |    0 |                    if (lessThanMax == Result.UNDEFINED && greaterThanMin == Result.UNDEFINED)  | 
  |  97 |    0 |                            return result.addNoConstraint(attributeValueReader, CONSTRAINT_NAME);  | 
  |  98 |     | 
                     | 
  |  99 |     | 
                     | 
  |  100 |    0 |                    return result.addSuccess(attributeValueReader, CONSTRAINT_NAME);  | 
  |  101 |     | 
           }  | 
  |  102 |     | 
             | 
  |  103 |    0 |                    String maxErrorParameter = maxLength != null ? maxLength.toString() : null;  | 
  |  104 |    0 |                    String minErrorParameter = minLength != null ? minLength.toString() : null;  | 
  |  105 |     | 
             | 
  |  106 |     | 
             | 
  |  107 |    0 |            if (lessThanMax != Result.UNDEFINED && greaterThanMin != Result.UNDEFINED)   | 
  |  108 |    0 |                    return result.addError(attributeValueReader, CONSTRAINT_NAME, RiceKeyConstants.ERROR_OUT_OF_RANGE, minErrorParameter, maxErrorParameter);  | 
  |  109 |     | 
             | 
  |  110 |    0 |            else if (lessThanMax == Result.INVALID)  | 
  |  111 |    0 |                    return result.addError(attributeValueReader, CONSTRAINT_NAME, RiceKeyConstants.ERROR_INCLUSIVE_MAX, maxErrorParameter);  | 
  |  112 |     | 
             | 
  |  113 |     | 
           else   | 
  |  114 |    0 |                    return result.addError(attributeValueReader, CONSTRAINT_NAME, RiceKeyConstants.ERROR_EXCLUSIVE_MIN, minErrorParameter);  | 
  |  115 |     | 
             | 
  |  116 |     | 
           }  | 
  |  117 |     | 
     | 
  |  118 |     | 
   }  |