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