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 | 0 | 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 | 0 | 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 | 0 | public ContextSession(String context, PromptBeforeValidationEvent event) { |
75 | 0 | this.event = event; |
76 | |
|
77 | 0 | this.event.setQuestionContext(context); |
78 | 0 | if (this.event.getQuestionContext() == null) { |
79 | 0 | this.event.setQuestionContext(""); |
80 | |
} |
81 | |
|
82 | 0 | } |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
public boolean hasAsked(String id) { |
91 | 0 | return StringUtils.contains(event.getQuestionContext(), id); |
92 | |
} |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
public void askQuestion(String id, String text) { |
101 | 0 | event.setQuestionId(id); |
102 | 0 | event.setQuestionType(KNSConstants.CONFIRMATION_QUESTION); |
103 | 0 | event.setQuestionText(text); |
104 | 0 | event.setPerformQuestion(true); |
105 | 0 | } |
106 | |
|
107 | |
public void setAttribute(String name, String value) { |
108 | 0 | if (LOG.isDebugEnabled()) { |
109 | 0 | LOG.debug("setAttribute(" + name + "," + value + ")"); |
110 | |
} |
111 | 0 | event.setQuestionContext(event.getQuestionContext() + DELIMITER + name + DELIMITER + value); |
112 | |
|
113 | 0 | } |
114 | |
|
115 | |
public String getAttribute(String name) { |
116 | 0 | if (LOG.isDebugEnabled()) { |
117 | 0 | LOG.debug("getAttribute(" + name + ")"); |
118 | |
} |
119 | 0 | String result = null; |
120 | |
|
121 | 0 | Iterator values = Arrays.asList(event.getQuestionContext().split("\\" + DELIMITER)).iterator(); |
122 | |
|
123 | 0 | while (values.hasNext()) { |
124 | 0 | if (values.next().equals(name)) { |
125 | |
try { |
126 | 0 | result = (String) values.next(); |
127 | |
} |
128 | 0 | catch (NoSuchElementException e) { |
129 | 0 | result = null; |
130 | 0 | } |
131 | |
} |
132 | |
} |
133 | 0 | if (LOG.isDebugEnabled()) { |
134 | 0 | LOG.debug("returning " + result); |
135 | |
} |
136 | 0 | 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 | 0 | public PromptBeforeValidationBase() { |
159 | 0 | } |
160 | |
|
161 | |
|
162 | |
public boolean processPrompts(ActionForm form, HttpServletRequest request, PromptBeforeValidationEvent event) { |
163 | 0 | question = request.getParameter(KNSConstants.QUESTION_INST_ATTRIBUTE_NAME); |
164 | 0 | buttonClicked = request.getParameter(KNSConstants.QUESTION_CLICKED_BUTTON); |
165 | 0 | this.event = event; |
166 | 0 | this.form = (KualiForm) form; |
167 | |
|
168 | 0 | if (LOG.isDebugEnabled()) { |
169 | 0 | LOG.debug("Question is: " + question); |
170 | 0 | LOG.debug("ButtonClicked: " + buttonClicked); |
171 | 0 | LOG.debug("QuestionContext() is: " + event.getQuestionContext()); |
172 | |
} |
173 | |
|
174 | 0 | session = new ContextSession(request.getParameter(KNSConstants.QUESTION_CONTEXT), event); |
175 | |
|
176 | 0 | boolean result = false; |
177 | |
|
178 | |
try { |
179 | 0 | result = doPrompts(event.getDocument()); |
180 | |
} |
181 | 0 | catch (IsAskingException e) { |
182 | 0 | return false; |
183 | 0 | } |
184 | |
|
185 | 0 | if (isAborting) { |
186 | 0 | return false; |
187 | |
} |
188 | |
|
189 | 0 | return result; |
190 | |
} |
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
public void abortRulesCheck() { |
198 | 0 | event.setActionForwardName(RiceConstants.MAPPING_BASIC); |
199 | 0 | isAborting = true; |
200 | 0 | } |
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
public boolean askOrAnalyzeYesNoQuestion(String id, String text) throws IsAskingException { |
214 | |
|
215 | 0 | if (LOG.isDebugEnabled()) { |
216 | 0 | LOG.debug("Entering askOrAnalyzeYesNoQuestion(" + id + "," + text + ")"); |
217 | |
} |
218 | |
|
219 | 0 | String cached = (String) session.getAttribute(id); |
220 | 0 | if (cached != null) { |
221 | 0 | LOG.debug("returning cached value: " + id + "=" + cached); |
222 | 0 | return new Boolean(cached).booleanValue(); |
223 | |
} |
224 | |
|
225 | 0 | if (id.equals(question)) { |
226 | 0 | session.setAttribute(id, Boolean.toString(!ConfirmationQuestion.NO.equals(buttonClicked))); |
227 | 0 | return !ConfirmationQuestion.NO.equals(buttonClicked); |
228 | |
} |
229 | 0 | else if (!session.hasAsked(id)) { |
230 | 0 | if (LOG.isDebugEnabled()) { |
231 | 0 | LOG.debug("Forcing question to be asked: " + id); |
232 | |
} |
233 | 0 | session.askQuestion(id, text); |
234 | |
} |
235 | |
|
236 | 0 | LOG.debug("Throwing Exception to force return to Action"); |
237 | 0 | throw new IsAskingException(); |
238 | |
} |
239 | |
|
240 | |
} |