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.UifParameters;
25 import org.kuali.rice.krad.uif.util.SessionTransient;
26 import org.kuali.rice.krad.uif.view.DialogManager;
27 import org.kuali.rice.krad.uif.view.History;
28 import org.kuali.rice.krad.uif.view.HistoryEntry;
29 import org.kuali.rice.krad.uif.view.View;
30 import org.kuali.rice.krad.uif.service.ViewService;
31 import org.kuali.rice.krad.uif.view.ViewModel;
32 import org.kuali.rice.krad.util.KRADUtils;
33 import org.springframework.web.multipart.MultipartFile;
34 import org.kuali.rice.krad.uif.UifConstants.ViewType;
35
36 import javax.servlet.http.HttpServletRequest;
37 import java.io.IOException;
38 import java.util.ArrayList;
39 import java.util.HashMap;
40 import java.util.List;
41 import java.util.Map;
42 import java.util.Properties;
43 import java.util.Set;
44 import java.util.UUID;
45
46
47
48
49
50
51
52
53
54
55
56 public class UifFormBase implements ViewModel {
57 private static final long serialVersionUID = 8432543267099454434L;
58
59
60 private static final Log LOG = LogFactory.getLog(UifFormBase.class);
61
62
63 protected String viewId;
64 protected String viewName;
65 protected ViewType viewTypeName;
66 protected String pageId;
67 protected String methodToCall;
68 protected String formKey;
69
70 @SessionTransient
71 protected String jumpToId;
72 @SessionTransient
73 protected String jumpToName;
74 @SessionTransient
75 protected String focusId;
76
77 protected String formPostUrl;
78
79 protected String state;
80 protected boolean defaultsApplied;
81 protected boolean renderedInLightBox;
82
83 @SessionTransient
84 protected String growlScript;
85 @SessionTransient
86 protected String lightboxScript;
87
88 protected View view;
89 protected View postedView;
90
91 protected Map<String, String> viewRequestParameters;
92 protected List<String> readOnlyFieldsList;
93 protected Map<String, Object> newCollectionLines;
94
95 @SessionTransient
96 protected Map<String, String> actionParameters;
97 protected Map<String, Object> clientStateForSyncing;
98 @SessionTransient
99 protected Map<String, Set<String>> selectedCollectionLines;
100
101 protected List<Object> addedCollectionItems;
102
103 protected MultipartFile attachmentFile;
104
105
106 protected String returnLocation;
107 protected String returnFormKey;
108
109 @SessionTransient
110 protected boolean ajaxRequest;
111 @SessionTransient
112 protected String ajaxReturnType;
113
114 protected History formHistory;
115
116
117 @SessionTransient
118 protected String dialogExplanation;
119 @SessionTransient
120 protected String dialogResponse;
121 protected DialogManager dialogManager;
122
123 @SessionTransient
124 protected boolean requestRedirected;
125 @SessionTransient
126 protected String updateComponentId;
127
128 public UifFormBase() {
129 formKey = generateFormKey();
130 defaultsApplied = false;
131 renderedInLightBox = false;
132 requestRedirected = false;
133
134 readOnlyFieldsList = new ArrayList<String>();
135 viewRequestParameters = new HashMap<String, String>();
136 newCollectionLines = new HashMap<String, Object>();
137 actionParameters = new HashMap<String, String>();
138 clientStateForSyncing = new HashMap<String, Object>();
139 selectedCollectionLines = new HashMap<String, Set<String>>();
140 addedCollectionItems = new ArrayList();
141 dialogManager = new DialogManager();
142 }
143
144
145
146
147
148
149
150 protected String generateFormKey() {
151 return UUID.randomUUID().toString();
152 }
153
154
155
156
157 @Override
158 public void postBind(HttpServletRequest request) {
159
160 formPostUrl = request.getRequestURL().toString();
161
162
163 if (request.getParameterMap().containsKey(UifParameters.CLIENT_VIEW_STATE)) {
164 String clientStateJSON = request.getParameter(UifParameters.CLIENT_VIEW_STATE);
165 if (StringUtils.isNotBlank(clientStateJSON)) {
166
167 clientStateJSON = StringUtils.replace(clientStateJSON, "'", "\"");
168
169 ObjectMapper mapper = new ObjectMapper();
170 try {
171 clientStateForSyncing = mapper.readValue(clientStateJSON, Map.class);
172 } catch (IOException e) {
173 throw new RuntimeException("Unable to decode client side state JSON", e);
174 }
175 }
176 }
177
178
179 if (request.getParameter(UifParameters.READ_ONLY_FIELDS) != null) {
180 String readOnlyFields = request.getParameter(UifParameters.READ_ONLY_FIELDS);
181 setReadOnlyFieldsList(KRADUtils.convertStringParameterToList(readOnlyFields));
182 }
183 }
184
185
186
187
188 @Override
189 public String getViewId() {
190 return this.viewId;
191 }
192
193
194
195
196 @Override
197 public void setViewId(String viewId) {
198 this.viewId = viewId;
199 }
200
201
202
203
204 @Override
205 public String getViewName() {
206 return this.viewName;
207 }
208
209
210
211
212 @Override
213 public void setViewName(String viewName) {
214 this.viewName = viewName;
215 }
216
217
218
219
220 @Override
221 public ViewType getViewTypeName() {
222 return this.viewTypeName;
223 }
224
225
226
227
228 @Override
229 public void setViewTypeName(ViewType viewTypeName) {
230 this.viewTypeName = viewTypeName;
231 }
232
233
234
235
236 @Override
237 public String getPageId() {
238 return this.pageId;
239 }
240
241
242
243
244 @Override
245 public void setPageId(String pageId) {
246 this.pageId = pageId;
247 }
248
249
250
251
252 @Override
253 public String getFormPostUrl() {
254 return this.formPostUrl;
255 }
256
257
258
259
260 @Override
261 public void setFormPostUrl(String formPostUrl) {
262 this.formPostUrl = formPostUrl;
263 }
264
265 public String getReturnLocation() {
266 return this.returnLocation;
267 }
268
269 public void setReturnLocation(String returnLocation) {
270 this.returnLocation = returnLocation;
271 }
272
273 public String getReturnFormKey() {
274 return this.returnFormKey;
275 }
276
277 public void setReturnFormKey(String returnFormKey) {
278 this.returnFormKey = returnFormKey;
279 }
280
281
282
283
284
285
286
287
288 public String getMethodToCall() {
289 return this.methodToCall;
290 }
291
292
293
294
295
296
297 public void setMethodToCall(String methodToCall) {
298 this.methodToCall = methodToCall;
299 }
300
301
302
303
304 @Override
305 public Map<String, String> getViewRequestParameters() {
306 return this.viewRequestParameters;
307 }
308
309
310
311
312 @Override
313 public void setViewRequestParameters(Map<String, String> viewRequestParameters) {
314 this.viewRequestParameters = viewRequestParameters;
315 }
316
317
318
319
320 @Override
321 public List<String> getReadOnlyFieldsList() {
322 return readOnlyFieldsList;
323 }
324
325
326
327
328 @Override
329 public void setReadOnlyFieldsList(List<String> readOnlyFieldsList) {
330 this.readOnlyFieldsList = readOnlyFieldsList;
331 }
332
333
334
335
336 @Override
337 public Map<String, Object> getNewCollectionLines() {
338 return this.newCollectionLines;
339 }
340
341
342
343
344 @Override
345 public void setNewCollectionLines(Map<String, Object> newCollectionLines) {
346 this.newCollectionLines = newCollectionLines;
347 }
348
349
350
351
352 @Override
353 public Map<String, String> getActionParameters() {
354 return this.actionParameters;
355 }
356
357
358
359
360
361
362 public Properties getActionParametersAsProperties() {
363 return KRADUtils.convertMapToProperties(actionParameters);
364 }
365
366
367
368
369 @Override
370 public void setActionParameters(Map<String, String> actionParameters) {
371 this.actionParameters = actionParameters;
372 }
373
374
375
376
377
378
379
380
381 public String getActionParamaterValue(String actionParameterName) {
382 if ((actionParameters != null) && actionParameters.containsKey(actionParameterName)) {
383 return actionParameters.get(actionParameterName);
384 }
385
386 return "";
387 }
388
389
390
391
392
393
394
395
396
397
398
399
400
401 public String getActionEvent() {
402 if ((actionParameters != null) && actionParameters.containsKey(UifConstants.UrlParams.ACTION_EVENT)) {
403 return actionParameters.get(UifConstants.UrlParams.ACTION_EVENT);
404 }
405
406 return "";
407 }
408
409
410
411
412 @Override
413 public Map<String, Object> getClientStateForSyncing() {
414 return clientStateForSyncing;
415 }
416
417
418
419
420
421
422 public void setClientStateForSyncing(Map<String, Object> clientStateForSyncing) {
423 this.clientStateForSyncing = clientStateForSyncing;
424 }
425
426
427
428
429 @Override
430 public Map<String, Set<String>> getSelectedCollectionLines() {
431 return selectedCollectionLines;
432 }
433
434
435
436
437 @Override
438 public void setSelectedCollectionLines(Map<String, Set<String>> selectedCollectionLines) {
439 this.selectedCollectionLines = selectedCollectionLines;
440 }
441
442
443
444
445
446
447
448
449
450
451
452
453 public String getFormKey() {
454 return this.formKey;
455 }
456
457
458
459
460
461
462 public void setFormKey(String formKey) {
463 this.formKey = formKey;
464 }
465
466
467
468
469 @Override
470 public boolean isDefaultsApplied() {
471 return this.defaultsApplied;
472 }
473
474
475
476
477 @Override
478 public void setDefaultsApplied(boolean defaultsApplied) {
479 this.defaultsApplied = defaultsApplied;
480 }
481
482
483
484
485
486
487 public boolean isRequestRedirected() {
488 return requestRedirected;
489 }
490
491
492
493
494
495
496 public void setRequestRedirected(boolean requestRedirected) {
497 this.requestRedirected = requestRedirected;
498 }
499
500
501
502
503
504
505 public MultipartFile getAttachmentFile() {
506 return this.attachmentFile;
507 }
508
509
510
511
512
513
514 public void setAttachmentFile(MultipartFile attachmentFile) {
515 this.attachmentFile = attachmentFile;
516 }
517
518
519
520
521
522
523 public String getUpdateComponentId() {
524 return updateComponentId;
525 }
526
527
528
529
530
531
532 public void setUpdateComponentId(String updateComponentId) {
533 this.updateComponentId = updateComponentId;
534 }
535
536
537
538
539 @Override
540 public View getView() {
541 return this.view;
542 }
543
544
545
546
547 @Override
548 public void setView(View view) {
549 this.view = view;
550 }
551
552
553
554
555 @Override
556 public View getPostedView() {
557 return this.postedView;
558 }
559
560
561
562
563 @Override
564 public void setPostedView(View postedView) {
565 this.postedView = postedView;
566 }
567
568
569
570
571
572
573
574 protected ViewService getViewService() {
575 return KRADServiceLocatorWeb.getViewService();
576 }
577
578
579
580
581
582
583
584
585
586 public String getJumpToId() {
587 return this.jumpToId;
588 }
589
590
591
592
593 public void setJumpToId(String jumpToId) {
594 this.jumpToId = jumpToId;
595 }
596
597
598
599
600
601
602
603
604 public String getJumpToName() {
605 return this.jumpToName;
606 }
607
608
609
610
611 public void setJumpToName(String jumpToName) {
612 this.jumpToName = jumpToName;
613 }
614
615
616
617
618
619
620
621 public String getFocusId() {
622 return this.focusId;
623 }
624
625
626
627
628 public void setFocusId(String focusId) {
629 this.focusId = focusId;
630 }
631
632
633
634
635
636
637
638
639
640
641
642
643 public History getFormHistory() {
644 return formHistory;
645 }
646
647
648
649
650
651
652 public void setFormHistory(History history) {
653 this.formHistory = history;
654 }
655
656
657
658
659
660
661
662
663
664
665
666
667 public boolean isRenderedInLightBox() {
668 return this.renderedInLightBox;
669 }
670
671
672
673
674
675
676 public void setRenderedInLightBox(boolean renderedInLightBox) {
677 this.renderedInLightBox = renderedInLightBox;
678 }
679
680
681
682
683 @Override
684 public String getGrowlScript() {
685 return growlScript;
686 }
687
688
689
690
691 @Override
692 public void setGrowlScript(String growlScript) {
693 this.growlScript = growlScript;
694 }
695
696
697
698
699 public String getState() {
700 return state;
701 }
702
703
704
705
706 public void setState(String state) {
707 this.state = state;
708 }
709
710
711
712
713 @Override
714 public String getLightboxScript() {
715 return lightboxScript;
716 }
717
718
719
720
721 @Override
722 public void setLightboxScript(String lightboxScript) {
723 this.lightboxScript = lightboxScript;
724 }
725
726
727
728
729 @Override
730 public boolean isAjaxRequest() {
731 return ajaxRequest;
732 }
733
734
735
736
737 @Override
738 public void setAjaxRequest(boolean ajaxRequest) {
739 this.ajaxRequest = ajaxRequest;
740 }
741
742
743
744
745 @Override
746 public String getAjaxReturnType() {
747 return ajaxReturnType;
748 }
749
750
751
752
753 @Override
754 public boolean isUpdateComponentRequest() {
755 return isAjaxRequest() && StringUtils.isNotBlank(getAjaxReturnType()) && getAjaxReturnType().equals(
756 UifConstants.AjaxReturnTypes.UPDATECOMPONENT.getKey());
757 }
758
759
760
761
762 @Override
763 public boolean isUpdateDialogRequest() {
764 return isAjaxRequest() && StringUtils.isNotBlank(getAjaxReturnType()) && getAjaxReturnType().equals(
765 UifConstants.AjaxReturnTypes.UPDATEDIALOG.getKey());
766 }
767
768
769
770
771 @Override
772 public boolean isUpdatePageRequest() {
773 return isAjaxRequest() && StringUtils.isNotBlank(getAjaxReturnType()) && getAjaxReturnType().equals(
774 UifConstants.AjaxReturnTypes.UPDATEPAGE.getKey());
775 }
776
777
778
779
780 @Override
781 public boolean isUpdateNoneRequest() {
782 return isAjaxRequest() && StringUtils.isNotBlank(getAjaxReturnType()) && getAjaxReturnType().equals(
783 UifConstants.AjaxReturnTypes.UPDATENONE.getKey());
784 }
785
786
787
788
789 @Override
790 public boolean isBuildViewRequest() {
791 return !isAjaxRequest() || (StringUtils.isNotBlank(getAjaxReturnType()) && (getAjaxReturnType().equals(
792 UifConstants.AjaxReturnTypes.UPDATEVIEW.getKey()) || isUpdatePageRequest()));
793 }
794
795
796
797
798 @Override
799 public boolean isUpdateViewRequest() {
800 return isAjaxRequest() &&
801 StringUtils.isNotBlank(getAjaxReturnType()) &&
802 (isUpdateComponentRequest() || getAjaxReturnType().equals(
803 UifConstants.AjaxReturnTypes.DISPLAYLIGHTBOX.getKey()));
804 }
805
806
807
808
809 @Override
810 public void setAjaxReturnType(String ajaxReturnType) {
811 this.ajaxReturnType = ajaxReturnType;
812 }
813
814
815
816
817
818
819
820
821
822
823 public String getDialogExplanation() {
824 return dialogExplanation;
825 }
826
827
828
829
830
831
832 public void setDialogExplanation(String dialogExplanation) {
833 this.dialogExplanation = dialogExplanation;
834 }
835
836
837
838
839
840
841
842
843
844
845
846 public String getDialogResponse() {
847 return dialogResponse;
848 }
849
850
851
852
853
854
855 public void setDialogResponse(String dialogResponse) {
856 this.dialogResponse = dialogResponse;
857 }
858
859
860
861
862
863
864
865
866
867 public DialogManager getDialogManager() {
868 return dialogManager;
869 }
870
871
872
873
874
875
876 public void setDialogManager(DialogManager dialogManager) {
877 this.dialogManager = dialogManager;
878 }
879
880
881
882
883
884
885
886
887
888
889
890 public List getAddedCollectionItems() {
891 return addedCollectionItems;
892 }
893
894
895
896
897
898
899 public void setAddedCollectionItems(List addedCollectionItems) {
900 this.addedCollectionItems = addedCollectionItems;
901 }
902
903
904
905
906
907
908
909
910
911
912
913
914 public boolean isAddedCollectionItem(Object item) {
915 return addedCollectionItems.contains(item);
916 }
917
918 }