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