KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > za > org > coefficient > modules > vote > VoteAdmin


1 /*
2  * Coefficient - facilitates project based collaboration
3  * Copyright (C) 2003, Dylan Etkin, CSIR icomtek
4  * PO Box 395
5  * Pretoria 0001, RSA
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package za.org.coefficient.modules.vote;
21
22 import net.sf.hibernate.Hibernate;
23 import net.sf.hibernate.HibernateException;
24
25 import za.org.coefficient.authentication.Role;
26 import za.org.coefficient.core.Project;
27 import za.org.coefficient.interfaces.CoefficientContext;
28 import za.org.coefficient.modules.BaseProjectModule;
29 import za.org.coefficient.util.common.DateUtil;
30 import net.sf.hibernate.util.HibernateUtil;
31 import za.org.coefficient.util.ejb.SecurityUtil;
32 import za.org.coefficient.util.ejb.VelocityScreenUtil;
33
34 import java.util.ArrayList JavaDoc;
35 import java.util.HashMap JavaDoc;
36 import java.util.List JavaDoc;
37
38 /**
39  * @pojo2ejb.class
40  * name="VoteAdmin"
41  * jndi-prefix="za/org/coefficient/orphan/"
42  * interface-extends="za.org.coefficient.interfaces.Module"
43  * interface-local-extends="za.org.coefficient.interfaces.ModuleLocal"
44  *
45  * @web.resource-env-ref
46  * name="za/org/coefficient/orphan/VoteAdmin"
47  * type="za.org.coefficient.modules.vote.VoteAdmin"
48  * @web.resource-env-ref
49  * name="VoteAdmin"
50  * type="za.org.coefficient.modules.vote.VoteAdmin"
51  */

52 public class VoteAdmin extends BaseProjectModule {
53     //~ Static fields/initializers =============================================
54

55     static {
56         try {
57             HibernateUtil.configureHibernateForClass(VoteData.class);
58         } catch (HibernateException he) {
59             he.printStackTrace();
60         }
61     }
62
63     /*
64      * button names
65      */

66     public static final String JavaDoc ADD = "add";
67     public static final String JavaDoc CLOSE = "close";
68     public static final String JavaDoc DELETE = "delete";
69     public static final String JavaDoc REOPEN = "reopen";
70     public static final String JavaDoc RESET = "reset";
71     public static final String JavaDoc EDIT = "edit";
72     public static final String JavaDoc UPDATE = "update";
73
74     /*
75      * context keywords
76      */

77     public static final String JavaDoc ERROR = "error";
78     public static final String JavaDoc MODULE = "module";
79     public static final String JavaDoc PROJECT = "project";
80     public static final String JavaDoc OP = "op";
81     public static final String JavaDoc BUTTON = "button";
82     public static final String JavaDoc DATEUTIL = "dateutil";
83     public static final String JavaDoc NAME = "name";
84     public static final String JavaDoc PERSON = "person";
85     public static final String JavaDoc QUESTION = "question";
86     public static final String JavaDoc SEQUENCE = "sequence";
87     public static final String JavaDoc CHOICE = "choice";
88     public static final String JavaDoc CHOICE1 = "choice1";
89     public static final String JavaDoc CHOICE2 = "choice2";
90     public static final String JavaDoc STARTYEAR = "startyear";
91     public static final String JavaDoc STARTMONTH = "startmonth";
92     public static final String JavaDoc STARTDAY = "startday";
93     public static final String JavaDoc ENDYEAR = "endyear";
94     public static final String JavaDoc ENDMONTH = "endmonth";
95     public static final String JavaDoc ENDDAY = "endday";
96     public static final String JavaDoc ROLES = "roles";
97     public static final String JavaDoc MINIMUMROLE = "minimumrole";
98     public static final String JavaDoc VOTE = "vote";
99     public static final String JavaDoc VOTES = "votes";
100     public static final String JavaDoc MORE = "more";
101     public static final String JavaDoc ON = "on";
102     public static final String JavaDoc ID = "id";
103     public static final String JavaDoc RESULTS = "results";
104     public static final String JavaDoc DEFAULTUSER = "guest";
105
106     /*
107      * form fields
108      */

109     /*
110      * velocity pages
111      */

112     public static final String JavaDoc ERRORPAGE = "error.vm";
113     public static final String JavaDoc ADMINPAGE = "admin.vm";
114     public static final String JavaDoc LISTPAGE = "list.vm";
115     public static final String JavaDoc INDEXPAGE = "index.vm";
116     public static final String JavaDoc RESULTSPAGE = "results.vm";
117     public static final String JavaDoc ADDMSG = "add.vm";
118     public static final String JavaDoc COMPLETEDMSG = "completed.vm";
119     public static final String JavaDoc FINISHED = "100%";
120
121     /*
122      * error messages
123      */

124     public static final String JavaDoc NOPROJECT = "You do not have a project";
125     public static final String JavaDoc MISSINGDATA = "Please fill in all the data";
126
127     //~ Instance fields ========================================================
128

129     public final long defSpread = 60 * 60 * 24 * 1000;
130
131     // roles which can execute the command method
132
// everybody can execute list
133
String JavaDoc[] commandRoles =
134     { SecurityUtil.SITE_ADMIN_ROLE_DESC, SecurityUtil.PROJECT_CHAMPION_ROLE_DESC };
135
136     // roles which can execute the member method
137
String JavaDoc[] memberRoles =
138     {
139         SecurityUtil.SITE_ADMIN_ROLE_DESC,
140         SecurityUtil.PROJECT_CHAMPION_ROLE_DESC,
141         SecurityUtil.PROJECT_MEMBER_ROLE_DESC
142     };
143
144     //~ Methods ================================================================
145

146     public String JavaDoc getMainMethod() {
147         return "doInitialWork";
148     }
149
150     public String JavaDoc getModuleDescription() {
151         return "vote module";
152     }
153
154     public String JavaDoc getModuleDisplayName() {
155         return "VoteAdmin";
156     }
157
158     public String JavaDoc getOp(CoefficientContext ctx) {
159         String JavaDoc role =
160             SecurityUtil.getHighestRoleForUser(ctx.getCurrentUser(),
161                                                ctx.getProject()).getDescription();
162         if (role.equals(SecurityUtil.SITE_ADMIN_ROLE_DESC)
163             || role.equals(SecurityUtil.PROJECT_CHAMPION_ROLE_DESC)) {
164             return "command";
165         } else {
166             if (role.equals(SecurityUtil.PROJECT_MEMBER_ROLE_DESC)) {
167                 return "member";
168             } else {
169                 return "list";
170             }
171         }
172     }
173
174     public String JavaDoc getFinishDay() {
175         return DateUtil.getDay(DateUtil.getFutureDate(defSpread));
176     }
177
178     public String JavaDoc getFinishMonth() {
179         return DateUtil.getMonth(DateUtil.getFutureDate(defSpread));
180     }
181
182     public String JavaDoc getFinishYear() {
183         return DateUtil.getYear(DateUtil.getFutureDate(defSpread));
184     }
185
186     public String JavaDoc canExecuteForRole(CoefficientContext ctx, String JavaDoc methodName,
187                                     Role usersHighestRole) {
188         String JavaDoc role = usersHighestRole.getDescription();
189
190         if (methodName.equals("command")) {
191             for(int i = 0; i < commandRoles.length; i++) {
192                 if (role.equals(commandRoles[i])) {
193                     return null;
194                 }
195             }
196             return "not authorised";
197         }
198
199         if (methodName.equals("delete") || methodName.equals("deleteAllForProject") ) {
200             for (int i = 0; i < commandRoles.length; i++) {
201                 if (role.equals(commandRoles[i])) {
202                     return null;
203                 }
204             }
205             return "not authorised";
206         }
207
208         if (methodName.equals("member")) {
209             for (int i = 0; i < memberRoles.length; i++) {
210                 if (role.equals(memberRoles[i])) {
211                     return null;
212                 }
213             }
214             return "not authorised";
215         }
216         return null;
217     }
218
219     /**
220      * executed by the project champion. gives full access to
221      * the poll/vote data
222      */

223     public CoefficientContext command(CoefficientContext ctx) {
224         HashMap JavaDoc map = new HashMap JavaDoc();
225         StringBuffer JavaDoc sb;
226         String JavaDoc st = baseCommand(ctx);
227
228         // check to make sure there is a project
229
if (st == null) {
230             Project project = ctx.getProject();
231             map.put(OP, getOp(ctx));
232             map.put(MODULE, this);
233             map.put(PROJECT, project);
234             map.put(ROLES, SecurityUtil.getRoles());
235
236             String JavaDoc id = ctx.getParameter(ID, "");
237
238             Long JavaDoc idLong = new Long JavaDoc(0);
239             try {
240                 idLong = new Long JavaDoc(id);
241             } catch (Throwable JavaDoc t) {
242                 idLong = new Long JavaDoc(0);
243             }
244
245             try {
246                 map.put(DATEUTIL, DateUtil.class.newInstance());
247             } catch (Throwable JavaDoc t) {
248                 System.out.println("Utility instantiation fails " + t);
249             }
250             try {
251                 map.put(PERSON, ctx.getCurrentUser().getUserName());
252             } catch (Throwable JavaDoc t) {
253                 map.put(PERSON, DEFAULTUSER);
254             }
255             String JavaDoc button = ctx.getParameter(BUTTON, "");
256
257             if (button.toLowerCase().equals(UPDATE)) {
258                 String JavaDoc name = ctx.getParameter(NAME, "");
259                 String JavaDoc person = ctx.getCurrentUser().getUserName();
260                 String JavaDoc question = ctx.getParameter(QUESTION, "");
261                 String JavaDoc choice1 = ctx.getParameter(CHOICE + 1, "");
262                 String JavaDoc choice2 = ctx.getParameter(CHOICE + 2, "");
263
264                 if (name.trim().equals("") || question.trim().equals("")
265                     || choice1.trim().equals("") || choice2.trim().equals("")) {
266                     map.put(ERROR, MISSINGDATA);
267                 } else {
268                     VoteData data = new VoteData();
269                     data.setId(idLong);
270                     data.setProjectId(project.getId());
271                     data.setStartDate(ctx.getParameter(STARTYEAR, ""),
272                                       ctx.getParameter(STARTMONTH, ""),
273                                       ctx.getParameter(STARTDAY, ""));
274                     data.setEndDate(ctx.getParameter(ENDYEAR, ""),
275                                     ctx.getParameter(ENDMONTH, ""),
276                                     ctx.getParameter(ENDDAY, ""));
277                     data.setName(name);
278                     data.setPerson(person);
279                     data.setQuestion(question);
280                     data.setSequence(ctx.getParameter(SEQUENCE, ""));
281                     data.setStatus(new Boolean JavaDoc(true));
282                     data.setMinimumRole(SecurityUtil.getRoleForDescription(ctx.getParameter(MINIMUMROLE, "")));
283                     transferChoices(ctx, data);
284                     if (ctx.getParameter(MORE, "").equals(ON)) {
285                         map.put(ID, "" + data.getId());
286                         map.put(VOTE, data);
287                     } else {
288                         try {
289                             HibernateUtil.saveOrUpdate(data);
290                         } catch (Throwable JavaDoc t) {
291                             System.out.println("Hibernate throws " + t);
292                         }
293                     }
294                 }
295             } else if (button.toLowerCase().equals(ADD)) {
296                 // handle add button
297
String JavaDoc name = ctx.getParameter(NAME, "");
298                 String JavaDoc person = ctx.getCurrentUser()
299                     .getUserName();
300                 String JavaDoc question = ctx.getParameter(QUESTION, "");
301                 String JavaDoc choice1 = ctx.getParameter(CHOICE + 1, "");
302                 String JavaDoc choice2 = ctx.getParameter(CHOICE + 2, "");
303
304                 if (name.trim().equals("") || question.trim().equals("")
305                     || choice1.trim().equals("") || choice2.trim().equals("")) {
306                     map.put(ERROR, MISSINGDATA);
307                 } else {
308                     VoteData data = new VoteData();
309                     data.setProjectId(project.getId());
310                     data.setStartDate(ctx.getParameter(STARTYEAR, ""),
311                                       ctx.getParameter(STARTMONTH, ""),
312                                       ctx.getParameter(STARTDAY, ""));
313                     data.setEndDate(ctx.getParameter(ENDYEAR, ""),
314                                     ctx.getParameter(ENDMONTH, ""),
315                                     ctx.getParameter(ENDDAY, ""));
316                     data.setName(name);
317                     data.setPerson(person);
318                     data.setQuestion(question);
319                     data.setStatus(new Boolean JavaDoc(true));
320                     data.setSequence(nextSequence(getExistingPolls(project)));
321                     data.setMinimumRole(SecurityUtil.getRoleForDescription(ctx.getParameter(MINIMUMROLE, "")));
322                     transferChoices(ctx, data);
323                     if (ctx.getParameter(MORE, "").equals(ON)) {
324                         map.put(ID, "" + data.getId());
325                         map.put(VOTE, data);
326                     } else {
327                         try {
328                             HibernateUtil.saveOrUpdate(data);
329                         } catch (Throwable JavaDoc t) {
330                             System.out.println("Hibernate throws " + t);
331                         }
332                     }
333                 }
334             } else if (button.toLowerCase().equals(EDIT)) {
335                 // handle edit button
336
try {
337                     VoteData vote = (VoteData)HibernateUtil.load(VoteData.class, idLong);
338                     map.put(VOTE, vote);
339                     map.put(ID, "" + vote.getId());
340                 } catch(HibernateException he) {
341                     map.put(ERROR, "You must tick a poll to edit");
342                 }
343             } else if (button.toLowerCase().equals(DELETE)) {
344                 // handle delete button
345
try {
346                     VoteData vote = (VoteData)HibernateUtil.load(VoteData.class, idLong);
347                     HibernateUtil.delete(vote);
348                 } catch(HibernateException he) {
349                     map.put(ERROR, "You must tick a poll to delete");
350                 }
351             } else if (button.toLowerCase().equals(CLOSE)) {
352                 // handle update button
353
try {
354                     VoteData vote = (VoteData)HibernateUtil.load(VoteData.class, idLong);
355                     vote.setStatus(new Boolean JavaDoc(false));
356                     HibernateUtil.delete(vote);
357                     HibernateUtil.saveOrUpdate(vote);
358                 } catch(HibernateException he) {
359                     map.put(ERROR, "You must tick a poll to close");
360                 }
361             } else if (button.toLowerCase().equals(REOPEN)) {
362                 // handle re-open button
363
try {
364                     VoteData vote = (VoteData)HibernateUtil.load(VoteData.class, idLong);
365                     vote.setStatus(new Boolean JavaDoc(true));
366                     HibernateUtil.saveOrUpdate(vote);
367                 } catch(HibernateException he) {
368                     map.put(ERROR, "You must tick a poll to re-open");
369                 }
370             } else if (button.toLowerCase().equals(RESET)) {
371                 // handle reset button
372
try {
373                     VoteData vote = (VoteData)HibernateUtil.load(VoteData.class, idLong);
374                     vote.setStatus(new Boolean JavaDoc(true));
375                     List JavaDoc counts = new ArrayList JavaDoc();
376                     for (int j = 0; j < vote.getAnswers().size(); j++) {
377                         counts.add(new Long JavaDoc(0));
378                     }
379
380                     vote.setCounts(counts);
381                     vote.setVoters(new ArrayList JavaDoc());
382                     vote.setStartDate(DateUtil.getTodayYear(),
383                                       DateUtil.getTodayMonth(), DateUtil.getTodayDay());
384                     vote.setEndDate(DateUtil.getDefaultFutureYear(),
385                                     DateUtil.getDefaultFutureMonth(),
386                                     DateUtil.getDefaultFutureDay());
387                     HibernateUtil.saveOrUpdate(vote);
388                 } catch(HibernateException he) {
389                     map.put(ERROR, "You must tick a poll to reset");
390                 }
391             }
392
393             map.put(VOTES, getExistingPolls(project));
394             sb = VelocityScreenUtil.getProcessedScreen(ADMINPAGE, map);
395             st = sb.toString();
396         }
397         ctx.setModuleContent(st, getModuleDisplayName());
398         return ctx;
399     }
400
401     /**
402      * executed by the project champion.
403      */

404     public CoefficientContext delete(CoefficientContext ctx) {
405         String JavaDoc name = ctx.getParameter(NAME, "");
406
407         List JavaDoc list = getExistingPolls(ctx.getProject());
408         VoteData vote = null;
409
410         for(int i = 0; i < list.size(); i++) {
411             vote = (VoteData) list.get(i);
412             if (vote.getName().equals(name)) {
413                 break;
414             }
415         }
416
417         if (vote != null) {
418             try {
419                 HibernateUtil.delete(vote);
420             } catch (Throwable JavaDoc t) {
421                 System.out.println("Hibernate throws " + t);
422             }
423         }
424         return ctx;
425     }
426
427     public CoefficientContext deleteAllForProject(CoefficientContext ctx) {
428         // do nothing for now
429
Project project = ctx.getProject();
430         if(project != null) {
431             List JavaDoc polls = getExistingPolls(project);
432             
433             VoteData vote;
434             for(int i = 0; i < polls.size(); i++) {
435                 vote = (VoteData)(polls.get(i));
436                 try {
437                     HibernateUtil.delete(vote);
438                 } catch ( Throwable JavaDoc t ) {
439                     System.out.println("couldn't delete polls for " + project.getName());
440                 }
441             }
442         }
443         
444         return ctx;
445     }
446
447     public List JavaDoc removePollsAlreadyVoted(List JavaDoc polls, String JavaDoc user) {
448         List JavaDoc list = new ArrayList JavaDoc();
449
450         for(int i = 0; i < polls.size(); i++) {
451             VoteData vote = (VoteData) (polls.get(i));
452             if (user.equals(DEFAULTUSER) || !vote.getVoters().contains(user)) {
453                 list.add(vote);
454             }
455         }
456
457         return list;
458     }
459
460     public List JavaDoc removePollsNotAllowed(CoefficientContext ctx, List JavaDoc polls) {
461         List JavaDoc list = new ArrayList JavaDoc();
462
463         for(int i = 0; i < polls.size(); i++) {
464             VoteData vote = (VoteData) (polls.get(i));
465             // special case for guest
466
if ((ctx.getCurrentUser() == null) && vote.getMinimumRole()
467                 .getDescription().equals(SecurityUtil.GUEST_ROLE_DESC)) {
468                 list.add(vote);
469             } else {
470                 String JavaDoc rolename =
471                     SecurityUtil.getHighestRoleForUser(ctx.getCurrentUser(),
472                                                        ctx.getProject()).getDescription();
473                 Role role = SecurityUtil.getRoleForDescription(rolename);
474                 if (vote.getMinimumRole().getRoleValue() >= role.getRoleValue()) {
475                     list.add(vote);
476                 }
477             }
478         }
479         
480         return list;
481     }
482
483     public CoefficientContext doInitialWork(CoefficientContext ctx) {
484         Project project = ctx.getProject();
485         HashMap JavaDoc map = new HashMap JavaDoc();
486         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("Problem here");
487         map.put(MODULE, this);
488         if (project != null) {
489             map.put(OP, getOp(ctx));
490             map.put(PROJECT, project);
491             List JavaDoc polls = getExistingOpenPolls(project);
492             List JavaDoc unvoted;
493             if (ctx.getCurrentUser() == null) {
494                 unvoted = removePollsAlreadyVoted(polls, DEFAULTUSER);
495             } else {
496                 unvoted = removePollsAlreadyVoted(polls, ctx.getCurrentUser()
497                                                   .getUserName());
498             }
499             List JavaDoc allowed;
500             allowed = removePollsNotAllowed(ctx, unvoted);
501
502             map.put(VOTES, allowed);
503             sb = VelocityScreenUtil.getProcessedScreen(INDEXPAGE, map);
504         } else {
505             map.put(ERROR, NOPROJECT);
506             sb = VelocityScreenUtil.getProcessedScreen(ERRORPAGE, map);
507         }
508
509         // Set the html into the context
510
ctx.setModuleContent(sb.toString(), getModuleDisplayName());
511         return ctx;
512     }
513
514     /**
515      * executed by the site members.
516      */

517     public CoefficientContext list(CoefficientContext ctx) {
518         HashMap JavaDoc map = new HashMap JavaDoc();
519         StringBuffer JavaDoc sb;
520         String JavaDoc st = baseCommand(ctx);
521
522         // check to make sure there is a project
523
if (st == null) {
524             Project project = ctx.getProject();
525             map.put(OP, getOp(ctx));
526             map.put(MODULE, this);
527             map.put(PROJECT, project);
528             map.put(ROLES, SecurityUtil.getRoles());
529
530             String JavaDoc id = ctx.getParameter(ID, "");
531
532             Long JavaDoc idLong = new Long JavaDoc(0);
533             try {
534                 idLong = new Long JavaDoc(id);
535             } catch (Throwable JavaDoc t) {
536                 idLong = new Long JavaDoc(0);
537             }
538
539             try {
540                 map.put(DATEUTIL, DateUtil.class.newInstance());
541             } catch (Throwable JavaDoc t) {
542                 System.out.println("Utility instantiation fails " + t);
543             }
544             try {
545                 map.put(PERSON, ctx.getCurrentUser().getUserName());
546             } catch (Throwable JavaDoc t) {
547                 map.put(PERSON, DEFAULTUSER);
548             }
549             String JavaDoc button = ctx.getParameter(BUTTON, "");
550
551             map.put(VOTES, getExistingPolls(project));
552             sb = VelocityScreenUtil.getProcessedScreen(ADMINPAGE, map);
553             st = sb.toString();
554         }
555         ctx.setModuleContent(st, getModuleDisplayName());
556         return ctx;
557     }
558
559     /**
560      * executed by the project members
561      */

562     public CoefficientContext member(CoefficientContext ctx) {
563         HashMap JavaDoc map = new HashMap JavaDoc();
564         StringBuffer JavaDoc sb;
565         String JavaDoc st = baseCommand(ctx);
566
567         // check to make sure there is a project
568
if (st == null) {
569             Project project = ctx.getProject();
570             map.put(OP, getOp(ctx));
571             map.put(MODULE, this);
572             map.put(PROJECT, project);
573             map.put(ROLES, SecurityUtil.getRoles());
574             
575             try {
576                 map.put(DATEUTIL, DateUtil.class.newInstance());
577             } catch (Throwable JavaDoc t) {
578                 System.out.println("Utility instantiation fails " + t);
579             }
580             try {
581                 map.put(PERSON, ctx.getCurrentUser().getUserName());
582             } catch (Throwable JavaDoc t) {
583                 map.put(PERSON, DEFAULTUSER);
584             }
585
586             map.put(VOTES, getExistingPolls(project));
587             sb = VelocityScreenUtil.getProcessedScreen(ADMINPAGE, map);
588             st = sb.toString();
589         }
590         ctx.setModuleContent(st, getModuleDisplayName());
591         return ctx;
592     }
593
594     /**
595      * workflow evaluation method
596      */

597     public Long JavaDoc percent(Project project, String JavaDoc voteName,
598                         String JavaDoc choice) {
599         List JavaDoc list = getExistingPolls(project);
600         VoteData vote = null;
601
602         for(int i = 0; i < list.size(); i++) {
603             vote = (VoteData) list.get(i);
604             if (vote.getName().equals(voteName)) {
605                 break;
606             }
607         }
608
609         if (vote != null) {
610             if (!vote.getStatus().booleanValue()) {
611                 long total = 0;
612                 long count = 0;
613
614                 for (int i = 0; i < vote.getAnswers().size(); i++) {
615                     total += ((Long JavaDoc) (vote.getCounts().get(i))).longValue();
616                     if (choice.toLowerCase().equals(((String JavaDoc) (vote.getAnswers().get(i)))
617                                                     .toLowerCase())) {
618                         count = ((Long JavaDoc) (vote.getCounts().get(i))).longValue();
619                     }
620                 }
621
622                 if (total != 0) {
623                     return new Long JavaDoc(((count * 100) / total));
624                 } else {
625                     return new Long JavaDoc(0);
626                 }
627             } else {
628                 return new Long JavaDoc(0);
629             }
630         }
631
632         return new Long JavaDoc(0);
633     }
634
635     /**
636      * workflow evaluation method
637      */

638     public Long JavaDoc percent(CoefficientContext ctx) {
639         String JavaDoc name = ctx.getParameter(NAME, "");
640         String JavaDoc choice = ctx.getParameter(CHOICE, "");
641         return percent(ctx.getProject(), name, choice);
642     }
643
644     private Long JavaDoc nextSequence(List JavaDoc polls) {
645         if (polls.size() == 0) {
646             return new Long JavaDoc(1);
647         }
648
649         Long JavaDoc max = new Long JavaDoc(0);
650         VoteData vote;
651         for(int i = 0; i < polls.size(); i++) {
652             vote = (VoteData) polls.get(i);
653             if (max.compareTo(vote.getSequence()) < 0) {
654                 max = vote.getSequence();
655             }
656         }
657
658         return new Long JavaDoc(max.intValue() + 1);
659     }
660
661     /**
662      * get only open polls (where people can still vote)
663      * for this project
664      */

665     public List JavaDoc getExistingOpenPolls(Project project) {
666         List JavaDoc list = getExistingPolls(project);
667         List JavaDoc open = new ArrayList JavaDoc();
668
669         for(int i = 0; i < list.size(); i++) {
670             VoteData poll = (VoteData) (list.get(i));
671             if (poll.getStatus().booleanValue()) {
672                 open.add(poll);
673             }
674         }
675         return open;
676     }
677
678     public CoefficientContext viewVoteSummaries(CoefficientContext ctx) throws Exception JavaDoc {
679         Project project = ctx.getProject();
680         HashMap JavaDoc map = new HashMap JavaDoc();
681         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("Problem here");
682         map.put(MODULE, this);
683         if (project != null) {
684             map.put(PROJECT, project);
685             List JavaDoc polls = getExistingOpenPolls(project);
686             map.put("votes", polls);
687
688             sb = VelocityScreenUtil.getProcessedScreen("results.vm", map);
689         } else {
690             map.put(ERROR, NOPROJECT);
691             sb = VelocityScreenUtil.getProcessedScreen(ERRORPAGE, map);
692         }
693
694         // Set the html into the context
695
ctx.setModuleContent(sb.toString(), getModuleDisplayName());
696         return ctx;
697     }
698
699     private void transferChoices(CoefficientContext ctx, VoteData data) {
700         for(int i = 1; true; i++) {
701             String JavaDoc choice = ctx.getParameter(CHOICE + i);
702             if (choice == null) {
703                 break;
704             }
705             String JavaDoc delete = ctx.getParameter(DELETE + i);
706             if (delete == null) {
707                 data.getAnswers().add(choice);
708                 data.getCounts().add(new Long JavaDoc(0));
709             }
710         }
711     }
712
713
714     /**
715      * get all polss for this project
716      */

717     private List JavaDoc getExistingPolls(Project project) {
718         List JavaDoc list = null;
719         try {
720             list = HibernateUtil.find("FROM " + VoteData.class.getName() + " as vote "
721                                 + "WHERE vote.projectId = ? " + "ORDER BY vote.id",
722                                 project.getId(), Hibernate.LONG);
723         } catch (Throwable JavaDoc t) {
724             System.out.println("Hibernate session throws " + t);
725         }
726         
727         return list;
728     }
729
730     /**
731      * returns null if ok otherwise it returns an error message
732      */

733     private String JavaDoc baseCommand(CoefficientContext ctx) {
734         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
735         Project project = ctx.getProject();
736         HashMap JavaDoc map = new HashMap JavaDoc();
737         map.put(MODULE, this);
738
739         if (project != null) {
740             return null;
741         } else {
742             map.put(ERROR, NOPROJECT);
743             sb = VelocityScreenUtil.getProcessedScreen(ERRORPAGE, map);
744         }
745
746         return sb.toString();
747     }
748
749 }
750
Popular Tags