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