001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kns.web.struts.form;
017
018import org.apache.commons.lang.StringUtils;
019import org.apache.log4j.Logger;
020import org.kuali.rice.core.api.CoreApiServiceLocator;
021import org.kuali.rice.kim.api.KimConstants;
022import org.kuali.rice.kns.service.KNSServiceLocator;
023import org.kuali.rice.krad.document.TransactionalDocument;
024import org.kuali.rice.krad.service.DataDictionaryService;
025import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
026import org.kuali.rice.krad.util.KRADConstants;
027
028import javax.servlet.http.HttpServletRequest;
029import java.util.HashMap;
030import java.util.Map;
031
032
033/**
034 * This class is the base action form for all transactional documents.
035 */
036public class KualiTransactionalDocumentFormBase extends KualiDocumentFormBase {
037        private static final Logger LOG = Logger.getLogger(KualiTransactionalDocumentFormBase.class);
038    /**
039         * 
040         */
041        private static final long serialVersionUID = 6463383454050206811L;
042        @SuppressWarnings("unchecked")
043        protected Map forcedReadOnlyFields;
044
045    /**
046     * This constructor sets up empty instances for the dependent objects...
047     */
048    @SuppressWarnings("unchecked")
049        public KualiTransactionalDocumentFormBase() {
050        super();
051
052        // create a blank DocumentActionFlags instance, since form-recreation needs it
053        forcedReadOnlyFields = new HashMap();
054    }
055
056    /**
057         * @see KualiDocumentFormBase#populate(javax.servlet.http.HttpServletRequest)
058         */
059    @SuppressWarnings("unchecked")
060        @Override
061        public void populate(HttpServletRequest request) {
062                super.populate(request);
063                populationSpecialEmptyFields (request);
064        }
065
066        /**
067     * This method retrieves an instance of the form.
068     * 
069     * @return
070     */
071    public TransactionalDocument getTransactionalDocument() {
072        return (TransactionalDocument) getDocument();
073    }
074    
075
076    /**
077     * Locates the <code>DictionaryService</code> to discover the type name of the document.
078     * 
079     * @return
080     */
081    protected String discoverDocumentTypeName() {
082        return ((DataDictionaryService) KRADServiceLocatorWeb.getDataDictionaryService()).getDataDictionary().getDocumentEntry(getDocument().getClass().getName()).getDocumentTypeName();
083    }
084
085    /**
086     * This method formats the given java.sql.Date as MMM d, yyyy.
087     * 
088     * @param reversalDate
089     * 
090     * @return String
091     */
092    protected static String formatReversalDate(java.sql.Date reversalDate) {
093        if (reversalDate == null) {
094            return "";
095        }
096        // new for thread safety
097        return CoreApiServiceLocator.getDateTimeService().toString(reversalDate, "MMM d, yyyy");
098    }
099
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}