Coverage Report - org.kuali.rice.kns.web.struts.form.InquiryForm
 
Classes in this File Line Coverage Branch Coverage Complexity
InquiryForm
0%
0/144
0%
0/44
2.458
 
 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.form;
 17  
 
 18  
 import java.lang.reflect.Constructor;
 19  
 import java.security.GeneralSecurityException;
 20  
 import java.util.ArrayList;
 21  
 import java.util.Enumeration;
 22  
 import java.util.HashMap;
 23  
 import java.util.List;
 24  
 import java.util.Map;
 25  
 
 26  
 import javax.servlet.http.HttpServletRequest;
 27  
 
 28  
 import org.apache.commons.lang.StringUtils;
 29  
 import org.kuali.rice.core.api.encryption.EncryptionService;
 30  
 import org.kuali.rice.core.api.services.CoreApiServiceLocator;
 31  
 import org.kuali.rice.kns.authorization.AuthorizationConstants;
 32  
 import org.kuali.rice.kns.bo.Exporter;
 33  
 import org.kuali.rice.kns.datadictionary.BusinessObjectEntry;
 34  
 import org.kuali.rice.kns.datadictionary.exception.UnknownBusinessClassAttributeException;
 35  
 import org.kuali.rice.kns.inquiry.Inquirable;
 36  
 import org.kuali.rice.kns.service.*;
 37  
 import org.kuali.rice.kns.util.KNSConstants;
 38  
 
 39  
 /**
 40  
  * This class is the action form for inquiries.
 41  
  */
 42  
 public class InquiryForm extends KualiForm {
 43  0
     private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(InquiryForm.class);
 44  
 
 45  
     private static final long serialVersionUID = 1L;
 46  
     private String fieldConversions;
 47  
     private List sections;
 48  
     private String businessObjectClassName;
 49  
     private Map editingMode;
 50  
     private String formKey;
 51  
     private boolean canExport;
 52  
 
 53  
     @Override
 54  
     public void addRequiredNonEditableProperties(){
 55  0
             super.addRequiredNonEditableProperties();
 56  0
             registerRequiredNonEditableProperty(KNSConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE);
 57  0
             registerRequiredNonEditableProperty(KNSConstants.DISPATCH_REQUEST_PARAMETER);
 58  0
             registerRequiredNonEditableProperty(KNSConstants.DOC_FORM_KEY);
 59  0
             registerRequiredNonEditableProperty(KNSConstants.FORM_KEY);
 60  0
             registerRequiredNonEditableProperty(KNSConstants.FIELDS_CONVERSION_PARAMETER);
 61  0
             registerRequiredNonEditableProperty(KNSConstants.BACK_LOCATION);
 62  0
     }
 63  
 
 64  
     /**
 65  
      * The following map is used to pass primary key values between invocations of the inquiry screens after the start method has been called.  Values in this map will remain encrypted
 66  
      * if the value was passed in as encrypted
 67  
      */
 68  
     private Map<String, String> inquiryPrimaryKeys;
 69  
 
 70  
     private Map<String, String> inquiryDecryptedPrimaryKeys;
 71  
 
 72  
     /**
 73  
      * A map of collection name -> Boolean mappings.  Used to denote whether a collection name is configured to show inactive records.
 74  
      */
 75  
     private Map<String, Boolean> inactiveRecordDisplay;
 76  
 
 77  
     private Inquirable inquirable;
 78  
 
 79  
     public InquiryForm() {
 80  0
         super();
 81  0
         this.editingMode = new HashMap();
 82  0
         this.editingMode.put(AuthorizationConstants.EditMode.VIEW_ONLY, "TRUE");
 83  0
         this.inactiveRecordDisplay = null;
 84  0
     }
 85  
 
 86  
         @Override
 87  
     public void populate(HttpServletRequest request) {
 88  
     // set to null for security reasons (so POJO form base can't access it), then we'll make an new instance of it after
 89  
     // POJO form base is done
 90  0
         this.inquirable = null;
 91  0
         super.populate(request);
 92  0
         if (request.getParameter("returnLocation") != null) {
 93  0
             setBackLocation(request.getParameter("returnLocation"));
 94  
         }
 95  0
         if (request.getParameter(KNSConstants.DOC_FORM_KEY) != null) {
 96  0
             setFormKey(request.getParameter(KNSConstants.DOC_FORM_KEY));
 97  
         }
 98  
         //if the action is download attachment then skip the following populate logic
 99  0
         if(!KNSConstants.DOWNLOAD_BO_ATTACHMENT_METHOD.equals(getMethodToCall())){
 100  0
                 inquirable = getInquirable(getBusinessObjectClassName());
 101  
 
 102  
                 // the following variable is true if the method to call is not start, meaning that we already called start
 103  0
                 boolean passedFromPreviousInquiry = !KNSConstants.START_METHOD.equals(getMethodToCall()) && !KNSConstants.CONTINUE_WITH_INQUIRY_METHOD_TO_CALL.equals(getMethodToCall()) && !KNSConstants.DOWNLOAD_CUSTOM_BO_ATTACHMENT_METHOD.equals(getMethodToCall());
 104  
 
 105  
                 // There is no requirement that an inquiry screen must display the primary key values.  However, when clicking on hide/show (without javascript) and
 106  
                 // hide/show inactive, the PK values are needed to allow the server to properly render results after the user clicks on a hide/show button that results
 107  
                 // in server processing.  This line will populate the form with the PK values so that they may be used in subsequent requests.  Note that encrypted
 108  
                 // values will remain encrypted in this map.
 109  0
                 this.inquiryPrimaryKeys = new HashMap<String, String>();
 110  0
                 this.inquiryDecryptedPrimaryKeys = new HashMap<String, String>();
 111  
 
 112  0
                 populatePKFieldValues(request, getBusinessObjectClassName(), passedFromPreviousInquiry);
 113  
 
 114  0
                 populateInactiveRecordsInIntoInquirable(inquirable, request);
 115  0
                 populateExportCapabilities(request, getBusinessObjectClassName());
 116  
         }
 117  0
     }
 118  
 
 119  
     protected Inquirable getInquirable(String boClassName) {
 120  
         try {
 121  0
             Class customInquirableClass = null;
 122  
 
 123  
             try {
 124  0
                 customInquirableClass = KNSServiceLocatorWeb.getDataDictionaryService().getDataDictionary().getBusinessObjectEntry(boClassName).getInquiryDefinition().getInquirableClass();
 125  
             }
 126  0
             catch (Exception e) {
 127  0
                 LOG.error("Unable to correlate business object class with maintenance document entry");
 128  0
             }
 129  
 
 130  0
             Inquirable kualiInquirable = KNSServiceLocatorWeb.getKualiInquirable(); // get inquirable impl from Spring
 131  
 
 132  0
             if (customInquirableClass != null) {
 133  0
                 Class[] defaultConstructor = new Class[] {};
 134  0
                 Constructor cons = customInquirableClass.getConstructor(defaultConstructor);
 135  0
                 kualiInquirable = (Inquirable) cons.newInstance();
 136  
             }
 137  
 
 138  0
             kualiInquirable.setBusinessObjectClass(Class.forName(boClassName));
 139  
 
 140  0
             return kualiInquirable;
 141  
         }
 142  0
         catch (Exception e) {
 143  0
             LOG.error("Error attempting to retrieve inquirable.", e);
 144  0
             throw new RuntimeException("Error attempting to retrieve inquirable.");
 145  
         }
 146  
     }
 147  
 
 148  
     /**
 149  
      * Gets the alt keys for a class.  Will not return null but and empty list if no keys exist.
 150  
      *
 151  
      * @param clazz the class.
 152  
      * @return the alt keys
 153  
      */
 154  
     private List<List<String>> getAltkeys(Class<?> clazz) {
 155  0
             final KualiModuleService kualiModuleService = KNSServiceLocatorWeb.getKualiModuleService();
 156  0
             final ModuleService moduleService = kualiModuleService.getResponsibleModuleService(clazz);
 157  
 
 158  0
         List<List<String>> altKeys = null;
 159  0
         if (moduleService != null) {
 160  0
                 altKeys = moduleService.listAlternatePrimaryKeyFieldNames(clazz);
 161  
         }
 162  
 
 163  0
         return altKeys != null ? altKeys : new ArrayList<List<String>>();
 164  
     }
 165  
 
 166  
     protected void populatePKFieldValues(HttpServletRequest request, String boClassName, boolean passedFromPreviousInquiry) {
 167  
         try {
 168  0
             EncryptionService encryptionService = CoreApiServiceLocator.getEncryptionService();
 169  0
             DataDictionaryService dataDictionaryService = KNSServiceLocatorWeb.getDataDictionaryService();
 170  0
             BusinessObjectAuthorizationService businessObjectAuthorizationService = KNSServiceLocatorWeb.getBusinessObjectAuthorizationService();
 171  0
             BusinessObjectMetaDataService businessObjectMetaDataService = KNSServiceLocatorWeb.getBusinessObjectMetaDataService();
 172  
 
 173  0
             Class businessObjectClass = Class.forName(boClassName);
 174  
 
 175  
             // build list of key values from request, if all keys not given throw error
 176  0
             List<String> boPKeys = businessObjectMetaDataService.listPrimaryKeyFieldNames(businessObjectClass);
 177  0
             final List<List<String>> altKeys = this.getAltkeys(businessObjectClass);
 178  
 
 179  0
             altKeys.add(boPKeys);
 180  0
             boolean bFound = false;
 181  0
             for(List<String> boKeys : altKeys ){
 182  0
                     if(bFound)
 183  0
                             break;
 184  0
                     int keyCount = boKeys.size();
 185  0
                     int foundCount = 0;
 186  0
                 for (String boKey : boKeys) {
 187  0
                     String pkParamName = boKey;
 188  0
                         if (passedFromPreviousInquiry) {
 189  0
                             pkParamName = KNSConstants.INQUIRY_PK_VALUE_PASSED_FROM_PREVIOUS_REQUEST_PREFIX + pkParamName;
 190  
                         }
 191  
 
 192  0
                         if (request.getParameter(pkParamName) != null) {
 193  0
                                 foundCount++;
 194  0
                                 String parameter = request.getParameter(pkParamName);
 195  
                         
 196  0
                         Boolean forceUppercase = Boolean.FALSE;
 197  
                         try {
 198  0
                             forceUppercase = dataDictionaryService.getAttributeForceUppercase(businessObjectClass, boKey);
 199  0
                         } catch (UnknownBusinessClassAttributeException ex) {
 200  
                             // swallowing exception because this check for ForceUppercase would
 201  
                             // require a DD entry for the attribute.  it is only checking keys
 202  
                             // so most likely there should be an entry.
 203  0
                             LOG.warn("BO class " + businessObjectClassName + " property " + boKey + " should probably have a DD definition.", ex);
 204  0
                         }
 205  0
                         String parameterCopy = parameter;
 206  0
                         if (forceUppercase) {
 207  0
                                         parameter = parameter.toUpperCase();
 208  
                                 }
 209  
 
 210  0
                         inquiryPrimaryKeys.put(boKey, parameter);
 211  0
                         if (businessObjectAuthorizationService.attributeValueNeedsToBeEncryptedOnFormsAndLinks(businessObjectClass, boKey)) {
 212  
                             try {
 213  0
                                 inquiryDecryptedPrimaryKeys.put(boKey, encryptionService.decrypt(parameterCopy));
 214  0
                             } catch (GeneralSecurityException e) {
 215  0
                                 LOG.error("BO class " + businessObjectClassName + " property " + boKey + " should have been encrypted, but there was a problem decrypting it.");
 216  0
                                 throw e;
 217  0
                             }
 218  
                         }
 219  
                         else {
 220  0
                             inquiryDecryptedPrimaryKeys.put(boKey, parameter);
 221  
                         }
 222  
                     }
 223  0
                 }
 224  0
                 if (foundCount == keyCount) {
 225  0
                     bFound = true;
 226  
                 }
 227  0
             }
 228  0
             if(!bFound){
 229  0
                 LOG.error("All keys not given to lookup for bo class name " + businessObjectClass.getName());
 230  0
                 throw new RuntimeException("All keys not given to lookup for bo class name " + businessObjectClass.getName());
 231  
             }
 232  
         }
 233  0
         catch (ClassNotFoundException e) {
 234  0
                 LOG.error("Can't instantiate class: " + boClassName, e);
 235  0
                 throw new RuntimeException("Can't instantiate class: " + boClassName);
 236  
         }
 237  0
         catch (GeneralSecurityException e) {
 238  0
                 LOG.error("Can't decrypt value", e);
 239  0
                 throw new RuntimeException("Can't decrypt value");
 240  0
         }
 241  0
     }
 242  
 
 243  
     /**
 244  
      * Examines the BusinessObject's data dictionary entry to determine if it supports
 245  
      * XML export or not and set's canExport appropriately.
 246  
      */
 247  
     protected void populateExportCapabilities(HttpServletRequest request, String boClassName) {
 248  0
             setCanExport(false);
 249  0
             BusinessObjectEntry businessObjectEntry = KNSServiceLocatorWeb.getDataDictionaryService().getDataDictionary().getBusinessObjectEntry(boClassName);
 250  0
             Class<? extends Exporter> exporterClass = businessObjectEntry.getExporterClass();
 251  0
             if (exporterClass != null) {
 252  
                     try {
 253  0
                             Exporter exporter = exporterClass.newInstance();
 254  0
                             if (exporter.getSupportedFormats(businessObjectEntry.getBusinessObjectClass()).contains(KNSConstants.XML_FORMAT)) {
 255  0
                                     setCanExport(true);
 256  
                             }
 257  0
                     } catch (Exception e) {
 258  0
                             LOG.error("Failed to locate or create exporter class: " + exporterClass, e);
 259  0
                             throw new RuntimeException("Failed to locate or create exporter class: " + exporterClass);
 260  0
                     }
 261  
             }
 262  0
     }
 263  
 
 264  
 
 265  
     /**
 266  
      * @return Returns the fieldConversions.
 267  
      */
 268  
     public String getFieldConversions() {
 269  0
         return fieldConversions;
 270  
     }
 271  
 
 272  
 
 273  
     /**
 274  
      * @param fieldConversions The fieldConversions to set.
 275  
      */
 276  
     public void setFieldConversions(String fieldConversions) {
 277  0
         this.fieldConversions = fieldConversions;
 278  0
     }
 279  
 
 280  
 
 281  
     /**
 282  
      * @return Returns the inquiry sections.
 283  
      */
 284  
     public List getSections() {
 285  0
         return sections;
 286  
     }
 287  
 
 288  
 
 289  
     /**
 290  
      * @param sections The sections to set.
 291  
      */
 292  
     public void setSections(List sections) {
 293  0
         this.sections = sections;
 294  0
     }
 295  
 
 296  
     /**
 297  
      * @return Returns the businessObjectClassName.
 298  
      */
 299  
     public String getBusinessObjectClassName() {
 300  0
         return businessObjectClassName;
 301  
     }
 302  
 
 303  
     /**
 304  
      * @param businessObjectClassName The businessObjectClassName to set.
 305  
      */
 306  
     public void setBusinessObjectClassName(String businessObjectClassName) {
 307  0
         this.businessObjectClassName = businessObjectClassName;
 308  0
     }
 309  
 
 310  
     public Map getEditingMode() {
 311  0
         return editingMode;
 312  
     }
 313  
 
 314  
     /**
 315  
      * Gets the map used to pass primary key values between invocations of the inquiry screens after the start method has been called.  All field values that were passed in encrypted will
 316  
      * be encrypted in this map
 317  
      *
 318  
      * @return
 319  
      */
 320  
     public Map<String, String> getInquiryPrimaryKeys() {
 321  0
         return this.inquiryPrimaryKeys;
 322  
     }
 323  
 
 324  
     /**
 325  
      * Gets the map used to pass primary key values between invocations of the inquiry screens after the start method has been called.  All fields will be decrypted
 326  
      *
 327  
      * Purposely not named as a getter, to make it harder for POJOFormBase to access it
 328  
      *
 329  
      * @return
 330  
      */
 331  
     public Map<String, String> retrieveInquiryDecryptedPrimaryKeys() {
 332  0
         return this.inquiryDecryptedPrimaryKeys;
 333  
     }
 334  
 
 335  
     /**
 336  
      * Sets the map used to pass primary key values between invocations of the inquiry screens after the start method has been called.
 337  
      *
 338  
      * @param inquiryPrimaryKeys
 339  
      */
 340  
     public void setInquiryPrimaryKeys(Map<String, String> inquiryPrimaryKeys) {
 341  0
         this.inquiryPrimaryKeys = inquiryPrimaryKeys;
 342  0
     }
 343  
 
 344  
     /**
 345  
      * Gets map of collection name -> Boolean mappings.  Used to denote whether a collection name is configured to show inactive records.
 346  
      *
 347  
      * @return
 348  
      */
 349  
     public Map<String, Boolean> getInactiveRecordDisplay() {
 350  0
         return getInquirable().getInactiveRecordDisplay();
 351  
     }
 352  
 
 353  
     public Inquirable getInquirable() {
 354  0
         return inquirable;
 355  
     }
 356  
 
 357  
     protected void populateInactiveRecordsInIntoInquirable(Inquirable inquirable, HttpServletRequest request) {
 358  0
         for (Enumeration e = request.getParameterNames(); e.hasMoreElements();) {
 359  0
             String paramName = (String) e.nextElement();
 360  0
             if (paramName.startsWith(KNSConstants.INACTIVE_RECORD_DISPLAY_PARAM_PREFIX)) {
 361  0
                 String collectionName = StringUtils.substringAfter(paramName, KNSConstants.INACTIVE_RECORD_DISPLAY_PARAM_PREFIX);
 362  0
                 Boolean showInactive = Boolean.parseBoolean(request.getParameter(paramName));
 363  0
                 inquirable.setShowInactiveRecords(collectionName, showInactive);
 364  
             }
 365  0
         }
 366  0
     }
 367  
 
 368  
     public String getFormKey() {
 369  0
         return this.formKey;
 370  
     }
 371  
 
 372  
     public void setFormKey(String formKey) {
 373  0
         this.formKey = formKey;
 374  0
     }
 375  
 
 376  
         /**
 377  
          * Returns true if this Inquiry supports XML export of the BusinessObject.
 378  
          */
 379  
         public boolean isCanExport() {
 380  0
                 return this.canExport;
 381  
         }
 382  
 
 383  
         /**
 384  
          * Sets whether or not this Inquiry supports XML export of it's BusinessObject.
 385  
          */
 386  
         public void setCanExport(boolean canExport) {
 387  0
                 this.canExport = canExport;
 388  0
         }
 389  
 
 390  
 
 391  
 }