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