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