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.core.api.uif.DataType; |
21 | |
import org.kuali.rice.krad.datadictionary.DataDictionaryEntry; |
22 | |
import org.kuali.rice.krad.datadictionary.exception.AttributeValidationException; |
23 | |
import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader; |
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 | 6 | 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 | 2 | if (null == caseConstraint) { |
54 | 0 | return new ProcessorResult(result.addNoConstraint(attributeValueReader, CONSTRAINT_NAME)); |
55 | |
} |
56 | 2 | AttributeValueReader constraintAttributeReader = ((DictionaryObjectAttributeValueReader)attributeValueReader).clone(); |
57 | |
|
58 | |
|
59 | |
|
60 | 2 | String operator = (ValidationUtils.hasText(caseConstraint.getOperator())) ? caseConstraint.getOperator() : "EQUALS"; |
61 | 2 | AttributeValueReader fieldPathReader = (ValidationUtils.hasText(caseConstraint.getPropertyName())) ? getChildAttributeValueReader(caseConstraint.getPropertyName(), attributeValueReader) : attributeValueReader; |
62 | |
|
63 | 2 | Constrainable caseField = (null != fieldPathReader) ? fieldPathReader.getDefinition(fieldPathReader.getAttributeName()) : null; |
64 | 2 | Object fieldValue = (null != fieldPathReader) ? fieldPathReader.getValue(fieldPathReader.getAttributeName()) : value; |
65 | 2 | DataType fieldDataType = (null != caseField && caseField instanceof DataTypeConstraint) ? ((DataTypeConstraint)caseField).getDataType() : null; |
66 | |
|
67 | |
|
68 | 2 | if (fieldDataType == null) |
69 | 2 | fieldDataType = DataType.STRING; |
70 | |
|
71 | |
|
72 | 2 | if (null == fieldValue) { |
73 | |
|
74 | 0 | return new ProcessorResult(result.addSkipped(attributeValueReader, CONSTRAINT_NAME), caseField, fieldPathReader); |
75 | |
} |
76 | |
|
77 | |
|
78 | 2 | for (WhenConstraint wc : caseConstraint.getWhenConstraint()) { |
79 | |
|
80 | 2 | if (ValidationUtils.hasText(wc.getValuePath())){ |
81 | 0 | Object whenValue = null; |
82 | 0 | AttributeValueReader whenValueReader = getChildAttributeValueReader(wc.getValuePath(), attributeValueReader); |
83 | 0 | whenValue = whenValueReader.getValue(whenValueReader.getAttributeName()); |
84 | 0 | if (ValidationUtils.compareValues(fieldValue, whenValue, fieldDataType, operator, caseConstraint.isCaseSensitive(), dateTimeService) && null != wc.getConstraint()) { |
85 | 0 | return new ProcessorResult(result.addSuccess(attributeValueReader, CONSTRAINT_NAME), null, constraintAttributeReader, wc.getConstraint()); |
86 | |
} |
87 | 0 | } else { |
88 | 2 | List<Object> whenValueList = wc.getValues(); |
89 | |
|
90 | 2 | for (Object whenValue : whenValueList) { |
91 | 2 | if (ValidationUtils.compareValues(fieldValue, whenValue, fieldDataType, operator, caseConstraint.isCaseSensitive(), dateTimeService) && null != wc.getConstraint()) { |
92 | 1 | return new ProcessorResult(result.addSuccess(attributeValueReader, CONSTRAINT_NAME), null, constraintAttributeReader, wc.getConstraint()); |
93 | |
} |
94 | |
} |
95 | 1 | } |
96 | |
} |
97 | |
|
98 | |
|
99 | 1 | return new ProcessorResult(result.addSkipped(attributeValueReader, CONSTRAINT_NAME)); |
100 | |
} |
101 | |
|
102 | |
@Override |
103 | |
public String getName() { |
104 | 2 | return CONSTRAINT_NAME; |
105 | |
} |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
@Override |
111 | |
public Class<? extends Constraint> getConstraintType() { |
112 | 0 | return CaseConstraint.class; |
113 | |
} |
114 | |
|
115 | |
private AttributeValueReader getChildAttributeValueReader(String key, AttributeValueReader attributeValueReader) throws AttributeValidationException { |
116 | 0 | String[] lookupPathTokens = ValidationUtils.getPathTokens(key); |
117 | |
|
118 | 0 | AttributeValueReader localAttributeValueReader = attributeValueReader; |
119 | 0 | for(int i = 0; i < lookupPathTokens.length; i++) { |
120 | 0 | for (Constrainable definition : localAttributeValueReader.getDefinitions()) { |
121 | 0 | String attributeName = definition.getName(); |
122 | 0 | if (attributeName.equals(lookupPathTokens[i])) { |
123 | 0 | if(i==lookupPathTokens.length-1){ |
124 | 0 | localAttributeValueReader.setAttributeName(attributeName); |
125 | 0 | return localAttributeValueReader; |
126 | |
} |
127 | 0 | if (definition instanceof HierarchicallyConstrainable) { |
128 | 0 | String childEntryName = ((HierarchicallyConstrainable)definition).getChildEntryName(); |
129 | 0 | DataDictionaryEntry entry = KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary().getDictionaryObjectEntry(childEntryName); |
130 | 0 | Object value = attributeValueReader.getValue(attributeName); |
131 | 0 | attributeValueReader.setAttributeName(attributeName); |
132 | 0 | String attributePath = attributeValueReader.getPath(); |
133 | 0 | localAttributeValueReader = new DictionaryObjectAttributeValueReader(value, childEntryName, entry, attributePath); |
134 | 0 | } |
135 | |
break; |
136 | |
} |
137 | 0 | } |
138 | |
} |
139 | 0 | return null; |
140 | |
} |
141 | |
|
142 | |
} |