1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.uif.field;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.core.api.config.property.ConfigurationService;
20 import org.kuali.rice.krad.service.KRADServiceLocator;
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.view.View;
24 import org.kuali.rice.krad.uif.component.Component;
25 import org.kuali.rice.krad.util.ErrorMessage;
26 import org.kuali.rice.krad.util.GlobalVariables;
27 import org.kuali.rice.krad.util.MessageMap;
28 import org.springframework.util.AutoPopulatingList;
29
30 import java.text.MessageFormat;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.LinkedHashSet;
34 import java.util.List;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 public class ErrorsField extends FieldBase {
50 private static final long serialVersionUID = 780940788435330077L;
51
52 private List<String> additionalKeysToMatch;
53
54 private boolean fireGrowlsForMessages;
55 private String growlScript = "";
56
57
58 private String errorTitle;
59 private String warningTitle;
60 private String infoTitle;
61
62 private boolean displayErrorTitle;
63 private boolean displayWarningTitle;
64 private boolean displayInfoTitle;
65
66
67 private boolean highlightOnError;
68 private boolean displayFieldErrorIcon;
69
70
71 private boolean displayFieldLabelWithMessages;
72 private boolean combineMessages;
73
74
75 private boolean displayNestedMessages;
76 private boolean allowMessageRepeat;
77
78 private boolean displayMessages;
79 private boolean displayErrorMessages;
80 private boolean displayInfoMessages;
81 private boolean displayWarningMessages;
82 private boolean displayCounts;
83 private boolean alternateContainer;
84
85
86 private List<String> errors;
87 private List<String> warnings;
88 private List<String> infos;
89
90
91 private int errorCount;
92 private int warningCount;
93 private int infoCount;
94
95
96 private boolean displayLockMessages;
97
98 public ErrorsField() {
99 super();
100 alternateContainer = false;
101 }
102
103
104
105
106
107
108
109
110
111
112
113 @Override
114 public void performFinalize(View view, Object model, Component parent) {
115 super.performFinalize(view, model, parent);
116
117 generateMessages(true, view, model, parent);
118 }
119
120 public void generateMessages(boolean reset, View view, Object model, Component parent) {
121 if (reset) {
122 errors = new ArrayList<String>();
123 warnings = new ArrayList<String>();
124 infos = new ArrayList<String>();
125 errorCount = 0;
126 warningCount = 0;
127 infoCount = 0;
128 }
129
130 List<String> masterKeyList = getKeys(parent);
131 MessageMap messageMap = GlobalVariables.getMessageMap();
132
133
134 if (!displayFieldLabelWithMessages) {
135 this.addStyleClass("noLabels");
136 }
137 if (!highlightOnError) {
138 this.addStyleClass("noHighlight");
139 }
140 if (displayFieldErrorIcon) {
141 this.addStyleClass("addFieldIcon");
142 }
143
144 if (displayMessages) {
145 if (displayNestedMessages) {
146 this.addNestedKeys(masterKeyList, parent);
147 }
148
149 for (String key : masterKeyList) {
150 if (displayErrorMessages) {
151 errors.addAll(getMessages(view, key, messageMap.getErrorMessagesForProperty(key, true)));
152 }
153 if (displayWarningMessages) {
154 warnings.addAll(getMessages(view, key, messageMap.getWarningMessagesForProperty(key, true)));
155 }
156 if (displayInfoMessages) {
157 infos.addAll(getMessages(view, key, messageMap.getInfoMessagesForProperty(key, true)));
158 }
159 }
160 } else if (displayFieldErrorIcon) {
161
162
163
164
165 for (String key : masterKeyList) {
166 if (!messageMap.getErrorMessagesForProperty(key, true).isEmpty()) {
167 errorCount = 1;
168 break;
169 }
170 }
171 }
172
173
174 if (parent instanceof PageGroup) {
175 if (errorCount < messageMap.getErrorCount()) {
176 List<String> diff = messageMap.getPropertiesWithErrors();
177 diff.removeAll(masterKeyList);
178 for (String key : diff) {
179 errors.addAll(getMessages(view, key, messageMap.getErrorMessagesForProperty(key, true)));
180 }
181
182 }
183 if (warningCount < messageMap.getWarningCount()) {
184 List<String> diff = messageMap.getPropertiesWithWarnings();
185 diff.removeAll(masterKeyList);
186 for (String key : diff) {
187 warnings.addAll(getMessages(view, key, messageMap.getWarningMessagesForProperty(key, true)));
188 }
189 }
190 if (infoCount < messageMap.getInfoCount()) {
191 List<String> diff = messageMap.getPropertiesWithInfo();
192 diff.removeAll(masterKeyList);
193 for (String key : diff) {
194 infos.addAll(getMessages(view, key, messageMap.getInfoMessagesForProperty(key, true)));
195 }
196 }
197
198
199 this.setId("errorsFieldForPage");
200 }
201
202 if (fireGrowlsForMessages) {
203
204 growlScript = getGrowlScript(view);
205 }
206
207
208 errors = new ArrayList<String>(new LinkedHashSet<String>(errors));
209 warnings = new ArrayList<String>(new LinkedHashSet<String>(warnings));
210 infos = new ArrayList<String>(new LinkedHashSet<String>(infos));
211
212 errorCount = errors.size();
213 warningCount = warnings.size();
214 infoCount = infos.size();
215
216
217 if (errorCount + warningCount + infoCount == 0 || !displayMessages) {
218
219 this.setStyle("display: none;");
220 } else {
221 this.setStyle("display: visible");
222 }
223 }
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238 private List<String> getMessages(View view, String key, List<AutoPopulatingList<ErrorMessage>> lists) {
239 List<String> result = new ArrayList<String>();
240 for (List<ErrorMessage> errorList : lists) {
241 if (errorList != null && StringUtils.isNotBlank(key)) {
242 ConfigurationService configService = KRADServiceLocator.getKualiConfigurationService();
243 String comboMessage = "";
244 String label = "";
245
246 for (ErrorMessage e : errorList) {
247 String message = configService.getPropertyValueAsString(e.getErrorKey());
248 if (message == null) {
249 message = "Intended message with key: " + e.getErrorKey() + " not found.";
250 }
251 if (e.getMessageParameters() != null) {
252 message = message.replace("'", "''");
253 message = MessageFormat.format(message, (Object[]) e.getMessageParameters());
254 }
255 if (displayFieldLabelWithMessages) {
256 InputField field = (InputField) view.getViewIndex().getDataFieldByPath(key);
257 if (field != null && field.getLabel() != null) {
258 label = field.getLabel();
259 }
260 }
261
262
263 if (combineMessages) {
264 if (comboMessage.isEmpty()) {
265 comboMessage = message;
266 } else {
267 comboMessage = comboMessage + ", " + message;
268 }
269 } else {
270
271 if (StringUtils.isNotEmpty(label)) {
272 result.add(label + " - " + message);
273 } else {
274 result.add(message);
275 }
276
277 }
278 }
279
280
281
282
283 if (StringUtils.isNotEmpty(comboMessage)) {
284 if (StringUtils.isNotEmpty(label)) {
285 result.add(label + " - " + comboMessage);
286 } else {
287 result.add(comboMessage);
288 }
289 }
290 }
291 }
292
293 return result;
294 }
295
296
297
298
299
300
301
302
303
304
305 protected List<String> getKeys(Component parent) {
306 List<String> keyList = new ArrayList<String>();
307 if (additionalKeysToMatch != null) {
308 keyList.addAll(additionalKeysToMatch);
309 }
310 if (StringUtils.isNotBlank(parent.getId())) {
311 keyList.add(parent.getId());
312 }
313 if (parent instanceof InputField) {
314 if (((InputField) parent).getBindingInfo() != null && StringUtils.isNotEmpty(
315 ((InputField) parent).getBindingInfo().getBindingPath())) {
316 keyList.add(((InputField) parent).getBindingInfo().getBindingPath());
317 }
318 }
319
320
321 return keyList;
322 }
323
324
325
326
327
328
329
330
331
332
333 private void addNestedKeys(List<String> keyList, Component component) {
334 for (Component c : component.getComponentsForLifecycle()) {
335 ErrorsField ef = null;
336 if (c instanceof InputField) {
337 ef = ((InputField) c).getErrorsField();
338 } else if (c instanceof ContainerBase) {
339 ef = ((ContainerBase) c).getErrorsField();
340 }
341 if (ef != null) {
342 if (!allowMessageRepeat) {
343 ef.setDisplayMessages(false);
344 }
345 keyList.addAll(ef.getKeys(c));
346 addNestedKeys(keyList, c);
347 }
348 }
349 }
350
351
352
353
354
355
356
357 public String getErrorTitle() {
358 return this.errorTitle;
359 }
360
361 public void setErrorTitle(String errorTitle) {
362 this.errorTitle = errorTitle;
363 }
364
365
366
367
368
369
370
371 public String getWarningTitle() {
372 return this.warningTitle;
373 }
374
375 public void setWarningTitle(String warningTitle) {
376 this.warningTitle = warningTitle;
377 }
378
379
380
381
382
383
384
385 public String getInfoTitle() {
386 return this.infoTitle;
387 }
388
389 public void setInfoTitle(String infoTitle) {
390 this.infoTitle = infoTitle;
391 }
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407 public boolean isDisplayErrorMessages() {
408 return this.displayErrorMessages;
409 }
410
411 public void setDisplayErrorMessages(boolean displayErrorMessages) {
412 this.displayErrorMessages = displayErrorMessages;
413 }
414
415
416
417
418
419
420
421
422 public boolean isDisplayInfoMessages() {
423 return this.displayInfoMessages;
424 }
425
426 public void setDisplayInfoMessages(boolean displayInfoMessages) {
427 this.displayInfoMessages = displayInfoMessages;
428 }
429
430 public boolean isDisplayLockMessages() {
431 return this.displayLockMessages;
432 }
433
434
435
436
437
438
439 public void setDisplayLockMessages(boolean displayLockMessages) {
440 this.displayLockMessages = displayLockMessages;
441 }
442
443
444
445
446
447
448
449
450 public boolean isDisplayWarningMessages() {
451 return this.displayWarningMessages;
452 }
453
454 public void setDisplayWarningMessages(boolean displayWarningMessages) {
455 this.displayWarningMessages = displayWarningMessages;
456 }
457
458
459
460
461
462
463
464
465
466 public List<String> getAdditionalKeysToMatch() {
467 return this.additionalKeysToMatch;
468 }
469
470
471
472
473
474
475
476 public void setAdditionalKeysToMatch(String additionalKeysToMatch) {
477 if (StringUtils.isNotBlank(additionalKeysToMatch)) {
478 this.additionalKeysToMatch = Arrays.asList(StringUtils.split(additionalKeysToMatch, ","));
479 }
480 }
481
482
483
484
485 public void setAdditionalKeysToMatch(List<String> additionalKeysToMatch) {
486 this.additionalKeysToMatch = additionalKeysToMatch;
487 }
488
489
490
491
492
493
494
495 public boolean isDisplayErrorTitle() {
496 return this.displayErrorTitle;
497 }
498
499
500
501
502 public void setDisplayErrorTitle(boolean displayErrorTitle) {
503 this.displayErrorTitle = displayErrorTitle;
504 }
505
506
507
508
509
510
511
512 public boolean isDisplayWarningTitle() {
513 return this.displayWarningTitle;
514 }
515
516
517
518
519 public void setDisplayWarningTitle(boolean displayWarningTitle) {
520 this.displayWarningTitle = displayWarningTitle;
521 }
522
523
524
525
526
527
528
529 public boolean isDisplayInfoTitle() {
530 return this.displayInfoTitle;
531 }
532
533
534
535
536 public void setDisplayInfoTitle(boolean displayInfoTitle) {
537 this.displayInfoTitle = displayInfoTitle;
538 }
539
540
541
542
543
544
545
546
547
548 public boolean isDisplayFieldLabelWithMessages() {
549 return this.displayFieldLabelWithMessages;
550 }
551
552
553
554
555 public void setDisplayFieldLabelWithMessages(boolean displayFieldLabelWithMessages) {
556 this.displayFieldLabelWithMessages = displayFieldLabelWithMessages;
557 }
558
559
560
561
562
563
564
565
566
567 public boolean isDisplayMessages() {
568 return this.displayMessages;
569 }
570
571
572
573
574 public void setDisplayMessages(boolean displayMessages) {
575 this.displayMessages = displayMessages;
576 }
577
578
579
580
581
582
583
584
585
586 public boolean isDisplayNestedMessages() {
587 return this.displayNestedMessages;
588 }
589
590
591
592
593 public void setDisplayNestedMessages(boolean displayNestedMessages) {
594 this.displayNestedMessages = displayNestedMessages;
595 }
596
597
598
599
600
601
602
603 public boolean isCombineMessages() {
604 return this.combineMessages;
605 }
606
607
608
609
610 public void setCombineMessages(boolean combineMessages) {
611 this.combineMessages = combineMessages;
612 }
613
614
615
616
617
618
619
620
621
622
623
624
625 public boolean isAllowMessageRepeat() {
626 return this.allowMessageRepeat;
627 }
628
629
630
631
632 public void setAllowMessageRepeat(boolean allowMessageRepeat) {
633 this.allowMessageRepeat = allowMessageRepeat;
634 }
635
636
637
638
639
640
641
642
643 public boolean isDisplayCounts() {
644 return this.displayCounts;
645 }
646
647
648
649
650 public void setDisplayCounts(boolean displayCounts) {
651 this.displayCounts = displayCounts;
652 }
653
654
655
656
657
658
659
660 public List<String> getErrors() {
661 return this.errors;
662 }
663
664
665
666
667
668
669
670 public List<String> getWarnings() {
671 return this.warnings;
672 }
673
674
675
676
677
678
679
680 public List<String> getInfos() {
681 return this.infos;
682 }
683
684
685
686
687
688
689
690 public int getErrorCount() {
691 return this.errorCount;
692 }
693
694
695
696
697
698
699
700 public int getWarningCount() {
701 return this.warningCount;
702 }
703
704
705
706
707
708
709
710 public int getInfoCount() {
711 return this.infoCount;
712 }
713
714
715
716
717
718
719
720
721
722
723
724
725
726 public boolean isAlternateContainer() {
727 return this.alternateContainer;
728 }
729
730
731
732
733 public void setAlternateContainer(boolean alternateContainer) {
734 this.alternateContainer = alternateContainer;
735 }
736
737
738
739
740
741
742
743
744
745
746
747 public boolean isDisplayFieldErrorIcon() {
748 return this.displayFieldErrorIcon;
749 }
750
751
752
753
754 public void setDisplayFieldErrorIcon(boolean displayFieldErrorIcon) {
755 this.displayFieldErrorIcon = displayFieldErrorIcon;
756 }
757
758
759
760
761
762
763
764
765
766 public void setHighlightOnError(boolean highlightOnError) {
767 this.highlightOnError = highlightOnError;
768 }
769
770
771
772
773 public boolean isHighlightOnError() {
774 return highlightOnError;
775 }
776
777 private String getGrowlScript(View view) {
778
779
780 String growlScript = "";
781 if (view.isGrowlMessagingEnabled()) {
782 ConfigurationService configService = KRADServiceLocator.getKualiConfigurationService();
783 MessageMap messageMap = GlobalVariables.getMessageMap();
784 if (messageMap.hasErrors()) {
785 String message = configService.getPropertyValueAsString("growl.hasErrors");
786 if (StringUtils.isNotBlank(message)) {
787 growlScript =
788 growlScript + "showGrowl('" + message + "', '" + configService.getPropertyValueAsString(
789 "general.error") + "', 'errorGrowl');";
790 }
791 }
792
793 if (messageMap.hasWarnings()) {
794 String message = configService.getPropertyValueAsString("growl.hasWarnings");
795 if (StringUtils.isNotBlank(message)) {
796 growlScript =
797 growlScript + "showGrowl('" + message + "', '" + configService.getPropertyValueAsString(
798 "general.warning") + "', 'warningGrowl');";
799 }
800 }
801
802 if (messageMap.hasInfo()) {
803 List<String> properties = messageMap.getPropertiesWithInfo();
804 String message = "";
805 for (String property : properties) {
806 List<AutoPopulatingList<ErrorMessage>> lists = messageMap.getInfoMessagesForProperty(property,
807 true);
808 for (List<ErrorMessage> errorList : lists) {
809 if (errorList != null) {
810 for (ErrorMessage e : errorList) {
811 if (StringUtils.isBlank(message)) {
812 message = configService.getPropertyValueAsString(e.getErrorKey());
813 } else {
814 message = message + "<br/>" + configService.getPropertyValueAsString(
815 e.getErrorKey());
816 }
817 if (e.getMessageParameters() != null) {
818 message = message.replace("'", "''");
819 message = MessageFormat.format(message, (Object[]) e.getMessageParameters());
820 }
821 }
822 }
823 }
824 }
825
826 if (StringUtils.isNotBlank(message)) {
827 growlScript =
828 growlScript + "showGrowl('" + message + "', '" + configService.getPropertyValueAsString(
829 "general.info") + "', 'infoGrowl');";
830 }
831 }
832 }
833 return growlScript;
834 }
835
836 public boolean isFireGrowlsForMessages() {
837 return fireGrowlsForMessages;
838 }
839
840 public void setFireGrowlsForMessages(boolean fireGrowlsForMessages) {
841 this.fireGrowlsForMessages = fireGrowlsForMessages;
842 }
843
844 public String getGrowlScript() {
845 return growlScript;
846 }
847 }