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