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 return result.addError(attributeValueReader, CONSTRAINT_NAME, RiceKeyConstants.ERROR_REQUIRED,
76 attributeValueReader.getLabel(attributeValueReader.getAttributeName()));
77 }
78 return result.addSuccess(attributeValueReader, CONSTRAINT_NAME);
79 }
80
81 return result.addSkipped(attributeValueReader, CONSTRAINT_NAME);
82 }
83
84
85
86
87
88
89
90
91
92 private boolean skipConstraint(AttributeValueReader attributeValueReader) {
93 boolean skipConstraint = false;
94 if (attributeValueReader instanceof DictionaryObjectAttributeValueReader) {
95 DictionaryObjectAttributeValueReader dictionaryValueReader =
96 (DictionaryObjectAttributeValueReader) attributeValueReader;
97 skipConstraint = dictionaryValueReader.isNestedAttribute() && dictionaryValueReader.isParentAttributeNull();
98 }
99 return skipConstraint;
100 }
101
102 }