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