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.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.Collection;
21 import java.util.LinkedList;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Queue;
25
26 import org.apache.commons.lang.StringUtils;
27 import org.kuali.rice.krad.datadictionary.parse.BeanTag;
28 import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
29 import org.kuali.rice.krad.datadictionary.uif.UifDictionaryBeanBase;
30 import org.kuali.rice.krad.uif.UifConstants;
31 import org.kuali.rice.krad.uif.component.Component;
32 import org.kuali.rice.krad.uif.container.Container;
33 import org.kuali.rice.krad.uif.container.ContainerBase;
34 import org.kuali.rice.krad.uif.field.FieldGroup;
35 import org.kuali.rice.krad.uif.field.InputField;
36 import org.kuali.rice.krad.uif.lifecycle.ViewLifecycleUtils;
37 import org.kuali.rice.krad.uif.util.LifecycleElement;
38 import org.kuali.rice.krad.uif.util.MessageStructureUtils;
39 import org.kuali.rice.krad.uif.util.RecycleUtils;
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59 @BeanTag(name = "validationMessages-bean", parent = "Uif-ValidationMessagesBase")
60 public class ValidationMessages extends UifDictionaryBeanBase {
61 private static final long serialVersionUID = 780940788435330077L;
62
63 private List<String> additionalKeysToMatch;
64
65 private boolean displayMessages;
66
67
68 private List<String> errors;
69 private List<String> warnings;
70 private List<String> infos;
71
72
73
74
75
76
77
78
79 public void generateMessages(View view, Object model, Component parent) {
80 errors = new ArrayList<String>();
81 warnings = new ArrayList<String>();
82 infos = new ArrayList<String>();
83
84 List<String> masterKeyList = getKeys(parent);
85 MessageMap messageMap = GlobalVariables.getMessageMap();
86
87 String parentContainerId = "";
88
89 Map<String, Object> parentContext = parent.getContext();
90 Object parentContainer = parentContext == null ? null : parentContext
91 .get(UifConstants.ContextVariableNames.PARENT);
92
93 if (parentContainer != null && (parentContainer instanceof Container
94 || parentContainer instanceof FieldGroup)) {
95 parentContainerId = ((Component) parentContainer).getId();
96 }
97
98
99 if (parentContainer != null && parentContainer instanceof Message && ((Message) parentContainer)
100 .isRenderWrapperTag()) {
101 parentContainerId = ((Component) parentContainer).getId();
102 }
103
104
105 if (parentContainer != null && parentContainer instanceof Component && StringUtils.isBlank(parentContainerId)) {
106 parentContext = ((Component) parentContainer).getContext();
107 parentContainer = parentContext == null ? null : parentContext
108 .get(UifConstants.ContextVariableNames.PARENT);
109 if (parentContainer != null && (parentContainer instanceof Container
110 || parentContainer instanceof FieldGroup)) {
111 parentContainerId = ((Component) parentContainer).getId();
112 }
113 }
114
115 if ((parent.getDataAttributes() == null) || (parent.getDataAttributes().get(UifConstants.DataAttributes.PARENT)
116 == null)) {
117 parent.addDataAttribute(UifConstants.DataAttributes.PARENT, parentContainerId);
118 }
119
120
121
122 if (parentContainer != null && parentContainer instanceof FieldGroup) {
123 masterKeyList.add(parentContainerId);
124 }
125
126 for (String key : masterKeyList) {
127 errors.addAll(getMessages(view, key, messageMap.getErrorMessagesForProperty(key, true)));
128 warnings.addAll(getMessages(view, key, messageMap.getWarningMessagesForProperty(key, true)));
129 infos.addAll(getMessages(view, key, messageMap.getInfoMessagesForProperty(key, true)));
130 }
131 }
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146 protected List<String> getMessages(View view, String key, List<List<ErrorMessage>> lists) {
147 List<String> result = new ArrayList<String>();
148 for (List<ErrorMessage> errorList : lists) {
149 if (errorList != null && StringUtils.isNotBlank(key)) {
150 for (ErrorMessage e : errorList) {
151 String message = KRADUtils.getMessageText(e, true);
152 message = MessageStructureUtils.translateStringMessage(message);
153
154 result.add(message);
155 }
156 }
157 }
158
159 return result;
160 }
161
162
163
164
165
166
167
168
169
170
171 protected List<String> getKeys(Component parent) {
172 List<String> keyList = new ArrayList<String>();
173
174 if (additionalKeysToMatch != null) {
175 keyList.addAll(additionalKeysToMatch);
176 }
177
178 if (StringUtils.isNotBlank(parent.getId())) {
179 keyList.add(parent.getId());
180 }
181
182 if (parent instanceof InputField) {
183 if (((InputField) parent).getBindingInfo() != null && StringUtils.isNotEmpty(
184 ((InputField) parent).getBindingInfo().getBindingPath())) {
185 keyList.add(((InputField) parent).getBindingInfo().getBindingPath());
186 }
187 }
188
189 return keyList;
190 }
191
192
193
194
195
196
197
198
199 protected void addNestedGroupKeys(Collection<String> keyList, Component component) {
200 @SuppressWarnings("unchecked")
201 Queue<LifecycleElement> elementQueue = RecycleUtils.getInstance(LinkedList.class);
202 try {
203 elementQueue.addAll(ViewLifecycleUtils.getElementsForLifecycle(component).values());
204 while (!elementQueue.isEmpty()) {
205 LifecycleElement element = elementQueue.poll();
206
207 ValidationMessages ef = null;
208 if (element instanceof ContainerBase) {
209 ef = ((ContainerBase) element).getValidationMessages();
210 } else if (element instanceof FieldGroup) {
211 ef = ((FieldGroup) element).getGroup().getValidationMessages();
212 }
213
214 if (ef != null) {
215 keyList.addAll(ef.getKeys((Component) element));
216 }
217
218 elementQueue.addAll(ViewLifecycleUtils.getElementsForLifecycle(element).values());
219 }
220 } finally {
221 elementQueue.clear();
222 RecycleUtils.recycle(elementQueue);
223 }
224 }
225
226
227
228
229
230
231
232
233
234 @BeanTagAttribute(name = "additionalKeysToMatch", type = BeanTagAttribute.AttributeType.LISTVALUE)
235 public List<String> getAdditionalKeysToMatch() {
236 return this.additionalKeysToMatch;
237 }
238
239
240
241
242
243
244
245 public void setAdditionalKeysToMatch(String additionalKeysToMatch) {
246 if (StringUtils.isNotBlank(additionalKeysToMatch)) {
247 this.additionalKeysToMatch = Arrays.asList(StringUtils.split(additionalKeysToMatch, ","));
248 }
249 }
250
251
252
253
254 public void setAdditionalKeysToMatch(List<String> additionalKeysToMatch) {
255 this.additionalKeysToMatch = additionalKeysToMatch;
256 }
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271 @BeanTagAttribute(name = "displayMessages")
272 public boolean isDisplayMessages() {
273 return this.displayMessages;
274 }
275
276
277
278
279 public void setDisplayMessages(boolean displayMessages) {
280 this.displayMessages = displayMessages;
281 }
282
283
284
285
286
287
288
289 @BeanTagAttribute(name = "errors", type = BeanTagAttribute.AttributeType.LISTVALUE)
290 public List<String> getErrors() {
291 return this.errors;
292 }
293
294
295
296
297
298
299
300 @BeanTagAttribute(name = "warnings", type = BeanTagAttribute.AttributeType.LISTVALUE)
301 public List<String> getWarnings() {
302 return this.warnings;
303 }
304
305
306
307
308
309
310
311 @BeanTagAttribute(name = "infos", type = BeanTagAttribute.AttributeType.LISTVALUE)
312 public List<String> getInfos() {
313 return this.infos;
314 }
315
316
317
318
319
320
321
322
323
324
325
326 protected void addValidationDataSettingsValue(Map<String, Object> valueMap, Map<String, String> defaults,
327 String key, Object value) {
328 String defaultValue = defaults.get(key);
329 if ((defaultValue != null && !value.toString().equals(defaultValue)) || (defaultValue != null && defaultValue
330 .equals("[]") && value instanceof List && !((List) value).isEmpty()) || defaultValue == null) {
331 valueMap.put(key, value);
332 }
333 }
334
335
336
337
338
339
340 protected void setErrors(List<String> errors) {
341 this.errors = errors;
342 }
343
344
345
346
347
348
349 protected void setWarnings(List<String> warnings) {
350 this.warnings = warnings;
351 }
352
353
354
355
356
357
358 protected void setInfos(List<String> infos) {
359 this.infos = infos;
360 }
361
362 }