1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
35
36
37
38 public class QuestionPromptAction extends KualiAction {
39
40
41
42
43
44
45 @Override
46 protected void checkAuthorization(ActionForm form, String methodToCall)
47 throws AuthorizationException {
48
49 }
50
51
52
53
54
55
56
57
58
59
60
61 public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
62
63 processErrorMessages(request);
64
65 return mapping.findForward(RiceConstants.MAPPING_BASIC);
66 }
67
68
69
70
71
72
73
74
75
76
77
78 public ActionForward processAnswer(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
79 QuestionPromptForm questionPromptForm = (QuestionPromptForm) form;
80
81 Properties parameters = new Properties();
82
83 parameters.put(KRADConstants.DOC_FORM_KEY, questionPromptForm.getFormKey());
84 parameters.put(KRADConstants.QUESTION_CLICKED_BUTTON, getSelectedButton(request));
85 parameters.put(KRADConstants.METHOD_TO_CALL_ATTRIBUTE, questionPromptForm.getCaller());
86 parameters.put(KRADConstants.REFRESH_CALLER, KRADConstants.QUESTION_REFRESH);
87 parameters.put(KRADConstants.QUESTION_INST_ATTRIBUTE_NAME, questionPromptForm.getQuestionIndex());
88 if(questionPromptForm.getDocNum() != null){
89 parameters.put(KRADConstants.DOC_NUM, questionPromptForm.getDocNum());
90 }
91
92 if (StringUtils.isNotBlank(questionPromptForm.getQuestionAnchor())) {
93 parameters.put(KRADConstants.ANCHOR, questionPromptForm.getQuestionAnchor());
94 }
95
96 String context = questionPromptForm.getContext();
97 if (StringUtils.isNotBlank(context)) {
98 parameters.put(KRADConstants.QUESTION_CONTEXT, context);
99 }
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
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
116
117
118
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
132
133
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 }