Coverage Report - org.kuali.rice.krad.web.struts.form.KualiTransactionalDocumentFormBase
 
Classes in this File Line Coverage Branch Coverage Complexity
KualiTransactionalDocumentFormBase
0%
0/51
0%
0/46
3.25
 
 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.form;
 17  
 
 18  
 import java.util.HashMap;
 19  
 import java.util.Map;
 20  
 
 21  
 import javax.servlet.http.HttpServletRequest;
 22  
 
 23  
 import org.apache.commons.lang.StringUtils;
 24  
 import org.apache.log4j.Logger;
 25  
 import org.kuali.rice.core.api.CoreApiServiceLocator;
 26  
 import org.kuali.rice.kim.util.KimConstants;
 27  
 import org.kuali.rice.krad.document.TransactionalDocument;
 28  
 import org.kuali.rice.krad.service.DataDictionaryService;
 29  
 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
 30  
 import org.kuali.rice.krad.util.KRADConstants;
 31  
 
 32  
 
 33  
 /**
 34  
  * This class is the base action form for all transactional documents.
 35  
  */
 36  
 public class KualiTransactionalDocumentFormBase extends KualiDocumentFormBase {
 37  0
         private static final Logger LOG = Logger.getLogger(KualiTransactionalDocumentFormBase.class);
 38  
     /**
 39  
          * 
 40  
          */
 41  
         private static final long serialVersionUID = 6463383454050206811L;
 42  
         @SuppressWarnings("unchecked")
 43  
         protected Map forcedReadOnlyFields;
 44  
 
 45  
     /**
 46  
      * This constructor sets up empty instances for the dependent objects...
 47  
      */
 48  
     @SuppressWarnings("unchecked")
 49  
         public KualiTransactionalDocumentFormBase() {
 50  0
         super();
 51  
 
 52  
         // create a blank DocumentActionFlags instance, since form-recreation needs it
 53  0
         forcedReadOnlyFields = new HashMap();
 54  0
     }
 55  
 
 56  
     /**
 57  
          * @see org.kuali.rice.krad.web.struts.form.KualiDocumentFormBase#populate(javax.servlet.http.HttpServletRequest)
 58  
          */
 59  
     @SuppressWarnings("unchecked")
 60  
         @Override
 61  
         public void populate(HttpServletRequest request) {
 62  0
                 super.populate(request);
 63  0
                 populationSpecialEmptyFields (request);
 64  0
         }
 65  
 
 66  
         /**
 67  
      * This method retrieves an instance of the form.
 68  
      * 
 69  
      * @return
 70  
      */
 71  
     public TransactionalDocument getTransactionalDocument() {
 72  0
         return (TransactionalDocument) getDocument();
 73  
     }
 74  
     
 75  
 
 76  
     /**
 77  
      * Locates the <code>DictionaryService</code> to discover the type name of the document.
 78  
      * 
 79  
      * @return
 80  
      */
 81  
     protected String discoverDocumentTypeName() {
 82  0
         return ((DataDictionaryService) KRADServiceLocatorWeb.getDataDictionaryService()).getDataDictionary().getDocumentEntry(getDocument().getClass().getName()).getDocumentTypeName();
 83  
     }
 84  
 
 85  
     /**
 86  
      * This method formats the given java.sql.Date as MMM d, yyyy.
 87  
      * 
 88  
      * @param reversalDate
 89  
      * 
 90  
      * @return String
 91  
      */
 92  
     protected static String formatReversalDate(java.sql.Date reversalDate) {
 93  0
         if (reversalDate == null) {
 94  0
             return "";
 95  
         }
 96  
         // new for thread safety
 97  0
         return CoreApiServiceLocator.getDateTimeService().toString(reversalDate, "MMM d, yyyy");
 98  
     }
 99  
 
 100  
     /**
 101  
      * Gets the forcedReadOnlyFields attribute.
 102  
      * 
 103  
      * @return Returns the forcedReadOnlyFields.
 104  
      */
 105  
     @SuppressWarnings("unchecked")
 106  
         public Map getForcedReadOnlyFields() {
 107  0
         return forcedReadOnlyFields;
 108  
     }
 109  
 
 110  
     /**
 111  
      * Sets the forcedReadOnlyFields attribute value.
 112  
      * 
 113  
      * @param forcedReadOnlyFields The forcedReadOnlyFields to set.
 114  
      */
 115  
     @SuppressWarnings("unchecked")
 116  
         public void setForcedReadOnlyFields(Map forcedReadOnlyFields) {
 117  0
         this.forcedReadOnlyFields = forcedReadOnlyFields;
 118  0
     }
 119  
     
 120  
     /**
 121  
      * Uses the "checkboxToReset" parameter to find checkboxes which had not been
 122  
      * populated in the request and attempts to populate them
 123  
      * 
 124  
      * @param request the request to populate
 125  
      */
 126  
     protected void populateFalseCheckboxes(HttpServletRequest request) {
 127  0
             Map<String, String[]> parameterMap = request.getParameterMap();
 128  0
             if (parameterMap.get("checkboxToReset") != null) {
 129  0
                     final String[] checkboxesToReset = request.getParameterValues("checkboxToReset");
 130  0
             if(checkboxesToReset != null && checkboxesToReset.length > 0) {
 131  0
                 for (int i = 0; i < checkboxesToReset.length; i++) {
 132  0
                     String propertyName = (String) checkboxesToReset[i];
 133  0
                     if ( !StringUtils.isBlank(propertyName) && parameterMap.get(propertyName) == null ) {
 134  0
                             populateForProperty(propertyName, KimConstants.KIM_ATTRIBUTE_BOOLEAN_FALSE_STR_VALUE_DISPLAY, parameterMap);
 135  
                     }  
 136  0
                     else if ( !StringUtils.isBlank(propertyName) && parameterMap.get(propertyName) != null && parameterMap.get(propertyName).length >= 1 && parameterMap.get(propertyName)[0].equalsIgnoreCase("on") ) {
 137  0
                             populateForProperty(propertyName, KimConstants.KIM_ATTRIBUTE_BOOLEAN_TRUE_STR_VALUE_DISPLAY, parameterMap); 
 138  
                     }
 139  
                 }
 140  
             }
 141  
             }
 142  0
     }
 143  
     
 144  
     /**
 145  
      * Uses the "checkboxToReset" parameter to find checkboxes which had not been
 146  
      * populated in the request and attempts to populate them
 147  
      * 
 148  
      * @param request the request to populate
 149  
      */
 150  
     protected void populateEmptyMultiSelect(HttpServletRequest request) {
 151  0
             Map<String, String[]> parameterMap = request.getParameterMap();
 152  0
             if (parameterMap.get("multiSelectToReset") != null) {
 153  0
                     final String[] multiSelectToReset = request.getParameterValues("multiSelectToReset");
 154  0
             if(multiSelectToReset != null && multiSelectToReset.length > 0) {
 155  0
                 for (int i = 0; i < multiSelectToReset.length; i++) {
 156  0
                     String propertyName = (String) multiSelectToReset[i];
 157  0
                     if ( !StringUtils.isBlank(propertyName) && parameterMap.get(propertyName) == null ) {
 158  0
                             populateForProperty(propertyName, "", parameterMap);
 159  
                     }  
 160  0
                     else if ( !StringUtils.isBlank(propertyName) && parameterMap.get(propertyName) != null && parameterMap.get(propertyName).length >= 1 && parameterMap.get(propertyName)[0].equalsIgnoreCase("on") ) {
 161  0
                             populateForProperty(propertyName, request.getParameter(propertyName), parameterMap); 
 162  
                     }
 163  
                 }
 164  
             }
 165  
             }
 166  0
     }
 167  
     
 168  
     protected void populationSpecialEmptyFields (HttpServletRequest request) {
 169  0
             populateFalseCheckboxes(request);
 170  0
                 populateEmptyMultiSelect(request);
 171  0
     }
 172  
 
 173  
     @SuppressWarnings("unchecked")
 174  
         protected TransactionalDocument instantiateTransactionalDocumentByDocumentTypeName( String documentTypeName ) {
 175  0
             Class<TransactionalDocument> transDocClass = KRADServiceLocatorWeb.getTransactionalDocumentDictionaryService().getDocumentClassByName(documentTypeName);
 176  0
             if ( transDocClass != null ) {
 177  
                     try {
 178  0
                             return transDocClass.newInstance();
 179  0
                     } catch (Exception ex) {
 180  0
                                 LOG.error( "Unable to instantiate transDocClass: " + transDocClass, ex);
 181  0
                         }
 182  
             } else {
 183  0
                     LOG.error( "Unable to retrieve transactional document class for type: " + documentTypeName);
 184  
             }
 185  0
             return null;
 186  
     }
 187  
 
 188  
     /**
 189  
          * This overridden method ...
 190  
          * 
 191  
          * @see org.kuali.rice.krad.web.struts.form.KualiForm#shouldMethodToCallParameterBeUsed(java.lang.String, java.lang.String, javax.servlet.http.HttpServletRequest)
 192  
          */
 193  
         @Override
 194  
         public boolean shouldMethodToCallParameterBeUsed(
 195  
                         String methodToCallParameterName,
 196  
                         String methodToCallParameterValue, HttpServletRequest request) {
 197  0
                 if (methodToCallParameterName.startsWith(KRADConstants.DISPATCH_REQUEST_PARAMETER + "." + KRADConstants.POST_TEXT_AREA_TO_PARENT)) {
 198  0
                         return true;
 199  
                 }
 200  0
                 return super.shouldMethodToCallParameterBeUsed(methodToCallParameterName, methodToCallParameterValue, request); 
 201  
         }
 202  
 }