View Javadoc

1   /**
2    * Copyright 2005-2012 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      protected void suppressActionsIfNeeded(LookupForm lookupForm) {
64  //        try {
65  //            // TODO; move to authorizer for lookup view
66  //            Class<?> dataObjectClass = Class.forName(lookupForm.getDataObjectClassName());
67  //            Person user = GlobalVariables.getUserSession().getPerson();
68  //            // check if creating documents is allowed
69  //            String documentTypeName = KRADServiceLocatorWeb.getDocumentDictionaryService()
70  //                    .getMaintenanceDocumentTypeName(dataObjectClass);
71  //            if ((documentTypeName != null) &&
72  //                    !KRADServiceLocatorWeb.getDocumentHelperService().getDocumentAuthorizer(documentTypeName)
73  //                            .canInitiate(documentTypeName, user)) {
74  //                ((LookupView) lookupForm.getView()).setSuppressActions(true);
75  //            }
76  //        } catch (ClassNotFoundException e) {
77  //            LOG.warn("Unable to load Data Object Class: " + lookupForm.getDataObjectClassName(), e);
78  //        }
79      }
80  
81      /**
82       * Invoked to request an lookup view for a data object class
83       *
84       * <p>
85       * Checks if the data object is externalizable and we need to redirect to the appropriate lookup URL, else
86       * continues with the lookup view display
87       * </p>
88       */
89      @RequestMapping(params = "methodToCall=start")
90      @Override
91      public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
92              HttpServletRequest request, HttpServletResponse response) {
93          LookupForm lookupForm = (LookupForm) form;
94  
95          // if request is not a redirect, determine if we need to redirect for an externalizable object lookup
96          if (!lookupForm.isRedirectedLookup()) {
97              Class lookupObjectClass = null;
98              try {
99                  lookupObjectClass = Class.forName(lookupForm.getDataObjectClassName());
100             } catch (ClassNotFoundException e) {
101                 throw new RiceRuntimeException("Unable to get class for name: " + lookupForm.getDataObjectClassName());
102             }
103 
104             ModuleService responsibleModuleService =
105                     KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(lookupObjectClass);
106             if (responsibleModuleService != null && responsibleModuleService.isExternalizable(lookupObjectClass)) {
107                 String lookupUrl = responsibleModuleService.getExternalizableDataObjectLookupUrl(lookupObjectClass,
108                         KRADUtils.convertRequestMapToProperties(request.getParameterMap()));
109 
110                 Properties redirectUrlProps = new Properties();
111                 redirectUrlProps.put(UifParameters.REDIRECTED_LOOKUP, "true");
112 
113                 return performRedirect(form, lookupUrl, redirectUrlProps);
114             }
115         }
116 
117         return super.start(lookupForm, result, request, response);
118     }
119 
120     /**
121      * Just returns as if return with no value was selected
122      */
123     @Override
124     @RequestMapping(params = "methodToCall=cancel")
125     public ModelAndView cancel(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
126             HttpServletRequest request, HttpServletResponse response) {
127         LookupForm lookupForm = (LookupForm) form;
128         suppressActionsIfNeeded(lookupForm);
129 
130         Properties props = new Properties();
131         props.put(UifParameters.METHOD_TO_CALL, UifConstants.MethodToCallNames.REFRESH);
132         if (StringUtils.isNotBlank(lookupForm.getReturnFormKey())) {
133             props.put(UifParameters.FORM_KEY, lookupForm.getReturnFormKey());
134         }
135         if (StringUtils.isNotBlank(lookupForm.getDocNum())) {
136             props.put(UifParameters.DOC_NUM, lookupForm.getDocNum());
137         }
138 
139         return performRedirect(lookupForm, lookupForm.getReturnLocation(), props);
140     }
141 
142     /**
143      * clearValues - clears the values of all the fields on the jsp.
144      */
145     @RequestMapping(params = "methodToCall=clearValues")
146     public ModelAndView clearValues(@ModelAttribute("KualiForm") LookupForm lookupForm, BindingResult result,
147             HttpServletRequest request, HttpServletResponse response) {
148         suppressActionsIfNeeded(lookupForm);
149 
150         Lookupable lookupable = (Lookupable) lookupForm.getLookupable();
151         lookupForm.setCriteriaFields(lookupable.performClear(lookupForm, lookupForm.getCriteriaFields()));
152 
153         return getUIFModelAndView(lookupForm);
154     }
155 
156     /**
157      * search - sets the values of the data entered on the form on the jsp into a map and then searches for the
158      * results.
159      */
160     @RequestMapping(params = "methodToCall=search")
161     public ModelAndView search(@ModelAttribute("KualiForm") LookupForm lookupForm, BindingResult result,
162             HttpServletRequest request, HttpServletResponse response) {
163         suppressActionsIfNeeded(lookupForm);
164 
165         Lookupable lookupable = lookupForm.getLookupable();
166         if (lookupable == null) {
167             LOG.error("Lookupable is null.");
168             throw new RuntimeException("Lookupable is null.");
169         }
170 
171         // validate search parameters
172         lookupable.validateSearchParameters(lookupForm, lookupForm.getCriteriaFields());
173 
174         Collection<?> displayList =
175                 lookupable.performSearch(lookupForm, lookupForm.getCriteriaFields(), true);
176 
177         if (displayList instanceof CollectionIncomplete<?>) {
178             request.setAttribute("reqSearchResultsActualSize",
179                     ((CollectionIncomplete<?>) displayList).getActualSizeIfTruncated());
180         } else {
181             request.setAttribute("reqSearchResultsActualSize", new Integer(displayList.size()));
182         }
183 
184         lookupForm.setSearchResults(displayList);
185 
186         return getUIFModelAndView(lookupForm);
187     }
188 
189     /**
190      * Invoked from the UI to return the selected lookup results lines, parameters are collected to build a URL to
191      * the caller and then a redirect is performed
192      *
193      * @param lookupForm - lookup form instance containing the selected results and lookup configuration
194      */
195     @RequestMapping(params = "methodToCall=returnSelected")
196     public ModelAndView returnSelected(@ModelAttribute("KualiForm") LookupForm lookupForm, BindingResult result,
197             HttpServletRequest request, HttpServletResponse response) {
198         Properties parameters = new Properties();
199         parameters.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.RETURN_METHOD_TO_CALL);
200 
201         if (StringUtils.isNotBlank(lookupForm.getReturnFormKey())) {
202             parameters.put(UifParameters.FORM_KEY, lookupForm.getReturnFormKey());
203         }
204 
205         parameters.put(KRADConstants.REFRESH_CALLER, lookupForm.getView().getId());
206         parameters.put(KRADConstants.REFRESH_CALLER_TYPE, UifConstants.RefreshCallerTypes.MULTI_VALUE_LOOKUP);
207         parameters.put(KRADConstants.REFRESH_DATA_OBJECT_CLASS, lookupForm.getDataObjectClassName());
208 
209         if (StringUtils.isNotBlank(lookupForm.getDocNum())) {
210             parameters.put(UifParameters.DOC_NUM, lookupForm.getDocNum());
211         }
212 
213         if (StringUtils.isNotBlank(lookupForm.getLookupCollectionName())) {
214             parameters.put(UifParameters.LOOKUP_COLLECTION_NAME, lookupForm.getLookupCollectionName());
215         }
216 
217         if (StringUtils.isNotBlank(lookupForm.getReferencesToRefresh())) {
218             parameters.put(KRADConstants.REFERENCES_TO_REFRESH, lookupForm.getReferencesToRefresh());
219         }
220 
221         // build string of select line identifiers
222         String selectedLineValues = "";
223         Set<String> selectedLines = lookupForm.getSelectedCollectionLines().get(UifPropertyPaths.SEARCH_RESULTS);
224         if (selectedLines != null) {
225             for (String selectedLine : selectedLines) {
226                 selectedLineValues += selectedLine + ",";
227             }
228             selectedLineValues = StringUtils.removeEnd(selectedLineValues, ",");
229         }
230 
231         parameters.put(UifParameters.SELECTED_LINE_VALUES, selectedLineValues);
232 
233         // clear lookup form from session
234         GlobalVariables.getUifFormManager().removeForm(lookupForm);
235 
236         return performRedirect(lookupForm, lookupForm.getReturnLocation(), parameters);
237     }
238 }