1 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 ; 35 import java.util.HashMap ; 36 import java.util.List ; 37 38 52 public class VoteAdmin extends BaseProjectModule { 53 55 static { 56 try { 57 HibernateUtil.configureHibernateForClass(VoteData.class); 58 } catch (HibernateException he) { 59 he.printStackTrace(); 60 } 61 } 62 63 66 public static final String ADD = "add"; 67 public static final String CLOSE = "close"; 68 public static final String DELETE = "delete"; 69 public static final String REOPEN = "reopen"; 70 public static final String RESET = "reset"; 71 public static final String EDIT = "edit"; 72 public static final String UPDATE = "update"; 73 74 77 public static final String ERROR = "error"; 78 public static final String MODULE = "module"; 79 public static final String PROJECT = "project"; 80 public static final String OP = "op"; 81 public static final String BUTTON = "button"; 82 public static final String DATEUTIL = "dateutil"; 83 public static final String NAME = "name"; 84 public static final String PERSON = "person"; 85 public static final String QUESTION = "question"; 86 public static final String SEQUENCE = "sequence"; 87 public static final String CHOICE = "choice"; 88 public static final String CHOICE1 = "choice1"; 89 public static final String CHOICE2 = "choice2"; 90 public static final String STARTYEAR = "startyear"; 91 public static final String STARTMONTH = "startmonth"; 92 public static final String STARTDAY = "startday"; 93 public static final String ENDYEAR = "endyear"; 94 public static final String ENDMONTH = "endmonth"; 95 public static final String ENDDAY = "endday"; 96 public static final String ROLES = "roles"; 97 public static final String MINIMUMROLE = "minimumrole"; 98 public static final String VOTE = "vote"; 99 public static final String VOTES = "votes"; 100 public static final String MORE = "more"; 101 public static final String ON = "on"; 102 public static final String ID = "id"; 103 public static final String RESULTS = "results"; 104 public static final String DEFAULTUSER = "guest"; 105 106 109 112 public static final String ERRORPAGE = "error.vm"; 113 public static final String ADMINPAGE = "admin.vm"; 114 public static final String LISTPAGE = "list.vm"; 115 public static final String INDEXPAGE = "index.vm"; 116 public static final String RESULTSPAGE = "results.vm"; 117 public static final String ADDMSG = "add.vm"; 118 public static final String COMPLETEDMSG = "completed.vm"; 119 public static final String FINISHED = "100%"; 120 121 124 public static final String NOPROJECT = "You do not have a project"; 125 public static final String MISSINGDATA = "Please fill in all the data"; 126 127 129 public final long defSpread = 60 * 60 * 24 * 1000; 130 131 String [] commandRoles = 134 { SecurityUtil.SITE_ADMIN_ROLE_DESC, SecurityUtil.PROJECT_CHAMPION_ROLE_DESC }; 135 136 String [] memberRoles = 138 { 139 SecurityUtil.SITE_ADMIN_ROLE_DESC, 140 SecurityUtil.PROJECT_CHAMPION_ROLE_DESC, 141 SecurityUtil.PROJECT_MEMBER_ROLE_DESC 142 }; 143 144 146 public String getMainMethod() { 147 return "doInitialWork"; 148 } 149 150 public String getModuleDescription() { 151 return "vote module"; 152 } 153 154 public String getModuleDisplayName() { 155 return "VoteAdmin"; 156 } 157 158 public String getOp(CoefficientContext ctx) { 159 String 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 getFinishDay() { 175 return DateUtil.getDay(DateUtil.getFutureDate(defSpread)); 176 } 177 178 public String getFinishMonth() { 179 return DateUtil.getMonth(DateUtil.getFutureDate(defSpread)); 180 } 181 182 public String getFinishYear() { 183 return DateUtil.getYear(DateUtil.getFutureDate(defSpread)); 184 } 185 186 public String canExecuteForRole(CoefficientContext ctx, String methodName, 187 Role usersHighestRole) { 188 String 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 223 public CoefficientContext command(CoefficientContext ctx) { 224 HashMap map = new HashMap (); 225 StringBuffer sb; 226 String st = baseCommand(ctx); 227 228 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 id = ctx.getParameter(ID, ""); 237 238 Long idLong = new Long (0); 239 try { 240 idLong = new Long (id); 241 } catch (Throwable t) { 242 idLong = new Long (0); 243 } 244 245 try { 246 map.put(DATEUTIL, DateUtil.class.newInstance()); 247 } catch (Throwable t) { 248 System.out.println("Utility instantiation fails " + t); 249 } 250 try { 251 map.put(PERSON, ctx.getCurrentUser().getUserName()); 252 } catch (Throwable t) { 253 map.put(PERSON, DEFAULTUSER); 254 } 255 String button = ctx.getParameter(BUTTON, ""); 256 257 if (button.toLowerCase().equals(UPDATE)) { 258 String name = ctx.getParameter(NAME, ""); 259 String person = ctx.getCurrentUser().getUserName(); 260 String question = ctx.getParameter(QUESTION, ""); 261 String choice1 = ctx.getParameter(CHOICE + 1, ""); 262 String 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 (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 t) { 291 System.out.println("Hibernate throws " + t); 292 } 293 } 294 } 295 } else if (button.toLowerCase().equals(ADD)) { 296 String name = ctx.getParameter(NAME, ""); 298 String person = ctx.getCurrentUser() 299 .getUserName(); 300 String question = ctx.getParameter(QUESTION, ""); 301 String choice1 = ctx.getParameter(CHOICE + 1, ""); 302 String 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 (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 t) { 330 System.out.println("Hibernate throws " + t); 331 } 332 } 333 } 334 } else if (button.toLowerCase().equals(EDIT)) { 335 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 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 try { 354 VoteData vote = (VoteData)HibernateUtil.load(VoteData.class, idLong); 355 vote.setStatus(new Boolean (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 try { 364 VoteData vote = (VoteData)HibernateUtil.load(VoteData.class, idLong); 365 vote.setStatus(new Boolean (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 try { 373 VoteData vote = (VoteData)HibernateUtil.load(VoteData.class, idLong); 374 vote.setStatus(new Boolean (true)); 375 List counts = new ArrayList (); 376 for (int j = 0; j < vote.getAnswers().size(); j++) { 377 counts.add(new Long (0)); 378 } 379 380 vote.setCounts(counts); 381 vote.setVoters(new ArrayList ()); 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 404 public CoefficientContext delete(CoefficientContext ctx) { 405 String name = ctx.getParameter(NAME, ""); 406 407 List 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 t) { 421 System.out.println("Hibernate throws " + t); 422 } 423 } 424 return ctx; 425 } 426 427 public CoefficientContext deleteAllForProject(CoefficientContext ctx) { 428 Project project = ctx.getProject(); 430 if(project != null) { 431 List 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 t ) { 439 System.out.println("couldn't delete polls for " + project.getName()); 440 } 441 } 442 } 443 444 return ctx; 445 } 446 447 public List removePollsAlreadyVoted(List polls, String user) { 448 List list = new ArrayList (); 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 removePollsNotAllowed(CoefficientContext ctx, List polls) { 461 List list = new ArrayList (); 462 463 for(int i = 0; i < polls.size(); i++) { 464 VoteData vote = (VoteData) (polls.get(i)); 465 if ((ctx.getCurrentUser() == null) && vote.getMinimumRole() 467 .getDescription().equals(SecurityUtil.GUEST_ROLE_DESC)) { 468 list.add(vote); 469 } else { 470 String 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 map = new HashMap (); 486 StringBuffer sb = new StringBuffer ("Problem here"); 487 map.put(MODULE, this); 488 if (project != null) { 489 map.put(OP, getOp(ctx)); 490 map.put(PROJECT, project); 491 List polls = getExistingOpenPolls(project); 492 List unvoted; 493 if (ctx.getCurrentUser() == null) { 494 unvoted = removePollsAlreadyVoted(polls, DEFAULTUSER); 495 } else { 496 unvoted = removePollsAlreadyVoted(polls, ctx.getCurrentUser() 497 .getUserName()); 498 } 499 List 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 ctx.setModuleContent(sb.toString(), getModuleDisplayName()); 511 return ctx; 512 } 513 514 517 public CoefficientContext list(CoefficientContext ctx) { 518 HashMap map = new HashMap (); 519 StringBuffer sb; 520 String st = baseCommand(ctx); 521 522 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 id = ctx.getParameter(ID, ""); 531 532 Long idLong = new Long (0); 533 try { 534 idLong = new Long (id); 535 } catch (Throwable t) { 536 idLong = new Long (0); 537 } 538 539 try { 540 map.put(DATEUTIL, DateUtil.class.newInstance()); 541 } catch (Throwable t) { 542 System.out.println("Utility instantiation fails " + t); 543 } 544 try { 545 map.put(PERSON, ctx.getCurrentUser().getUserName()); 546 } catch (Throwable t) { 547 map.put(PERSON, DEFAULTUSER); 548 } 549 String 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 562 public CoefficientContext member(CoefficientContext ctx) { 563 HashMap map = new HashMap (); 564 StringBuffer sb; 565 String st = baseCommand(ctx); 566 567 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 t) { 578 System.out.println("Utility instantiation fails " + t); 579 } 580 try { 581 map.put(PERSON, ctx.getCurrentUser().getUserName()); 582 } catch (Throwable 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 597 public Long percent(Project project, String voteName, 598 String choice) { 599 List 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 ) (vote.getCounts().get(i))).longValue(); 616 if (choice.toLowerCase().equals(((String ) (vote.getAnswers().get(i))) 617 .toLowerCase())) { 618 count = ((Long ) (vote.getCounts().get(i))).longValue(); 619 } 620 } 621 622 if (total != 0) { 623 return new Long (((count * 100) / total)); 624 } else { 625 return new Long (0); 626 } 627 } else { 628 return new Long (0); 629 } 630 } 631 632 return new Long (0); 633 } 634 635 638 public Long percent(CoefficientContext ctx) { 639 String name = ctx.getParameter(NAME, ""); 640 String choice = ctx.getParameter(CHOICE, ""); 641 return percent(ctx.getProject(), name, choice); 642 } 643 644 private Long nextSequence(List polls) { 645 if (polls.size() == 0) { 646 return new Long (1); 647 } 648 649 Long max = new Long (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 (max.intValue() + 1); 659 } 660 661 665 public List getExistingOpenPolls(Project project) { 666 List list = getExistingPolls(project); 667 List open = new ArrayList (); 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 { 679 Project project = ctx.getProject(); 680 HashMap map = new HashMap (); 681 StringBuffer sb = new StringBuffer ("Problem here"); 682 map.put(MODULE, this); 683 if (project != null) { 684 map.put(PROJECT, project); 685 List 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 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 choice = ctx.getParameter(CHOICE + i); 702 if (choice == null) { 703 break; 704 } 705 String delete = ctx.getParameter(DELETE + i); 706 if (delete == null) { 707 data.getAnswers().add(choice); 708 data.getCounts().add(new Long (0)); 709 } 710 } 711 } 712 713 714 717 private List getExistingPolls(Project project) { 718 List 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 t) { 724 System.out.println("Hibernate session throws " + t); 725 } 726 727 return list; 728 } 729 730 733 private String baseCommand(CoefficientContext ctx) { 734 StringBuffer sb = new StringBuffer (); 735 Project project = ctx.getProject(); 736 HashMap map = new HashMap (); 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 |