001/**
002 * Copyright 2005-2018 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 *
036 * @deprecated Use {@link org.kuali.rice.krad.web.form.TransactionalDocumentFormBase}.
037 */
038@Deprecated
039public class KualiTransactionalDocumentFormBase extends KualiDocumentFormBase {
040        private static final Logger LOG = Logger.getLogger(KualiTransactionalDocumentFormBase.class);
041    /**
042         * 
043         */
044        private static final long serialVersionUID = 6463383454050206811L;
045        @SuppressWarnings("unchecked")
046        protected Map forcedReadOnlyFields;
047
048    /**
049     * This constructor sets up empty instances for the dependent objects...
050     */
051    @SuppressWarnings("unchecked")
052        public KualiTransactionalDocumentFormBase() {
053        super();
054
055        // create a blank DocumentActionFlags instance, since form-recreation needs it
056        forcedReadOnlyFields = new HashMap();
057    }
058
059    /**
060         * @see KualiDocumentFormBase#populate(javax.servlet.http.HttpServletRequest)
061         */
062    @SuppressWarnings("unchecked")
063        @Override
064        public void populate(HttpServletRequest request) {
065                super.populate(request);
066                populationSpecialEmptyFields (request);
067        }
068
069        /**
070     * This method retrieves an instance of the form.
071     * 
072     * @return
073     */
074    public TransactionalDocument getTransactionalDocument() {
075        return (TransactionalDocument) getDocument();
076    }
077    
078
079    /**
080     * Locates the <code>DictionaryService</code> to discover the type name of the document.
081     * 
082     * @return
083     */
084    protected String discoverDocumentTypeName() {
085        return ((DataDictionaryService) KRADServiceLocatorWeb.getDataDictionaryService()).getDataDictionary().getDocumentEntry(getDocument().getClass().getName()).getDocumentTypeName();
086    }
087
088    /**
089     * This method formats the given java.sql.Date as MMM d, yyyy.
090     * 
091     * @param reversalDate
092     * 
093     * @return String
094     */
095    protected static String formatReversalDate(java.sql.Date reversalDate) {
096        if (reversalDate == null) {
097            return "";
098        }
099        // 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}