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