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 java.util.List; |
19 | |
|
20 | |
import org.kuali.rice.krad.datadictionary.DataDictionaryEntry; |
21 | |
import org.kuali.rice.krad.datadictionary.exception.AttributeValidationException; |
22 | |
import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader; |
23 | |
import org.kuali.rice.krad.datadictionary.validation.DataType; |
24 | |
import org.kuali.rice.krad.datadictionary.validation.DictionaryObjectAttributeValueReader; |
25 | |
import org.kuali.rice.krad.datadictionary.validation.ValidationUtils; |
26 | |
import org.kuali.rice.krad.datadictionary.validation.capability.Constrainable; |
27 | |
import org.kuali.rice.krad.datadictionary.validation.capability.HierarchicallyConstrainable; |
28 | |
import org.kuali.rice.krad.datadictionary.validation.constraint.CaseConstraint; |
29 | |
import org.kuali.rice.krad.datadictionary.validation.constraint.Constraint; |
30 | |
import org.kuali.rice.krad.datadictionary.validation.constraint.DataTypeConstraint; |
31 | |
import org.kuali.rice.krad.datadictionary.validation.constraint.WhenConstraint; |
32 | |
import org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult; |
33 | |
import org.kuali.rice.krad.datadictionary.validation.result.ProcessorResult; |
34 | |
import org.kuali.rice.krad.service.KRADServiceLocatorWeb; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | 0 | public class CaseConstraintProcessor extends MandatoryElementConstraintProcessor<CaseConstraint> { |
43 | |
|
44 | |
private static final String CONSTRAINT_NAME = "case constraint"; |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
@Override |
50 | |
public ProcessorResult process(DictionaryValidationResult result, Object value, CaseConstraint caseConstraint, AttributeValueReader attributeValueReader) throws AttributeValidationException { |
51 | |
|
52 | |
|
53 | 0 | if (null == caseConstraint) { |
54 | 0 | return new ProcessorResult(result.addNoConstraint(attributeValueReader, CONSTRAINT_NAME)); |
55 | |
} |
56 | |
|
57 | 0 | String operator = (ValidationUtils.hasText(caseConstraint.getOperator())) ? caseConstraint.getOperator() : "EQUALS"; |
58 | 0 | AttributeValueReader nestedReader = (ValidationUtils.hasText(caseConstraint.getFieldPath())) ? getChildAttributeValueReader(caseConstraint.getFieldPath(), attributeValueReader) : attributeValueReader; |
59 | |
|
60 | |
|
61 | 0 | Constrainable caseField = (null != nestedReader) ? nestedReader.getDefinition(nestedReader.getAttributeName()) : null; |
62 | 0 | Object fieldValue = (null != nestedReader) ? nestedReader.getValue(nestedReader.getAttributeName()) : value; |
63 | 0 | DataType fieldDataType = (null != caseField && caseField instanceof DataTypeConstraint) ? ((DataTypeConstraint)caseField).getDataType() : null; |
64 | |
|
65 | |
|
66 | 0 | if (fieldDataType == null) |
67 | 0 | fieldDataType = DataType.STRING; |
68 | |
|
69 | |
|
70 | 0 | if (null == fieldValue) { |
71 | |
|
72 | 0 | return new ProcessorResult(result.addSkipped(attributeValueReader, CONSTRAINT_NAME), caseField, nestedReader); |
73 | |
} |
74 | |
|
75 | |
|
76 | 0 | for (WhenConstraint wc : caseConstraint.getWhenConstraint()) { |
77 | |
|
78 | 0 | List<Object> whenValueList = wc.getValues(); |
79 | |
|
80 | 0 | for (Object whenValue : whenValueList) { |
81 | 0 | if (ValidationUtils.compareValues(fieldValue, whenValue, fieldDataType, operator, caseConstraint.isCaseSensitive(), dateTimeService) && null != wc.getConstraint()) { |
82 | 0 | if (nestedReader != null && wc.getValuePath() != null) |
83 | 0 | nestedReader.setAttributeName(wc.getValuePath()); |
84 | 0 | return new ProcessorResult(result.addSuccess(nestedReader, CONSTRAINT_NAME), null, nestedReader, wc.getConstraint()); |
85 | |
} |
86 | |
} |
87 | 0 | } |
88 | |
|
89 | |
|
90 | 0 | return new ProcessorResult(result.addSkipped(attributeValueReader, CONSTRAINT_NAME)); |
91 | |
} |
92 | |
|
93 | |
@Override |
94 | |
public String getName() { |
95 | 0 | return CONSTRAINT_NAME; |
96 | |
} |
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
@Override |
102 | |
public Class<? extends Constraint> getConstraintType() { |
103 | 0 | return CaseConstraint.class; |
104 | |
} |
105 | |
|
106 | |
private AttributeValueReader getChildAttributeValueReader(String key, AttributeValueReader attributeValueReader) throws AttributeValidationException { |
107 | 0 | String[] lookupPathTokens = ValidationUtils.getPathTokens(key); |
108 | |
|
109 | 0 | AttributeValueReader localAttributeValueReader = attributeValueReader; |
110 | 0 | for(int i = 0; i < lookupPathTokens.length; i++) { |
111 | 0 | for (Constrainable definition : localAttributeValueReader.getDefinitions()) { |
112 | 0 | String attributeName = definition.getName(); |
113 | 0 | if (attributeName.equals(lookupPathTokens[i])) { |
114 | 0 | if(i==lookupPathTokens.length-1){ |
115 | 0 | localAttributeValueReader.setAttributeName(attributeName); |
116 | 0 | return localAttributeValueReader; |
117 | |
} |
118 | 0 | if (definition instanceof HierarchicallyConstrainable) { |
119 | 0 | String childEntryName = ((HierarchicallyConstrainable)definition).getChildEntryName(); |
120 | 0 | DataDictionaryEntry entry = KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary().getDictionaryObjectEntry(childEntryName); |
121 | 0 | Object value = attributeValueReader.getValue(attributeName); |
122 | 0 | attributeValueReader.setAttributeName(attributeName); |
123 | 0 | String attributePath = attributeValueReader.getPath(); |
124 | 0 | localAttributeValueReader = new DictionaryObjectAttributeValueReader(value, childEntryName, entry, attributePath); |
125 | 0 | } |
126 | |
break; |
127 | |
} |
128 | 0 | } |
129 | |
} |
130 | 0 | return null; |
131 | |
} |
132 | |
|
133 | |
} |