Coverage Report - org.kuali.rice.krad.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.krad.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.Map;
 24  
 
 25  
 import javax.servlet.ServletException;
 26  
 import javax.servlet.http.HttpServletRequest;
 27  
 import javax.servlet.http.HttpServletResponse;
 28  
 
 29  
 import org.apache.commons.lang.StringUtils;
 30  
 import org.apache.struts.action.ActionForm;
 31  
 import org.apache.struts.action.ActionForward;
 32  
 import org.apache.struts.action.ActionMapping;
 33  
 import org.kuali.rice.core.api.mo.common.Attributes;
 34  
 import org.kuali.rice.core.util.RiceConstants;
 35  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 36  
 import org.kuali.rice.kim.util.KimConstants;
 37  
 import org.kuali.rice.krad.exception.AuthorizationException;
 38  
 import org.kuali.rice.krad.lookup.CollectionIncomplete;
 39  
 import org.kuali.rice.krad.lookup.Lookupable;
 40  
 import org.kuali.rice.krad.service.DocumentHelperService;
 41  
 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
 42  
 import org.kuali.rice.krad.service.MaintenanceDocumentDictionaryService;
 43  
 import org.kuali.rice.krad.util.GlobalVariables;
 44  
 import org.kuali.rice.krad.util.KRADConstants;
 45  
 import org.kuali.rice.krad.util.KRADUtils;
 46  
 import org.kuali.rice.krad.web.struts.form.LookupForm;
 47  
 import org.kuali.rice.krad.web.ui.Field;
 48  
 import org.kuali.rice.krad.web.ui.ResultRow;
 49  
 import org.kuali.rice.krad.web.ui.Row;
 50  
 import org.springframework.web.util.HtmlUtils;
 51  
 
 52  
 /**
 53  
  * This class handles Actions for lookup flow
 54  
  *
 55  
  *
 56  
  */
 57  
 
 58  0
 public class KualiLookupAction extends KualiAction {
 59  0
     private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(KualiLookupAction.class);
 60  
 
 61  
     @Override
 62  
     protected void checkAuthorization(ActionForm form, String methodToCall) throws AuthorizationException {
 63  0
         if (!(form instanceof LookupForm)) {
 64  0
             super.checkAuthorization(form, methodToCall);
 65  
         } else {
 66  
             try {
 67  0
                 Class businessObjectClass = Class.forName(((LookupForm) form).getBusinessObjectClassName());
 68  0
                 if (!KimApiServiceLocator.getIdentityManagementService().isAuthorizedByTemplateName(GlobalVariables.getUserSession().getPrincipalId(), KRADConstants.KRAD_NAMESPACE, KimConstants.PermissionTemplateNames.LOOK_UP_RECORDS,
 69  
                         Attributes.fromMap(KRADUtils.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 = KRADServiceLocatorWeb.getMaintenanceDocumentDictionaryService();
 87  
             }
 88  0
             return maintenanceDocumentDictionaryService;
 89  
     }
 90  
     private static DocumentHelperService getDocumentHelperService() {
 91  0
             if (documentHelperService == null) {
 92  0
                 documentHelperService = KRADServiceLocatorWeb.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(KRADConstants.PARAM_MAINTENANCE_VIEW_MODE, KRADConstants.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 = KRADServiceLocatorWeb.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(KRADConstants.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
         ArrayList<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(KRADConstants.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(KRADConstants.SEARCH_LIST_REQUEST_KEY) != null) {
 236  0
             GlobalVariables.getUserSession().removeObject(request.getParameter(KRADConstants.SEARCH_LIST_REQUEST_KEY));
 237  
         }
 238  
         
 239  0
              request.setAttribute(KRADConstants.SEARCH_LIST_REQUEST_KEY, GlobalVariables.getUserSession().addObjectWithGeneratedKey(resultTable, KRADConstants.SEARCH_LIST_KEY_PREFIX));
 240  
                       
 241  0
         request.getParameter(KRADConstants.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  
     @Override
 251  
         public ActionForward refresh(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 252  0
         LookupForm lookupForm = (LookupForm) form;
 253  0
         Lookupable kualiLookupable = lookupForm.getLookupable();
 254  0
         if (kualiLookupable == null) {
 255  0
             LOG.error("Lookupable is null.");
 256  0
             throw new RuntimeException("Lookupable is null.");
 257  
         }
 258  
 
 259  0
         if(StringUtils.equals(lookupForm.getRefreshCaller(),"customLookupAction")) {
 260  0
                 return this.customLookupableMethodCall(mapping, lookupForm, request, response);
 261  
         }
 262  
 
 263  0
         Map fieldValues = new HashMap();
 264  0
         Map values = lookupForm.getFields();
 265  
 
 266  0
         for (Iterator iter = kualiLookupable.getRows().iterator(); iter.hasNext();) {
 267  0
                 Row row = (Row) iter.next();
 268  
 
 269  0
                 for (Object element : row.getFields()) {
 270  0
                         Field field = (Field) element;
 271  
 
 272  0
                         if (field.getPropertyName() != null && !field.getPropertyName().equals("")) {
 273  0
                                 if (request.getParameter(field.getPropertyName()) != null) {
 274  0
                                         if(!Field.MULTI_VALUE_FIELD_TYPES.contains(field.getFieldType())) {
 275  0
                                                 field.setPropertyValue(request.getParameter(field.getPropertyName()));
 276  
                                         } else {
 277  
                                                 //multi value, set to values
 278  0
                                                 field.setPropertyValues(request.getParameterValues(field.getPropertyName()));
 279  
                                         }
 280  
                                 }
 281  
                         }
 282  0
                         else if (values.get(field.getPropertyName()) != null) {
 283  0
                                 field.setPropertyValue(values.get(field.getPropertyName()));
 284  
                         }
 285  
 
 286  0
                         kualiLookupable.applyFieldAuthorizationsFromNestedLookups(field);
 287  
 
 288  0
                         fieldValues.put(field.getPropertyName(), field.getPropertyValue());
 289  0
                 }
 290  0
         }
 291  0
         fieldValues.put("docFormKey", lookupForm.getFormKey());
 292  0
         fieldValues.put("backLocation", lookupForm.getBackLocation());
 293  0
         fieldValues.put("docNum", lookupForm.getDocNum());
 294  
 
 295  0
         if (kualiLookupable.checkForAdditionalFields(fieldValues)) {
 296  0
             for (Iterator iter = kualiLookupable.getRows().iterator(); iter.hasNext();) {
 297  0
                 Row row = (Row) iter.next();
 298  0
                 for (Object element : row.getFields()) {
 299  0
                     Field field = (Field) element;
 300  0
                     if (field.getPropertyName() != null && !field.getPropertyName().equals("")) {
 301  0
                         if (request.getParameter(field.getPropertyName()) != null) {
 302  
 //                            field.setPropertyValue(request.getParameter(field.getPropertyName()));
 303  0
                                             if(!Field.MULTI_VALUE_FIELD_TYPES.contains(field.getFieldType())) {
 304  0
                                                     field.setPropertyValue(request.getParameter(field.getPropertyName()));
 305  
                                             } else {
 306  
                                                     //multi value, set to values
 307  0
                                                     field.setPropertyValues(request.getParameterValues(field.getPropertyName()));
 308  
                                             }
 309  
                             //FIXME: any reason this is inside this "if" instead of the outer one, like above - this seems inconsistent
 310  0
                             fieldValues.put(field.getPropertyName(), request.getParameter(field.getPropertyName()));
 311  
                         }
 312  0
                         else if (values.get(field.getPropertyName()) != null) {
 313  0
                             field.setPropertyValue(values.get(field.getPropertyName()));
 314  
                         }
 315  
                     }
 316  0
                 }
 317  0
             }
 318  
         }
 319  0
         return mapping.findForward(RiceConstants.MAPPING_BASIC);
 320  
     }
 321  
 
 322  
     /**
 323  
      * Just returns as if return with no value was selected.
 324  
      */
 325  
     public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 326  0
         LookupForm lookupForm = (LookupForm) form;
 327  
 
 328  0
         String backUrl = lookupForm.getBackLocation() + "?methodToCall=refresh&docFormKey=" + lookupForm.getFormKey()+"&docNum="+lookupForm.getDocNum();
 329  0
         return new ActionForward(backUrl, true);
 330  
     }
 331  
 
 332  
 
 333  
     /**
 334  
      * clearValues - clears the values of all the fields on the jsp.
 335  
      */
 336  
     public ActionForward clearValues(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
 337  0
         LookupForm lookupForm = (LookupForm) form;
 338  0
         Lookupable kualiLookupable = lookupForm.getLookupable();
 339  0
         if (kualiLookupable == null) {
 340  0
             LOG.error("Lookupable is null.");
 341  0
             throw new RuntimeException("Lookupable is null.");
 342  
         }
 343  
 
 344  0
         kualiLookupable.performClear(lookupForm);
 345  
 
 346  
 
 347  0
         return mapping.findForward(RiceConstants.MAPPING_BASIC);
 348  
     }
 349  
 
 350  
 
 351  
     public ActionForward viewResults(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 352  0
         LookupForm lookupForm = (LookupForm) form;
 353  0
         if (lookupForm.isSearchUsingOnlyPrimaryKeyValues()) {
 354  0
             lookupForm.setPrimaryKeyFieldLabels(lookupForm.getLookupable().getPrimaryKeyFieldLabels());
 355  
         }
 356  0
         request.setAttribute(KRADConstants.SEARCH_LIST_REQUEST_KEY, request.getParameter(KRADConstants.SEARCH_LIST_REQUEST_KEY));
 357  0
         request.setAttribute("reqSearchResults", GlobalVariables.getUserSession().retrieveObject(request.getParameter(
 358  
                 KRADConstants.SEARCH_LIST_REQUEST_KEY)));
 359  0
         request.setAttribute("reqSearchResultsActualSize", request.getParameter("reqSearchResultsActualSize"));
 360  0
         return mapping.findForward(RiceConstants.MAPPING_BASIC);
 361  
     }
 362  
 
 363  
     public ActionForward customLookupableMethodCall(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 364  
 //            lookupableMethodToCall
 365  0
             Lookupable kualiLookupable = ((LookupForm)form).getLookupable();
 366  0
             if (kualiLookupable == null) {
 367  0
             LOG.error("Lookupable is null.");
 368  0
             throw new RuntimeException("Lookupable is null.");
 369  
         }
 370  
 
 371  0
             boolean ignoreErrors=false;
 372  0
                 if(StringUtils.equals(((LookupForm)form).getRefreshCaller(),"customLookupAction")) {
 373  0
                         ignoreErrors=true;
 374  
             }
 375  
 
 376  0
                 if(kualiLookupable.performCustomAction(ignoreErrors)) {
 377  
                         //redo the search if the method comes back
 378  0
                         return search(mapping, form, request, response);
 379  
                 }
 380  0
                 return mapping.findForward(RiceConstants.MAPPING_BASIC);
 381  
 
 382  
     }
 383  
 
 384  
 }