001/**
002 * Copyright 2005-2015 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.action;
017
018import org.apache.commons.lang.StringUtils;
019import org.apache.struts.action.ActionForm;
020import org.apache.struts.action.ActionForward;
021import org.apache.struts.action.ActionMapping;
022import org.kuali.rice.core.api.util.RiceConstants;
023import org.kuali.rice.kns.web.struts.form.QuestionPromptForm;
024import org.kuali.rice.krad.exception.AuthorizationException;
025import org.kuali.rice.krad.util.GlobalVariables;
026import org.kuali.rice.krad.util.KRADConstants;
027import org.kuali.rice.krad.util.UrlFactory;
028
029import javax.servlet.http.HttpServletRequest;
030import javax.servlet.http.HttpServletResponse;
031import java.util.Properties;
032
033/**
034 * This class handles Actions for QuestionPromp.
035 *
036 * @deprecated KNS Struts deprecated, use KRAD and the Spring MVC framework.
037 */
038@Deprecated
039public class QuestionPromptAction extends KualiAction {
040        
041    /**
042         * This overridden method ...
043         * 
044         * @see org.kuali.rice.krad.web.struts.action.KualiAction#checkAuthorization(org.apache.struts.action.ActionForm, java.lang.String)
045         */
046        @Override
047        protected void checkAuthorization(ActionForm form, String methodToCall)
048                        throws AuthorizationException {
049                // no authorization required
050        }
051
052        /**
053     * This method is the entry point action for the question prompt component.
054     *
055     * @param mapping
056     * @param form
057     * @param request
058     * @param response
059     * @return ActionForward
060     * @throws Exception
061     */
062    public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
063        // deal with the fact that some requests might be reposts from errors on the reason field
064        processErrorMessages(request);
065
066        return mapping.findForward(RiceConstants.MAPPING_BASIC);
067    }
068
069    /**
070     * This method handles gathering all input and passing control back to the caller action.
071     *
072     * @param mapping
073     * @param form
074     * @param request
075     * @param response
076     * @return ActionForward
077     * @throws Exception
078     */
079    public ActionForward processAnswer(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
080        QuestionPromptForm questionPromptForm = (QuestionPromptForm) form;
081
082        Properties parameters = new Properties();
083
084        parameters.put(KRADConstants.DOC_FORM_KEY, questionPromptForm.getFormKey());
085        parameters.put(KRADConstants.QUESTION_CLICKED_BUTTON, getSelectedButton(request));
086        parameters.put(KRADConstants.METHOD_TO_CALL_ATTRIBUTE, questionPromptForm.getCaller());
087        parameters.put(KRADConstants.REFRESH_CALLER, KRADConstants.QUESTION_REFRESH);
088        parameters.put(KRADConstants.QUESTION_INST_ATTRIBUTE_NAME, questionPromptForm.getQuestionIndex());
089        if(questionPromptForm.getDocNum() != null){
090                parameters.put(KRADConstants.DOC_NUM, questionPromptForm.getDocNum());
091        }
092        
093        if (StringUtils.isNotBlank(questionPromptForm.getQuestionAnchor())) {
094            parameters.put(KRADConstants.ANCHOR, questionPromptForm.getQuestionAnchor());
095        }
096
097        String context = questionPromptForm.getContext();
098        if (StringUtils.isNotBlank(context)) {
099            parameters.put(KRADConstants.QUESTION_CONTEXT, context);
100        }
101        String reason = questionPromptForm.getReason();
102        if (StringUtils.isNotBlank(reason)) {
103            parameters.put(KRADConstants.QUESTION_REASON_ATTRIBUTE_NAME, reason);
104        }
105        if (StringUtils.isNotBlank(questionPromptForm.getMethodToCallPath())) {
106            // For header tab navigation. Leaving it blank will just kick user back to page.
107            parameters.put(questionPromptForm.getMethodToCallPath(), "present");
108        }
109
110        String returnUrl = UrlFactory.parameterizeUrl(questionPromptForm.getBackLocation(), parameters);
111
112        return new ActionForward(returnUrl, true);
113    }
114
115    /**
116     * Parses the method to call attribute to pick off the button number that was pressed.
117     *
118     * @param request
119     * @return int
120     */
121    private String getSelectedButton(HttpServletRequest request) {
122        String selectedButton = "-1";
123        String parameterName = (String) request.getAttribute(KRADConstants.METHOD_TO_CALL_ATTRIBUTE);
124        if (StringUtils.isNotBlank(parameterName)) {
125            selectedButton = StringUtils.substringBetween(parameterName, ".button", ".");
126        }
127
128        return selectedButton;
129    }
130
131    /**
132     * This method handles processing any error messages coming in the door.
133     *
134     * @param request
135     */
136    private void processErrorMessages(HttpServletRequest request) {
137        String errorKey = request.getParameter(KRADConstants.QUESTION_ERROR_KEY);
138        String errorPropertyName = request.getParameter(KRADConstants.QUESTION_ERROR_PROPERTY_NAME);
139        String errorParameter = request.getParameter(KRADConstants.QUESTION_ERROR_PARAMETER);
140
141        if (StringUtils.isNotBlank(errorKey)) {
142            if (StringUtils.isBlank(errorPropertyName)) {
143                throw new IllegalStateException("Both the errorKey and the errorPropertyName must be filled in, " + "in order for errors to be displayed by the question component.  Currently, " + "only the errorKey has a value specified.");
144            }
145            else {
146                if (StringUtils.isBlank(errorParameter)) {
147                    GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(errorPropertyName, errorKey);
148                }
149                else {
150                    GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(errorPropertyName, errorKey, errorParameter);
151                }
152            }
153        }
154    }
155}