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