1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.web.form;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.codehaus.jackson.map.ObjectMapper;
22 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
23 import org.kuali.rice.krad.uif.UifConstants;
24 import org.kuali.rice.krad.uif.UifConstants.ViewType;
25 import org.kuali.rice.krad.uif.UifParameters;
26 import org.kuali.rice.krad.uif.service.ViewService;
27 import org.kuali.rice.krad.uif.util.SessionTransient;
28 import org.kuali.rice.krad.uif.view.DialogManager;
29 import org.kuali.rice.krad.uif.view.View;
30 import org.kuali.rice.krad.uif.view.ViewModel;
31 import org.kuali.rice.krad.util.KRADUtils;
32 import org.springframework.web.multipart.MultipartFile;
33
34 import javax.servlet.http.HttpServletRequest;
35 import java.io.IOException;
36 import java.util.ArrayList;
37 import java.util.HashMap;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.Properties;
41 import java.util.Set;
42 import java.util.UUID;
43
44
45
46
47
48
49
50
51
52
53
54 public class UifFormBase implements ViewModel {
55 private static final long serialVersionUID = 8432543267099454434L;
56
57
58 private static final Log LOG = LogFactory.getLog(UifFormBase.class);
59
60
61 protected String viewId;
62 protected String viewName;
63 protected ViewType viewTypeName;
64 protected String pageId;
65 protected String methodToCall;
66 protected String formKey;
67 @SessionTransient
68 protected String requestedFormKey;
69 protected String flowKey;
70 protected String sessionId;
71 protected int sessionTimeoutInterval;
72
73 @SessionTransient
74 protected HistoryFlow historyFlow;
75 @SessionTransient
76 protected HistoryManager historyManager;
77
78 @SessionTransient
79 protected String jumpToId;
80 @SessionTransient
81 protected String jumpToName;
82 @SessionTransient
83 protected String focusId;
84 @SessionTransient
85 protected boolean dirtyForm;
86
87 protected String formPostUrl;
88 protected String controllerMapping;
89
90 @SessionTransient
91 private String requestUrl;
92 private Map<String, String> initialRequestParameters;
93
94 protected String state;
95 protected boolean defaultsApplied;
96 protected boolean renderedInLightBox;
97
98 @SessionTransient
99 protected String growlScript;
100 @SessionTransient
101 protected String lightboxScript;
102
103 protected View view;
104 protected View postedView;
105
106 protected Map<String, String> viewRequestParameters;
107 protected List<String> readOnlyFieldsList;
108 protected Map<String, Object> newCollectionLines;
109
110 @SessionTransient
111 protected Map<String, String> actionParameters;
112 protected Map<String, Object> clientStateForSyncing;
113 @SessionTransient
114 protected Map<String, Set<String>> selectedCollectionLines;
115
116 protected List<Object> addedCollectionItems;
117
118 @SessionTransient
119 protected MultipartFile attachmentFile;
120
121
122 protected String returnLocation;
123 protected String returnFormKey;
124
125 @SessionTransient
126 protected boolean ajaxRequest;
127 @SessionTransient
128 protected String ajaxReturnType;
129 @SessionTransient
130 private String requestJsonTemplate;
131
132
133 @SessionTransient
134 protected String dialogExplanation;
135 @SessionTransient
136 protected String dialogResponse;
137 protected DialogManager dialogManager;
138
139 @SessionTransient
140 protected boolean requestRedirected;
141 @SessionTransient
142 protected String updateComponentId;
143
144 protected Map<String, Object> extensionData;
145
146 public UifFormBase() {
147 defaultsApplied = false;
148 renderedInLightBox = false;
149 requestRedirected = false;
150
151 readOnlyFieldsList = new ArrayList<String>();
152 viewRequestParameters = new HashMap<String, String>();
153 newCollectionLines = new HashMap<String, Object>();
154 actionParameters = new HashMap<String, String>();
155 clientStateForSyncing = new HashMap<String, Object>();
156 selectedCollectionLines = new HashMap<String, Set<String>>();
157 addedCollectionItems = new ArrayList();
158 dialogManager = new DialogManager();
159 extensionData = new HashMap<String, Object>();
160 }
161
162
163
164
165 @Override
166 public void postBind(HttpServletRequest request) {
167
168 UifFormManager uifFormManager = (UifFormManager) request.getSession().getAttribute(UifParameters.FORM_MANAGER);
169 if (StringUtils.isBlank(formKey) || !uifFormManager.hasSessionForm(formKey)) {
170 formKey = generateFormKey();
171 }
172
173
174 formPostUrl = request.getRequestURL().toString();
175
176 if (request.getSession() != null) {
177 sessionId = request.getSession().getId();
178 sessionTimeoutInterval = request.getSession().getMaxInactiveInterval();
179 }
180
181
182 controllerMapping = request.getPathInfo();
183
184
185 if (request.getParameterMap().containsKey(UifParameters.CLIENT_VIEW_STATE)) {
186 String clientStateJSON = request.getParameter(UifParameters.CLIENT_VIEW_STATE);
187 if (StringUtils.isNotBlank(clientStateJSON)) {
188
189 clientStateJSON = StringUtils.replace(clientStateJSON, "'", "\"");
190
191 ObjectMapper mapper = new ObjectMapper();
192 try {
193 clientStateForSyncing = mapper.readValue(clientStateJSON, Map.class);
194 } catch (IOException e) {
195 throw new RuntimeException("Unable to decode client side state JSON", e);
196 }
197 }
198 }
199
200
201 if (request.getParameter(UifParameters.READ_ONLY_FIELDS) != null) {
202 String readOnlyFields = request.getParameter(UifParameters.READ_ONLY_FIELDS);
203 setReadOnlyFieldsList(KRADUtils.convertStringParameterToList(readOnlyFields));
204 }
205
206
207 this.pageId = KRADUtils.stripXSSPatterns(this.pageId);
208 this.methodToCall = KRADUtils.stripXSSPatterns(this.methodToCall);
209 this.formKey = KRADUtils.stripXSSPatterns(this.formKey);
210 this.requestedFormKey = KRADUtils.stripXSSPatterns(this.requestedFormKey);
211 this.flowKey = KRADUtils.stripXSSPatterns(this.flowKey);
212 this.sessionId = KRADUtils.stripXSSPatterns(this.sessionId);
213 this.formPostUrl = KRADUtils.stripXSSPatterns(this.formPostUrl);
214 this.returnLocation = KRADUtils.stripXSSPatterns(this.returnLocation);
215 this.returnFormKey = KRADUtils.stripXSSPatterns(this.returnFormKey);
216 this.requestUrl = KRADUtils.stripXSSPatterns(this.requestUrl);
217 }
218
219
220
221
222
223
224
225 protected String generateFormKey() {
226 return UUID.randomUUID().toString();
227 }
228
229
230
231
232 @Override
233 public String getViewId() {
234 return this.viewId;
235 }
236
237
238
239
240 @Override
241 public void setViewId(String viewId) {
242 this.viewId = viewId;
243 }
244
245
246
247
248 @Override
249 public String getViewName() {
250 return this.viewName;
251 }
252
253
254
255
256 @Override
257 public void setViewName(String viewName) {
258 this.viewName = viewName;
259 }
260
261
262
263
264 @Override
265 public ViewType getViewTypeName() {
266 return this.viewTypeName;
267 }
268
269
270
271
272 @Override
273 public void setViewTypeName(ViewType viewTypeName) {
274 this.viewTypeName = viewTypeName;
275 }
276
277
278
279
280 @Override
281 public String getPageId() {
282 return this.pageId;
283 }
284
285
286
287
288 @Override
289 public void setPageId(String pageId) {
290 this.pageId = pageId;
291 }
292
293
294
295
296 @Override
297 public String getFormPostUrl() {
298 return this.formPostUrl;
299 }
300
301
302
303
304 @Override
305 public void setFormPostUrl(String formPostUrl) {
306 this.formPostUrl = formPostUrl;
307 }
308
309
310
311
312
313
314 public String getControllerMapping() {
315 return controllerMapping;
316 }
317
318
319
320
321
322
323
324 public HistoryFlow getHistoryFlow() {
325 return historyFlow;
326 }
327
328
329
330
331
332
333 public void setHistoryFlow(HistoryFlow historyFlow) {
334 this.historyFlow = historyFlow;
335 }
336
337
338
339
340
341
342
343
344 public HistoryManager getHistoryManager() {
345 return historyManager;
346 }
347
348
349
350
351
352
353 public void setHistoryManager(HistoryManager historyManager) {
354 this.historyManager = historyManager;
355 }
356
357
358
359
360
361
362
363
364
365
366 public String getFlowKey() {
367 return flowKey;
368 }
369
370
371
372
373
374
375 public void setFlowKey(String flowKey) {
376 this.flowKey = flowKey;
377 }
378
379
380
381
382
383
384
385 public String getRequestUrl() {
386 return requestUrl;
387 }
388
389
390
391
392
393
394 public void setRequestUrl(String requestUrl) {
395 this.requestUrl = requestUrl;
396 }
397
398
399
400
401
402
403
404 public Map<String, String> getInitialRequestParameters() {
405 return initialRequestParameters;
406 }
407
408
409
410
411
412
413 public void setInitialRequestParameters(Map<String, String> requestParameters) {
414 this.initialRequestParameters = requestParameters;
415 }
416
417 public String getReturnLocation() {
418 return this.returnLocation;
419 }
420
421 public void setReturnLocation(String returnLocation) {
422 this.returnLocation = returnLocation;
423 }
424
425 public String getReturnFormKey() {
426 return this.returnFormKey;
427 }
428
429 public void setReturnFormKey(String returnFormKey) {
430 this.returnFormKey = returnFormKey;
431 }
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447 public String getSessionId() {
448 return sessionId;
449 }
450
451
452
453
454
455
456
457
458
459
460
461
462 public int getSessionTimeoutInterval() {
463 return sessionTimeoutInterval;
464 }
465
466
467
468
469
470
471
472
473 public String getMethodToCall() {
474 return this.methodToCall;
475 }
476
477
478
479
480
481
482 public void setMethodToCall(String methodToCall) {
483 this.methodToCall = methodToCall;
484 }
485
486
487
488
489 @Override
490 public Map<String, String> getViewRequestParameters() {
491 return this.viewRequestParameters;
492 }
493
494
495
496
497 @Override
498 public void setViewRequestParameters(Map<String, String> viewRequestParameters) {
499 this.viewRequestParameters = viewRequestParameters;
500 }
501
502
503
504
505 @Override
506 public List<String> getReadOnlyFieldsList() {
507 return readOnlyFieldsList;
508 }
509
510
511
512
513 @Override
514 public void setReadOnlyFieldsList(List<String> readOnlyFieldsList) {
515 this.readOnlyFieldsList = readOnlyFieldsList;
516 }
517
518
519
520
521 @Override
522 public Map<String, Object> getNewCollectionLines() {
523 return this.newCollectionLines;
524 }
525
526
527
528
529 @Override
530 public void setNewCollectionLines(Map<String, Object> newCollectionLines) {
531 this.newCollectionLines = newCollectionLines;
532 }
533
534
535
536
537 @Override
538 public Map<String, String> getActionParameters() {
539 return this.actionParameters;
540 }
541
542
543
544
545
546
547 public Properties getActionParametersAsProperties() {
548 return KRADUtils.convertMapToProperties(actionParameters);
549 }
550
551
552
553
554 @Override
555 public void setActionParameters(Map<String, String> actionParameters) {
556 this.actionParameters = actionParameters;
557 }
558
559
560
561
562
563
564
565
566 public String getActionParamaterValue(String actionParameterName) {
567 if ((actionParameters != null) && actionParameters.containsKey(actionParameterName)) {
568 return actionParameters.get(actionParameterName);
569 }
570
571 return "";
572 }
573
574
575
576
577
578
579
580
581
582
583
584
585
586 public String getActionEvent() {
587 if ((actionParameters != null) && actionParameters.containsKey(UifConstants.UrlParams.ACTION_EVENT)) {
588 return actionParameters.get(UifConstants.UrlParams.ACTION_EVENT);
589 }
590
591 return "";
592 }
593
594
595
596
597 @Override
598 public Map<String, Object> getClientStateForSyncing() {
599 return clientStateForSyncing;
600 }
601
602
603
604
605
606
607 public void setClientStateForSyncing(Map<String, Object> clientStateForSyncing) {
608 this.clientStateForSyncing = clientStateForSyncing;
609 }
610
611
612
613
614 @Override
615 public Map<String, Set<String>> getSelectedCollectionLines() {
616 return selectedCollectionLines;
617 }
618
619
620
621
622 @Override
623 public void setSelectedCollectionLines(Map<String, Set<String>> selectedCollectionLines) {
624 this.selectedCollectionLines = selectedCollectionLines;
625 }
626
627
628
629
630
631
632
633
634
635
636
637
638 public String getFormKey() {
639 return this.formKey;
640 }
641
642
643
644
645
646
647 public void setFormKey(String formKey) {
648 this.formKey = formKey;
649 }
650
651
652
653
654
655
656
657 public String getRequestedFormKey() {
658 return requestedFormKey;
659 }
660
661
662
663
664
665
666 public void setRequestedFormKey(String requestedFormKey) {
667 this.requestedFormKey = requestedFormKey;
668 }
669
670
671
672
673 @Override
674 public boolean isDefaultsApplied() {
675 return this.defaultsApplied;
676 }
677
678
679
680
681 @Override
682 public void setDefaultsApplied(boolean defaultsApplied) {
683 this.defaultsApplied = defaultsApplied;
684 }
685
686
687
688
689
690
691 public boolean isRequestRedirected() {
692 return requestRedirected;
693 }
694
695
696
697
698
699
700 public void setRequestRedirected(boolean requestRedirected) {
701 this.requestRedirected = requestRedirected;
702 }
703
704
705
706
707
708
709 public MultipartFile getAttachmentFile() {
710 return this.attachmentFile;
711 }
712
713
714
715
716
717
718 public void setAttachmentFile(MultipartFile attachmentFile) {
719 this.attachmentFile = attachmentFile;
720 }
721
722
723
724
725 public String getUpdateComponentId() {
726 return updateComponentId;
727 }
728
729
730
731
732 public void setUpdateComponentId(String updateComponentId) {
733 this.updateComponentId = updateComponentId;
734 }
735
736
737
738
739 @Override
740 public View getView() {
741 return this.view;
742 }
743
744
745
746
747 @Override
748 public void setView(View view) {
749 this.view = view;
750 }
751
752
753
754
755 @Override
756 public View getPostedView() {
757 return this.postedView;
758 }
759
760
761
762
763 @Override
764 public void setPostedView(View postedView) {
765 this.postedView = postedView;
766 }
767
768
769
770
771
772
773
774 protected ViewService getViewService() {
775 return KRADServiceLocatorWeb.getViewService();
776 }
777
778
779
780
781
782
783
784
785
786 public String getJumpToId() {
787 return this.jumpToId;
788 }
789
790
791
792
793 public void setJumpToId(String jumpToId) {
794 this.jumpToId = jumpToId;
795 }
796
797
798
799
800
801
802
803
804 public String getJumpToName() {
805 return this.jumpToName;
806 }
807
808
809
810
811 public void setJumpToName(String jumpToName) {
812 this.jumpToName = jumpToName;
813 }
814
815
816
817
818
819
820
821 public String getFocusId() {
822 return this.focusId;
823 }
824
825
826
827
828 public void setFocusId(String focusId) {
829 this.focusId = focusId;
830 }
831
832
833
834
835
836
837
838
839
840
841
842
843 public boolean isDirtyForm() {
844 return dirtyForm;
845 }
846
847
848
849
850
851
852
853
854
855
856
857
858 public void setDirtyForm(boolean dirtyForm) {
859 this.dirtyForm = dirtyForm;
860 }
861
862
863
864
865
866
867 public void setDirtyForm(String dirtyForm) {
868 if(dirtyForm != null){
869 this.dirtyForm = Boolean.parseBoolean(dirtyForm);
870 }
871 }
872
873
874
875
876
877
878
879
880
881
882
883
884 public boolean isRenderedInLightBox() {
885 return this.renderedInLightBox;
886 }
887
888
889
890
891
892
893 public void setRenderedInLightBox(boolean renderedInLightBox) {
894 this.renderedInLightBox = renderedInLightBox;
895 }
896
897
898
899
900 @Override
901 public String getGrowlScript() {
902 return growlScript;
903 }
904
905
906
907
908 @Override
909 public void setGrowlScript(String growlScript) {
910 this.growlScript = growlScript;
911 }
912
913
914
915
916 public String getState() {
917 return state;
918 }
919
920
921
922
923 public void setState(String state) {
924 this.state = state;
925 }
926
927
928
929
930 @Override
931 public String getLightboxScript() {
932 return lightboxScript;
933 }
934
935
936
937
938 @Override
939 public void setLightboxScript(String lightboxScript) {
940 this.lightboxScript = lightboxScript;
941 }
942
943
944
945
946 @Override
947 public boolean isAjaxRequest() {
948 return ajaxRequest;
949 }
950
951
952
953
954 @Override
955 public void setAjaxRequest(boolean ajaxRequest) {
956 this.ajaxRequest = ajaxRequest;
957 }
958
959
960
961
962 @Override
963 public String getAjaxReturnType() {
964 return ajaxReturnType;
965 }
966
967
968
969
970 @Override
971 public boolean isUpdateComponentRequest() {
972 return isAjaxRequest() && StringUtils.isNotBlank(getAjaxReturnType()) && getAjaxReturnType().equals(
973 UifConstants.AjaxReturnTypes.UPDATECOMPONENT.getKey());
974 }
975
976
977
978
979 @Override
980 public boolean isUpdateDialogRequest() {
981 return isAjaxRequest() && StringUtils.isNotBlank(getAjaxReturnType()) && getAjaxReturnType().equals(
982 UifConstants.AjaxReturnTypes.UPDATEDIALOG.getKey());
983 }
984
985
986
987
988 @Override
989 public boolean isUpdatePageRequest() {
990 return isAjaxRequest() && StringUtils.isNotBlank(getAjaxReturnType()) && getAjaxReturnType().equals(
991 UifConstants.AjaxReturnTypes.UPDATEPAGE.getKey());
992 }
993
994
995
996
997 @Override
998 public boolean isUpdateNoneRequest() {
999 return isAjaxRequest() && StringUtils.isNotBlank(getAjaxReturnType()) && getAjaxReturnType().equals(
1000 UifConstants.AjaxReturnTypes.UPDATENONE.getKey());
1001 }
1002
1003
1004
1005
1006 @Override
1007 public boolean isBuildViewRequest() {
1008 return !isAjaxRequest() || (StringUtils.isNotBlank(getAjaxReturnType()) && (getAjaxReturnType().equals(
1009 UifConstants.AjaxReturnTypes.UPDATEVIEW.getKey()) || isUpdatePageRequest()));
1010 }
1011
1012
1013
1014
1015 @Override
1016 public boolean isUpdateViewRequest() {
1017 return isAjaxRequest() &&
1018 StringUtils.isNotBlank(getAjaxReturnType()) &&
1019 (isUpdateComponentRequest() || getAjaxReturnType().equals(
1020 UifConstants.AjaxReturnTypes.DISPLAYLIGHTBOX.getKey()));
1021 }
1022
1023
1024
1025
1026 @Override
1027 public void setAjaxReturnType(String ajaxReturnType) {
1028 this.ajaxReturnType = ajaxReturnType;
1029 }
1030
1031
1032
1033
1034 public boolean isJsonRequest() {
1035 return StringUtils.isNotBlank(getRequestJsonTemplate());
1036 }
1037
1038
1039
1040
1041 public String getRequestJsonTemplate() {
1042 return requestJsonTemplate;
1043 }
1044
1045
1046
1047
1048 public void setRequestJsonTemplate(String requestJsonTemplate) {
1049 this.requestJsonTemplate = requestJsonTemplate;
1050 }
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061 public String getDialogExplanation() {
1062 return dialogExplanation;
1063 }
1064
1065
1066
1067
1068
1069
1070 public void setDialogExplanation(String dialogExplanation) {
1071 this.dialogExplanation = dialogExplanation;
1072 }
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084 public String getDialogResponse() {
1085 return dialogResponse;
1086 }
1087
1088
1089
1090
1091
1092
1093 public void setDialogResponse(String dialogResponse) {
1094 this.dialogResponse = dialogResponse;
1095 }
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106 public DialogManager getDialogManager() {
1107 return dialogManager;
1108 }
1109
1110
1111
1112
1113
1114
1115 public void setDialogManager(DialogManager dialogManager) {
1116 this.dialogManager = dialogManager;
1117 }
1118
1119
1120
1121
1122 public Map<String, Object> getExtensionData() {
1123 return extensionData;
1124 }
1125
1126
1127
1128
1129 public void setExtensionData(Map<String, Object> extensionData) {
1130 this.extensionData = extensionData;
1131 }
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142 public List getAddedCollectionItems() {
1143 return addedCollectionItems;
1144 }
1145
1146
1147
1148
1149
1150
1151 public void setAddedCollectionItems(List addedCollectionItems) {
1152 this.addedCollectionItems = addedCollectionItems;
1153 }
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166 public boolean isAddedCollectionItem(Object item) {
1167 return addedCollectionItems.contains(item);
1168 }
1169
1170 }