View Javadoc

1   /**
2    * Copyright 2005-2011 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  public class KualiTransactionalDocumentFormBase extends KualiDocumentFormBase {
37  	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          super();
51  
52          // create a blank DocumentActionFlags instance, since form-recreation needs it
53          forcedReadOnlyFields = new HashMap();
54      }
55  
56      /**
57  	 * @see KualiDocumentFormBase#populate(javax.servlet.http.HttpServletRequest)
58  	 */
59      @SuppressWarnings("unchecked")
60  	@Override
61  	public void populate(HttpServletRequest request) {
62  		super.populate(request);
63  		populationSpecialEmptyFields (request);
64  	}
65  
66  	/**
67       * This method retrieves an instance of the form.
68       * 
69       * @return
70       */
71      public TransactionalDocument getTransactionalDocument() {
72          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          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          if (reversalDate == null) {
94              return "";
95          }
96          // new for thread safety
97          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         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         this.forcedReadOnlyFields = forcedReadOnlyFields;
118     }
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     	Map<String, String[]> parameterMap = request.getParameterMap();
128     	if (parameterMap.get("checkboxToReset") != null) {
129     		final String[] checkboxesToReset = request.getParameterValues("checkboxToReset");
130             if(checkboxesToReset != null && checkboxesToReset.length > 0) {
131                 for (int i = 0; i < checkboxesToReset.length; i++) {
132                     String propertyName = (String) checkboxesToReset[i];
133                     if ( !StringUtils.isBlank(propertyName) && parameterMap.get(propertyName) == null ) {
134                     	populateForProperty(propertyName, KimConstants.KIM_ATTRIBUTE_BOOLEAN_FALSE_STR_VALUE_DISPLAY, parameterMap);
135                     }  
136                     else if ( !StringUtils.isBlank(propertyName) && parameterMap.get(propertyName) != null && parameterMap.get(propertyName).length >= 1 && parameterMap.get(propertyName)[0].equalsIgnoreCase("on") ) {
137                     	populateForProperty(propertyName, KimConstants.KIM_ATTRIBUTE_BOOLEAN_TRUE_STR_VALUE_DISPLAY, parameterMap);
138                     }
139                 }
140             }
141     	}
142     }
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     	Map<String, String[]> parameterMap = request.getParameterMap();
152     	if (parameterMap.get("multiSelectToReset") != null) {
153     		final String[] multiSelectToReset = request.getParameterValues("multiSelectToReset");
154             if(multiSelectToReset != null && multiSelectToReset.length > 0) {
155                 for (int i = 0; i < multiSelectToReset.length; i++) {
156                     String propertyName = (String) multiSelectToReset[i];
157                     if ( !StringUtils.isBlank(propertyName) && parameterMap.get(propertyName) == null ) {
158                     	populateForProperty(propertyName, "", parameterMap);
159                     }  
160                     else if ( !StringUtils.isBlank(propertyName) && parameterMap.get(propertyName) != null && parameterMap.get(propertyName).length >= 1 && parameterMap.get(propertyName)[0].equalsIgnoreCase("on") ) {
161                     	populateForProperty(propertyName, request.getParameter(propertyName), parameterMap); 
162                     }
163                 }
164             }
165     	}
166     }
167     
168     protected void populationSpecialEmptyFields (HttpServletRequest request) {
169     	populateFalseCheckboxes(request);
170 		populateEmptyMultiSelect(request);
171     }
172 
173     @SuppressWarnings("unchecked")
174 	protected TransactionalDocument instantiateTransactionalDocumentByDocumentTypeName( String documentTypeName ) {
175     	Class<TransactionalDocument> transDocClass = KNSServiceLocator.getTransactionalDocumentDictionaryService().getDocumentClassByName(documentTypeName);
176     	if ( transDocClass != null ) {
177     		try {
178     			return transDocClass.newInstance();
179     		} catch (Exception ex) {
180 				LOG.error( "Unable to instantiate transDocClass: " + transDocClass, ex);
181 			}
182     	} else {
183     		LOG.error( "Unable to retrieve transactional document class for type: " + documentTypeName);
184     	}
185     	return null;
186     }
187 
188     /**
189 	 * This overridden method ...
190 	 * 
191 	 * @see 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 		if (methodToCallParameterName.startsWith(KRADConstants.DISPATCH_REQUEST_PARAMETER + "." + KRADConstants.POST_TEXT_AREA_TO_PARENT)) {
198 			return true;
199 		}
200 		return super.shouldMethodToCallParameterBeUsed(methodToCallParameterName, methodToCallParameterValue, request); 
201 	}
202 }