Coverage Report - org.kuali.rice.kns.web.spring.controller.LookupController
 
Classes in this File Line Coverage Branch Coverage Complexity
LookupController
0%
0/44
0%
0/16
2.833
 
 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.kns.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.bo.Person;
 27  
 import org.kuali.rice.kim.service.KIMServiceLocator;
 28  
 import org.kuali.rice.kim.util.KimConstants;
 29  
 import org.kuali.rice.kns.exception.AuthorizationException;
 30  
 import org.kuali.rice.kns.lookup.CollectionIncomplete;
 31  
 import org.kuali.rice.kns.service.KNSServiceLocatorWeb;
 32  
 import org.kuali.rice.kns.uif.UifConstants;
 33  
 import org.kuali.rice.kns.uif.UifParameters;
 34  
 import org.kuali.rice.kns.uif.container.LookupView;
 35  
 import org.kuali.rice.kns.uif.core.Component;
 36  
 import org.kuali.rice.kns.uif.service.LookupViewHelperService;
 37  
 import org.kuali.rice.kns.util.GlobalVariables;
 38  
 import org.kuali.rice.kns.util.KNSConstants;
 39  
 import org.kuali.rice.kns.util.KNSUtils;
 40  
 import org.kuali.rice.kns.web.spring.form.LookupForm;
 41  
 import org.kuali.rice.kns.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.kns.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  
     /**
 67  
      * @see org.kuali.rice.kns.web.spring.controller.UifControllerBase#checkAuthorization(org.kuali.rice.kns.web.spring.form.UifFormBase, java.lang.String)
 68  
      */
 69  
     @Override
 70  
         public void checkAuthorization(UifFormBase form, String methodToCall) throws AuthorizationException {
 71  0
         if (!(form instanceof LookupForm)) {
 72  0
             super.checkAuthorization(form, methodToCall);
 73  
         } else {
 74  0
                 LookupForm lookupForm = (LookupForm) form;
 75  
             try {
 76  0
                 Class<?> dataObjectClass = Class.forName(lookupForm.getDataObjectClassName());
 77  0
                     Person user = GlobalVariables.getUserSession().getPerson();
 78  
                     // check if creating documents is allowed
 79  0
                 String documentTypeName = KNSServiceLocatorWeb.getMaintenanceDocumentDictionaryService().getDocumentTypeName(dataObjectClass);
 80  0
                 if ((documentTypeName != null) && !KNSServiceLocatorWeb.getDocumentHelperService().getDocumentAuthorizer(documentTypeName).canInitiate(documentTypeName, user)) {
 81  0
                     lookupForm.setSuppressActions( true );
 82  
                 }
 83  
                     // check if user is allowed to lookup object
 84  0
                 if (!KIMServiceLocator.getIdentityManagementService().isAuthorizedByTemplateName(user.getPrincipalId(), KNSConstants.KNS_NAMESPACE, KimConstants.PermissionTemplateNames.LOOK_UP_RECORDS, KNSUtils.getNamespaceAndComponentSimpleName(dataObjectClass), null)) {
 85  0
                     throw new AuthorizationException(user.getPrincipalName(),
 86  
                                     KimConstants.PermissionTemplateNames.LOOK_UP_RECORDS,
 87  
                                     dataObjectClass.getSimpleName());
 88  
                 }
 89  
             }
 90  0
             catch (ClassNotFoundException e) {
 91  0
                     LOG.warn("Unable to load BusinessObject class: " + lookupForm.getDataObjectClassName(), e);
 92  0
                 super.checkAuthorization(lookupForm, methodToCall);
 93  0
             }
 94  
         }
 95  0
     }
 96  
 
 97  
         @RequestMapping(params = "methodToCall=start")
 98  
         public ModelAndView start(@ModelAttribute("KualiForm") LookupForm lookupForm, BindingResult result, HttpServletRequest request, HttpServletResponse response) {
 99  
 //                checkAuthorization(lookupForm, request.getParameter("methodToCall"));
 100  0
                 return getUIFModelAndView(lookupForm);
 101  
         }
 102  
 
 103  
     /**
 104  
      * Just returns as if return with no value was selected.
 105  
      */
 106  
         @Override
 107  
     @RequestMapping(params = "methodToCall=cancel")
 108  
         public ModelAndView cancel(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, HttpServletRequest request, HttpServletResponse response) {
 109  0
             LookupForm lookupForm = (LookupForm)form;
 110  
             
 111  0
             Properties props = new Properties();
 112  0
             props.put(UifParameters.METHOD_TO_CALL, UifConstants.MethodToCallNames.REFRESH);
 113  0
             if (StringUtils.isNotBlank(lookupForm.getReturnFormKey())) {
 114  0
             props.put(UifParameters.FORM_KEY, lookupForm.getReturnFormKey());
 115  
         }
 116  0
             if (StringUtils.isNotBlank(lookupForm.getDocNum())) {
 117  0
                 props.put(UifParameters.DOC_NUM, lookupForm.getDocNum());
 118  
             }
 119  0
             return performRedirect(lookupForm, lookupForm.getReturnLocation(), props);
 120  
     }
 121  
 
 122  
     /**
 123  
      * clearValues - clears the values of all the fields on the jsp.
 124  
      */
 125  
     @RequestMapping(params = "methodToCall=clearValues")
 126  
         public ModelAndView clearValues(@ModelAttribute("KualiForm") LookupForm lookupForm, BindingResult result, HttpServletRequest request, HttpServletResponse response) {
 127  
 //        LookupViewHelperService lookupViewHelperService = lookupForm.getLookupViewHelperService();
 128  0
         LookupViewHelperService lookupViewHelperService = (LookupViewHelperService) lookupForm.getView().getViewHelperService();
 129  0
         lookupForm.setCriteriaFields(lookupViewHelperService.performClear(lookupForm.getCriteriaFieldsForLookup()));
 130  0
                 return getUIFModelAndView(lookupForm);
 131  
     }
 132  
 
 133  
     /**
 134  
      * search - sets the values of the data entered on the form on the jsp into a map and then searches for the results.
 135  
      */
 136  
         @RequestMapping(params = "methodToCall=search")
 137  
         public ModelAndView search(@ModelAttribute("KualiForm") LookupForm lookupForm, BindingResult result, HttpServletRequest request, HttpServletResponse response) {
 138  0
                 GlobalVariables.getUserSession().removeObjectsByPrefix(KNSConstants.SEARCH_METHOD);
 139  
 
 140  
 //        LookupViewHelperService lookupViewHelperService = lookupForm.getLookupViewHelperService();
 141  0
         LookupViewHelperService lookupViewHelperService = (LookupViewHelperService) lookupForm.getView().getViewHelperService();
 142  0
         if (lookupViewHelperService == null) {
 143  0
             LOG.error("LookupViewHelperService is null.");
 144  0
             throw new RuntimeException("LookupViewHelperService is null.");
 145  
         }
 146  
 
 147  
         // validate search parameters
 148  0
         List<? extends Component> criteriaComponents = ((LookupView) lookupForm.getView()).getCriteriaGroup().getItems();
 149  0
         lookupViewHelperService.validateSearchParameters(criteriaComponents, lookupForm.getCriteriaFields());
 150  
 
 151  0
         Collection<?> displayList = lookupViewHelperService.performSearch(lookupForm.getCriteriaFieldsForLookup(), true);
 152  
 
 153  0
         if ( displayList instanceof CollectionIncomplete<?> ){
 154  0
             request.setAttribute("reqSearchResultsActualSize", ((CollectionIncomplete<?>) displayList).getActualSizeIfTruncated());
 155  
         } else {
 156  0
             request.setAttribute("reqSearchResultsActualSize", new Integer(displayList.size()) );
 157  
         }
 158  
 
 159  0
         lookupForm.setSearchResults(displayList);
 160  
 
 161  0
         return getUIFModelAndView(lookupForm);
 162  
     }
 163  
 
 164  
 }