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 | 12 | 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 | 6 | return new ProcessorResult(processSingleExistenceConstraint(result, value, constraint, attributeValueReader)); |
48 | |
} |
49 | |
|
50 | |
@Override |
51 | |
public String getName() { |
52 | 6 | return CONSTRAINT_NAME; |
53 | |
} |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
@Override |
59 | |
public Class<? extends Constraint> getConstraintType() { |
60 | 0 | return ExistenceConstraint.class; |
61 | |
} |
62 | |
|
63 | |
protected ConstraintValidationResult processSingleExistenceConstraint(DictionaryValidationResult result, Object value, ExistenceConstraint constraint, AttributeValueReader attributeValueReader) throws AttributeValidationException { |
64 | |
|
65 | 6 | if (constraint.isRequired() == null) |
66 | 2 | return result.addNoConstraint(attributeValueReader, CONSTRAINT_NAME); |
67 | |
|
68 | 4 | if (constraint.isRequired().booleanValue() && !skipConstraint(attributeValueReader)) { |
69 | |
|
70 | 2 | if (ValidationUtils.isNullOrEmpty(value)) |
71 | 1 | return result.addError(attributeValueReader, CONSTRAINT_NAME, RiceKeyConstants.ERROR_REQUIRED); |
72 | |
|
73 | 1 | return result.addSuccess(attributeValueReader, CONSTRAINT_NAME); |
74 | |
} |
75 | |
|
76 | 2 | return result.addSkipped(attributeValueReader, CONSTRAINT_NAME); |
77 | |
} |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
private boolean skipConstraint(AttributeValueReader attributeValueReader) { |
87 | 2 | boolean skipConstraint = false; |
88 | 2 | if (attributeValueReader instanceof DictionaryObjectAttributeValueReader){ |
89 | 0 | DictionaryObjectAttributeValueReader dictionaryValueReader = (DictionaryObjectAttributeValueReader)attributeValueReader; |
90 | 0 | skipConstraint = dictionaryValueReader.isNestedAttribute() && dictionaryValueReader.isParentAttributeNull(); |
91 | |
} |
92 | 2 | return skipConstraint; |
93 | |
} |
94 | |
|
95 | |
|
96 | |
} |