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.DictionaryObjectAttributeValueReader;
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.ExistenceConstraint;
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
30
31
32 public class ExistenceConstraintProcessor extends OptionalElementConstraintProcessor<ExistenceConstraint> {
33
34 private static final String CONSTRAINT_NAME = "existence constraint";
35
36
37
38
39
40
41 @Override
42 public ProcessorResult process(DictionaryValidationResult result, Object value, ExistenceConstraint constraint,
43 AttributeValueReader attributeValueReader) throws AttributeValidationException {
44
45
46
47
48 return new ProcessorResult(processSingleExistenceConstraint(result, value, constraint, attributeValueReader));
49 }
50
51 @Override
52 public String getName() {
53 return CONSTRAINT_NAME;
54 }
55
56
57
58
59 @Override
60 public Class<? extends Constraint> getConstraintType() {
61 return ExistenceConstraint.class;
62 }
63
64 protected ConstraintValidationResult processSingleExistenceConstraint(DictionaryValidationResult result,
65 Object value, ExistenceConstraint constraint,
66 AttributeValueReader attributeValueReader) throws AttributeValidationException {
67
68 if (constraint.isRequired() == null) {
69 return result.addNoConstraint(attributeValueReader, CONSTRAINT_NAME);
70 }
71
72 if (constraint.isRequired().booleanValue() && !skipConstraint(attributeValueReader)) {
73
74 if (ValidationUtils.isNullOrEmpty(value)) {
75 String errorParameter = attributeValueReader.getLabel(attributeValueReader.getAttributeName());
76 if (ValidationUtils.isNullOrEmpty(errorParameter)) {
77 errorParameter = attributeValueReader.getAttributeName();
78 }
79 return result.addError(attributeValueReader, CONSTRAINT_NAME, RiceKeyConstants.ERROR_REQUIRED,
80 errorParameter);
81 }
82 return result.addSuccess(attributeValueReader, CONSTRAINT_NAME);
83 }
84
85 return result.addSkipped(attributeValueReader, CONSTRAINT_NAME);
86 }
87
88
89
90
91
92
93
94
95
96 private boolean skipConstraint(AttributeValueReader attributeValueReader) {
97 boolean skipConstraint = false;
98 if (attributeValueReader instanceof DictionaryObjectAttributeValueReader) {
99 DictionaryObjectAttributeValueReader dictionaryValueReader =
100 (DictionaryObjectAttributeValueReader) attributeValueReader;
101 skipConstraint = dictionaryValueReader.isNestedAttribute() && dictionaryValueReader.isParentAttributeNull();
102 }
103 return skipConstraint;
104 }
105
106 }