| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.krad.datadictionary.validation.processor; |
| 17 | |
|
| 18 | |
import org.kuali.rice.core.api.uif.DataType; |
| 19 | |
import org.kuali.rice.core.api.util.RiceKeyConstants; |
| 20 | |
import org.kuali.rice.krad.datadictionary.exception.AttributeValidationException; |
| 21 | |
import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader; |
| 22 | |
import org.kuali.rice.krad.datadictionary.validation.ValidationUtils; |
| 23 | |
import org.kuali.rice.krad.datadictionary.validation.ValidationUtils.Result; |
| 24 | |
import org.kuali.rice.krad.datadictionary.validation.constraint.Constraint; |
| 25 | |
import org.kuali.rice.krad.datadictionary.validation.constraint.LengthConstraint; |
| 26 | |
import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult; |
| 27 | |
import org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult; |
| 28 | |
import org.kuali.rice.krad.datadictionary.validation.result.ProcessorResult; |
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | 12 | 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 | 6 | return new ProcessorResult(processSingleLengthConstraint(result, value, constraint, attributeValueReader)); |
| 48 | |
} |
| 49 | |
|
| 50 | |
@Override |
| 51 | |
public String getName() { |
| 52 | 6 | 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 | 6 | if (ValidationUtils.isNullOrEmpty(value)) |
| 66 | 0 | return result.addSkipped(attributeValueReader, CONSTRAINT_NAME); |
| 67 | |
|
| 68 | 6 | DataType dataType = constraint.getDataType(); |
| 69 | 6 | Object typedValue = value; |
| 70 | |
|
| 71 | 6 | if (dataType != null) { |
| 72 | 6 | typedValue = ValidationUtils.convertToDataType(value, dataType, dateTimeService); |
| 73 | |
} |
| 74 | |
|
| 75 | |
|
| 76 | 6 | if (typedValue instanceof String) { |
| 77 | 6 | 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 | 6 | Integer valueLength = Integer.valueOf(value.length()); |
| 86 | |
|
| 87 | 6 | Integer maxLength = constraint.getMaxLength(); |
| 88 | 6 | Integer minLength = constraint.getMinLength(); |
| 89 | |
|
| 90 | 6 | Result lessThanMax = ValidationUtils.isLessThanOrEqual(valueLength, maxLength); |
| 91 | 6 | Result greaterThanMin = ValidationUtils.isGreaterThanOrEqual(valueLength, minLength); |
| 92 | |
|
| 93 | |
|
| 94 | 6 | if (lessThanMax != Result.INVALID && greaterThanMin != Result.INVALID) { |
| 95 | |
|
| 96 | 4 | if (lessThanMax == Result.UNDEFINED && greaterThanMin == Result.UNDEFINED) |
| 97 | 1 | return result.addNoConstraint(attributeValueReader, CONSTRAINT_NAME); |
| 98 | |
|
| 99 | |
|
| 100 | 3 | return result.addSuccess(attributeValueReader, CONSTRAINT_NAME); |
| 101 | |
} |
| 102 | |
|
| 103 | 2 | String maxErrorParameter = maxLength != null ? maxLength.toString() : null; |
| 104 | 2 | String minErrorParameter = minLength != null ? minLength.toString() : null; |
| 105 | |
|
| 106 | |
|
| 107 | 2 | if (lessThanMax != Result.UNDEFINED && greaterThanMin != Result.UNDEFINED) |
| 108 | 2 | 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 | |
} |