View Javadoc

1   /**
2    * Copyright 2012 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   *
15   */
16  package org.kuali.student.enrollment.uif.controller;
17  
18  import org.apache.commons.lang.BooleanUtils;
19  import org.apache.commons.lang.StringUtils;
20  import org.apache.log4j.Logger;
21  import org.kuali.rice.core.api.exception.RiceRuntimeException;
22  import org.kuali.rice.krad.datadictionary.DataObjectEntry;
23  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
24  import org.kuali.rice.krad.service.ModuleService;
25  import org.kuali.rice.krad.uif.UifConstants;
26  import org.kuali.rice.krad.uif.UifParameters;
27  import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
28  import org.kuali.rice.krad.util.GlobalVariables;
29  import org.kuali.rice.krad.util.KRADConstants;
30  import org.kuali.rice.krad.util.KRADUtils;
31  import org.kuali.rice.krad.web.controller.LookupController;
32  import org.kuali.rice.krad.web.controller.UifControllerHelper;
33  import org.kuali.rice.krad.web.form.LookupForm;
34  import org.kuali.rice.krad.web.form.UifFormBase;
35  import org.kuali.student.enrollment.uif.view.KSLookupView;
36  import org.springframework.validation.BindingResult;
37  import org.springframework.stereotype.Controller;
38  import org.springframework.web.bind.annotation.ModelAttribute;
39  import org.springframework.web.bind.annotation.RequestMapping;
40  import org.springframework.web.servlet.ModelAndView;
41  
42  import javax.servlet.http.HttpServletRequest;
43  import javax.servlet.http.HttpServletResponse;
44  import java.util.List;
45  import java.util.Properties;
46  
47  /**
48   * This is the base class for the KS Lookup controller which extends from KRAD controller class. This class is intended to
49   * provide common functionalities at the KS level.
50   *
51   * @author Kuali Student Team
52   */
53  @Controller
54  @RequestMapping(value = "/lookup")
55  public class KSLookupController extends LookupController {
56  
57      private static final Logger LOG = Logger.getLogger(KSLookupController.class);
58  
59      @RequestMapping(params = "methodToCall=start")
60      @Override
61      public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
62              HttpServletRequest request, HttpServletResponse response) {
63  
64          LookupForm lookupForm = (LookupForm) form;
65  
66          // if request is not a redirect, determine if we need to redirect for an externalizable object lookup
67          if (!lookupForm.isRedirectedLookup()) {
68              Class lookupObjectClass = null;
69              try {
70                  lookupObjectClass = Class.forName(lookupForm.getDataObjectClassName());
71              } catch (ClassNotFoundException e) {
72                  throw new RiceRuntimeException("Unable to get class for name: " + lookupForm.getDataObjectClassName());
73              }
74  
75              ModuleService responsibleModuleService =
76                      KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(lookupObjectClass);
77              if (responsibleModuleService != null && responsibleModuleService.isExternalizable(lookupObjectClass)) {
78                  String lookupUrl = responsibleModuleService.getExternalizableDataObjectLookupUrl(lookupObjectClass,
79                          KRADUtils.convertRequestMapToProperties(request.getParameterMap()));
80  
81                  Properties redirectUrlProps = new Properties();
82                  redirectUrlProps.put(UifParameters.REDIRECTED_LOOKUP, "true");
83                  UifControllerHelper.prepareHistory(request, form);
84                  // clear current form from session
85                  GlobalVariables.getUifFormManager().removeSessionForm(form);
86  
87                  return performRedirect(form, lookupUrl, redirectUrlProps);
88              }
89          }
90  
91          return super.start(lookupForm, result, request, response);
92      }
93  
94      /**
95       * Overrides the KRAD search functionality to perform redirect on single search result.
96       */
97      @RequestMapping(params = "methodToCall=search")
98      public ModelAndView search(@ModelAttribute("KualiForm") LookupForm lookupForm, BindingResult result,
99                                 HttpServletRequest request, HttpServletResponse response) {
100 
101         ModelAndView modelAndView = super.search(lookupForm,result,request,response);
102 
103         if(lookupForm.getPostedView() instanceof KSLookupView){
104             KSLookupView ksLookupView = (KSLookupView)lookupForm.getPostedView();
105             String defaultAction = ksLookupView.getDefaultSingleLookupResultAction();
106             if (StringUtils.isNotBlank(defaultAction) && lookupForm.getLookupResults() != null && lookupForm.getLookupResults().size() == 1){
107                 Object object = lookupForm.getLookupResults().iterator().next();
108 
109                 Properties props = new Properties();
110 
111                 DataObjectEntry ddEntry = KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary().getDataObjectEntry(lookupForm.getDataObjectClassName());
112 
113                 List<String> pkKeys = ddEntry.getPrimaryKeys();
114                 for (String pkKey : pkKeys) {
115                     props.put(pkKey,ObjectPropertyUtils.getPropertyValue(object, pkKey));
116                 }
117 
118                 if(StringUtils.equals(defaultAction,KRADConstants.PARAM_MAINTENANCE_VIEW_MODE_MAINTENANCE)){
119                     props.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.Maintenance.METHOD_TO_CALL_EDIT);
120                 }  else{
121                     props.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, UifConstants.MethodToCallNames.START);
122                 }
123                 props.put(UifConstants.UrlParams.SHOW_HISTORY, BooleanUtils.toStringTrueFalse(false));
124                 props.put(UifConstants.UrlParams.SHOW_HOME,BooleanUtils.toStringTrueFalse(false));
125                 props.put(KRADConstants.DATA_OBJECT_CLASS_ATTRIBUTE,lookupForm.getDataObjectClassName());
126 
127                 return performRedirect(lookupForm,defaultAction,props );
128             }
129         }
130 
131         return modelAndView;
132     }
133 
134 }