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.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.ValidationUtils; |
22 | |
import org.kuali.rice.krad.datadictionary.validation.ValidationUtils.Result; |
23 | |
import org.kuali.rice.krad.datadictionary.validation.constraint.CollectionSizeConstraint; |
24 | |
import org.kuali.rice.krad.datadictionary.validation.constraint.Constraint; |
25 | |
import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult; |
26 | |
import org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult; |
27 | |
import org.kuali.rice.krad.datadictionary.validation.result.ProcessorResult; |
28 | |
|
29 | |
import java.util.Collection; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | 12 | public class CollectionSizeConstraintProcessor implements CollectionConstraintProcessor<Collection<?>, CollectionSizeConstraint> { |
37 | |
|
38 | |
private static final String CONSTRAINT_NAME = "collection size constraint"; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
@Override |
44 | |
public ProcessorResult process(DictionaryValidationResult result, Collection<?> collection, CollectionSizeConstraint constraint, AttributeValueReader attributeValueReader) throws AttributeValidationException { |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | 6 | return new ProcessorResult(processSingleCollectionSizeConstraint(result, collection, constraint, attributeValueReader)); |
50 | |
} |
51 | |
|
52 | |
@Override |
53 | |
public String getName() { |
54 | 6 | return CONSTRAINT_NAME; |
55 | |
} |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
@Override |
61 | |
public Class<? extends Constraint> getConstraintType() { |
62 | 0 | return CollectionSizeConstraint.class; |
63 | |
} |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
@Override |
69 | |
public boolean isOptional() { |
70 | 0 | return false; |
71 | |
} |
72 | |
|
73 | |
protected ConstraintValidationResult processSingleCollectionSizeConstraint(DictionaryValidationResult result, Collection<?> collection, CollectionSizeConstraint constraint, AttributeValueReader attributeValueReader) throws AttributeValidationException { |
74 | 6 | Integer sizeOfCollection = new Integer(0); |
75 | 6 | if (collection != null){ |
76 | 6 | sizeOfCollection = Integer.valueOf(collection.size()); |
77 | |
} |
78 | |
|
79 | 6 | Integer maxOccurances = constraint.getMaximumNumberOfElements(); |
80 | 6 | Integer minOccurances = constraint.getMinimumNumberOfElements(); |
81 | |
|
82 | 6 | Result lessThanMax = ValidationUtils.isLessThanOrEqual(sizeOfCollection, maxOccurances); |
83 | 6 | Result greaterThanMin = ValidationUtils.isGreaterThanOrEqual(sizeOfCollection, minOccurances); |
84 | |
|
85 | |
|
86 | 6 | if (lessThanMax != Result.INVALID && greaterThanMin != Result.INVALID) { |
87 | |
|
88 | 4 | if (lessThanMax == Result.UNDEFINED && greaterThanMin == Result.UNDEFINED) |
89 | 1 | return result.addNoConstraint(attributeValueReader, CONSTRAINT_NAME); |
90 | |
|
91 | |
|
92 | 3 | return result.addSuccess(attributeValueReader, CONSTRAINT_NAME); |
93 | |
} |
94 | |
|
95 | 2 | String maxErrorParameter = maxOccurances != null ? maxOccurances.toString() : null; |
96 | 2 | String minErrorParameter = minOccurances != null ? minOccurances.toString() : null; |
97 | |
|
98 | |
|
99 | 2 | if (lessThanMax != Result.UNDEFINED && greaterThanMin != Result.UNDEFINED) |
100 | 2 | return result.addError(attributeValueReader, CONSTRAINT_NAME, RiceKeyConstants.ERROR_QUANTITY_RANGE, minErrorParameter, maxErrorParameter); |
101 | |
|
102 | 0 | else if (lessThanMax == Result.INVALID) |
103 | 0 | return result.addError(attributeValueReader, CONSTRAINT_NAME, RiceKeyConstants.ERROR_MAX_OCCURS, maxErrorParameter); |
104 | |
|
105 | |
else |
106 | 0 | return result.addError(attributeValueReader, CONSTRAINT_NAME, RiceKeyConstants.ERROR_MIN_OCCURS, minErrorParameter); |
107 | |
|
108 | |
|
109 | |
} |
110 | |
|
111 | |
} |