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