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