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.ErrorLevel; |
22 | |
import org.kuali.rice.krad.datadictionary.validation.ValidationUtils; |
23 | |
import org.kuali.rice.krad.datadictionary.validation.constraint.Constraint; |
24 | |
import org.kuali.rice.krad.datadictionary.validation.constraint.MustOccurConstraint; |
25 | |
import org.kuali.rice.krad.datadictionary.validation.constraint.PrerequisiteConstraint; |
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 | |
import java.util.List; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | 9 | public class MustOccurConstraintProcessor extends BasePrerequisiteConstraintProcessor<MustOccurConstraint> { |
37 | |
|
38 | |
private static final String CONSTRAINT_NAME = "must occur constraint"; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
@Override |
44 | |
public ProcessorResult process(DictionaryValidationResult result, |
45 | |
Object value, MustOccurConstraint constraint, AttributeValueReader attributeValueReader) |
46 | |
throws AttributeValidationException { |
47 | |
|
48 | 3 | if (ValidationUtils.isNullOrEmpty(value)) |
49 | 0 | return new ProcessorResult(result.addSkipped(attributeValueReader, CONSTRAINT_NAME)); |
50 | |
|
51 | |
|
52 | 3 | ConstraintValidationResult constraintValidationResult = new ConstraintValidationResult(CONSTRAINT_NAME); |
53 | |
|
54 | 3 | if (!processMustOccurConstraint(constraintValidationResult, constraint, attributeValueReader)) { |
55 | |
|
56 | 1 | constraintValidationResult.setError(RiceKeyConstants.ERROR_OCCURS); |
57 | |
} |
58 | |
|
59 | |
|
60 | 3 | constraintValidationResult.setConstraintLabelKey(constraint.getLabelKey()); |
61 | |
|
62 | 3 | result.addConstraintValidationResult(attributeValueReader, constraintValidationResult); |
63 | |
|
64 | 3 | return new ProcessorResult(constraintValidationResult); |
65 | |
|
66 | |
} |
67 | |
|
68 | |
@Override |
69 | |
public String getName() { |
70 | 12 | return CONSTRAINT_NAME; |
71 | |
} |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
@Override |
77 | |
public Class<? extends Constraint> getConstraintType() { |
78 | 0 | return MustOccurConstraint.class; |
79 | |
} |
80 | |
|
81 | |
protected boolean processMustOccurConstraint(ConstraintValidationResult topLevelResult, MustOccurConstraint constraint, AttributeValueReader attributeValueReader) throws AttributeValidationException { |
82 | |
|
83 | 6 | boolean isSuccessful = false; |
84 | 6 | int trueCount = 0; |
85 | |
|
86 | 6 | List<PrerequisiteConstraint> prerequisiteConstraints = constraint.getPrerequisiteConstraints(); |
87 | 6 | if (prerequisiteConstraints != null) { |
88 | 6 | for (PrerequisiteConstraint prerequisiteConstraint : prerequisiteConstraints) { |
89 | 9 | ConstraintValidationResult constraintValidationResult = processPrerequisiteConstraint(prerequisiteConstraint, attributeValueReader); |
90 | |
|
91 | 9 | topLevelResult.addChild(constraintValidationResult); |
92 | 9 | trueCount += (constraintValidationResult.getStatus().getLevel() <= ErrorLevel.WARN.getLevel()) ? 1 : 0; |
93 | 9 | } |
94 | |
} |
95 | |
|
96 | 6 | List<MustOccurConstraint> mustOccurConstraints = constraint.getMustOccurConstraints(); |
97 | 6 | if (mustOccurConstraints != null) { |
98 | 3 | for (MustOccurConstraint mustOccurConstraint : mustOccurConstraints) { |
99 | |
|
100 | |
|
101 | 3 | ConstraintValidationResult constraintValidationResult = new ConstraintValidationResult(CONSTRAINT_NAME); |
102 | 3 | topLevelResult.addChild(constraintValidationResult); |
103 | 3 | trueCount += (processMustOccurConstraint(constraintValidationResult, mustOccurConstraint, attributeValueReader)) ? 1 : 0; |
104 | 3 | } |
105 | |
} |
106 | |
|
107 | 6 | int minimum = constraint.getMin() != null ? constraint.getMin().intValue() : 0; |
108 | 6 | int maximum = constraint.getMax() != null ? constraint.getMax().intValue() : 0; |
109 | |
|
110 | 6 | isSuccessful = (trueCount >= minimum && trueCount <= maximum) ? true : false; |
111 | |
|
112 | 6 | return isSuccessful; |
113 | |
} |
114 | |
|
115 | |
} |