Coverage Report - org.kuali.rice.kns.web.struts.action.KualiLookupAction
 
Classes in this File Line Coverage Branch Coverage Complexity
KualiLookupAction
0%
0/149
0%
0/84
4.333
 
 1  
 /*
 2  
  * Copyright 2005-2007 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.kns.web.struts.action;
 17  
 
 18  
 import java.io.IOException;
 19  
 import java.util.ArrayList;
 20  
 import java.util.Collection;
 21  
 import java.util.HashMap;
 22  
 import java.util.Iterator;
 23  
 import java.util.List;
 24  
 import java.util.Map;
 25  
 
 26  
 import javax.servlet.ServletException;
 27  
 import javax.servlet.http.HttpServletRequest;
 28  
 import javax.servlet.http.HttpServletResponse;
 29  
 
 30  
 import org.apache.commons.lang.StringUtils;
 31  
 import org.apache.struts.action.ActionForm;
 32  
 import org.apache.struts.action.ActionForward;
 33  
 import org.apache.struts.action.ActionMapping;
 34  
 import org.kuali.rice.core.util.RiceConstants;
 35  
 import org.kuali.rice.kim.service.KIMServiceLocator;
 36  
 import org.kuali.rice.kim.util.KimCommonUtils;
 37  
 import org.kuali.rice.kim.util.KimConstants;
 38  
 import org.kuali.rice.kns.datadictionary.BusinessObjectEntry;
 39  
 import org.kuali.rice.kns.exception.AuthorizationException;
 40  
 import org.kuali.rice.kns.lookup.CollectionIncomplete;
 41  
 import org.kuali.rice.kns.lookup.Lookupable;
 42  
 import org.kuali.rice.kns.service.DocumentHelperService;
 43  
 import org.kuali.rice.kns.service.KNSServiceLocator;
 44  
 import org.kuali.rice.kns.service.MaintenanceDocumentDictionaryService;
 45  
 import org.kuali.rice.kns.util.GlobalVariables;
 46  
 import org.kuali.rice.kns.util.KNSConstants;
 47  
 import org.kuali.rice.kns.web.struts.form.LookupForm;
 48  
 import org.kuali.rice.kns.web.ui.Field;
 49  
 import org.kuali.rice.kns.web.ui.ResultRow;
 50  
 import org.kuali.rice.kns.web.ui.Row;
 51  
 import org.springframework.web.util.HtmlUtils;
 52  
 
 53  
 /**
 54  
  * This class handles Actions for lookup flow
 55  
  *
 56  
  *
 57  
  */
 58  
 
 59  0
 public class KualiLookupAction extends KualiAction {
 60  0
     private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(KualiLookupAction.class);
 61  
 
 62  
     @Override
 63  
     protected void checkAuthorization(ActionForm form, String methodToCall) throws AuthorizationException {
 64  0
         if (!(form instanceof LookupForm)) {
 65  0
             super.checkAuthorization(form, methodToCall);
 66  
         } else {
 67  
             try {
 68  0
                 Class businessObjectClass = Class.forName(((LookupForm) form).getBusinessObjectClassName());
 69  0
                 if (!KIMServiceLocator.getIdentityManagementService().isAuthorizedByTemplateName(GlobalVariables.getUserSession().getPrincipalId(), KNSConstants.KNS_NAMESPACE, KimConstants.PermissionTemplateNames.LOOK_UP_RECORDS, KimCommonUtils.getNamespaceAndComponentSimpleName(businessObjectClass), null)) {
 70  0
                     throw new AuthorizationException(GlobalVariables.getUserSession().getPerson().getPrincipalName(),
 71  
                                     KimConstants.PermissionTemplateNames.LOOK_UP_RECORDS,
 72  
                                     businessObjectClass.getSimpleName());
 73  
                 }
 74  
             }
 75  0
             catch (ClassNotFoundException e) {
 76  0
                     LOG.warn("Unable to load BusinessObject class: " + ((LookupForm) form).getBusinessObjectClassName(), e);
 77  0
                 super.checkAuthorization(form, methodToCall);
 78  0
             }
 79  
         }
 80  0
     }
 81  
 
 82  
     private static MaintenanceDocumentDictionaryService maintenanceDocumentDictionaryService;
 83  
     private static DocumentHelperService documentHelperService;
 84  
     private static MaintenanceDocumentDictionaryService getMaintenanceDocumentDictionaryService() {
 85  0
             if (maintenanceDocumentDictionaryService == null) {
 86  0
                     maintenanceDocumentDictionaryService = KNSServiceLocator.getMaintenanceDocumentDictionaryService();
 87  
             }
 88  0
             return maintenanceDocumentDictionaryService;
 89  
     }
 90  
     private static DocumentHelperService getDocumentHelperService() {
 91  0
             if (documentHelperService == null) {
 92  0
                 documentHelperService = KNSServiceLocator.getDocumentHelperService();
 93  
             }
 94  0
             return documentHelperService;
 95  
     }
 96  
     /**
 97  
      * Checks if the user can create a document for this business object.  Used to suppress the actions on the results.
 98  
      *
 99  
      * @param form
 100  
      * @return
 101  
      * @throws ClassNotFoundException
 102  
      */
 103  
     protected void supressActionsIfNeeded( ActionForm form ) throws ClassNotFoundException {
 104  0
         if ((form instanceof LookupForm) && ( ((LookupForm)form).getBusinessObjectClassName() != null )) {
 105  0
             Class businessObjectClass = Class.forName( ((LookupForm)form).getBusinessObjectClassName() );
 106  
             // check if creating documents is allowed
 107  0
             String documentTypeName = getMaintenanceDocumentDictionaryService().getDocumentTypeName(businessObjectClass);
 108  0
             if ((documentTypeName != null) && !getDocumentHelperService().getDocumentAuthorizer(documentTypeName).canInitiate(documentTypeName, GlobalVariables.getUserSession().getPerson())) {
 109  0
                 ((LookupForm)form).setSuppressActions( true );
 110  
             }
 111  
         }
 112  0
     }
 113  
 
 114  
     /**
 115  
          * This method hides the criteria if set in parameter or lookupable
 116  
          *
 117  
          * @param form
 118  
          */
 119  
         private void setCriteriaEnabled(ActionForm form) {
 120  0
                  LookupForm lookupForm = (LookupForm) form;
 121  0
                  if(lookupForm.isLookupCriteriaEnabled()) {
 122  
                          //only overide if it's enabled, if disabled don't call lookupable
 123  
                  }
 124  0
         }
 125  
         /**
 126  
          * This method hides actions that are not related to the maintenance (as opposed to supressActionsIfNeeded)
 127  
          *
 128  
          * @param form
 129  
          */
 130  
         private void suppressNonMaintActionsIfNeeded(ActionForm form) {
 131  0
                 LookupForm lookupForm = (LookupForm) form;
 132  0
                 if(lookupForm.getLookupable()!=null) {
 133  0
                         if(StringUtils.isNotEmpty(lookupForm.getLookupable().getSupplementalMenuBar())) {
 134  0
                                 lookupForm.setSupplementalActionsEnabled(true);
 135  
                         }
 136  
 
 137  
                 }
 138  0
         }
 139  
 
 140  
         @Override
 141  
         public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
 142  
                         HttpServletResponse response) throws Exception {
 143  0
                 LookupForm lookupForm = (LookupForm) form;
 144  
 
 145  0
                 request.setAttribute(KNSConstants.PARAM_MAINTENANCE_VIEW_MODE, KNSConstants.PARAM_MAINTENANCE_VIEW_MODE_LOOKUP);
 146  0
                 supressActionsIfNeeded(form);
 147  0
                 suppressNonMaintActionsIfNeeded(form);
 148  0
                 setCriteriaEnabled(form);
 149  
 
 150  0
                 hideHeaderBarIfNeeded(form, request);
 151  
 
 152  0
                 int numCols = KNSServiceLocator.getBusinessObjectDictionaryService().getLookupNumberOfColumns(
 153  
                                 Class.forName(lookupForm.getBusinessObjectClassName()));
 154  0
                 lookupForm.setNumColumns(numCols);
 155  
 
 156  0
                 ActionForward forward = super.execute(mapping, form, request, response);
 157  
 
 158  
                 // apply conditional logic after all setting of field values has been completed
 159  0
                 lookupForm.getLookupable().applyConditionalLogicForFieldDisplay();
 160  
 
 161  0
                 return forward;
 162  
         }
 163  
 
 164  
         private void hideHeaderBarIfNeeded(ActionForm form, HttpServletRequest request) {
 165  0
                 if (!((LookupForm) form).isHeaderBarEnabled()) {
 166  0
                         ((LookupForm) form).setHeaderBarEnabled(false);
 167  
                 }
 168  0
         }
 169  
 
 170  
         
 171  
         /**
 172  
      * Entry point to lookups, forwards to jsp for search render.
 173  
      */
 174  
     public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 175  0
         return mapping.findForward(RiceConstants.MAPPING_BASIC);
 176  
     }
 177  
 
 178  
     /**
 179  
      * search - sets the values of the data entered on the form on the jsp into a map and then searches for the results.
 180  
      */
 181  
     public ActionForward search(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 182  0
         LookupForm lookupForm = (LookupForm) form;
 183  
                         
 184  
        
 185  0
                 String methodToCall = findMethodToCall(form, request);
 186  0
                 if (methodToCall.equalsIgnoreCase("search")) {
 187  0
                         GlobalVariables.getUserSession().removeObjectsByPrefix(KNSConstants.SEARCH_METHOD);
 188  
                 }
 189  
                 
 190  
 
 191  
                 
 192  0
         Lookupable kualiLookupable = lookupForm.getLookupable();
 193  0
         if (kualiLookupable == null) {
 194  0
             LOG.error("Lookupable is null.");
 195  0
             throw new RuntimeException("Lookupable is null.");
 196  
         }
 197  
 
 198  0
         Collection displayList = new ArrayList();
 199  0
         List<ResultRow> resultTable = new ArrayList<ResultRow>();
 200  
 
 201  
         // validate search parameters
 202  0
         kualiLookupable.validateSearchParameters(lookupForm.getFields());
 203  
 
 204  0
         boolean bounded = true;
 205  
 
 206  0
         displayList = kualiLookupable.performLookup(lookupForm, resultTable, bounded);
 207  
 
 208  0
         if (kualiLookupable.isSearchUsingOnlyPrimaryKeyValues()) {
 209  0
             lookupForm.setSearchUsingOnlyPrimaryKeyValues(true);
 210  0
             lookupForm.setPrimaryKeyFieldLabels(kualiLookupable.getPrimaryKeyFieldLabels());
 211  
         }
 212  
         else {
 213  0
             lookupForm.setSearchUsingOnlyPrimaryKeyValues(false);
 214  0
             lookupForm.setPrimaryKeyFieldLabels(KNSConstants.EMPTY_STRING);
 215  
         }
 216  
 
 217  0
         if ( displayList instanceof CollectionIncomplete ){
 218  0
             request.setAttribute("reqSearchResultsActualSize", ((CollectionIncomplete) displayList).getActualSizeIfTruncated());
 219  
         } else {
 220  0
             request.setAttribute("reqSearchResultsActualSize", displayList.size() );
 221  
         }
 222  
 
 223  
         // Determine if at least one table entry has an action available. If any non-breaking space (&nbsp; or '\u00A0') characters
 224  
         // exist in the URL's value, they will be converted to regular whitespace ('\u0020').
 225  0
         boolean hasActionUrls = false;
 226  0
         for (Iterator<ResultRow> iterator = resultTable.iterator(); !hasActionUrls && iterator.hasNext();) {
 227  0
                         if (StringUtils.isNotBlank(HtmlUtils.htmlUnescape(iterator.next().getActionUrls()).replace('\u00A0', '\u0020'))) {
 228  0
                                 hasActionUrls = true;
 229  
                         }
 230  
                 }
 231  0
                 lookupForm.setActionUrlsExist(hasActionUrls);
 232  
 
 233  0
         request.setAttribute("reqSearchResults", resultTable);
 234  
 
 235  0
         if (request.getParameter(KNSConstants.SEARCH_LIST_REQUEST_KEY) != null) {
 236  0
             GlobalVariables.getUserSession().removeObject(request.getParameter(KNSConstants.SEARCH_LIST_REQUEST_KEY));
 237  
         }
 238  
         
 239  0
              request.setAttribute(KNSConstants.SEARCH_LIST_REQUEST_KEY, GlobalVariables.getUserSession().addObject(resultTable, KNSConstants.SEARCH_LIST_KEY_PREFIX));
 240  
                       
 241  0
         String refreshCaller = request.getParameter(KNSConstants.REFRESH_CALLER);
 242  
 
 243  0
         return mapping.findForward(RiceConstants.MAPPING_BASIC);
 244  
     }
 245  
 
 246  
 
 247  
     /**
 248  
      * refresh - is called when one quickFinder returns to the previous one. Sets all the values and performs the new search.
 249  
      */
 250  
     public ActionForward refresh(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 251  0
         LookupForm lookupForm = (LookupForm) form;
 252  0
         Lookupable kualiLookupable = lookupForm.getLookupable();
 253  0
         if (kualiLookupable == null) {
 254  0
             LOG.error("Lookupable is null.");
 255  0
             throw new RuntimeException("Lookupable is null.");
 256  
         }
 257  
 
 258  0
         if(StringUtils.equals(lookupForm.getRefreshCaller(),"customLookupAction")) {
 259  0
                 return this.customLookupableMethodCall(mapping, lookupForm, request, response);
 260  
         }
 261  
 
 262  0
         Map fieldValues = new HashMap();
 263  0
         Map values = lookupForm.getFields();
 264  
 
 265  0
         for (Iterator iter = kualiLookupable.getRows().iterator(); iter.hasNext();) {
 266  0
                 Row row = (Row) iter.next();
 267  
 
 268  0
                 for (Iterator iterator = row.getFields().iterator(); iterator.hasNext();) {
 269  0
                         Field field = (Field) iterator.next();
 270  
 
 271  0
                         if (field.getPropertyName() != null && !field.getPropertyName().equals("")) {
 272  0
                                 if (request.getParameter(field.getPropertyName()) != null) {
 273  0
                                         if(!Field.MULTI_VALUE_FIELD_TYPES.contains(field.getFieldType())) {
 274  0
                                                 field.setPropertyValue(request.getParameter(field.getPropertyName()));
 275  
                                         } else {
 276  
                                                 //multi value, set to values
 277  0
                                                 field.setPropertyValues(request.getParameterValues(field.getPropertyName()));
 278  
                                         }
 279  
                                 }
 280  
                         }
 281  0
                         else if (values.get(field.getPropertyName()) != null) {
 282  0
                                 field.setPropertyValue(values.get(field.getPropertyName()));
 283  
                         }
 284  
 
 285  0
                         kualiLookupable.applyFieldAuthorizationsFromNestedLookups(field);
 286  
 
 287  0
                         fieldValues.put(field.getPropertyName(), field.getPropertyValue());
 288  0
                 }
 289  0
         }
 290  0
         fieldValues.put("docFormKey", lookupForm.getFormKey());
 291  0
         fieldValues.put("backLocation", lookupForm.getBackLocation());
 292  0
         fieldValues.put("docNum", lookupForm.getDocNum());
 293  
 
 294  0
         if (kualiLookupable.checkForAdditionalFields(fieldValues)) {
 295  0
             for (Iterator iter = kualiLookupable.getRows().iterator(); iter.hasNext();) {
 296  0
                 Row row = (Row) iter.next();
 297  0
                 for (Iterator iterator = row.getFields().iterator(); iterator.hasNext();) {
 298  0
                     Field field = (Field) iterator.next();
 299  0
                     if (field.getPropertyName() != null && !field.getPropertyName().equals("")) {
 300  0
                         if (request.getParameter(field.getPropertyName()) != null) {
 301  
 //                            field.setPropertyValue(request.getParameter(field.getPropertyName()));
 302  0
                                             if(!Field.MULTI_VALUE_FIELD_TYPES.contains(field.getFieldType())) {
 303  0
                                                     field.setPropertyValue(request.getParameter(field.getPropertyName()));
 304  
                                             } else {
 305  
                                                     //multi value, set to values
 306  0
                                                     field.setPropertyValues(request.getParameterValues(field.getPropertyName()));
 307  
                                             }
 308  
                             //FIXME: any reason this is inside this "if" instead of the outer one, like above - this seems inconsistent
 309  0
                             fieldValues.put(field.getPropertyName(), request.getParameter(field.getPropertyName()));
 310  
                         }
 311  0
                         else if (values.get(field.getPropertyName()) != null) {
 312  0
                             field.setPropertyValue(values.get(field.getPropertyName()));
 313  
                         }
 314  
                     }
 315  0
                 }
 316  0
             }
 317  
         }
 318  0
         return mapping.findForward(RiceConstants.MAPPING_BASIC);
 319  
     }
 320  
 
 321  
     /**
 322  
      * Just returns as if return with no value was selected.
 323  
      */
 324  
     public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 325  0
         LookupForm lookupForm = (LookupForm) form;
 326  
 
 327  0
         String backUrl = lookupForm.getBackLocation() + "?methodToCall=refresh&docFormKey=" + lookupForm.getFormKey()+"&docNum="+lookupForm.getDocNum();
 328  0
         return new ActionForward(backUrl, true);
 329  
     }
 330  
 
 331  
 
 332  
     /**
 333  
      * clearValues - clears the values of all the fields on the jsp.
 334  
      */
 335  
     public ActionForward clearValues(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
 336  0
         LookupForm lookupForm = (LookupForm) form;
 337  0
         Lookupable kualiLookupable = lookupForm.getLookupable();
 338  0
         if (kualiLookupable == null) {
 339  0
             LOG.error("Lookupable is null.");
 340  0
             throw new RuntimeException("Lookupable is null.");
 341  
         }
 342  
 
 343  0
         kualiLookupable.performClear(lookupForm);
 344  
 
 345  
 
 346  0
         return mapping.findForward(RiceConstants.MAPPING_BASIC);
 347  
     }
 348  
 
 349  
 
 350  
     public ActionForward viewResults(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 351  0
         LookupForm lookupForm = (LookupForm) form;
 352  0
         if (lookupForm.isSearchUsingOnlyPrimaryKeyValues()) {
 353  0
             lookupForm.setPrimaryKeyFieldLabels(lookupForm.getLookupable().getPrimaryKeyFieldLabels());
 354  
         }
 355  0
         request.setAttribute(KNSConstants.SEARCH_LIST_REQUEST_KEY, request.getParameter(KNSConstants.SEARCH_LIST_REQUEST_KEY));
 356  0
         request.setAttribute("reqSearchResults", GlobalVariables.getUserSession().retrieveObject(request.getParameter(KNSConstants.SEARCH_LIST_REQUEST_KEY)));
 357  0
         request.setAttribute("reqSearchResultsActualSize", request.getParameter("reqSearchResultsActualSize"));
 358  0
         return mapping.findForward(RiceConstants.MAPPING_BASIC);
 359  
     }
 360  
 
 361  
     public ActionForward customLookupableMethodCall(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 362  
 //            lookupableMethodToCall
 363  0
             Lookupable kualiLookupable = ((LookupForm)form).getLookupable();
 364  0
             if (kualiLookupable == null) {
 365  0
             LOG.error("Lookupable is null.");
 366  0
             throw new RuntimeException("Lookupable is null.");
 367  
         }
 368  
 
 369  0
             boolean ignoreErrors=false;
 370  0
                 if(StringUtils.equals(((LookupForm)form).getRefreshCaller(),"customLookupAction")) {
 371  0
                         ignoreErrors=true;
 372  
             }
 373  
 
 374  0
                 if(kualiLookupable.performCustomAction(ignoreErrors)) {
 375  
                         //redo the search if the method comes back
 376  0
                         return search(mapping, form, request, response);
 377  
                 }
 378  0
                 return mapping.findForward(RiceConstants.MAPPING_BASIC);
 379  
 
 380  
     }
 381  
 
 382  
 }