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 org.apache.commons.lang.StringUtils;
 19  
 import org.apache.struts.action.ActionForm;
 20  
 import org.apache.struts.action.ActionForward;
 21  
 import org.apache.struts.action.ActionMapping;
 22  
 import org.kuali.rice.core.api.util.RiceConstants;
 23  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 24  
 import org.kuali.rice.kim.util.KimConstants;
 25  
 import org.kuali.rice.kns.lookup.Lookupable;
 26  
 import org.kuali.rice.kns.service.KNSServiceLocator;
 27  
 import org.kuali.rice.kns.service.MaintenanceDocumentDictionaryService;
 28  
 import org.kuali.rice.kns.web.struts.form.LookupForm;
 29  
 import org.kuali.rice.kns.web.ui.Field;
 30  
 import org.kuali.rice.kns.web.ui.ResultRow;
 31  
 import org.kuali.rice.kns.web.ui.Row;
 32  
 import org.kuali.rice.krad.exception.AuthorizationException;
 33  
 import org.kuali.rice.krad.lookup.CollectionIncomplete;
 34  
 import org.kuali.rice.krad.service.DocumentHelperService;
 35  
 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
 36  
 import org.kuali.rice.krad.util.GlobalVariables;
 37  
 import org.kuali.rice.krad.util.KRADConstants;
 38  
 import org.kuali.rice.krad.util.KRADUtils;
 39  
 import org.springframework.web.util.HtmlUtils;
 40  
 
 41  
 import javax.servlet.ServletException;
 42  
 import javax.servlet.http.HttpServletRequest;
 43  
 import javax.servlet.http.HttpServletResponse;
 44  
 import java.io.IOException;
 45  
 import java.util.ArrayList;
 46  
 import java.util.Collection;
 47  
 import java.util.HashMap;
 48  
 import java.util.Iterator;
 49  
 import java.util.Map;
 50  
 
 51  
 /**
 52  
  * This class handles Actions for lookup flow
 53  
  *
 54  
  *
 55  
  */
 56  
 
 57  0
 public class KualiLookupAction extends KualiAction {
 58  0
     private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(KualiLookupAction.class);
 59  
 
 60  
     @Override
 61  
     protected void checkAuthorization(ActionForm form, String methodToCall) throws AuthorizationException {
 62  0
         if (!(form instanceof LookupForm)) {
 63  0
             super.checkAuthorization(form, methodToCall);
 64  
         } else {
 65  
             try {
 66  0
                 Class businessObjectClass = Class.forName(((LookupForm) form).getBusinessObjectClassName());
 67  0
                 if (!KimApiServiceLocator.getPermissionService().isAuthorizedByTemplateName(GlobalVariables.getUserSession().getPrincipalId(), KRADConstants.KRAD_NAMESPACE, KimConstants.PermissionTemplateNames.LOOK_UP_RECORDS,
 68  
                         KRADUtils.getNamespaceAndComponentSimpleName(businessObjectClass), null)) {
 69  0
                     throw new AuthorizationException(GlobalVariables.getUserSession().getPerson().getPrincipalName(),
 70  
                                     KimConstants.PermissionTemplateNames.LOOK_UP_RECORDS,
 71  
                                     businessObjectClass.getSimpleName());
 72  
                 }
 73  
             }
 74  0
             catch (ClassNotFoundException e) {
 75  0
                     LOG.warn("Unable to load BusinessObject class: " + ((LookupForm) form).getBusinessObjectClassName(), e);
 76  0
                 super.checkAuthorization(form, methodToCall);
 77  0
             }
 78  
         }
 79  0
     }
 80  
 
 81  
     private static MaintenanceDocumentDictionaryService maintenanceDocumentDictionaryService;
 82  
     private static DocumentHelperService documentHelperService;
 83  
     private static MaintenanceDocumentDictionaryService getMaintenanceDocumentDictionaryService() {
 84  0
             if (maintenanceDocumentDictionaryService == null) {
 85  0
                     maintenanceDocumentDictionaryService = KNSServiceLocator.getMaintenanceDocumentDictionaryService();
 86  
             }
 87  0
             return maintenanceDocumentDictionaryService;
 88  
     }
 89  
     private static DocumentHelperService getDocumentHelperService() {
 90  0
             if (documentHelperService == null) {
 91  0
                 documentHelperService = KRADServiceLocatorWeb.getDocumentHelperService();
 92  
             }
 93  0
             return documentHelperService;
 94  
     }
 95  
     /**
 96  
      * Checks if the user can create a document for this business object.  Used to suppress the actions on the results.
 97  
      *
 98  
      * @param form
 99  
      * @return
 100  
      * @throws ClassNotFoundException
 101  
      */
 102  
     protected void supressActionsIfNeeded( ActionForm form ) throws ClassNotFoundException {
 103  0
         if ((form instanceof LookupForm) && ( ((LookupForm)form).getBusinessObjectClassName() != null )) {
 104  0
             Class businessObjectClass = Class.forName( ((LookupForm)form).getBusinessObjectClassName() );
 105  
             // check if creating documents is allowed
 106  0
             String documentTypeName = getMaintenanceDocumentDictionaryService().getDocumentTypeName(businessObjectClass);
 107  0
             if ((documentTypeName != null) && !getDocumentHelperService().getDocumentAuthorizer(documentTypeName).canInitiate(documentTypeName, GlobalVariables.getUserSession().getPerson())) {
 108  0
                 ((LookupForm)form).setSuppressActions( true );
 109  
             }
 110  
         }
 111  0
     }
 112  
 
 113  
     /**
 114  
          * This method hides the criteria if set in parameter or lookupable
 115  
          *
 116  
          * @param form
 117  
          */
 118  
         private void setCriteriaEnabled(ActionForm form) {
 119  0
                  LookupForm lookupForm = (LookupForm) form;
 120  0
                  if(lookupForm.isLookupCriteriaEnabled()) {
 121  
                          //only overide if it's enabled, if disabled don't call lookupable
 122  
                  }
 123  0
         }
 124  
         /**
 125  
          * This method hides actions that are not related to the maintenance (as opposed to supressActionsIfNeeded)
 126  
          *
 127  
          * @param form
 128  
          */
 129  
         private void suppressNonMaintActionsIfNeeded(ActionForm form) {
 130  0
                 LookupForm lookupForm = (LookupForm) form;
 131  0
                 if(lookupForm.getLookupable()!=null) {
 132  0
                         if(StringUtils.isNotEmpty(lookupForm.getLookupable().getSupplementalMenuBar())) {
 133  0
                                 lookupForm.setSupplementalActionsEnabled(true);
 134  
                         }
 135  
 
 136  
                 }
 137  0
         }
 138  
 
 139  
         @Override
 140  
         public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
 141  
                         HttpServletResponse response) throws Exception {
 142  0
                 LookupForm lookupForm = (LookupForm) form;
 143  
 
 144  0
                 request.setAttribute(KRADConstants.PARAM_MAINTENANCE_VIEW_MODE, KRADConstants.PARAM_MAINTENANCE_VIEW_MODE_LOOKUP);
 145  0
                 supressActionsIfNeeded(form);
 146  0
                 suppressNonMaintActionsIfNeeded(form);
 147  0
                 setCriteriaEnabled(form);
 148  
 
 149  0
                 hideHeaderBarIfNeeded(form, request);
 150  
 
 151  0
                 int numCols = KRADServiceLocatorWeb.getBusinessObjectDictionaryService().getLookupNumberOfColumns(
 152  
                                 Class.forName(lookupForm.getBusinessObjectClassName()));
 153  0
                 lookupForm.setNumColumns(numCols);
 154  
 
 155  0
                 ActionForward forward = super.execute(mapping, form, request, response);
 156  
 
 157  
                 // apply conditional logic after all setting of field values has been completed
 158  0
                 lookupForm.getLookupable().applyConditionalLogicForFieldDisplay();
 159  
 
 160  0
                 return forward;
 161  
         }
 162  
 
 163  
         private void hideHeaderBarIfNeeded(ActionForm form, HttpServletRequest request) {
 164  0
                 if (!((LookupForm) form).isHeaderBarEnabled()) {
 165  0
                         ((LookupForm) form).setHeaderBarEnabled(false);
 166  
                 }
 167  0
         }
 168  
 
 169  
         
 170  
         /**
 171  
      * Entry point to lookups, forwards to jsp for search render.
 172  
      */
 173  
     public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 174  0
         return mapping.findForward(RiceConstants.MAPPING_BASIC);
 175  
     }
 176  
 
 177  
     /**
 178  
      * search - sets the values of the data entered on the form on the jsp into a map and then searches for the results.
 179  
      */
 180  
     public ActionForward search(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 181  0
         LookupForm lookupForm = (LookupForm) form;
 182  
                         
 183  
        
 184  0
                 String methodToCall = findMethodToCall(form, request);
 185  0
                 if (methodToCall.equalsIgnoreCase("search")) {
 186  0
                         GlobalVariables.getUserSession().removeObjectsByPrefix(KRADConstants.SEARCH_METHOD);
 187  
                 }
 188  
                 
 189  
 
 190  
                 
 191  0
         Lookupable kualiLookupable = lookupForm.getLookupable();
 192  0
         if (kualiLookupable == null) {
 193  0
             LOG.error("Lookupable is null.");
 194  0
             throw new RuntimeException("Lookupable is null.");
 195  
         }
 196  
 
 197  0
         Collection displayList = new ArrayList();
 198  0
         ArrayList<ResultRow> resultTable = new ArrayList<ResultRow>();
 199  
 
 200  
         // validate search parameters
 201  0
         kualiLookupable.validateSearchParameters(lookupForm.getFields());
 202  
 
 203  0
         boolean bounded = true;
 204  
 
 205  0
         displayList = kualiLookupable.performLookup(lookupForm, resultTable, bounded);
 206  
 
 207  0
         if (kualiLookupable.isSearchUsingOnlyPrimaryKeyValues()) {
 208  0
             lookupForm.setSearchUsingOnlyPrimaryKeyValues(true);
 209  0
             lookupForm.setPrimaryKeyFieldLabels(kualiLookupable.getPrimaryKeyFieldLabels());
 210  
         }
 211  
         else {
 212  0
             lookupForm.setSearchUsingOnlyPrimaryKeyValues(false);
 213  0
             lookupForm.setPrimaryKeyFieldLabels(KRADConstants.EMPTY_STRING);
 214  
         }
 215  
 
 216  0
         if ( displayList instanceof CollectionIncomplete ){
 217  0
             request.setAttribute("reqSearchResultsActualSize", ((CollectionIncomplete) displayList).getActualSizeIfTruncated());
 218  
         } else {
 219  0
             request.setAttribute("reqSearchResultsActualSize", displayList.size() );
 220  
         }
 221  
 
 222  
         // Determine if at least one table entry has an action available. If any non-breaking space (&nbsp; or '\u00A0') characters
 223  
         // exist in the URL's value, they will be converted to regular whitespace ('\u0020').
 224  0
         boolean hasActionUrls = false;
 225  0
         for (Iterator<ResultRow> iterator = resultTable.iterator(); !hasActionUrls && iterator.hasNext();) {
 226  0
                         if (StringUtils.isNotBlank(HtmlUtils.htmlUnescape(iterator.next().getActionUrls()).replace('\u00A0', '\u0020'))) {
 227  0
                                 hasActionUrls = true;
 228  
                         }
 229  
                 }
 230  0
                 lookupForm.setActionUrlsExist(hasActionUrls);
 231  
 
 232  0
         request.setAttribute("reqSearchResults", resultTable);
 233  
 
 234  0
         if (request.getParameter(KRADConstants.SEARCH_LIST_REQUEST_KEY) != null) {
 235  0
             GlobalVariables.getUserSession().removeObject(request.getParameter(KRADConstants.SEARCH_LIST_REQUEST_KEY));
 236  
         }
 237  
         
 238  0
              request.setAttribute(KRADConstants.SEARCH_LIST_REQUEST_KEY, GlobalVariables.getUserSession().addObjectWithGeneratedKey(resultTable, KRADConstants.SEARCH_LIST_KEY_PREFIX));
 239  
                       
 240  0
         request.getParameter(KRADConstants.REFRESH_CALLER);
 241  
 
 242  0
         return mapping.findForward(RiceConstants.MAPPING_BASIC);
 243  
     }
 244  
 
 245  
 
 246  
     /**
 247  
      * refresh - is called when one quickFinder returns to the previous one. Sets all the values and performs the new search.
 248  
      */
 249  
     @Override
 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 (Object element : row.getFields()) {
 269  0
                         Field field = (Field) element;
 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 (Object element : row.getFields()) {
 298  0
                     Field field = (Field) element;
 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(KRADConstants.SEARCH_LIST_REQUEST_KEY, request.getParameter(KRADConstants.SEARCH_LIST_REQUEST_KEY));
 356  0
         request.setAttribute("reqSearchResults", GlobalVariables.getUserSession().retrieveObject(request.getParameter(
 357  
                 KRADConstants.SEARCH_LIST_REQUEST_KEY)));
 358  0
         request.setAttribute("reqSearchResultsActualSize", request.getParameter("reqSearchResultsActualSize"));
 359  0
         return mapping.findForward(RiceConstants.MAPPING_BASIC);
 360  
     }
 361  
 
 362  
     public ActionForward customLookupableMethodCall(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 363  
 //            lookupableMethodToCall
 364  0
             Lookupable kualiLookupable = ((LookupForm)form).getLookupable();
 365  0
             if (kualiLookupable == null) {
 366  0
             LOG.error("Lookupable is null.");
 367  0
             throw new RuntimeException("Lookupable is null.");
 368  
         }
 369  
 
 370  0
             boolean ignoreErrors=false;
 371  0
                 if(StringUtils.equals(((LookupForm)form).getRefreshCaller(),"customLookupAction")) {
 372  0
                         ignoreErrors=true;
 373  
             }
 374  
 
 375  0
                 if(kualiLookupable.performCustomAction(ignoreErrors)) {
 376  
                         //redo the search if the method comes back
 377  0
                         return search(mapping, form, request, response);
 378  
                 }
 379  0
                 return mapping.findForward(RiceConstants.MAPPING_BASIC);
 380  
 
 381  
     }
 382  
 
 383  
 }