1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.web.controller;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.codehaus.jackson.map.ObjectMapper;
20 import org.kuali.rice.core.api.config.property.ConfigContext;
21 import org.kuali.rice.core.api.exception.RiceRuntimeException;
22 import org.kuali.rice.kim.api.identity.Person;
23 import org.kuali.rice.krad.exception.AuthorizationException;
24 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
25 import org.kuali.rice.krad.service.ModuleService;
26 import org.kuali.rice.krad.uif.UifConstants;
27 import org.kuali.rice.krad.uif.UifParameters;
28 import org.kuali.rice.krad.uif.UifPropertyPaths;
29 import org.kuali.rice.krad.uif.component.Component;
30 import org.kuali.rice.krad.uif.container.Group;
31 import org.kuali.rice.krad.uif.field.AttributeQueryResult;
32 import org.kuali.rice.krad.uif.service.ViewService;
33 import org.kuali.rice.krad.uif.util.LookupInquiryUtils;
34 import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
35 import org.kuali.rice.krad.uif.view.DialogManager;
36 import org.kuali.rice.krad.uif.view.MessageView;
37 import org.kuali.rice.krad.uif.view.View;
38 import org.kuali.rice.krad.util.GlobalVariables;
39 import org.kuali.rice.krad.util.KRADConstants;
40 import org.kuali.rice.krad.util.KRADUtils;
41 import org.kuali.rice.krad.util.UrlFactory;
42 import org.kuali.rice.krad.web.controller.helper.CollectionPagingHelper;
43 import org.kuali.rice.krad.web.controller.helper.DataTablesPagingHelper;
44 import org.kuali.rice.krad.web.form.HistoryFlow;
45 import org.kuali.rice.krad.web.form.HistoryManager;
46 import org.kuali.rice.krad.web.form.UifFormBase;
47 import org.kuali.rice.krad.web.form.UifFormManager;
48 import org.springframework.util.Assert;
49 import org.springframework.validation.BindingResult;
50 import org.springframework.web.bind.annotation.ModelAttribute;
51 import org.springframework.web.bind.annotation.RequestMapping;
52 import org.springframework.web.bind.annotation.RequestMethod;
53 import org.springframework.web.bind.annotation.ResponseBody;
54 import org.springframework.web.servlet.ModelAndView;
55 import org.springframework.web.servlet.support.RequestContextUtils;
56 import org.springframework.web.servlet.view.UrlBasedViewResolver;
57
58 import javax.servlet.http.HttpServletRequest;
59 import javax.servlet.http.HttpServletResponse;
60 import java.util.Enumeration;
61 import java.util.HashMap;
62 import java.util.Map;
63 import java.util.Map.Entry;
64 import java.util.Properties;
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83 public abstract class UifControllerBase {
84 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(UifControllerBase.class);
85
86 private UrlBasedViewResolver viewResolver;
87
88
89
90
91
92
93
94
95
96 @ModelAttribute(value = "KualiForm")
97 public UifFormBase initForm(HttpServletRequest request, HttpServletResponse response) {
98 UifFormBase requestForm = null;
99
100
101 UifFormManager uifFormManager = (UifFormManager) request.getSession().getAttribute(UifParameters.FORM_MANAGER);
102 if (uifFormManager == null) {
103 uifFormManager = new UifFormManager();
104 request.getSession().setAttribute(UifParameters.FORM_MANAGER, uifFormManager);
105 }
106
107
108 GlobalVariables.setUifFormManager(uifFormManager);
109
110
111 requestForm = createInitialForm(request);
112
113 String formKeyParam = request.getParameter(UifParameters.FORM_KEY);
114 if (StringUtils.isNotBlank(formKeyParam)) {
115
116 uifFormManager.updateFormWithSession(requestForm, formKeyParam);
117 }
118
119
120 String requestedFormKey = request.getParameter(UifParameters.REQUESTED_FORM_KEY);
121 if (StringUtils.isNotBlank(requestedFormKey)) {
122 requestForm.setRequestedFormKey(requestedFormKey);
123 } else {
124 requestForm.setRequestedFormKey(formKeyParam);
125 }
126
127
128 String referer = request.getHeader(UifConstants.REFERER);
129
130
131 if (StringUtils.isBlank(referer) && StringUtils.isBlank(requestForm.getReturnLocation())) {
132 requestForm.setReturnLocation(UifConstants.NO_RETURN);
133 } else if (StringUtils.isBlank(requestForm.getReturnLocation())) {
134 requestForm.setReturnLocation(referer);
135 }
136
137
138 if (requestForm.getInitialRequestParameters() == null) {
139 Map<String, String> requestParams = new HashMap<String, String>();
140 Enumeration<String> names = request.getParameterNames();
141
142 while (names != null && names.hasMoreElements()) {
143 String name = KRADUtils.stripXSSPatterns(names.nextElement());
144 String value = KRADUtils.stripXSSPatterns(request.getParameter(name));
145
146 requestParams.put(name, value);
147 }
148
149 requestParams.remove(UifConstants.UrlParams.LOGIN_USER);
150 requestForm.setInitialRequestParameters(requestParams);
151 }
152
153
154 String requestUrl = KRADUtils.stripXSSPatterns(KRADUtils.getFullURL(request));
155 requestForm.setRequestUrl(requestUrl);
156
157 Object historyManager = request.getSession().getAttribute(UifConstants.HistoryFlow.HISTORY_MANAGER);
158 String flowKey = request.getParameter(UifConstants.HistoryFlow.FLOW);
159
160
161 if (requestForm != null && historyManager != null && historyManager instanceof HistoryManager) {
162 requestForm.setHistoryManager((HistoryManager) historyManager);
163 requestForm.setFlowKey(flowKey);
164 }
165
166
167 request.setAttribute(UifConstants.REQUEST_FORM, requestForm);
168
169 return requestForm;
170 }
171
172
173
174
175
176
177
178
179 protected abstract UifFormBase createInitialForm(HttpServletRequest request);
180
181
182
183
184 @RequestMapping()
185 public ModelAndView defaultMapping(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
186 HttpServletRequest request, HttpServletResponse response) {
187 return start(form, result, request, response);
188 }
189
190
191
192
193
194 @RequestMapping(params = "methodToCall=start")
195 public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
196 HttpServletRequest request, HttpServletResponse response) {
197
198
199
200 if (form.getView() != null) {
201 String methodToCall = request.getParameter(KRADConstants.DISPATCH_REQUEST_PARAMETER);
202 checkViewAuthorization(form, methodToCall);
203 }
204
205 return getUIFModelAndView(form);
206 }
207
208
209
210
211
212
213
214
215
216
217
218
219
220 public void checkViewAuthorization(UifFormBase form, String methodToCall) throws AuthorizationException {
221
222 if (GlobalVariables.getUserSession() == null) {
223 return;
224 }
225
226 Person user = GlobalVariables.getUserSession().getPerson();
227
228 boolean canOpenView = form.getView().getAuthorizer().canOpenView(form.getView(), form, user);
229 if (!canOpenView) {
230 throw new AuthorizationException(user.getPrincipalName(), "open", form.getView().getId(),
231 "User '" + user.getPrincipalName() + "' is not authorized to open view ID: " + form.getView()
232 .getId(), null);
233 }
234 }
235
236
237
238
239 @RequestMapping(params = "methodToCall=sessionTimeout")
240 public ModelAndView sessionTimeout(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
241 HttpServletRequest request, HttpServletResponse response) {
242 return getUIFModelAndView(form);
243 }
244
245
246
247
248
249
250 @RequestMapping(method = RequestMethod.POST, params = "methodToCall=addLine")
251 public ModelAndView addLine(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
252 HttpServletRequest request, HttpServletResponse response) {
253
254 String selectedCollectionPath = uifForm.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
255 if (StringUtils.isBlank(selectedCollectionPath)) {
256 throw new RuntimeException("Selected collection was not set for add line action, cannot add new line");
257 }
258
259 View view = uifForm.getPostedView();
260 view.getViewHelperService().processCollectionAddLine(view, uifForm, selectedCollectionPath);
261
262 return getUIFModelAndView(uifForm);
263 }
264
265
266
267
268
269
270
271
272
273
274
275
276 @RequestMapping(method = RequestMethod.POST, params = "methodToCall=addBlankLine")
277 public ModelAndView addBlankLine(@ModelAttribute("KualiForm") UifFormBase uifForm) {
278
279 String selectedCollectionPath = uifForm.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
280 if (StringUtils.isBlank(selectedCollectionPath)) {
281 throw new RuntimeException("Selected collection was not set for add line action, cannot add new line");
282 }
283
284 View view = uifForm.getPostedView();
285 view.getViewHelperService().processCollectionAddBlankLine(view, uifForm, selectedCollectionPath);
286
287 return getUIFModelAndView(uifForm);
288 }
289
290
291
292
293
294 @RequestMapping(method = RequestMethod.POST, params = "methodToCall=saveLine")
295 public ModelAndView saveLine(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
296 HttpServletRequest request, HttpServletResponse response) {
297
298 String selectedCollectionPath = uifForm.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
299 if (StringUtils.isBlank(selectedCollectionPath)) {
300 throw new RuntimeException("Selected collection was not set for add line action, cannot add new line");
301 }
302
303 int selectedLineIndex = -1;
304 String selectedLine = uifForm.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX);
305 if (StringUtils.isNotBlank(selectedLine)) {
306 selectedLineIndex = Integer.parseInt(selectedLine);
307 }
308
309 if (selectedLineIndex == -1) {
310 throw new RuntimeException("Selected line index was not set for delete line action, cannot delete line");
311 }
312
313 View view = uifForm.getPostedView();
314 view.getViewHelperService().processCollectionSaveLine(view, uifForm, selectedCollectionPath, selectedLineIndex);
315
316 return getUIFModelAndView(uifForm);
317 }
318
319
320
321
322
323
324
325 @RequestMapping(method = RequestMethod.POST, params = "methodToCall=deleteLine")
326 public ModelAndView deleteLine(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
327 HttpServletRequest request, HttpServletResponse response) {
328
329 String selectedCollectionPath = uifForm.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
330 if (StringUtils.isBlank(selectedCollectionPath)) {
331 throw new RuntimeException("Selected collection was not set for delete line action, cannot delete line");
332 }
333
334 int selectedLineIndex = -1;
335 String selectedLine = uifForm.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX);
336 if (StringUtils.isNotBlank(selectedLine)) {
337 selectedLineIndex = Integer.parseInt(selectedLine);
338 }
339
340 if (selectedLineIndex == -1) {
341 throw new RuntimeException("Selected line index was not set for delete line action, cannot delete line");
342 }
343
344 View view = uifForm.getPostedView();
345 view.getViewHelperService().processCollectionDeleteLine(view, uifForm, selectedCollectionPath,
346 selectedLineIndex);
347
348 return getUIFModelAndView(uifForm);
349 }
350
351
352
353
354 @RequestMapping(params = "methodToCall=cancel")
355 public ModelAndView cancel(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
356 HttpServletRequest request, HttpServletResponse response) {
357 return back(form, result, request, response);
358 }
359
360
361
362
363
364 @RequestMapping(params = "methodToCall=back")
365 public ModelAndView back(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
366 HttpServletRequest request, HttpServletResponse response) {
367 Properties props = new Properties();
368 props.put(UifParameters.METHOD_TO_CALL, UifConstants.MethodToCallNames.REFRESH);
369
370 if (StringUtils.isNotBlank(form.getReturnFormKey())) {
371 props.put(UifParameters.FORM_KEY, form.getReturnFormKey());
372 }
373
374 HistoryFlow historyFlow = form.getHistoryManager().getMostRecentFlowByFormKey(form.getFlowKey(),
375 form.getRequestedFormKey());
376
377 String returnUrl = form.getReturnLocation();
378
379
380 if (historyFlow != null) {
381 returnUrl = historyFlow.getFlowReturnPoint();
382 }
383
384
385 String returnToStart = form.getActionParamaterValue(UifConstants.HistoryFlow.RETURN_TO_START);
386 if (StringUtils.isBlank(returnToStart)) {
387 returnToStart = request.getParameter(UifConstants.HistoryFlow.RETURN_TO_START);
388 }
389
390 if (StringUtils.isNotBlank(returnToStart)
391 && Boolean.parseBoolean(returnToStart)
392 && historyFlow != null
393 && StringUtils.isNotBlank(historyFlow.getFlowStartPoint())) {
394 returnUrl = historyFlow.getFlowStartPoint();
395 }
396
397
398 if (StringUtils.isBlank(returnUrl) || returnUrl.equals(UifConstants.NO_RETURN)) {
399 returnUrl = ConfigContext.getCurrentContextConfig().getProperty(KRADConstants.APPLICATION_URL_KEY);
400 }
401
402
403 GlobalVariables.getUifFormManager().removeSessionForm(form);
404
405 return performRedirect(form, returnUrl, props);
406 }
407
408
409
410
411
412
413 @RequestMapping(params = "methodToCall=returnToPrevious")
414 public ModelAndView returnToPrevious(@ModelAttribute("KualiForm") UifFormBase form) {
415
416 return returnToHistory(form, false);
417 }
418
419
420
421
422
423
424 @RequestMapping(params = "methodToCall=returnToHub")
425 public ModelAndView returnToHub(@ModelAttribute("KualiForm") UifFormBase form) {
426
427 return returnToHistory(form, true);
428 }
429
430
431
432
433
434
435
436
437
438 public ModelAndView returnToHistory(UifFormBase form, boolean homeFlag) {
439 String returnUrl = form.getReturnLocation();
440
441 if (StringUtils.isBlank(returnUrl) || homeFlag) {
442 returnUrl = ConfigContext.getCurrentContextConfig().getProperty(KRADConstants.APPLICATION_URL_KEY);
443 }
444
445
446 Properties props = new Properties();
447 props.put(UifParameters.METHOD_TO_CALL, UifConstants.MethodToCallNames.REFRESH);
448
449
450 GlobalVariables.getUifFormManager().removeSessionForm(form);
451
452 return performRedirect(form, returnUrl, props);
453 }
454
455
456
457
458 @RequestMapping(method = RequestMethod.POST, params = "methodToCall=navigate")
459 public ModelAndView navigate(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
460 HttpServletRequest request, HttpServletResponse response) {
461 String pageId = form.getActionParamaterValue(UifParameters.NAVIGATE_TO_PAGE_ID);
462
463
464 form.setDirtyForm(false);
465
466 return getUIFModelAndView(form, pageId);
467 }
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484 @RequestMapping(params = "methodToCall=refresh")
485 public ModelAndView refresh(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
486 HttpServletRequest request, HttpServletResponse response) throws Exception {
487
488
489 String flashMapSelectedLineValues = "";
490 if (RequestContextUtils.getInputFlashMap(request) != null) {
491 flashMapSelectedLineValues = (String) RequestContextUtils.getInputFlashMap(request).get(
492 UifParameters.SELECTED_LINE_VALUES);
493 }
494 String refreshCallerType = "";
495 if (request.getParameterMap().containsKey(KRADConstants.REFRESH_CALLER_TYPE)) {
496 refreshCallerType = request.getParameter(KRADConstants.REFRESH_CALLER_TYPE);
497 }
498
499
500 if (StringUtils.equals(refreshCallerType, UifConstants.RefreshCallerTypes.MULTI_VALUE_LOOKUP)) {
501 String lookupCollectionName = "";
502 if (request.getParameterMap().containsKey(UifParameters.LOOKUP_COLLECTION_NAME)) {
503 lookupCollectionName = request.getParameter(UifParameters.LOOKUP_COLLECTION_NAME);
504 }
505
506 if (StringUtils.isBlank(lookupCollectionName)) {
507 throw new RuntimeException(
508 "Lookup collection name is required for processing multi-value lookup results");
509 }
510
511 String selectedLineValues = "";
512 if (request.getParameterMap().containsKey(UifParameters.SELECTED_LINE_VALUES)) {
513 selectedLineValues = request.getParameter(UifParameters.SELECTED_LINE_VALUES);
514 }
515 if (!StringUtils.isBlank(flashMapSelectedLineValues)) {
516 selectedLineValues = flashMapSelectedLineValues;
517 }
518
519
520 form.getPostedView().getViewHelperService().processMultipleValueLookupResults(form.getPostedView(), form,
521 lookupCollectionName, selectedLineValues);
522 }
523
524 if (request.getParameterMap().containsKey(KRADConstants.REFERENCES_TO_REFRESH)) {
525 String referencesToRefresh = request.getParameter(KRADConstants.REFERENCES_TO_REFRESH);
526 form.getPostedView().getViewHelperService().refreshReferences(form, referencesToRefresh);
527 }
528
529 return getUIFModelAndView(form);
530 }
531
532
533
534
535
536 @RequestMapping(method = RequestMethod.POST, params = "methodToCall=performLookup")
537 public ModelAndView performLookup(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
538 HttpServletRequest request, HttpServletResponse response) {
539 Properties lookupParameters = form.getActionParametersAsProperties();
540
541 String lookupObjectClassName = (String) lookupParameters.get(UifParameters.DATA_OBJECT_CLASS_NAME);
542 Class<?> lookupObjectClass = null;
543 try {
544 lookupObjectClass = Class.forName(lookupObjectClassName);
545 } catch (ClassNotFoundException e) {
546 LOG.error("Unable to get class for name: " + lookupObjectClassName);
547 throw new RuntimeException("Unable to get class for name: " + lookupObjectClassName, e);
548 }
549
550
551 String lookupParameterString = (String) lookupParameters.get(UifParameters.LOOKUP_PARAMETERS);
552 if (lookupParameterString != null) {
553 Map<String, String> lookupParameterFields = KRADUtils.getMapFromParameterString(lookupParameterString);
554 for (Entry<String, String> lookupParameter : lookupParameterFields.entrySet()) {
555 String lookupParameterValue = LookupInquiryUtils.retrieveLookupParameterValue(form, request,
556 lookupObjectClass, lookupParameter.getValue(), lookupParameter.getKey());
557
558 if (StringUtils.isNotBlank(lookupParameterValue)) {
559 lookupParameters.put(UifPropertyPaths.LOOKUP_CRITERIA + "['" + lookupParameter.getValue() + "']",
560 lookupParameterValue);
561 }
562 }
563
564 lookupParameters.remove(UifParameters.LOOKUP_PARAMETERS);
565 }
566
567
568
569 String baseLookupUrl = (String) lookupParameters.get(UifParameters.BASE_LOOKUP_URL);
570 lookupParameters.remove(UifParameters.BASE_LOOKUP_URL);
571
572
573 lookupParameters.put(UifParameters.METHOD_TO_CALL, UifConstants.MethodToCallNames.START);
574 String autoSearchString = (String) lookupParameters.get(UifParameters.AUTO_SEARCH);
575 if (Boolean.parseBoolean(autoSearchString)) {
576 lookupParameters.put(UifParameters.METHOD_TO_CALL, UifConstants.MethodToCallNames.SEARCH);
577 }
578
579 lookupParameters.put(UifParameters.RETURN_LOCATION, form.getFormPostUrl());
580 lookupParameters.put(UifParameters.RETURN_FORM_KEY, form.getFormKey());
581
582
583 if (lookupObjectClass != null) {
584 ModuleService responsibleModuleService =
585 KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(lookupObjectClass);
586 if (responsibleModuleService != null && responsibleModuleService.isExternalizable(lookupObjectClass)) {
587 String lookupUrl = responsibleModuleService.getExternalizableDataObjectLookupUrl(lookupObjectClass,
588 lookupParameters);
589
590 return performRedirect(form, lookupUrl, new Properties());
591 }
592 }
593
594 return performRedirect(form, baseLookupUrl, lookupParameters);
595 }
596
597
598
599
600
601 @RequestMapping(method = RequestMethod.POST, params = "methodToCall=checkForm")
602 public ModelAndView checkForm(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
603 HttpServletRequest request, HttpServletResponse response) {
604 KRADServiceLocatorWeb.getViewValidationService().validateViewSimulation(form.getPostedView(), form);
605
606 return getUIFModelAndView(form);
607 }
608
609
610
611
612
613
614
615 @RequestMapping(method = RequestMethod.GET, params = "methodToCall=performFieldSuggest")
616 public
617 @ResponseBody
618 AttributeQueryResult performFieldSuggest(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
619 HttpServletRequest request, HttpServletResponse response) {
620
621
622 Map<String, String> queryParameters = new HashMap<String, String>();
623 for (Object parameterName : request.getParameterMap().keySet()) {
624 if (parameterName.toString().startsWith(UifParameters.QUERY_PARAMETER + ".")) {
625 String fieldName = StringUtils.substringAfter(parameterName.toString(),
626 UifParameters.QUERY_PARAMETER + ".");
627 String fieldValue = request.getParameter(parameterName.toString());
628 queryParameters.put(fieldName, fieldValue);
629 }
630 }
631
632
633 String queryFieldId = request.getParameter(UifParameters.QUERY_FIELD_ID);
634 if (StringUtils.isBlank(queryFieldId)) {
635 throw new RuntimeException("Unable to find id for field to perform query on under request parameter name: "
636 + UifParameters.QUERY_FIELD_ID);
637 }
638
639
640 String queryTerm = request.getParameter(UifParameters.QUERY_TERM);
641 if (StringUtils.isBlank(queryTerm)) {
642 throw new RuntimeException(
643 "Unable to find id for query term value for attribute query on under request parameter name: "
644 + UifParameters.QUERY_TERM);
645 }
646
647
648 AttributeQueryResult queryResult = KRADServiceLocatorWeb.getAttributeQueryService().performFieldSuggestQuery(
649 form.getPostedView(), queryFieldId, queryTerm, queryParameters);
650
651 return queryResult;
652 }
653
654
655
656
657
658
659
660
661 @RequestMapping(method = RequestMethod.GET, params = "methodToCall=performFieldQuery")
662 public
663 @ResponseBody
664 AttributeQueryResult performFieldQuery(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
665 HttpServletRequest request, HttpServletResponse response) {
666
667
668 Map<String, String> queryParameters = new HashMap<String, String>();
669 for (Object parameterName : request.getParameterMap().keySet()) {
670 if (parameterName.toString().startsWith(UifParameters.QUERY_PARAMETER + ".")) {
671 String fieldName = StringUtils.substringAfter(parameterName.toString(),
672 UifParameters.QUERY_PARAMETER + ".");
673 String fieldValue = request.getParameter(parameterName.toString());
674 queryParameters.put(fieldName, fieldValue);
675 }
676 }
677
678
679 String queryFieldId = request.getParameter(UifParameters.QUERY_FIELD_ID);
680 if (StringUtils.isBlank(queryFieldId)) {
681 throw new RuntimeException("Unable to find id for field to perform query on under request parameter name: "
682 + UifParameters.QUERY_FIELD_ID);
683 }
684
685
686 AttributeQueryResult queryResult = KRADServiceLocatorWeb.getAttributeQueryService().performFieldQuery(
687 form.getPostedView(), queryFieldId, queryParameters);
688
689 return queryResult;
690 }
691
692
693
694
695
696
697
698
699 protected boolean hasDialogBeenDisplayed(String dialogId, UifFormBase form) {
700 return (form.getDialogManager().hasDialogBeenDisplayed(dialogId));
701 }
702
703
704
705
706
707
708
709
710 protected boolean hasDialogBeenAnswered(String dialogId, UifFormBase form) {
711 return (form.getDialogManager().hasDialogBeenAnswered(dialogId));
712 }
713
714
715
716
717
718
719
720
721 protected void resetDialogStatus(String dialogId, UifFormBase form) {
722 form.getDialogManager().resetDialogStatus(dialogId);
723 }
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752 protected boolean getBooleanDialogResponse(String dialogId, UifFormBase form, HttpServletRequest request,
753 HttpServletResponse response) {
754 DialogManager dm = form.getDialogManager();
755 if (!dm.hasDialogBeenAnswered(dialogId)) {
756
757
758
759
760 throw new RiceRuntimeException("Dialog has not yet been answered by client. "
761 + "Check that hasDialogBeenAnswered(id) returns true.");
762 }
763
764 return dm.wasDialogAnswerAffirmative(dialogId);
765 }
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795 protected String getStringDialogResponse(String dialogId, UifFormBase form, HttpServletRequest request,
796 HttpServletResponse response) {
797 DialogManager dm = form.getDialogManager();
798 if (!dm.hasDialogBeenAnswered(dialogId)) {
799
800
801
802 throw new RiceRuntimeException("Dialog has not yet been answered by client. "
803 + "Check that hasDialogBeenAnswered(id) returns true.");
804 }
805
806 return dm.getDialogAnswer(dialogId);
807 }
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825 protected ModelAndView showDialog(String dialogId, UifFormBase form, HttpServletRequest request,
826 HttpServletResponse response) {
827
828 form.setLightboxScript("openLightboxOnLoad('" + dialogId + "');");
829 form.getDialogManager().addDialog(dialogId, form.getMethodToCall());
830
831
832
833 if (form.isAjaxRequest()) {
834 form.setAjaxReturnType(UifConstants.AjaxReturnTypes.UPDATEDIALOG.getKey());
835 form.setUpdateComponentId(dialogId);
836 }
837
838 return getUIFModelAndView(form);
839 }
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856 @RequestMapping(params = "methodToCall=returnFromLightbox")
857 public ModelAndView returnFromLightbox(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
858 HttpServletRequest request, HttpServletResponse response) {
859 String newMethodToCall = "";
860
861
862 DialogManager dm = form.getDialogManager();
863 String dialogId = dm.getCurrentDialogId();
864 if (dialogId == null) {
865
866
867
868 newMethodToCall = "start";
869 } else {
870 dm.setDialogAnswer(dialogId, form.getDialogResponse());
871 dm.setDialogExplanation(dialogId, form.getDialogExplanation());
872 newMethodToCall = dm.getDialogReturnMethod(dialogId);
873 dm.setCurrentDialogId(null);
874 }
875
876
877 Properties props = new Properties();
878 props.put(UifParameters.METHOD_TO_CALL, newMethodToCall);
879 props.put(UifParameters.VIEW_ID, form.getViewId());
880 props.put(UifParameters.FORM_KEY, form.getFormKey());
881 props.put(UifParameters.AJAX_REQUEST, "false");
882
883 return performRedirect(form, form.getFormPostUrl(), props);
884 }
885
886
887
888
889
890
891
892
893
894
895
896 protected ModelAndView performRedirect(UifFormBase form, String baseUrl, Properties urlParameters) {
897 String redirectUrl = UrlFactory.parameterizeUrl(baseUrl, urlParameters);
898
899 return performRedirect(form, redirectUrl);
900 }
901
902
903
904
905
906
907
908
909 protected ModelAndView performRedirect(UifFormBase form, String redirectUrl) {
910
911 form.setRequestRedirected(true);
912
913
914 form.setAjaxReturnType(UifConstants.AjaxReturnTypes.REDIRECT.getKey());
915
916 ModelAndView modelAndView;
917 if (form.isAjaxRequest()) {
918 modelAndView = getUIFModelAndView(form, form.getPageId());
919 modelAndView.addObject("redirectUrl", redirectUrl);
920 } else {
921 modelAndView = new ModelAndView(UifConstants.REDIRECT_PREFIX + redirectUrl);
922 }
923
924 return modelAndView;
925 }
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941 protected ModelAndView getMessageView(UifFormBase form, String headerText, String messageText) {
942
943 MessageView messageView = (MessageView) getViewService().getViewById(UifConstants.MESSAGE_VIEW_ID);
944
945 messageView.setHeaderText(headerText);
946 messageView.setMessageText(messageText);
947
948 form.setViewId(UifConstants.MESSAGE_VIEW_ID);
949 form.setView(messageView);
950
951 return getUIFModelAndView(form);
952 }
953
954
955
956
957
958
959
960
961 protected ModelAndView getUIFModelAndView(UifFormBase form) {
962 return getUIFModelAndView(form, form.getPageId());
963 }
964
965
966
967
968
969
970
971
972
973
974 protected ModelAndView getUIFModelAndView(UifFormBase form, String pageId) {
975 return UifControllerHelper.getUIFModelAndView(form, pageId);
976 }
977
978
979
980
981
982
983
984
985
986 protected ModelAndView getUIFModelAndViewWithInit(UifFormBase form, String viewId) {
987 View view = getViewService().getViewById(viewId);
988
989 Assert.notNull(view, "View not found with id: " + viewId);
990
991 form.setView(view);
992 form.setViewId(viewId);
993
994 return UifControllerHelper.getUIFModelAndView(form, form.getPageId());
995 }
996
997
998
999
1000
1001
1002
1003
1004
1005
1006 protected ModelAndView getUIFModelAndView(UifFormBase form, Map<String, Object> additionalViewAttributes) {
1007 ModelAndView modelAndView = UifControllerHelper.getUIFModelAndView(form, form.getPageId());
1008
1009 if (additionalViewAttributes != null) {
1010 for (Map.Entry<String, Object> additionalViewAttribute : additionalViewAttributes.entrySet()) {
1011 modelAndView.getModelMap().put(additionalViewAttribute.getKey(), additionalViewAttribute.getValue());
1012 }
1013 }
1014
1015 return modelAndView;
1016 }
1017
1018 protected ViewService getViewService() {
1019 return KRADServiceLocatorWeb.getViewService();
1020 }
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031 @RequestMapping(method = RequestMethod.GET, params = "methodToCall=" + UifConstants.MethodToCallNames.TABLE_CSV,
1032 produces = {"text/csv"})
1033 @ResponseBody
1034 public String tableCsvRetrieval(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
1035 HttpServletRequest request, HttpServletResponse response) {
1036 LOG.debug("processing csv table data request");
1037
1038 return retrieveTableData(form, result, request, response);
1039 }
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050 @RequestMapping(method = RequestMethod.GET, params = "methodToCall=" + UifConstants.MethodToCallNames.TABLE_XLS,
1051 produces = {"application/vnd.ms-excel"})
1052 @ResponseBody
1053 public String tableXlsRetrieval(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
1054 HttpServletRequest request, HttpServletResponse response) {
1055 LOG.debug("processing xls table data request");
1056
1057 return retrieveTableData(form, result, request, response);
1058 }
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069 @RequestMapping(method = RequestMethod.GET, params = "methodToCall=" + UifConstants.MethodToCallNames.TABLE_XML,
1070 produces = {"application/xml"})
1071 @ResponseBody
1072 public String tableXmlRetrieval(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
1073 HttpServletRequest request, HttpServletResponse response) {
1074 LOG.debug("processing xml table data request");
1075
1076 return retrieveTableData(form, result, request, response);
1077 }
1078
1079
1080
1081
1082 private String retrieveTableData(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
1083 HttpServletRequest request, HttpServletResponse response) {
1084 LOG.debug("processing table data request");
1085
1086 String tableData = "";
1087 String formatType = getValidatedFormatType(request.getParameter("formatType"));
1088 String contentType = getContentType(formatType);
1089
1090 UifFormManager uifFormManager = (UifFormManager) request.getSession().getAttribute(UifParameters.FORM_MANAGER);
1091 String formKey = request.getParameter(UifParameters.FORM_KEY);
1092 String tableId = request.getParameter(UifParameters.TABLE_ID);
1093 UifFormBase currentForm = uifFormManager.getSessionForm(formKey);
1094 View view;
1095 if (currentForm.getPostedView() != null) {
1096 view = currentForm.getPostedView();
1097 } else {
1098 view = currentForm.getView();
1099 }
1100
1101 LOG.debug("identifying table from model and form");
1102 tableData = view.getViewHelperService().buildExportTableData(view, currentForm, tableId, formatType);
1103
1104
1105 response.setHeader("content-type", contentType);
1106 response.setHeader("Content-disposition", "attachment; filename=\"export." + formatType + "\"");
1107 response.setHeader("Expires", "0");
1108 response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
1109 response.setHeader("Pragma", "public");
1110
1111 return tableData;
1112 }
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126 @RequestMapping(params = "methodToCall=retrieveCollectionPage")
1127 public ModelAndView retrieveCollectionPage(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
1128 HttpServletRequest request, HttpServletResponse response) throws Exception {
1129 String collectionId = request.getParameter(UifParameters.UPDATE_COMPONENT_ID);
1130 String pageNumber = request.getParameter(UifConstants.PageRequest.PAGE_NUMBER);
1131
1132 CollectionPagingHelper pagingHelper = new CollectionPagingHelper();
1133 pagingHelper.processPagingRequest(form.getPostedView(), collectionId, form, pageNumber);
1134
1135 return getUIFModelAndView(form);
1136 }
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159 @RequestMapping(params = "methodToCall=retrieveOriginalComponent")
1160 public ModelAndView retrieveOriginalComponent(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
1161 HttpServletRequest request, HttpServletResponse response) throws Exception {
1162 String componentId = request.getParameter(UifParameters.UPDATE_COMPONENT_ID);
1163 form.setOriginalComponentRequest(true);
1164
1165 return getUIFModelAndView(form);
1166 }
1167
1168
1169
1170
1171
1172
1173
1174 private String getValidatedFormatType(String formatType) {
1175 if ("xls".equals(formatType) || "xml".equals(formatType) || "csv".equals(formatType)) {
1176 return formatType;
1177 }
1178 return "csv";
1179 }
1180
1181
1182
1183
1184
1185
1186
1187 private String getContentType(String formatType) {
1188 if ("csv".equals(formatType)) {
1189 return "text/csv";
1190 } else if ("xls".equals(formatType)) {
1191 return "application/vnd.ms-excel";
1192 } else if ("xml".equals(formatType)) {
1193 return "application/xml";
1194 }
1195 return "text/csv";
1196 }
1197
1198
1199
1200
1201
1202
1203
1204 @RequestMapping(method = RequestMethod.GET, params = "methodToCall=tableJsonRetrieval")
1205 public ModelAndView tableJsonRetrieval(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
1206 HttpServletRequest request, HttpServletResponse response) {
1207 String tableId = request.getParameter(UifParameters.TABLE_ID);
1208
1209 DataTablesPagingHelper.DataTablesInputs dataTablesInputs = new DataTablesPagingHelper.DataTablesInputs(request);
1210
1211 DataTablesPagingHelper pagingHelper = createDataTablesPagingHelperInstance(form, request);
1212 pagingHelper.processPagingRequest(form.getPostedView(), tableId, form, dataTablesInputs);
1213
1214 Map<String, Object> additionalViewAttributes = new HashMap<String, Object>();
1215 additionalViewAttributes.put(UifParameters.DATA_TABLES_PAGING_HELPER, pagingHelper);
1216
1217 return getUIFModelAndView(form, additionalViewAttributes);
1218 }
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231 protected DataTablesPagingHelper createDataTablesPagingHelperInstance(UifFormBase form,
1232 HttpServletRequest request) {
1233 return new DataTablesPagingHelper();
1234 }
1235 }