View Javadoc
1   /**
2    * Copyright 2005-2014 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.action;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.struts.action.ActionForm;
20  import org.apache.struts.action.ActionForward;
21  import org.apache.struts.action.ActionMapping;
22  import org.kuali.rice.core.api.util.RiceConstants;
23  import org.kuali.rice.kns.web.struts.form.QuestionPromptForm;
24  import org.kuali.rice.krad.exception.AuthorizationException;
25  import org.kuali.rice.krad.util.GlobalVariables;
26  import org.kuali.rice.krad.util.KRADConstants;
27  import org.kuali.rice.krad.util.UrlFactory;
28  
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  import java.util.Properties;
32  
33  /**
34   * This class handles Actions for QuestionPromp.
35   *
36   * @deprecated KNS Struts deprecated, use KRAD and the Spring MVC framework.
37   */
38  @Deprecated
39  public class QuestionPromptAction extends KualiAction {
40  	
41      /**
42  	 * This overridden method ...
43  	 * 
44  	 * @see org.kuali.rice.krad.web.struts.action.KualiAction#checkAuthorization(org.apache.struts.action.ActionForm, java.lang.String)
45  	 */
46  	@Override
47  	protected void checkAuthorization(ActionForm form, String methodToCall)
48  			throws AuthorizationException {
49  		// no authorization required
50  	}
51  
52  	/**
53       * This method is the entry point action for the question prompt component.
54       *
55       * @param mapping
56       * @param form
57       * @param request
58       * @param response
59       * @return ActionForward
60       * @throws Exception
61       */
62      public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
63          // deal with the fact that some requests might be reposts from errors on the reason field
64          processErrorMessages(request);
65  
66          return mapping.findForward(RiceConstants.MAPPING_BASIC);
67      }
68  
69      /**
70       * This method handles gathering all input and passing control back to the caller action.
71       *
72       * @param mapping
73       * @param form
74       * @param request
75       * @param response
76       * @return ActionForward
77       * @throws Exception
78       */
79      public ActionForward processAnswer(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
80          QuestionPromptForm questionPromptForm = (QuestionPromptForm) form;
81  
82          Properties parameters = new Properties();
83  
84          parameters.put(KRADConstants.DOC_FORM_KEY, questionPromptForm.getFormKey());
85          parameters.put(KRADConstants.QUESTION_CLICKED_BUTTON, getSelectedButton(request));
86          parameters.put(KRADConstants.METHOD_TO_CALL_ATTRIBUTE, questionPromptForm.getCaller());
87          parameters.put(KRADConstants.REFRESH_CALLER, KRADConstants.QUESTION_REFRESH);
88          parameters.put(KRADConstants.QUESTION_INST_ATTRIBUTE_NAME, questionPromptForm.getQuestionIndex());
89          if(questionPromptForm.getDocNum() != null){
90          	parameters.put(KRADConstants.DOC_NUM, questionPromptForm.getDocNum());
91          }
92          
93          if (StringUtils.isNotBlank(questionPromptForm.getQuestionAnchor())) {
94              parameters.put(KRADConstants.ANCHOR, questionPromptForm.getQuestionAnchor());
95          }
96  
97          String context = questionPromptForm.getContext();
98          if (StringUtils.isNotBlank(context)) {
99              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 }