1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.uif.element;
17
18 import java.beans.PropertyEditor;
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.Collection;
22 import java.util.HashMap;
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Set;
27
28 import org.apache.commons.lang.StringUtils;
29 import org.kuali.rice.krad.datadictionary.parse.BeanTag;
30 import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
31 import org.kuali.rice.krad.uif.UifConstants;
32 import org.kuali.rice.krad.uif.component.Component;
33 import org.kuali.rice.krad.uif.container.Container;
34 import org.kuali.rice.krad.uif.container.ContainerBase;
35 import org.kuali.rice.krad.uif.container.PageGroup;
36 import org.kuali.rice.krad.uif.field.FieldGroup;
37 import org.kuali.rice.krad.uif.field.InputField;
38 import org.kuali.rice.krad.uif.lifecycle.ViewLifecycle;
39 import org.kuali.rice.krad.uif.util.MessageStructureUtils;
40 import org.kuali.rice.krad.uif.view.View;
41 import org.kuali.rice.krad.util.ErrorMessage;
42 import org.kuali.rice.krad.util.GlobalVariables;
43 import org.kuali.rice.krad.util.KRADUtils;
44 import org.kuali.rice.krad.util.MessageMap;
45 import org.springframework.util.AutoPopulatingList;
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60 @BeanTag(name = "validationMessages-bean", parent = "Uif-ValidationMessagesBase")
61 public class ValidationMessages extends ContentElementBase {
62 private static final long serialVersionUID = 780940788435330077L;
63
64 private List<String> additionalKeysToMatch;
65
66 private boolean displayMessages;
67
68
69 private List<String> errors;
70 private List<String> warnings;
71 private List<String> infos;
72
73 private Map<String, String> validationDataDefaults;
74
75
76
77
78
79
80
81
82
83
84
85 @Override
86 public void performFinalize(Object model, Component parent) {
87 super.performFinalize(model, parent);
88
89 generateMessages(true, ViewLifecycle.getActiveLifecycle().getView(), model, parent);
90 }
91
92
93
94
95
96
97
98
99
100 public void generateMessages(boolean reset, View view, Object model, Component parent) {
101 if (reset) {
102 errors = new ArrayList<String>();
103 warnings = new ArrayList<String>();
104 infos = new ArrayList<String>();
105 }
106
107 List<String> masterKeyList = getKeys(parent);
108 MessageMap messageMap = GlobalVariables.getMessageMap();
109
110 String parentContainerId = "";
111
112 Map<String, Object> parentContext = parent.getContext();
113 Object parentContainer = parentContext == null ? null : parentContext
114 .get(UifConstants.ContextVariableNames.PARENT);
115
116 if (parentContainer != null && (parentContainer instanceof Container
117 || parentContainer instanceof FieldGroup)) {
118 parentContainerId = ((Component) parentContainer).getId();
119 }
120
121
122 if (parentContainer != null && parentContainer instanceof Message && ((Message) parentContainer)
123 .isGenerateSpan()) {
124 parentContainerId = ((Component) parentContainer).getId();
125 }
126
127
128 if (parentContainer != null && parentContainer instanceof Component && StringUtils.isBlank(parentContainerId)) {
129 parentContext = ((Component) parentContainer).getContext();
130 parentContainer = parentContext == null ? null : parentContext
131 .get(UifConstants.ContextVariableNames.PARENT);
132 if (parentContainer != null && (parentContainer instanceof Container
133 || parentContainer instanceof FieldGroup)) {
134 parentContainerId = ((Component) parentContainer).getId();
135 }
136 }
137
138
139 this.addDataAttribute(UifConstants.DataAttributes.MESSAGES_FOR, parent.getId());
140
141 if ((parent.getDataAttributes() == null) || (parent.getDataAttributes().get(UifConstants.DataAttributes.PARENT)
142 == null)) {
143 parent.addDataAttribute(UifConstants.DataAttributes.PARENT, parentContainerId);
144 }
145
146
147
148 if (parentContainer != null && parentContainer instanceof FieldGroup) {
149 masterKeyList.add(parentContainerId);
150 }
151
152
153
154 if (parent instanceof PageGroup) {
155 Map<String, PropertyEditor> propertyEditors = view.getViewIndex().getFieldPropertyEditors();
156 Map<String, PropertyEditor> securePropertyEditors = view.getViewIndex().getSecureFieldPropertyEditors();
157 List<String> allPossibleKeys = new ArrayList<String>(propertyEditors.keySet());
158 allPossibleKeys.addAll(securePropertyEditors.keySet());
159
160 this.addNestedGroupKeys(allPossibleKeys, parent);
161 if (additionalKeysToMatch != null) {
162 allPossibleKeys.addAll(additionalKeysToMatch);
163 }
164 if (StringUtils.isNotBlank(parent.getId())) {
165 allPossibleKeys.add(parent.getId());
166 }
167
168 Set<String> messageKeys = new HashSet<String>();
169 messageKeys.addAll(messageMap.getAllPropertiesWithErrors());
170 messageKeys.addAll(messageMap.getAllPropertiesWithWarnings());
171 messageKeys.addAll(messageMap.getAllPropertiesWithInfo());
172
173 messageKeys.removeAll(allPossibleKeys);
174
175 masterKeyList.addAll(messageKeys);
176 }
177
178 for (String key : masterKeyList) {
179 errors.addAll(getMessages(view, key, messageMap.getErrorMessagesForProperty(key, true)));
180 warnings.addAll(getMessages(view, key, messageMap.getWarningMessagesForProperty(key, true)));
181 infos.addAll(getMessages(view, key, messageMap.getInfoMessagesForProperty(key, true)));
182 }
183 }
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198 private List<String> getMessages(View view, String key, List<AutoPopulatingList<ErrorMessage>> lists) {
199 List<String> result = new ArrayList<String>();
200 for (List<ErrorMessage> errorList : lists) {
201 if (errorList != null && StringUtils.isNotBlank(key)) {
202 for (ErrorMessage e : errorList) {
203 String message = KRADUtils.getMessageText(e, true);
204 message = MessageStructureUtils.translateStringMessage(message);
205
206 result.add(message);
207 }
208 }
209 }
210
211 return result;
212 }
213
214
215
216
217
218
219
220
221
222
223 protected List<String> getKeys(Component parent) {
224 List<String> keyList = new ArrayList<String>();
225 if (additionalKeysToMatch != null) {
226 keyList.addAll(additionalKeysToMatch);
227 }
228 if (StringUtils.isNotBlank(parent.getId())) {
229 keyList.add(parent.getId());
230 }
231 if (parent instanceof InputField) {
232 if (((InputField) parent).getBindingInfo() != null && StringUtils.isNotEmpty(
233 ((InputField) parent).getBindingInfo().getBindingPath())) {
234 keyList.add(((InputField) parent).getBindingInfo().getBindingPath());
235 }
236 }
237
238 return keyList;
239 }
240
241
242
243
244
245
246
247
248 private void addNestedGroupKeys(Collection<String> keyList, Component component) {
249 for (Component c : component.getComponentsForLifecycle()) {
250 ValidationMessages ef = null;
251 if (c instanceof ContainerBase) {
252 ef = ((ContainerBase) c).getValidationMessages();
253 } else if (c instanceof FieldGroup) {
254 ef = ((FieldGroup) c).getGroup().getValidationMessages();
255 }
256 if (ef != null) {
257 keyList.addAll(ef.getKeys(c));
258 addNestedGroupKeys(keyList, c);
259 }
260 }
261 }
262
263
264
265
266
267
268
269
270
271 @BeanTagAttribute(name = "additionalKeysToMatch", type = BeanTagAttribute.AttributeType.LISTVALUE)
272 public List<String> getAdditionalKeysToMatch() {
273 return this.additionalKeysToMatch;
274 }
275
276
277
278
279
280
281
282 public void setAdditionalKeysToMatch(String additionalKeysToMatch) {
283 if (StringUtils.isNotBlank(additionalKeysToMatch)) {
284 this.additionalKeysToMatch = Arrays.asList(StringUtils.split(additionalKeysToMatch, ","));
285 }
286 }
287
288
289
290
291 public void setAdditionalKeysToMatch(List<String> additionalKeysToMatch) {
292 this.additionalKeysToMatch = additionalKeysToMatch;
293 }
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308 @BeanTagAttribute(name = "displayMessages")
309 public boolean isDisplayMessages() {
310 return this.displayMessages;
311 }
312
313
314
315
316 public void setDisplayMessages(boolean displayMessages) {
317 this.displayMessages = displayMessages;
318 }
319
320
321
322
323
324
325
326 @BeanTagAttribute(name = "errors", type = BeanTagAttribute.AttributeType.LISTVALUE)
327 public List<String> getErrors() {
328 return this.errors;
329 }
330
331
332
333
334
335
336
337 @BeanTagAttribute(name = "warnings", type = BeanTagAttribute.AttributeType.LISTVALUE)
338 public List<String> getWarnings() {
339 return this.warnings;
340 }
341
342
343
344
345
346
347
348 @BeanTagAttribute(name = "infos", type = BeanTagAttribute.AttributeType.LISTVALUE)
349 public List<String> getInfos() {
350 return this.infos;
351 }
352
353 public Map<String, String> getValidationDataDefaults() {
354 return validationDataDefaults;
355 }
356
357 public void setValidationDataDefaults(Map<String, String> validationDataDefaults) {
358 this.validationDataDefaults = validationDataDefaults;
359 }
360
361 protected void addValidationDataSettingsValue(Map<String, Object> valueMap, Map<String, String> defaults,
362 String key, Object value) {
363 String defaultValue = defaults.get(key);
364 if ((defaultValue != null && !value.toString().equals(defaultValue)) || (defaultValue != null && defaultValue
365 .equals("[]") && value instanceof List && !((List) value).isEmpty()) || defaultValue == null) {
366 valueMap.put(key, value);
367 }
368 }
369
370
371
372
373
374
375 protected void setErrors(List<String> errors) {
376 this.errors = errors;
377 }
378
379
380
381
382
383
384 protected void setWarnings(List<String> warnings) {
385 this.warnings = warnings;
386 }
387
388
389
390
391
392
393 protected void setInfos(List<String> infos) {
394 this.infos = infos;
395 }
396
397
398
399
400 @Override
401 protected <T> void copyProperties(T component) {
402 super.copyProperties(component);
403
404 ValidationMessages validationMessagesCopy = (ValidationMessages) component;
405
406 if (additionalKeysToMatch != null) {
407 validationMessagesCopy.setAdditionalKeysToMatch(new ArrayList<String>(this.additionalKeysToMatch));
408 }
409
410 validationMessagesCopy.setDisplayMessages(this.displayMessages);
411
412 if (warnings != null) {
413 validationMessagesCopy.setWarnings(new ArrayList<String>(this.warnings));
414 }
415
416 if (errors != null) {
417 validationMessagesCopy.setErrors(new ArrayList<String>(this.errors));
418 }
419
420 if (infos != null) {
421 validationMessagesCopy.setInfos(new ArrayList<String>(this.infos));
422 }
423
424 if (this.getValidationDataDefaults() != null) {
425 validationMessagesCopy.setValidationDataDefaults(new HashMap<String, String>(this.validationDataDefaults));
426 }
427 }
428
429 }