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.kuali.rice.core.api.config.property.ConfigContext;
20 import org.kuali.rice.core.web.format.BooleanFormatter;
21 import org.kuali.rice.kim.api.identity.Person;
22 import org.kuali.rice.krad.exception.AuthorizationException;
23 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
24 import org.kuali.rice.krad.service.ModuleService;
25 import org.kuali.rice.krad.service.SessionDocumentService;
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.CollectionGroup;
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.ComponentFactory;
34 import org.kuali.rice.krad.uif.util.LookupInquiryUtils;
35 import org.kuali.rice.krad.uif.util.UifFormManager;
36 import org.kuali.rice.krad.uif.util.UifWebUtils;
37 import org.kuali.rice.krad.uif.view.History;
38 import org.kuali.rice.krad.uif.view.HistoryEntry;
39 import org.kuali.rice.krad.uif.view.View;
40 import org.kuali.rice.krad.util.GlobalVariables;
41 import org.kuali.rice.krad.util.KRADConstants;
42 import org.kuali.rice.krad.util.KRADUtils;
43 import org.kuali.rice.krad.util.UrlFactory;
44 import org.kuali.rice.krad.web.form.UifFormBase;
45 import org.springframework.validation.BindingResult;
46 import org.springframework.web.bind.annotation.ModelAttribute;
47 import org.springframework.web.bind.annotation.RequestMapping;
48 import org.springframework.web.bind.annotation.RequestMethod;
49 import org.springframework.web.bind.annotation.ResponseBody;
50 import org.springframework.web.servlet.ModelAndView;
51
52 import javax.servlet.http.HttpServletRequest;
53 import javax.servlet.http.HttpServletResponse;
54 import java.util.HashMap;
55 import java.util.List;
56 import java.util.Map;
57 import java.util.Map.Entry;
58 import java.util.Properties;
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 public abstract class UifControllerBase {
78 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(UifControllerBase.class);
79
80 protected static final String REDIRECT_PREFIX = "redirect:";
81
82
83
84
85
86
87
88
89 @ModelAttribute(value = "KualiForm")
90 public final UifFormBase initForm(HttpServletRequest request) {
91 UifFormBase form = null;
92
93
94 UifFormManager uifFormManager = (UifFormManager) request.getSession().getAttribute(
95 UifParameters.FORM_MANAGER);
96 if (uifFormManager == null) {
97 uifFormManager = new UifFormManager();
98 request.getSession().setAttribute(UifParameters.FORM_MANAGER, uifFormManager);
99 }
100
101
102 GlobalVariables.setUifFormManager(uifFormManager);
103
104 String formKeyParam = request.getParameter(UifParameters.FORM_KEY);
105 if (StringUtils.isNotBlank(formKeyParam)) {
106 form = uifFormManager.getForm(formKeyParam);
107 } else {
108 form = createInitialForm(request);
109 }
110
111 uifFormManager.setCurrentForm(form);
112
113 return form;
114 }
115
116
117
118
119
120
121
122
123 protected abstract UifFormBase createInitialForm(HttpServletRequest request);
124
125
126
127
128
129 @RequestMapping(params = "methodToCall=start")
130 public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
131 HttpServletRequest request, HttpServletResponse response) {
132
133
134
135 if (form.getView() != null) {
136 String methodToCall = request.getParameter(KRADConstants.DISPATCH_REQUEST_PARAMETER);
137 checkViewAuthorization(form, methodToCall);
138 }
139
140 return getUIFModelAndView(form);
141 }
142
143
144
145
146
147
148
149
150
151
152
153
154
155 public void checkViewAuthorization(UifFormBase form, String methodToCall) throws AuthorizationException {
156 Person user = GlobalVariables.getUserSession().getPerson();
157
158 boolean canOpenView = form.getView().getAuthorizer().canOpenView(form.getView(), form, user);
159 if (!canOpenView) {
160 throw new AuthorizationException(user.getPrincipalName(), "open", form.getView().getId(),
161 "User '" + user.getPrincipalName() + "' is not authorized to open view ID: " + form.getView()
162 .getId(), null);
163 }
164 }
165
166
167
168
169
170
171 @RequestMapping(method = RequestMethod.POST, params = "methodToCall=addLine")
172 public ModelAndView addLine(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
173 HttpServletRequest request, HttpServletResponse response) {
174
175 String selectedCollectionPath = uifForm.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
176 if (StringUtils.isBlank(selectedCollectionPath)) {
177 throw new RuntimeException("Selected collection was not set for add line action, cannot add new line");
178 }
179
180 View view = uifForm.getPreviousView();
181 view.getViewHelperService().processCollectionAddLine(view, uifForm, selectedCollectionPath);
182
183 return updateComponent(uifForm, result, request, response);
184 }
185
186
187
188
189
190
191
192 @RequestMapping(method = RequestMethod.POST, params = "methodToCall=deleteLine")
193 public ModelAndView deleteLine(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
194 HttpServletRequest request, HttpServletResponse response) {
195
196 String selectedCollectionPath = uifForm.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
197 if (StringUtils.isBlank(selectedCollectionPath)) {
198 throw new RuntimeException("Selected collection was not set for delete line action, cannot delete line");
199 }
200
201 int selectedLineIndex = -1;
202 String selectedLine = uifForm.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX);
203 if (StringUtils.isNotBlank(selectedLine)) {
204 selectedLineIndex = Integer.parseInt(selectedLine);
205 }
206
207 if (selectedLineIndex == -1) {
208 throw new RuntimeException("Selected line index was not set for delete line action, cannot delete line");
209 }
210
211 View view = uifForm.getPreviousView();
212 view.getViewHelperService().processCollectionDeleteLine(view, uifForm, selectedCollectionPath,
213 selectedLineIndex);
214
215 return updateComponent(uifForm, result, request, response);
216 }
217
218
219
220
221
222
223
224
225 @RequestMapping(method = RequestMethod.POST, params = "methodToCall=toggleInactiveRecordDisplay")
226 public ModelAndView toggleInactiveRecordDisplay(@ModelAttribute("KualiForm") UifFormBase uifForm,
227 BindingResult result, HttpServletRequest request, HttpServletResponse response) {
228 String collectionGroupId = request.getParameter(UifParameters.REQUESTED_COMPONENT_ID);
229 if (StringUtils.isBlank(collectionGroupId)) {
230 throw new RuntimeException(
231 "Collection group id to update for inactive record display not found in request");
232 }
233
234 String showInactiveStr = request.getParameter(UifParameters.SHOW_INACTIVE_RECORDS);
235 Boolean showInactive = false;
236 if (StringUtils.isNotBlank(showInactiveStr)) {
237
238 showInactive = (Boolean) (new BooleanFormatter()).convertFromPresentationFormat(showInactiveStr);
239 } else {
240 throw new RuntimeException("Show inactive records flag not found in request");
241 }
242
243 CollectionGroup collectionGroup = (CollectionGroup) ComponentFactory.getNewInstanceForRefresh(uifForm.getView(),
244 collectionGroupId);
245
246
247 collectionGroup.setShowInactive(showInactive);
248
249
250 uifForm.getView().getViewHelperService().performComponentLifecycle(uifForm.getView(), uifForm, collectionGroup,
251 collectionGroupId);
252
253 return UifWebUtils.getComponentModelAndView(collectionGroup, uifForm);
254 }
255
256
257
258
259 @RequestMapping(params = "methodToCall=cancel")
260 public ModelAndView cancel(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
261 HttpServletRequest request, HttpServletResponse response) {
262 return close(form, result, request, response);
263 }
264
265
266
267
268 @RequestMapping(params = "methodToCall=close")
269 public ModelAndView close(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
270 HttpServletRequest request, HttpServletResponse response) {
271 Properties props = new Properties();
272 props.put(UifParameters.METHOD_TO_CALL, UifConstants.MethodToCallNames.REFRESH);
273 if (StringUtils.isNotBlank(form.getReturnFormKey())) {
274 props.put(UifParameters.FORM_KEY, form.getReturnFormKey());
275 }
276
277
278
279 String returnUrl = form.getReturnLocation();
280 if (StringUtils.isBlank(returnUrl)) {
281 returnUrl = ConfigContext.getCurrentContextConfig().getProperty(KRADConstants.APPLICATION_URL_KEY);
282 }
283
284 return performRedirect(form, returnUrl, props);
285 }
286
287
288
289
290
291
292 @RequestMapping(params = "methodToCall=returnToPrevious")
293 public ModelAndView returnToPrevious(@ModelAttribute("KualiForm") UifFormBase form) {
294
295 return returnToHistory(form, false);
296 }
297
298
299
300
301
302
303 @RequestMapping(params = "methodToCall=returnToHub")
304 public ModelAndView returnToHub(@ModelAttribute("KualiForm") UifFormBase form) {
305
306 return returnToHistory(form, true);
307 }
308
309
310
311
312
313
314
315
316
317 public ModelAndView returnToHistory(UifFormBase form, boolean homeFlag) {
318
319 History hist = form.getFormHistory();
320 List<HistoryEntry> histEntries = hist.getHistoryEntries();
321
322
323 String histUrl = null;
324 if (histEntries.isEmpty()) {
325 histUrl = ConfigContext.getCurrentContextConfig().getProperty(KRADConstants.APPLICATION_URL_KEY);
326 } else {
327
328
329 if (homeFlag) {
330 histUrl = histEntries.get(0).getUrl();
331 histEntries.clear();
332 } else {
333 histUrl = histEntries.get(histEntries.size() - 1).getUrl();
334 histEntries.remove(histEntries.size() - 1);
335 hist.setCurrent(null);
336 }
337 }
338
339
340 Properties props = new Properties();
341 props.put(UifParameters.METHOD_TO_CALL, UifConstants.MethodToCallNames.REFRESH);
342
343 return performRedirect(form, histUrl, props);
344 }
345
346
347
348
349 @RequestMapping(method = RequestMethod.POST, params = "methodToCall=navigate")
350 public ModelAndView navigate(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
351 HttpServletRequest request, HttpServletResponse response) {
352 String pageId = form.getActionParamaterValue(UifParameters.NAVIGATE_TO_PAGE_ID);
353
354
355 form.setRenderFullView(false);
356
357 return getUIFModelAndView(form, pageId);
358 }
359
360 @RequestMapping(params = "methodToCall=refresh")
361 public ModelAndView refresh(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
362 HttpServletRequest request, HttpServletResponse response) throws Exception {
363
364 String refreshCallerType = "";
365 if (request.getParameterMap().containsKey(KRADConstants.REFRESH_CALLER_TYPE)) {
366 refreshCallerType = request.getParameter(KRADConstants.REFRESH_CALLER_TYPE);
367 }
368
369
370 if (StringUtils.equals(refreshCallerType, UifConstants.RefreshCallerTypes.MULTI_VALUE_LOOKUP)) {
371 String lookupCollectionName = "";
372 if (request.getParameterMap().containsKey(UifParameters.LOOKUP_COLLECTION_NAME)) {
373 lookupCollectionName = request.getParameter(UifParameters.LOOKUP_COLLECTION_NAME);
374 }
375
376 if (StringUtils.isBlank(lookupCollectionName)) {
377 throw new RuntimeException(
378 "Lookup collection name is required for processing multi-value lookup results");
379 }
380
381 String selectedLineValues = "";
382 if (request.getParameterMap().containsKey(UifParameters.SELECTED_LINE_VALUES)) {
383 selectedLineValues = request.getParameter(UifParameters.SELECTED_LINE_VALUES);
384 }
385
386
387 form.getPreviousView().getViewHelperService().processMultipleValueLookupResults(form.getPreviousView(),
388 form, lookupCollectionName, selectedLineValues);
389 }
390
391 form.setRenderFullView(true);
392
393 return getUIFModelAndView(form);
394 }
395
396
397
398
399
400
401
402 @RequestMapping(method = RequestMethod.POST, params = "methodToCall=updateComponent")
403 public ModelAndView updateComponent(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
404 HttpServletRequest request, HttpServletResponse response) {
405 String requestedComponentId = request.getParameter(UifParameters.REQUESTED_COMPONENT_ID);
406 if (StringUtils.isBlank(requestedComponentId)) {
407 throw new RuntimeException("Requested component id for update not found in request");
408 }
409
410
411 Component comp = ComponentFactory.getNewInstanceForRefresh(form.getView(), requestedComponentId);
412
413
414 form.getView().getViewHelperService().performComponentLifecycle(form.getView(), form, comp,
415 requestedComponentId);
416
417
418 form.getView().getCurrentPage().getErrorsField().setDisplayNestedMessages(true);
419 form.getView().getCurrentPage().getErrorsField().generateMessages(false, form.getView(),
420 form,form.getView().getCurrentPage());
421
422 return UifWebUtils.getComponentModelAndView(comp, form);
423 }
424
425
426
427
428
429 @RequestMapping(method = RequestMethod.POST, params = "methodToCall=performLookup")
430 public ModelAndView performLookup(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
431 HttpServletRequest request, HttpServletResponse response) {
432 Properties lookupParameters = form.getActionParametersAsProperties();
433
434 String lookupObjectClassName = (String) lookupParameters.get(UifParameters.DATA_OBJECT_CLASS_NAME);
435 Class<?> lookupObjectClass = null;
436 try {
437 lookupObjectClass = Class.forName(lookupObjectClassName);
438 } catch (ClassNotFoundException e) {
439 LOG.error("Unable to get class for name: " + lookupObjectClassName);
440 throw new RuntimeException("Unable to get class for name: " + lookupObjectClassName, e);
441 }
442
443
444 String lookupParameterString = (String) lookupParameters.get(UifParameters.LOOKUP_PARAMETERS);
445 if (lookupParameterString != null) {
446 Map<String, String> lookupParameterFields = KRADUtils.getMapFromParameterString(lookupParameterString);
447 for (Entry<String, String> lookupParameter : lookupParameterFields.entrySet()) {
448 String lookupParameterValue = LookupInquiryUtils.retrieveLookupParameterValue(form, request,
449 lookupObjectClass, lookupParameter.getValue(), lookupParameter.getKey());
450
451 if (StringUtils.isNotBlank(lookupParameterValue)) {
452 lookupParameters.put(UifPropertyPaths.CRITERIA_FIELDS + "['" + lookupParameter.getValue() + "']",
453 lookupParameterValue);
454 }
455 }
456 }
457
458
459
460 String baseLookupUrl = (String) lookupParameters.get(UifParameters.BASE_LOOKUP_URL);
461 lookupParameters.remove(UifParameters.BASE_LOOKUP_URL);
462
463
464 lookupParameters.put(UifParameters.METHOD_TO_CALL, UifConstants.MethodToCallNames.START);
465 String autoSearchString = (String) lookupParameters.get(UifParameters.AUTO_SEARCH);
466 if (Boolean.parseBoolean(autoSearchString)) {
467 lookupParameters.put(UifParameters.METHOD_TO_CALL, UifConstants.MethodToCallNames.SEARCH);
468 }
469
470 lookupParameters.put(UifParameters.RETURN_LOCATION, form.getFormPostUrl());
471 lookupParameters.put(UifParameters.RETURN_FORM_KEY, form.getFormKey());
472
473
474 if (lookupObjectClass != null) {
475 ModuleService responsibleModuleService =
476 KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(lookupObjectClass);
477 if (responsibleModuleService != null && responsibleModuleService.isExternalizable(lookupObjectClass)) {
478 String lookupUrl = responsibleModuleService.getExternalizableDataObjectLookupUrl(lookupObjectClass,
479 lookupParameters);
480
481 Properties externalInquiryProperties = new Properties();
482 if (lookupParameters.containsKey(UifParameters.LIGHTBOX_CALL)) {
483 externalInquiryProperties.put(UifParameters.LIGHTBOX_CALL, lookupParameters.get(
484 UifParameters.LIGHTBOX_CALL));
485 }
486
487 return performRedirect(form, lookupUrl, externalInquiryProperties);
488 }
489 }
490
491 return performRedirect(form, baseLookupUrl, lookupParameters);
492 }
493
494
495
496
497
498
499
500 @RequestMapping(method = RequestMethod.GET, params = "methodToCall=performFieldSuggest")
501 public
502 @ResponseBody
503 AttributeQueryResult performFieldSuggest(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
504 HttpServletRequest request, HttpServletResponse response) {
505
506
507 Map<String, String> queryParameters = new HashMap<String, String>();
508 for (Object parameterName : request.getParameterMap().keySet()) {
509 if (parameterName.toString().startsWith(UifParameters.QUERY_PARAMETER + ".")) {
510 String fieldName = StringUtils.substringAfter(parameterName.toString(),
511 UifParameters.QUERY_PARAMETER + ".");
512 String fieldValue = request.getParameter(parameterName.toString());
513 queryParameters.put(fieldName, fieldValue);
514 }
515 }
516
517
518 String queryFieldId = request.getParameter(UifParameters.QUERY_FIELD_ID);
519 if (StringUtils.isBlank(queryFieldId)) {
520 throw new RuntimeException("Unable to find id for field to perform query on under request parameter name: "
521 + UifParameters.QUERY_FIELD_ID);
522 }
523
524
525 String queryTerm = request.getParameter(UifParameters.QUERY_TERM);
526 if (StringUtils.isBlank(queryTerm)) {
527 throw new RuntimeException(
528 "Unable to find id for query term value for attribute query on under request parameter name: "
529 + UifParameters.QUERY_TERM);
530 }
531
532
533 AttributeQueryResult queryResult = KRADServiceLocatorWeb.getAttributeQueryService().performFieldSuggestQuery(
534 form.getView(), queryFieldId, queryTerm, queryParameters);
535
536 return queryResult;
537 }
538
539
540
541
542
543
544
545
546 @RequestMapping(method = RequestMethod.GET, params = "methodToCall=performFieldQuery")
547 public
548 @ResponseBody
549 AttributeQueryResult performFieldQuery(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
550 HttpServletRequest request, HttpServletResponse response) {
551
552
553 Map<String, String> queryParameters = new HashMap<String, String>();
554 for (Object parameterName : request.getParameterMap().keySet()) {
555 if (parameterName.toString().startsWith(UifParameters.QUERY_PARAMETER + ".")) {
556 String fieldName = StringUtils.substringAfter(parameterName.toString(),
557 UifParameters.QUERY_PARAMETER + ".");
558 String fieldValue = request.getParameter(parameterName.toString());
559 queryParameters.put(fieldName, fieldValue);
560 }
561 }
562
563
564 String queryFieldId = request.getParameter(UifParameters.QUERY_FIELD_ID);
565 if (StringUtils.isBlank(queryFieldId)) {
566 throw new RuntimeException("Unable to find id for field to perform query on under request parameter name: "
567 + UifParameters.QUERY_FIELD_ID);
568 }
569
570
571 AttributeQueryResult queryResult = KRADServiceLocatorWeb.getAttributeQueryService().performFieldQuery(
572 form.getView(), queryFieldId, queryParameters);
573
574 return queryResult;
575 }
576
577
578
579
580
581
582
583
584
585
586
587 protected ModelAndView performRedirect(UifFormBase form, String baseUrl, Properties urlParameters) {
588
589 form.setView(form.getPreviousView());
590
591
592 GlobalVariables.getUifFormManager().removeForm(form);
593
594
595 urlParameters.setProperty(UifConstants.UrlParams.HISTORY, form.getFormHistory().getHistoryParameterString());
596
597
598
599 if (urlParameters.get(UifParameters.LIGHTBOX_CALL) != null &&
600 urlParameters.get(UifParameters.LIGHTBOX_CALL).equals("true")) {
601 urlParameters.remove(UifParameters.LIGHTBOX_CALL);
602 String redirectUrl = UrlFactory.parameterizeUrl(baseUrl, urlParameters);
603
604 ModelAndView modelAndView = new ModelAndView(UifConstants.SPRING_REDIRECT_ID);
605 modelAndView.addObject("redirectUrl", redirectUrl);
606 return modelAndView;
607 }
608
609 String redirectUrl = UrlFactory.parameterizeUrl(baseUrl, urlParameters);
610 ModelAndView modelAndView = new ModelAndView(REDIRECT_PREFIX + redirectUrl);
611
612 return modelAndView;
613 }
614
615 protected ModelAndView getUIFModelAndView(UifFormBase form) {
616 return getUIFModelAndView(form, form.getPageId());
617 }
618
619
620
621
622
623
624
625
626
627
628 protected ModelAndView getUIFModelAndView(UifFormBase form, String pageId) {
629 return UifWebUtils.getUIFModelAndView(form, pageId);
630 }
631
632 protected ViewService getViewService() {
633 return KRADServiceLocatorWeb.getViewService();
634 }
635
636 public SessionDocumentService getSessionDocumentService() {
637 return KRADServiceLocatorWeb.getSessionDocumentService();
638 }
639
640 }