1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kns.rules;
17
18 import java.util.Arrays;
19 import java.util.Iterator;
20 import java.util.NoSuchElementException;
21
22 import javax.servlet.http.HttpServletRequest;
23
24 import org.apache.commons.lang.StringUtils;
25 import org.apache.struts.action.ActionForm;
26 import org.kuali.rice.core.util.RiceConstants;
27 import org.kuali.rice.kns.document.Document;
28 import org.kuali.rice.kns.question.ConfirmationQuestion;
29 import org.kuali.rice.kns.rule.PromptBeforeValidation;
30 import org.kuali.rice.kns.rule.event.PromptBeforeValidationEvent;
31 import org.kuali.rice.kns.util.KNSConstants;
32 import org.kuali.rice.kns.web.struts.form.KualiForm;
33
34
35
36
37
38
39
40
41
42
43
44
45
46 public abstract class PromptBeforeValidationBase implements PromptBeforeValidation {
47
48 protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PromptBeforeValidationBase.class);
49
50 protected String question;
51 protected String buttonClicked;
52 protected PromptBeforeValidationEvent event;
53 protected KualiForm form;
54
55 private class IsAskingException extends RuntimeException {
56 }
57
58
59
60
61
62
63
64
65
66
67
68
69
70 public class ContextSession {
71 private final static String DELIMITER = ".";
72 PromptBeforeValidationEvent event;
73
74 public ContextSession(String context, PromptBeforeValidationEvent event) {
75 this.event = event;
76
77 this.event.setQuestionContext(context);
78 if (this.event.getQuestionContext() == null) {
79 this.event.setQuestionContext("");
80 }
81
82 }
83
84
85
86
87
88
89
90 public boolean hasAsked(String id) {
91 return StringUtils.contains(event.getQuestionContext(), id);
92 }
93
94
95
96
97
98
99
100 public void askQuestion(String id, String text) {
101 event.setQuestionId(id);
102 event.setQuestionType(KNSConstants.CONFIRMATION_QUESTION);
103 event.setQuestionText(text);
104 event.setPerformQuestion(true);
105 }
106
107 public void setAttribute(String name, String value) {
108 if (LOG.isDebugEnabled()) {
109 LOG.debug("setAttribute(" + name + "," + value + ")");
110 }
111 event.setQuestionContext(event.getQuestionContext() + DELIMITER + name + DELIMITER + value);
112
113 }
114
115 public String getAttribute(String name) {
116 if (LOG.isDebugEnabled()) {
117 LOG.debug("getAttribute(" + name + ")");
118 }
119 String result = null;
120
121 Iterator values = Arrays.asList(event.getQuestionContext().split("\\" + DELIMITER)).iterator();
122
123 while (values.hasNext()) {
124 if (values.next().equals(name)) {
125 try {
126 result = (String) values.next();
127 }
128 catch (NoSuchElementException e) {
129 result = null;
130 }
131 }
132 }
133 if (LOG.isDebugEnabled()) {
134 LOG.debug("returning " + result);
135 }
136 return result;
137 }
138
139 }
140
141
142
143
144
145
146
147
148
149
150
151
152 public abstract boolean doPrompts(Document document);
153
154 private boolean isAborting;
155
156 ContextSession session;
157
158 public PromptBeforeValidationBase() {
159 }
160
161
162 public boolean processPrompts(ActionForm form, HttpServletRequest request, PromptBeforeValidationEvent event) {
163 question = request.getParameter(KNSConstants.QUESTION_INST_ATTRIBUTE_NAME);
164 buttonClicked = request.getParameter(KNSConstants.QUESTION_CLICKED_BUTTON);
165 this.event = event;
166 this.form = (KualiForm) form;
167
168 if (LOG.isDebugEnabled()) {
169 LOG.debug("Question is: " + question);
170 LOG.debug("ButtonClicked: " + buttonClicked);
171 LOG.debug("QuestionContext() is: " + event.getQuestionContext());
172 }
173
174 session = new ContextSession(request.getParameter(KNSConstants.QUESTION_CONTEXT), event);
175
176 boolean result = false;
177
178 try {
179 result = doPrompts(event.getDocument());
180 }
181 catch (IsAskingException e) {
182 return false;
183 }
184
185 if (isAborting) {
186 return false;
187 }
188
189 return result;
190 }
191
192
193
194
195
196
197 public void abortRulesCheck() {
198 event.setActionForwardName(RiceConstants.MAPPING_BASIC);
199 isAborting = true;
200 }
201
202
203
204
205
206
207
208
209
210
211
212
213 public boolean askOrAnalyzeYesNoQuestion(String id, String text) throws IsAskingException {
214
215 if (LOG.isDebugEnabled()) {
216 LOG.debug("Entering askOrAnalyzeYesNoQuestion(" + id + "," + text + ")");
217 }
218
219 String cached = (String) session.getAttribute(id);
220 if (cached != null) {
221 LOG.debug("returning cached value: " + id + "=" + cached);
222 return new Boolean(cached).booleanValue();
223 }
224
225 if (id.equals(question)) {
226 session.setAttribute(id, Boolean.toString(!ConfirmationQuestion.NO.equals(buttonClicked)));
227 return !ConfirmationQuestion.NO.equals(buttonClicked);
228 }
229 else if (!session.hasAsked(id)) {
230 if (LOG.isDebugEnabled()) {
231 LOG.debug("Forcing question to be asked: " + id);
232 }
233 session.askQuestion(id, text);
234 }
235
236 LOG.debug("Throwing Exception to force return to Action");
237 throw new IsAskingException();
238 }
239
240 }