View Javadoc

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