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