Coverage Report - org.kuali.rice.krad.web.controller.LookupController
 
Classes in this File Line Coverage Branch Coverage Complexity
LookupController
0%
0/54
0%
0/16
2.714
 
 1  
 /*
 2  
  * Copyright 2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 1.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/ecl1.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 org.apache.commons.lang.StringUtils;
 19  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 20  
 import org.kuali.rice.kim.bo.Person;
 21  
 import org.kuali.rice.kim.util.KimConstants;
 22  
 import org.kuali.rice.krad.exception.AuthorizationException;
 23  
 import org.kuali.rice.krad.lookup.CollectionIncomplete;
 24  
 import org.kuali.rice.krad.lookup.Lookupable;
 25  
 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
 26  
 import org.kuali.rice.krad.uif.UifConstants;
 27  
 import org.kuali.rice.krad.uif.UifParameters;
 28  
 import org.kuali.rice.krad.uif.container.LookupView;
 29  
 import org.kuali.rice.krad.util.GlobalVariables;
 30  
 import org.kuali.rice.krad.util.KRADConstants;
 31  
 import org.kuali.rice.krad.util.KRADUtils;
 32  
 import org.kuali.rice.krad.web.form.LookupForm;
 33  
 import org.kuali.rice.krad.web.form.UifFormBase;
 34  
 import org.springframework.stereotype.Controller;
 35  
 import org.springframework.validation.BindingResult;
 36  
 import org.springframework.web.bind.annotation.ModelAttribute;
 37  
 import org.springframework.web.bind.annotation.RequestMapping;
 38  
 import org.springframework.web.servlet.ModelAndView;
 39  
 
 40  
 import javax.servlet.http.HttpServletRequest;
 41  
 import javax.servlet.http.HttpServletResponse;
 42  
 import java.util.Collection;
 43  
 import java.util.Properties;
 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  0
 @Controller
 51  
 @RequestMapping(value = "/lookup")
 52  0
 public class LookupController extends UifControllerBase {
 53  0
     private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(LookupController.class);
 54  
 
 55  
     /**
 56  
      * @see UifControllerBase#createInitialForm(javax.servlet.http.HttpServletRequest)
 57  
      */
 58  
     @Override
 59  
     protected LookupForm createInitialForm(HttpServletRequest request) {
 60  0
         return new LookupForm();
 61  
     }
 62  
 
 63  
     protected void supressActionsIfNeeded(LookupForm lookupForm) {
 64  
         try {
 65  0
             Class<?> dataObjectClass = Class.forName(lookupForm.getDataObjectClassName());
 66  0
             Person user = GlobalVariables.getUserSession().getPerson();
 67  
             // check if creating documents is allowed
 68  0
             String documentTypeName = KRADServiceLocatorWeb.getDocumentDictionaryService()
 69  
                     .getMaintenanceDocumentTypeName(dataObjectClass);
 70  0
             if ((documentTypeName != null) &&
 71  
                     !KRADServiceLocatorWeb.getDocumentHelperService().getDocumentAuthorizer(documentTypeName)
 72  
                             .canInitiate(documentTypeName, user)) {
 73  0
                 ((LookupView) lookupForm.getView()).setSuppressActions(true);
 74  
             }
 75  0
         } catch (ClassNotFoundException e) {
 76  0
             LOG.warn("Unable to load Data Object Class: " + lookupForm.getDataObjectClassName(), e);
 77  0
         }
 78  0
     }
 79  
 
 80  
     /**
 81  
      * @see UifControllerBase#checkAuthorization(org.kuali.rice.krad.web.form.UifFormBase, java.lang.String)
 82  
      */
 83  
     @Override
 84  
     public void checkAuthorization(UifFormBase form, String methodToCall) throws AuthorizationException {
 85  0
         if (!(form instanceof LookupForm)) {
 86  0
             super.checkAuthorization(form, methodToCall);
 87  
         } else {
 88  0
             LookupForm lookupForm = (LookupForm) form;
 89  
             try {
 90  0
                 Class<?> dataObjectClass = Class.forName(lookupForm.getDataObjectClassName());
 91  0
                 Person user = GlobalVariables.getUserSession().getPerson();
 92  
                 // check if user is allowed to lookup object
 93  0
                 if (!KimApiServiceLocator.getPermissionService()
 94  
                         .isAuthorizedByTemplateName(user.getPrincipalId(), KRADConstants.KRAD_NAMESPACE,
 95  
                                 KimConstants.PermissionTemplateNames.LOOK_UP_RECORDS,
 96  
                                 KRADUtils.getNamespaceAndComponentSimpleName(dataObjectClass),
 97  
                                 null)) {
 98  0
                     throw new AuthorizationException(user.getPrincipalName(),
 99  
                             KimConstants.PermissionTemplateNames.LOOK_UP_RECORDS, dataObjectClass.getSimpleName());
 100  
                 }
 101  0
             } catch (ClassNotFoundException e) {
 102  0
                 LOG.warn("Unable to load Data Object Class class: " + lookupForm.getDataObjectClassName(), e);
 103  0
                 super.checkAuthorization(lookupForm, methodToCall);
 104  0
             }
 105  
         }
 106  0
     }
 107  
 
 108  
     @RequestMapping(params = "methodToCall=start")
 109  
     @Override
 110  
     public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
 111  
             HttpServletRequest request, HttpServletResponse response) {
 112  0
         LookupForm lookupForm = (LookupForm) form;
 113  
 //                checkAuthorization(lookupForm, request.getParameter("methodToCall"));
 114  0
         supressActionsIfNeeded(lookupForm);
 115  
 
 116  0
         return super.start(lookupForm, result, request, response);
 117  
     }
 118  
 
 119  
     /**
 120  
      * Just returns as if return with no value was selected.
 121  
      */
 122  
     @Override
 123  
     @RequestMapping(params = "methodToCall=cancel")
 124  
     public ModelAndView cancel(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
 125  
             HttpServletRequest request, HttpServletResponse response) {
 126  0
         LookupForm lookupForm = (LookupForm) form;
 127  0
         supressActionsIfNeeded(lookupForm);
 128  
 
 129  0
         Properties props = new Properties();
 130  0
         props.put(UifParameters.METHOD_TO_CALL, UifConstants.MethodToCallNames.REFRESH);
 131  0
         if (StringUtils.isNotBlank(lookupForm.getReturnFormKey())) {
 132  0
             props.put(UifParameters.FORM_KEY, lookupForm.getReturnFormKey());
 133  
         }
 134  0
         if (StringUtils.isNotBlank(lookupForm.getDocNum())) {
 135  0
             props.put(UifParameters.DOC_NUM, lookupForm.getDocNum());
 136  
         }
 137  
 
 138  0
         return performRedirect(lookupForm, lookupForm.getReturnLocation(), props);
 139  
     }
 140  
 
 141  
     /**
 142  
      * clearValues - clears the values of all the fields on the jsp.
 143  
      */
 144  
     @RequestMapping(params = "methodToCall=clearValues")
 145  
     public ModelAndView clearValues(@ModelAttribute("KualiForm") LookupForm lookupForm, BindingResult result,
 146  
             HttpServletRequest request, HttpServletResponse response) {
 147  0
         supressActionsIfNeeded(lookupForm);
 148  
 
 149  0
         Lookupable lookupable = (Lookupable) lookupForm.getLookupable();
 150  0
         lookupForm.setCriteriaFields(lookupable.performClear(lookupForm, lookupForm.getCriteriaFields()));
 151  
 
 152  0
         return getUIFModelAndView(lookupForm);
 153  
     }
 154  
 
 155  
     /**
 156  
      * search - sets the values of the data entered on the form on the jsp into a map and then searches for the
 157  
      * results.
 158  
      */
 159  
     @RequestMapping(params = "methodToCall=search")
 160  
     public ModelAndView search(@ModelAttribute("KualiForm") LookupForm lookupForm, BindingResult result,
 161  
             HttpServletRequest request, HttpServletResponse response) {
 162  0
         supressActionsIfNeeded(lookupForm);
 163  0
         GlobalVariables.getUserSession().removeObjectsByPrefix(KRADConstants.SEARCH_METHOD);
 164  
 
 165  0
         Lookupable lookupable = lookupForm.getLookupable();
 166  0
         if (lookupable == null) {
 167  0
             LOG.error("Lookupable is null.");
 168  0
             throw new RuntimeException("Lookupable is null.");
 169  
         }
 170  
 
 171  
         // validate search parameters
 172  0
         lookupable.validateSearchParameters(lookupForm, lookupForm.getCriteriaFields());
 173  
 
 174  0
         Collection<?> displayList =
 175  
                 lookupable.performSearch(lookupForm, lookupForm.getCriteriaFields(), true);
 176  
 
 177  0
         if (displayList instanceof CollectionIncomplete<?>) {
 178  0
             request.setAttribute("reqSearchResultsActualSize",
 179  
                     ((CollectionIncomplete<?>) displayList).getActualSizeIfTruncated());
 180  
         } else {
 181  0
             request.setAttribute("reqSearchResultsActualSize", new Integer(displayList.size()));
 182  
         }
 183  
 
 184  0
         lookupForm.setSearchResults(displayList);
 185  
 
 186  0
         return getUIFModelAndView(lookupForm);
 187  
     }
 188  
 }