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