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.apache.commons.lang.StringUtils;
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.ErrorLevel;
23 import org.kuali.rice.krad.datadictionary.validation.ValidationUtils;
24 import org.kuali.rice.krad.datadictionary.validation.constraint.Constraint;
25 import org.kuali.rice.krad.datadictionary.validation.constraint.MustOccurConstraint;
26 import org.kuali.rice.krad.datadictionary.validation.constraint.PrerequisiteConstraint;
27 import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult;
28 import org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult;
29 import org.kuali.rice.krad.datadictionary.validation.result.ProcessorResult;
30 import org.kuali.rice.krad.uif.UifConstants;
31
32 import java.util.List;
33
34
35
36
37 public class MustOccurConstraintProcessor extends BasePrerequisiteConstraintProcessor<MustOccurConstraint> {
38
39 private static final String CONSTRAINT_NAME = "must occur constraint";
40 private static final String FALLBACK_KEY = "mustoccursFallback";
41
42
43
44
45
46
47 @Override
48 public ProcessorResult process(DictionaryValidationResult result, Object value, MustOccurConstraint constraint,
49 AttributeValueReader attributeValueReader) throws AttributeValidationException {
50
51 if (ValidationUtils.isNullOrEmpty(value)) {
52 return new ProcessorResult(result.addSkipped(attributeValueReader, CONSTRAINT_NAME));
53 }
54
55 ConstraintValidationResult constraintValidationResult = new ConstraintValidationResult(CONSTRAINT_NAME);
56 if (StringUtils.isNotBlank(constraint.getMessageKey())) {
57 constraintValidationResult.setConstraintLabelKey(constraint.getMessageKey());
58 } else {
59 constraintValidationResult.setConstraintLabelKey(
60 UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + FALLBACK_KEY);
61 }
62
63 constraintValidationResult.setErrorParameters(constraint.getValidationMessageParamsArray());
64
65
66 if (!processMustOccurConstraint(constraintValidationResult, constraint, attributeValueReader)) {
67
68 if (attributeValueReader.getAttributeName() == null) {
69 constraintValidationResult.setAttributeName(attributeValueReader.getEntryName());
70 } else {
71 constraintValidationResult.setAttributeName(attributeValueReader.getAttributeName());
72 constraintValidationResult.setAttributePath(attributeValueReader.getPath());
73 }
74 constraintValidationResult.setError(RiceKeyConstants.ERROR_OCCURS);
75 }
76
77
78
79
80 result.addConstraintValidationResult(attributeValueReader, constraintValidationResult);
81
82 return new ProcessorResult(constraintValidationResult);
83
84 }
85
86 @Override
87 public String getName() {
88 return CONSTRAINT_NAME;
89 }
90
91
92
93
94 @Override
95 public Class<? extends Constraint> getConstraintType() {
96 return MustOccurConstraint.class;
97 }
98
99 protected boolean processMustOccurConstraint(ConstraintValidationResult topLevelResult,
100 MustOccurConstraint constraint,
101 AttributeValueReader attributeValueReader) throws AttributeValidationException {
102
103 boolean isSuccessful = false;
104 int trueCount = 0;
105
106 List<PrerequisiteConstraint> prerequisiteConstraints = constraint.getPrerequisiteConstraints();
107 if (prerequisiteConstraints != null) {
108 for (PrerequisiteConstraint prerequisiteConstraint : prerequisiteConstraints) {
109 ConstraintValidationResult constraintValidationResult = processPrerequisiteConstraint(
110 prerequisiteConstraint, attributeValueReader);
111 constraintValidationResult.setConstraintLabelKey(prerequisiteConstraint.getMessageKey());
112 constraintValidationResult.setErrorParameters(prerequisiteConstraint.getValidationMessageParamsArray());
113
114 topLevelResult.addChild(constraintValidationResult);
115 trueCount += (constraintValidationResult.getStatus().getLevel() <= ErrorLevel.WARN.getLevel()) ? 1 : 0;
116 }
117 }
118
119 List<MustOccurConstraint> mustOccurConstraints = constraint.getMustOccurConstraints();
120 if (mustOccurConstraints != null) {
121 for (MustOccurConstraint mustOccurConstraint : mustOccurConstraints) {
122
123
124 ConstraintValidationResult constraintValidationResult = new ConstraintValidationResult(CONSTRAINT_NAME);
125 constraintValidationResult.setConstraintLabelKey(mustOccurConstraint.getMessageKey());
126 constraintValidationResult.setErrorParameters(mustOccurConstraint.getValidationMessageParamsArray());
127 topLevelResult.addChild(constraintValidationResult);
128 trueCount += (processMustOccurConstraint(constraintValidationResult, mustOccurConstraint,
129 attributeValueReader)) ? 1 : 0;
130 }
131 }
132
133 int minimum = constraint.getMin() != null ? constraint.getMin().intValue() : 0;
134 int maximum = constraint.getMax() != null ? constraint.getMax().intValue() : 0;
135
136 isSuccessful = (trueCount >= minimum && trueCount <= maximum) ? true : false;
137
138 return isSuccessful;
139 }
140
141 }