1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.datadictionary.validation.constraint;
17
18 import org.kuali.rice.krad.datadictionary.parse.BeanTag;
19 import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
20 import org.kuali.rice.krad.datadictionary.validator.ErrorReport;
21 import org.kuali.rice.krad.datadictionary.validator.ValidationTrace;
22
23 import java.util.ArrayList;
24 import java.util.List;
25
26
27
28
29
30
31
32
33
34
35
36
37 @BeanTag(name = "constraint-bean")
38 public class BaseConstraint implements Constraint {
39 protected String messageNamespaceCode;
40 protected String messageComponentCode;
41 protected String messageKey;
42
43 protected Boolean applyClientSide;
44
45 protected List<String> validationMessageParams;
46 protected List<String> states;
47 protected List<? extends BaseConstraint> constraintStateOverrides;
48
49 public BaseConstraint() {
50 applyClientSide = Boolean.valueOf(true);
51 }
52
53
54
55
56
57
58
59
60
61
62
63 @BeanTagAttribute(name = "messageNamespaceCode")
64 public String getMessageNamespaceCode() {
65 return messageNamespaceCode;
66 }
67
68
69
70
71
72
73 public void setMessageNamespaceCode(String messageNamespaceCode) {
74 this.messageNamespaceCode = messageNamespaceCode;
75 }
76
77
78
79
80
81
82
83
84
85
86
87 @BeanTagAttribute(name = "messageComponentCode")
88 public String getMessageComponentCode() {
89 return messageComponentCode;
90 }
91
92
93
94
95
96
97 public void setMessageComponentCode(String messageComponentCode) {
98 this.messageComponentCode = messageComponentCode;
99 }
100
101
102
103
104
105
106
107 @BeanTagAttribute(name = "messageKey")
108 public String getMessageKey() {
109 return messageKey;
110 }
111
112
113
114
115
116
117 public void setMessageKey(String messageKey) {
118 this.messageKey = messageKey;
119 }
120
121
122
123
124
125
126
127 @BeanTagAttribute(name = "applyClientSide")
128 public Boolean getApplyClientSide() {
129 return this.applyClientSide;
130 }
131
132
133
134
135 public void setApplyClientSide(Boolean applyClientSide) {
136 this.applyClientSide = applyClientSide;
137 }
138
139
140
141
142
143
144
145 @BeanTagAttribute(name = "validationMessageParams", type = BeanTagAttribute.AttributeType.LISTVALUE)
146 public List<String> getValidationMessageParams() {
147 return this.validationMessageParams;
148 }
149
150
151
152
153
154
155
156 public String[] getValidationMessageParamsArray() {
157 if (this.getValidationMessageParams() != null) {
158 return this.getValidationMessageParams().toArray(new String[this.getValidationMessageParams().size()]);
159 } else {
160 return null;
161 }
162
163 }
164
165
166
167
168 public void setValidationMessageParams(List<String> validationMessageParams) {
169 this.validationMessageParams = validationMessageParams;
170 }
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194 @BeanTagAttribute(name = "states", type = BeanTagAttribute.AttributeType.LISTVALUE)
195 public List<String> getStates() {
196 if (states == null) {
197 states = new ArrayList<String>();
198 }
199 return states;
200 }
201
202
203
204
205
206
207 public void setStates(List<String> states) {
208 this.states = states;
209 }
210
211
212
213
214
215
216
217
218
219
220
221
222
223 @BeanTagAttribute(name = "constraintStateOverrides", type = BeanTagAttribute.AttributeType.LISTBEAN)
224 public List<? extends BaseConstraint> getConstraintStateOverrides() {
225 return constraintStateOverrides;
226 }
227
228
229
230
231
232
233 public void setConstraintStateOverrides(List<? extends BaseConstraint> constraintStateOverrides) {
234 if (constraintStateOverrides != null) {
235 for (BaseConstraint bc : constraintStateOverrides) {
236 if (!bc.getClass().equals(this.getClass())) {
237 List<Class<?>> superClasses = new ArrayList<Class<?>>();
238 Class<?> o = bc.getClass();
239 while (o != null && !o.equals(BaseConstraint.class)) {
240 superClasses.add(o);
241 o = o.getSuperclass();
242 }
243
244 List<Class<?>> thisSuperClasses = new ArrayList<Class<?>>();
245 o = this.getClass();
246 while (o != null && !o.equals(BaseConstraint.class)) {
247 thisSuperClasses.add(o);
248 o = o.getSuperclass();
249 }
250 superClasses.retainAll(thisSuperClasses);
251
252 if (superClasses.isEmpty()) {
253 throw new RuntimeException("Constraint State Override is not a correct type, type should be " +
254 this.getClass().toString() + " (or child/parent of that constraint type)");
255 }
256 }
257 if (bc.getStates().isEmpty()) {
258 throw new RuntimeException(
259 "Constraint State Overrides MUST declare the states they apply to. No states"
260 + "were declared.");
261 }
262 }
263 }
264 this.constraintStateOverrides = constraintStateOverrides;
265 }
266
267
268
269
270
271
272
273 public void completeValidation(ValidationTrace tracer) {
274 tracer.addBean("BaseConstraint", getMessageKey());
275
276 if (getConstraintStateOverrides() != null) {
277 for (int i = 0; i < constraintStateOverrides.size(); i++) {
278 if (constraintStateOverrides.get(i).getStates() == null) {
279 String currentValues[] =
280 {"constraintStateOverrides(" + i + ").messageKey =" + constraintStateOverrides.get(i)
281 .getMessageKey()};
282 tracer.createError("Constraints set in State Overrides must have there states property set",
283 currentValues);
284 }
285 constraintStateOverrides.get(i).completeValidation(tracer.getCopy());
286 }
287 }
288
289 if (getMessageKey() == null) {
290 String currentValues[] = {"messageKey =" + getMessageKey()};
291 tracer.createWarning("Message key is not set", currentValues);
292 ErrorReport error = new ErrorReport(ErrorReport.WARNING);
293 }
294 }
295 }