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.PrerequisiteConstraint;
25 import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult;
26
27 import java.util.Collection;
28
29
30
31
32
33 public abstract class BasePrerequisiteConstraintProcessor<C extends Constraint> extends MandatoryElementConstraintProcessor<C> {
34
35 protected ConstraintValidationResult processPrerequisiteConstraint(PrerequisiteConstraint constraint,
36 AttributeValueReader attributeValueReader) throws AttributeValidationException {
37 ConstraintValidationResult constraintValidationResult = new ConstraintValidationResult(getName());
38
39 if (constraint == null) {
40 constraintValidationResult.setStatus(ErrorLevel.NOCONSTRAINT);
41 return constraintValidationResult;
42 }
43
44
45 String attributeName = constraint.getPropertyName();
46
47 if (ValidationUtils.isNullOrEmpty(attributeName)) {
48 throw new AttributeValidationException(
49 "Prerequisite constraints must include the name of the attribute that is required");
50 }
51
52 Object value = attributeValueReader.getValue(attributeName);
53
54 boolean isSuccessful = true;
55
56 if (value instanceof java.lang.String) {
57 isSuccessful = ValidationUtils.hasText((String) value);
58 } else if (value instanceof Collection) {
59 isSuccessful = (((Collection<?>) value).size() > 0);
60 } else {
61 isSuccessful = (null != value) ? true : false;
62 }
63
64 if (!isSuccessful) {
65 String label = attributeValueReader.getLabel(attributeName);
66 if (label != null) {
67 attributeName = label;
68 }
69
70 constraintValidationResult.setError(RiceKeyConstants.ERROR_REQUIRES_FIELD, attributeName);
71 constraintValidationResult.setConstraintLabelKey(constraint.getMessageKey());
72 constraintValidationResult.setErrorParameters(constraint.getValidationMessageParamsArray());
73 }
74
75 return constraintValidationResult;
76 }
77
78 }