View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.web.controller;
17  
18  import java.util.Collection;
19  import java.util.Map;
20  import java.util.Properties;
21  import java.util.Set;
22  
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  
26  import org.apache.commons.lang.StringUtils;
27  import org.kuali.rice.core.api.exception.RiceRuntimeException;
28  import org.kuali.rice.krad.lookup.CollectionIncomplete;
29  import org.kuali.rice.krad.lookup.LookupUtils;
30  import org.kuali.rice.krad.lookup.Lookupable;
31  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
32  import org.kuali.rice.krad.service.ModuleService;
33  import org.kuali.rice.krad.uif.UifConstants;
34  import org.kuali.rice.krad.uif.UifParameters;
35  import org.kuali.rice.krad.uif.UifPropertyPaths;
36  import org.kuali.rice.krad.util.GlobalVariables;
37  import org.kuali.rice.krad.util.KRADConstants;
38  import org.kuali.rice.krad.util.KRADUtils;
39  import org.kuali.rice.krad.web.form.LookupForm;
40  import org.kuali.rice.krad.web.form.UifFormBase;
41  import org.springframework.stereotype.Controller;
42  import org.springframework.validation.BindingResult;
43  import org.springframework.web.bind.annotation.ModelAttribute;
44  import org.springframework.web.bind.annotation.RequestMapping;
45  import org.springframework.web.bind.annotation.RequestMethod;
46  import org.springframework.web.servlet.ModelAndView;
47  
48  /**
49   * Controller that handles requests coming from a <code>LookupView</code>
50   *
51   * @author Kuali Rice Team (rice.collab@kuali.org)
52   */
53  @Controller
54  @RequestMapping(value = "/lookup")
55  public class LookupController extends UifControllerBase {
56      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(LookupController.class);
57  
58      /**
59       * @see org.kuali.rice.krad.web.controller.UifControllerBase#createInitialForm(javax.servlet.http.HttpServletRequest)
60       */
61      @Override
62      protected LookupForm createInitialForm(HttpServletRequest request) {
63          return new LookupForm();
64      }
65  
66      /**
67       * Invoked to request an lookup view for a data object class
68       *
69       * <p>
70       * Checks if the data object is externalizable and we need to redirect to the appropriate lookup URL, else
71       * continues with the lookup view display
72       * </p>
73       */
74      @RequestMapping(params = "methodToCall=start")
75      @Override
76      public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
77              HttpServletRequest request, HttpServletResponse response) {
78          LookupForm lookupForm = (LookupForm) form;
79  
80          Lookupable lookupable = lookupForm.getLookupable();
81          if (lookupable == null) {
82              LOG.error("Lookupable is null.");
83              throw new RuntimeException("Lookupable is null.");
84          }
85          lookupable.initSuppressAction(lookupForm);
86  
87          // if request is not a redirect, determine if we need to redirect for an externalizable object lookup
88          if (!lookupForm.isRedirectedLookup()) {
89              Class lookupObjectClass = null;
90              try {
91                  lookupObjectClass = Class.forName(lookupForm.getDataObjectClassName());
92              } catch (ClassNotFoundException e) {
93                  throw new RiceRuntimeException("Unable to get class for name: " + lookupForm.getDataObjectClassName());
94              }
95  
96              ModuleService responsibleModuleService =
97                      KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(lookupObjectClass);
98              if (responsibleModuleService != null && responsibleModuleService.isExternalizable(lookupObjectClass)) {
99                  String lookupUrl = responsibleModuleService.getExternalizableDataObjectLookupUrl(lookupObjectClass,
100                         KRADUtils.convertRequestMapToProperties(request.getParameterMap()));
101 
102                 Properties redirectUrlProps = new Properties();
103                 redirectUrlProps.put(UifParameters.REDIRECTED_LOOKUP, "true");
104 
105                 // clear current form from session
106                 GlobalVariables.getUifFormManager().removeSessionForm(form);
107 
108                 return performRedirect(form, lookupUrl, redirectUrlProps);
109             }
110         }
111 
112         return super.start(lookupForm, result, request, response);
113     }
114 
115     /**
116      * Just returns as if return with no value was selected
117      */
118     @Override
119     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=cancel")
120     public ModelAndView cancel(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
121             HttpServletRequest request, HttpServletResponse response) {
122         LookupForm lookupForm = (LookupForm) form;
123 
124         Lookupable lookupable = lookupForm.getLookupable();
125         if (lookupable == null) {
126             LOG.error("Lookupable is null.");
127             throw new RuntimeException("Lookupable is null.");
128         }
129         lookupable.initSuppressAction(lookupForm);
130 
131         Properties props = new Properties();
132         props.put(UifParameters.METHOD_TO_CALL, UifConstants.MethodToCallNames.REFRESH);
133         if (StringUtils.isNotBlank(lookupForm.getReturnFormKey())) {
134             props.put(UifParameters.FORM_KEY, lookupForm.getReturnFormKey());
135         }
136         if (StringUtils.isNotBlank(lookupForm.getDocNum())) {
137             props.put(UifParameters.DOC_NUM, lookupForm.getDocNum());
138         }
139 
140         // clear current form from session
141         GlobalVariables.getUifFormManager().removeSessionForm(form);
142 
143         return performRedirect(lookupForm, lookupForm.getReturnLocation(), props);
144     }
145 
146     /**
147      * clearValues - clears the values of all the fields on the jsp.
148      */
149     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=clearValues")
150     public ModelAndView clearValues(@ModelAttribute("KualiForm") LookupForm lookupForm, BindingResult result,
151             HttpServletRequest request, HttpServletResponse response) {
152 
153         Lookupable lookupable = lookupForm.getLookupable();
154         if (lookupable == null) {
155             LOG.error("Lookupable is null.");
156             throw new RuntimeException("Lookupable is null.");
157         }
158         lookupable.initSuppressAction(lookupForm);
159         lookupForm.setLookupCriteria(lookupable.performClear(lookupForm, lookupForm.getLookupCriteria()));
160 
161         return getUIFModelAndView(lookupForm);
162     }
163 
164     /**
165      * search - sets the values of the data entered on the form on the jsp into a map and then searches for the
166      * results.
167      */
168     @RequestMapping(params = "methodToCall=search")
169     public ModelAndView search(@ModelAttribute("KualiForm") LookupForm lookupForm, BindingResult result,
170             HttpServletRequest request, HttpServletResponse response) {
171 
172         Lookupable lookupable = lookupForm.getLookupable();
173         if (lookupable == null) {
174             LOG.error("Lookupable is null.");
175             throw new RuntimeException("Lookupable is null.");
176         }
177         lookupable.initSuppressAction(lookupForm);
178         // Need to process date range fields before validating
179         Map<String, String> searchCriteria = LookupUtils.preprocessDateFields(lookupForm.getLookupCriteria());
180         // validate search parameters
181         boolean searchValid = lookupable.validateSearchParameters(lookupForm, searchCriteria);
182 
183         if (searchValid) {
184             Collection<?> displayList = lookupable.performSearch(lookupForm, searchCriteria, true);
185 
186             if (displayList instanceof CollectionIncomplete<?>) {
187                 request.setAttribute("reqSearchResultsActualSize",
188                         ((CollectionIncomplete<?>) displayList).getActualSizeIfTruncated());
189             } else {
190                 request.setAttribute("reqSearchResultsActualSize", new Integer(displayList.size()));
191             }
192 
193             lookupForm.setLookupResults(displayList);
194         }
195 
196         return getUIFModelAndView(lookupForm);
197     }
198 
199     /**
200      * Invoked from the UI to return the selected lookup results lines, parameters are collected to build a URL to
201      * the caller and then a redirect is performed
202      *
203      * @param lookupForm - lookup form instance containing the selected results and lookup configuration
204      */
205     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=returnSelected")
206     public ModelAndView returnSelected(@ModelAttribute("KualiForm") LookupForm lookupForm, BindingResult result,
207             HttpServletRequest request, HttpServletResponse response) {
208         Properties parameters = new Properties();
209         parameters.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.RETURN_METHOD_TO_CALL);
210 
211         if (StringUtils.isNotBlank(lookupForm.getReturnFormKey())) {
212             parameters.put(UifParameters.FORM_KEY, lookupForm.getReturnFormKey());
213         }
214 
215         parameters.put(KRADConstants.REFRESH_CALLER, lookupForm.getView().getId());
216         parameters.put(KRADConstants.REFRESH_CALLER_TYPE, UifConstants.RefreshCallerTypes.MULTI_VALUE_LOOKUP);
217         parameters.put(KRADConstants.REFRESH_DATA_OBJECT_CLASS, lookupForm.getDataObjectClassName());
218 
219         if (StringUtils.isNotBlank(lookupForm.getDocNum())) {
220             parameters.put(UifParameters.DOC_NUM, lookupForm.getDocNum());
221         }
222 
223         if (StringUtils.isNotBlank(lookupForm.getLookupCollectionName())) {
224             parameters.put(UifParameters.LOOKUP_COLLECTION_NAME, lookupForm.getLookupCollectionName());
225         }
226 
227         if (StringUtils.isNotBlank(lookupForm.getReferencesToRefresh())) {
228             parameters.put(KRADConstants.REFERENCES_TO_REFRESH, lookupForm.getReferencesToRefresh());
229         }
230 
231         // build string of select line identifiers
232         String selectedLineValues = "";
233         Set<String> selectedLines = lookupForm.getSelectedCollectionLines().get(UifPropertyPaths.SEARCH_RESULTS);
234         if (selectedLines != null) {
235             for (String selectedLine : selectedLines) {
236                 selectedLineValues += selectedLine + ",";
237             }
238             selectedLineValues = StringUtils.removeEnd(selectedLineValues, ",");
239         }
240 
241         parameters.put(UifParameters.SELECTED_LINE_VALUES, selectedLineValues);
242 
243         // clear current form from session
244         GlobalVariables.getUifFormManager().removeSessionForm(lookupForm);
245 
246         return performRedirect(lookupForm, lookupForm.getReturnLocation(), parameters);
247     }
248 }