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