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