Mercurial Hosting > nabble
comparison src/nabble/modules/poll/NodeNamespaceExt.java @ 0:7ecd1a4ef557
add content
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 21 Mar 2019 19:15:52 -0600 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:7ecd1a4ef557 |
---|---|
1 package nabble.modules.poll; | |
2 | |
3 import fschmidt.db.DbDatabase; | |
4 import fschmidt.util.java.DateUtils; | |
5 import nabble.model.Node; | |
6 import nabble.model.Person; | |
7 import nabble.model.User; | |
8 import nabble.naml.compiler.Command; | |
9 import nabble.naml.compiler.CommandSpec; | |
10 import nabble.naml.compiler.IPrintWriter; | |
11 import nabble.naml.compiler.Interpreter; | |
12 import nabble.naml.compiler.Namespace; | |
13 import nabble.naml.compiler.NamespaceExtension; | |
14 import nabble.naml.compiler.ScopedInterpreter; | |
15 import nabble.naml.namespaces.StringList; | |
16 import nabble.view.web.template.DateNamespace; | |
17 import nabble.view.web.template.NodeNamespace; | |
18 import nabble.view.web.template.RequestNamespace; | |
19 import nabble.view.web.template.ServletNamespace; | |
20 import nabble.view.web.template.ServletNamespaceUtils; | |
21 | |
22 import javax.servlet.ServletException; | |
23 import java.util.ArrayList; | |
24 import java.util.Arrays; | |
25 import java.util.Date; | |
26 import java.util.List; | |
27 | |
28 @NamespaceExtension( | |
29 name = "node_poll", | |
30 target = NodeNamespace.class | |
31 ) | |
32 public class NodeNamespaceExt { | |
33 | |
34 private final Node node; | |
35 private final ServletNamespaceUtils servletNsUtils; | |
36 | |
37 public NodeNamespaceExt(NodeNamespace ns) { | |
38 this.node = ns.node(); | |
39 this.servletNsUtils = ns.servletNsUtils; | |
40 } | |
41 | |
42 private Poll getPoll() { | |
43 return Poll.of(node); | |
44 } | |
45 | |
46 public static final CommandSpec set_poll = CommandSpec.DO() | |
47 .parameters("poll_question", "poll_options") | |
48 .optionalParameters("poll_max_choices","poll_days_left", | |
49 "poll_allow_vote_change","poll_show_results_before_vote", | |
50 "poll_show_results_before_end") | |
51 .requiredInStack(ServletNamespace.class) | |
52 .build() | |
53 ; | |
54 | |
55 @Command public void set_poll(IPrintWriter out,ScopedInterpreter<NodeNamespace> interp) | |
56 throws ServletException, Poll.PollEditException, Poll.PollFormatException | |
57 { | |
58 User user = servletNsUtils.visitorUser(interp); | |
59 if (!node.getOwner().equals(user)) | |
60 throw new Poll.PollEditException(); | |
61 String pollQuestion = interp.getArgString("poll_question"); | |
62 RequestNamespace.ParameterValueList pollOptionsList = interp.getArgAsNamespace(RequestNamespace.ParameterValueList.class, "poll_options"); | |
63 String[] pollOptions = pollOptionsList.values().toArray(new String[0]); | |
64 int maxChoices = interp.getArgAsInt("poll_max_choices",1); | |
65 int daysLeft = interp.getArgAsInt("poll_days_left", 0); | |
66 boolean allowVoteChange = interp.getArgAsBoolean("poll_allow_vote_change",false); | |
67 boolean showResultsBeforeVote = interp.getArgAsBoolean("poll_show_results_before_vote",false); | |
68 boolean showResultsBeforeEnd = interp.getArgAsBoolean("poll_show_results_before_end",false); | |
69 setPoll( | |
70 pollQuestion, | |
71 pollOptions, | |
72 maxChoices, | |
73 daysLeft, | |
74 allowVoteChange, | |
75 showResultsBeforeVote, | |
76 showResultsBeforeEnd | |
77 ); | |
78 } | |
79 | |
80 private void setPoll(String pollQuestion, String[] pollOptions, int maxChoices, int daysLeft, boolean allowVoteChange, boolean showResultsBeforeVote, boolean showResultsBeforeEnd) | |
81 throws Poll.PollFormatException | |
82 { | |
83 DbDatabase db = node.getSite().getDb(); | |
84 if( !db.isInTransaction() ) { | |
85 db.beginTransaction(); | |
86 try { | |
87 setPoll( | |
88 pollQuestion, | |
89 pollOptions, | |
90 maxChoices, | |
91 daysLeft, | |
92 allowVoteChange, | |
93 showResultsBeforeVote, | |
94 showResultsBeforeEnd | |
95 ); | |
96 db.commitTransaction(); | |
97 } finally { | |
98 db.endTransaction(); | |
99 } | |
100 return; | |
101 } | |
102 Poll poll = new Poll( node.getGoodCopy(), pollOptions.length ); | |
103 poll.set(pollQuestion, pollOptions); | |
104 poll.setMaxChoices(maxChoices); | |
105 if (daysLeft > 0) poll.setEndDate(DateUtils.addDays(new Date(), daysLeft)); | |
106 poll.setAllowVoteChange(allowVoteChange); | |
107 poll.setShowResultsBeforeVote(showResultsBeforeVote); | |
108 poll.setShowResultsBeforeEnd(showResultsBeforeEnd); | |
109 } | |
110 | |
111 public static final CommandSpec edit_poll = CommandSpec.DO() | |
112 .optionalParameters("poll_max_choices","poll_days_left", | |
113 "poll_allow_vote_change","poll_show_results_before_vote", | |
114 "poll_show_results_before_end") | |
115 .requiredInStack(ServletNamespace.class) | |
116 .build() | |
117 ; | |
118 | |
119 @Command public void edit_poll(IPrintWriter out,ScopedInterpreter<NodeNamespace> interp) | |
120 throws ServletException, Poll.PollEditException, Poll.PollFormatException | |
121 { | |
122 User user = servletNsUtils.visitorUser(interp); | |
123 if (!node.getOwner().equals(user)) | |
124 throw new Poll.PollEditException(); | |
125 Poll poll = getPoll(); | |
126 poll.setMaxChoices(interp.getArgAsInt("poll_max_choices",1)); | |
127 String daysLeftS = interp.getArgString("poll_days_left"); | |
128 if (daysLeftS != null) { | |
129 int daysLeft; | |
130 try { | |
131 daysLeft = Integer.parseInt(daysLeftS); | |
132 } catch (NumberFormatException e) { | |
133 throw new Poll.PollFormatException(); | |
134 } | |
135 Date endDate = poll.endDate(); | |
136 Date now = new Date(); | |
137 if (endDate==null) { | |
138 poll.setEndDate(DateUtils.addDays(now, daysLeft)); | |
139 } else { | |
140 int daysLeftOrig = DateUtils.datesBetween(now, endDate); | |
141 if (daysLeftOrig != daysLeft) { | |
142 poll.setEndDate(DateUtils.addDays(endDate, daysLeft-daysLeftOrig)); | |
143 } | |
144 } | |
145 } else { | |
146 poll.setEndDate(null); | |
147 } | |
148 poll.setAllowVoteChange(interp.getArgAsBoolean("poll_allow_vote_change",false)); | |
149 poll.setShowResultsBeforeVote(interp.getArgAsBoolean("poll_show_results_before_vote",false)); | |
150 poll.setShowResultsBeforeEnd(interp.getArgAsBoolean("poll_show_results_before_end",false)); | |
151 } | |
152 | |
153 public static final CommandSpec delete_poll = CommandSpec.DO() | |
154 .requiredInStack(ServletNamespace.class) | |
155 .build() | |
156 ; | |
157 | |
158 @Command public void delete_poll(IPrintWriter out,ScopedInterpreter<NodeNamespace> interp) | |
159 throws ServletException, Poll.PollEditException | |
160 { | |
161 User user = servletNsUtils.visitorUser(interp); | |
162 if (!node.getOwner().equals(user)) | |
163 throw new Poll.PollEditException(); | |
164 DbDatabase db = node.getSite().getDb(); | |
165 db.beginTransaction(); | |
166 try { | |
167 Poll poll = Poll.of( node.getGoodCopy() ); | |
168 if( poll != null ) | |
169 poll.delete(); | |
170 db.commitTransaction(); | |
171 } finally { | |
172 db.endTransaction(); | |
173 } | |
174 } | |
175 | |
176 public static final CommandSpec has_poll = CommandSpec.DO; | |
177 | |
178 @Command public void has_poll(IPrintWriter out, Interpreter interp) { | |
179 out.print(getPoll()!=null); | |
180 } | |
181 | |
182 public static final CommandSpec poll_question = CommandSpec.DO; | |
183 | |
184 @Command public void poll_question(IPrintWriter out,ScopedInterpreter<NodeNamespace> interp) { | |
185 out.print(getPoll().getQuestion()); | |
186 } | |
187 | |
188 public static final CommandSpec poll_option_list = CommandSpec.DO; | |
189 | |
190 @Command public void poll_option_list(IPrintWriter out,ScopedInterpreter<PollOptionList> interp) { | |
191 List<String> options = Arrays.asList(getPoll().getOptions()); | |
192 Object block = interp.getArg(new PollOptionList(options, getPoll().getNode().getId()),"do"); | |
193 out.print(block); | |
194 } | |
195 | |
196 public static final CommandSpec poll_max_choices = CommandSpec.DO; | |
197 | |
198 @Command public void poll_max_choices(IPrintWriter out,ScopedInterpreter<NodeNamespace> interp) { | |
199 out.print(getPoll().maxChoices()); | |
200 } | |
201 | |
202 public static final CommandSpec poll_has_end_date = CommandSpec.DO; | |
203 | |
204 @Command public void poll_has_end_date(IPrintWriter out,ScopedInterpreter<NodeNamespace> interp) { | |
205 out.print(getPoll().endDate()!=null); | |
206 } | |
207 | |
208 public static final CommandSpec poll_end_date = CommandSpec.DO; | |
209 | |
210 @Command public void poll_end_date(IPrintWriter out,ScopedInterpreter<DateNamespace> interp) { | |
211 out.print(interp.getArg( new DateNamespace(getPoll().endDate()), "do" )); | |
212 } | |
213 | |
214 public static final CommandSpec poll_days_left = CommandSpec.DO; | |
215 | |
216 @Command public void poll_days_left(IPrintWriter out,ScopedInterpreter<NodeNamespace> interp) { | |
217 Date endDate = getPoll().endDate(); | |
218 if (endDate!=null) { | |
219 out.print(DateUtils.datesBetween(new Date(), endDate)); | |
220 } | |
221 } | |
222 | |
223 public static final CommandSpec poll_allow_vote_change = CommandSpec.DO; | |
224 | |
225 @Command public void poll_allow_vote_change(IPrintWriter out,ScopedInterpreter<NodeNamespace> interp) { | |
226 out.print(getPoll().allowVoteChange()); | |
227 } | |
228 | |
229 public static final CommandSpec poll_show_results_before_vote = CommandSpec.DO; | |
230 | |
231 @Command public void poll_show_results_before_vote(IPrintWriter out,ScopedInterpreter<NodeNamespace> interp) { | |
232 out.print(getPoll().showResultsBeforeVote()); | |
233 } | |
234 | |
235 public static final CommandSpec poll_show_results_before_end = CommandSpec.DO; | |
236 | |
237 @Command public void poll_show_results_before_end(IPrintWriter out,ScopedInterpreter<NodeNamespace> interp) { | |
238 out.print(getPoll().showResultsBeforeEnd()); | |
239 } | |
240 | |
241 public static final CommandSpec poll_vote_counts = CommandSpec.DO() | |
242 .build() | |
243 ; | |
244 | |
245 @Command public void poll_vote_counts(IPrintWriter out,ScopedInterpreter<PollOptionList> interp) { | |
246 int[] v = getPoll().getVoteCounts(); | |
247 List<String> voteCounts = new ArrayList<String>(); | |
248 for (int n : v) { | |
249 voteCounts.add(String.valueOf(n)); | |
250 } | |
251 Object block = interp.getArg(new PollOptionList(voteCounts, getPoll().getNode().getId()),"do"); | |
252 out.print(block); | |
253 } | |
254 | |
255 public static final CommandSpec poll_visitor_has_voted = CommandSpec.DO() | |
256 .dotParameter("index") | |
257 .requiredInStack(ServletNamespace.class) | |
258 .build() | |
259 ; | |
260 | |
261 @Command public void poll_visitor_has_voted(IPrintWriter out,ScopedInterpreter<StringList> interp) | |
262 throws ServletException | |
263 { | |
264 int index = interp.getArgAsInt("index"); | |
265 User viewer = servletNsUtils.visitorUser(interp); | |
266 int[] v = getPoll().getVotes(viewer); | |
267 for (int i : v) { | |
268 if (i==index) { | |
269 out.print(true); | |
270 return; | |
271 } | |
272 } | |
273 out.print(false); | |
274 } | |
275 | |
276 public static final CommandSpec poll_visitor_can_see_votes = CommandSpec.DO() | |
277 .requiredInStack(ServletNamespace.class) | |
278 .build() | |
279 ; | |
280 | |
281 @Command public void poll_visitor_can_see_votes(IPrintWriter out,Interpreter interp) | |
282 throws ServletException | |
283 { | |
284 Poll poll = getPoll(); | |
285 User viewer = servletNsUtils.visitorUser(interp); | |
286 Person owner = node.getOwner(); | |
287 if (viewer!=null && viewer.equals(owner)) { | |
288 out.print(true); | |
289 return; | |
290 } | |
291 if (poll.endDate()!=null) { | |
292 Date now = new Date(); | |
293 if (!poll.showResultsBeforeEnd() && poll.endDate().after(now)) { | |
294 out.print(false); | |
295 return; | |
296 } | |
297 if (poll.endDate().before(now)) { | |
298 out.print(true); | |
299 return; | |
300 } | |
301 } | |
302 if (!poll.showResultsBeforeVote() && (viewer==null || poll.getVotes(viewer).length==0)) { | |
303 out.print(false); | |
304 return; | |
305 } | |
306 out.print(true); | |
307 } | |
308 | |
309 public static final CommandSpec poll_has_ended = CommandSpec.DO; | |
310 | |
311 @Command public void poll_has_ended(IPrintWriter out,Interpreter interp) | |
312 throws ServletException | |
313 { | |
314 Poll poll = getPoll(); | |
315 boolean hasEnded = poll.endDate() != null && poll.endDate().before(new Date()); | |
316 out.print(hasEnded); | |
317 } | |
318 | |
319 public static final CommandSpec poll_visitor_can_vote = CommandSpec.DO() | |
320 .requiredInStack(ServletNamespace.class) | |
321 .build() | |
322 ; | |
323 | |
324 @Command public void poll_visitor_can_vote(IPrintWriter out,Interpreter interp) | |
325 throws ServletException | |
326 { | |
327 Poll poll = getPoll(); | |
328 User viewer = servletNsUtils.visitorUser(interp); | |
329 if (poll.endDate()!=null && poll.endDate().before(new Date())) { | |
330 out.print(false); | |
331 return; | |
332 } | |
333 if (!poll.allowVoteChange() && (viewer==null || poll.getVotes(viewer).length>0)) { | |
334 out.print(false); | |
335 return; | |
336 } | |
337 out.print(true); | |
338 } | |
339 | |
340 public static final CommandSpec poll_vote = CommandSpec.DO() | |
341 .parameters("votes") | |
342 .requiredInStack(ServletNamespace.class) | |
343 .build() | |
344 ; | |
345 | |
346 @Command public void poll_vote(IPrintWriter out,ScopedInterpreter<NodeNamespace> interp) | |
347 throws ServletException, Poll.PollVoteException { | |
348 Poll poll = getPoll(); | |
349 User voter = servletNsUtils.visitorUser(interp); | |
350 RequestNamespace.ParameterValueList vlist = interp.getArgAsNamespace(RequestNamespace.ParameterValueList.class, "votes"); | |
351 if (vlist==null) return; | |
352 List<String> votes = vlist.values(); | |
353 int[] v = new int[votes.size()]; | |
354 for (int i=0; i<v.length; i++) | |
355 v[i] = Integer.parseInt(votes.get(i)); | |
356 poll.vote(voter, v); | |
357 } | |
358 | |
359 @Namespace( | |
360 name = "poll_option_list", | |
361 global = true | |
362 ) | |
363 public static final class PollOptionList extends StringList { | |
364 | |
365 private final long nodeId; | |
366 | |
367 PollOptionList(List<String> list, long nodeId) { | |
368 super(list); | |
369 this.nodeId = nodeId; | |
370 } | |
371 | |
372 @Command public void option_id(IPrintWriter out,Interpreter interp) { | |
373 out.print(nodeId + "-" + index); | |
374 } | |
375 } | |
376 } |