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.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 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 if (ValidationUtils.isNullOrEmpty(value))
49 return new ProcessorResult(result.addSkipped(attributeValueReader, CONSTRAINT_NAME));
50
51
52 ConstraintValidationResult constraintValidationResult = new ConstraintValidationResult(CONSTRAINT_NAME);
53 constraintValidationResult.setConstraintLabelKey(constraint.getLabelKey());
54 constraintValidationResult.setErrorParameters(constraint.getValidationMessageParamsArray());
55
56 if (!processMustOccurConstraint(constraintValidationResult, constraint, attributeValueReader)) {
57
58 if (attributeValueReader.getAttributeName() == null){
59 constraintValidationResult.setAttributeName(attributeValueReader.getPath());
60 } else{
61 constraintValidationResult.setAttributeName(attributeValueReader.getAttributeName());
62 constraintValidationResult.setAttributePath(attributeValueReader.getPath());
63 }
64 constraintValidationResult.setError(RiceKeyConstants.ERROR_OCCURS);
65 }
66
67
68
69
70 result.addConstraintValidationResult(attributeValueReader, constraintValidationResult);
71
72 return new ProcessorResult(constraintValidationResult);
73
74 }
75
76 @Override
77 public String getName() {
78 return CONSTRAINT_NAME;
79 }
80
81
82
83
84 @Override
85 public Class<? extends Constraint> getConstraintType() {
86 return MustOccurConstraint.class;
87 }
88
89 protected boolean processMustOccurConstraint(ConstraintValidationResult topLevelResult, MustOccurConstraint constraint, AttributeValueReader attributeValueReader) throws AttributeValidationException {
90
91 boolean isSuccessful = false;
92 int trueCount = 0;
93
94 List<PrerequisiteConstraint> prerequisiteConstraints = constraint.getPrerequisiteConstraints();
95 if (prerequisiteConstraints != null) {
96 for (PrerequisiteConstraint prerequisiteConstraint : prerequisiteConstraints) {
97 ConstraintValidationResult constraintValidationResult = processPrerequisiteConstraint(prerequisiteConstraint, attributeValueReader);
98 constraintValidationResult.setConstraintLabelKey(prerequisiteConstraint.getLabelKey());
99 constraintValidationResult.setErrorParameters(prerequisiteConstraint.getValidationMessageParamsArray());
100
101 topLevelResult.addChild(constraintValidationResult);
102 trueCount += (constraintValidationResult.getStatus().getLevel() <= ErrorLevel.WARN.getLevel()) ? 1 : 0;
103 }
104 }
105
106 List<MustOccurConstraint> mustOccurConstraints = constraint.getMustOccurConstraints();
107 if (mustOccurConstraints != null) {
108 for (MustOccurConstraint mustOccurConstraint : mustOccurConstraints) {
109
110
111 ConstraintValidationResult constraintValidationResult = new ConstraintValidationResult(CONSTRAINT_NAME);
112 constraintValidationResult.setConstraintLabelKey(mustOccurConstraint.getLabelKey());
113 constraintValidationResult.setErrorParameters(mustOccurConstraint.getValidationMessageParamsArray());
114 topLevelResult.addChild(constraintValidationResult);
115 trueCount += (processMustOccurConstraint(constraintValidationResult, mustOccurConstraint, attributeValueReader)) ? 1 : 0;
116 }
117 }
118
119 int minimum = constraint.getMin() != null ? constraint.getMin().intValue() : 0;
120 int maximum = constraint.getMax() != null ? constraint.getMax().intValue() : 0;
121
122 isSuccessful = (trueCount >= minimum && trueCount <= maximum) ? true : false;
123
124 return isSuccessful;
125 }
126
127 }