View Javadoc

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