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