1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.uif.util;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.krad.datadictionary.state.StateMapping;
20 import org.kuali.rice.krad.datadictionary.validation.constraint.BaseConstraint;
21 import org.kuali.rice.krad.datadictionary.validation.constraint.Constraint;
22 import org.kuali.rice.krad.uif.view.View;
23
24 import java.util.ArrayList;
25 import java.util.List;
26
27
28
29
30
31
32
33 public class ConstraintStateUtils {
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public static boolean constraintAppliesForState(String applicableState, Constraint constraint,
49 StateMapping stateMapping) {
50 List<String> stateOrder = new ArrayList<String>();
51 if (stateMapping != null) {
52 stateOrder = stateMapping.getStates();
53 }
54
55 if (stateMapping == null || !(constraint instanceof BaseConstraint) || StringUtils.isEmpty(applicableState)) {
56
57
58 return true;
59 } else if (((BaseConstraint) constraint).getStates() == null || ((BaseConstraint) constraint).getStates()
60 .isEmpty()) {
61
62 return true;
63 } else if (((BaseConstraint) constraint).getStates().contains(applicableState) && stateOrder.contains(
64 applicableState)) {
65
66 return true;
67 } else {
68 for (String constraintState : ((BaseConstraint) constraint).getStates()) {
69
70 if (constraintState.contains(">")) {
71 String[] rangeArray = constraintState.split(">");
72 if (rangeArray[1].endsWith("+")) {
73
74
75 constraintState = rangeArray[1];
76 rangeArray[1] = StringUtils.removeEnd(rangeArray[1], "+");
77 }
78 if (stateOrder.contains(rangeArray[0]) && stateOrder.contains(rangeArray[1])) {
79 for (int i = stateOrder.indexOf(rangeArray[0]); i <= stateOrder.indexOf(rangeArray[1]); i++) {
80 if (stateOrder.get(i).equals(applicableState)) {
81 return true;
82 }
83 }
84 } else {
85 throw new RuntimeException("Invalid state range: " + constraintState);
86 }
87 }
88
89
90 if (constraintState.contains("+")) {
91 constraintState = StringUtils.removeEnd(constraintState, "+");
92 if (stateOrder.contains(constraintState)) {
93 for (int i = stateOrder.indexOf(constraintState); i < stateOrder.size(); i++) {
94 if (stateOrder.get(i).equals(applicableState)) {
95 return true;
96 }
97 }
98 } else {
99 throw new RuntimeException("Invalid constraint state: " + constraintState);
100 }
101 }
102 }
103 }
104
105 return false;
106 }
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127 public static <T extends Constraint> T getApplicableConstraint(T constraint, String validationState,
128 StateMapping stateMapping) {
129
130
131 if (constraint != null && constraint instanceof BaseConstraint && stateMapping != null &&
132 StringUtils.isNotBlank(validationState)) {
133
134
135 if (((BaseConstraint) constraint).getConstraintStateOverrides() != null && !((BaseConstraint) constraint)
136 .getConstraintStateOverrides().isEmpty()) {
137 T override = null;
138 BaseConstraint theConstraint = ((BaseConstraint) constraint);
139 for (BaseConstraint bc : theConstraint.getConstraintStateOverrides()) {
140
141 if (!bc.getStates().isEmpty() && ConstraintStateUtils.constraintAppliesForState(validationState, bc,
142 stateMapping)) {
143 try {
144
145 override = (T) bc;
146 } catch (ClassCastException e) {
147 throw new RuntimeException("Replacement state constraint for this constraint is not an "
148 + "appropriate type: "
149 + constraint.getClass().toString()
150 + " cannot be cast to "
151 + bc.getClass().toString());
152 }
153 }
154 }
155
156 if(override != null){
157 return override;
158 }
159 else if(override == null && ConstraintStateUtils.constraintAppliesForState(validationState, constraint, stateMapping)){
160
161 return constraint;
162 }
163 else{
164
165 return null;
166 }
167 } else if(ConstraintStateUtils.constraintAppliesForState(validationState, constraint, stateMapping)) {
168
169 return constraint;
170 }
171 else{
172
173 return null;
174 }
175 }
176
177
178
179 return constraint;
180 }
181
182
183
184
185
186
187
188
189
190
191
192 public static String getClientViewValidationState(Object model, View view){
193 String validationState = null;
194 String path = view.getStateObjectBindingPath();
195 Object stateObject;
196
197 if (StringUtils.isNotBlank(path)) {
198 stateObject = ObjectPropertyUtils.getPropertyValue(model, path);
199 } else {
200 stateObject = model;
201 }
202 StateMapping stateMapping = view.getStateMapping();
203 if (stateMapping != null) {
204 validationState = stateMapping.getNextState(stateObject);
205
206 if (stateMapping.getCustomClientSideValidationStates() != null) {
207 String currentState = stateMapping.getCurrentState(stateObject);
208 validationState = stateMapping.getCustomClientSideValidationStates().get(currentState);
209 if (StringUtils.isBlank(validationState)) {
210 validationState = stateMapping.getNextState(stateObject);
211 }
212 }
213 }
214 return validationState;
215 }
216 }