1 19 20 package za.org.coefficient.modules.vote; 21 22 import net.sf.hibernate.Hibernate; 23 import net.sf.hibernate.HibernateException; 24 import net.sf.hibernate.Session; 25 26 import za.org.coefficient.authentication.Role; 27 import za.org.coefficient.core.Project; 28 import za.org.coefficient.interfaces.CoefficientContext; 29 import za.org.coefficient.modules.BaseProjectModule; 30 import za.org.coefficient.modules.project.ProjectConstants; 31 import za.org.coefficient.util.common.InvokerFactory; 32 import net.sf.hibernate.util.HibernateUtil; 33 import za.org.coefficient.util.ejb.SecurityUtil; 34 import za.org.coefficient.util.ejb.VelocityScreenUtil; 35 36 import java.util.ArrayList ; 37 import java.util.HashMap ; 38 import java.util.List ; 39 import java.util.StringTokenizer ; 40 41 55 public class Vote extends BaseProjectModule { 56 58 static { 59 try { 60 HibernateUtil.configureHibernateForClass(VoteData.class); 61 } catch (HibernateException he) { 62 he.printStackTrace(); 63 } 64 } 65 66 69 public static final String ADD = "add"; 70 public static final String CLOSE = "close"; 71 public static final String DELETE = "delete"; 72 public static final String REOPEN = "reopen"; 73 public static final String RESET = "reset"; 74 public static final String EDIT = "edit"; 75 76 79 public static final String ERROR = "error"; 80 public static final String MODULE = "module"; 81 public static final String PROJECT = "project"; 82 public static final String OP = "op"; 83 public static final String BUTTON = "button"; 84 public static final String DATEUTIL = "dateutil"; 85 public static final String NAME = "name"; 86 public static final String PERSON = "person"; 87 public static final String QUESTION = "question"; 88 public static final String CHOICE = "choice"; 89 public static final String CHOICE1 = "choice1"; 90 public static final String CHOICE2 = "choice2"; 91 public static final String STARTYEAR = "startyear"; 92 public static final String STARTMONTH = "startmonth"; 93 public static final String STARTDAY = "startday"; 94 public static final String ENDYEAR = "endyear"; 95 public static final String ENDMONTH = "endmonth"; 96 public static final String ENDDAY = "endday"; 97 public static final String ROLES = "roles"; 98 public static final String MINIMUMROLE = "minimumrole"; 99 public static final String VOTE = "vote"; 100 public static final String VOTES = "votes"; 101 public static final String MORE = "more"; 102 public static final String ON = "on"; 103 public static final String ID = "id"; 104 public static final String RESULTS = "results"; 105 public static final String DEFAULTUSER = "guest"; 106 107 110 113 public static final String ERRORPAGE = "error.vm"; 114 public static final String ADMINPAGE = "admin.vm"; 115 public static final String LISTPAGE = "list.vm"; 116 public static final String INDEXPAGE = "index.vm"; 117 public static final String RESULTSPAGE = "results.vm"; 118 public static final String ADDMSG = "add.vm"; 119 public static final String COMPLETEDMSG = "completed.vm"; 120 public static final String FINISHED = "100%"; 121 122 125 public static final String NOPROJECT = "You do not have a project"; 126 public static final String MISSINGDATA = "Please fill in all the data"; 127 128 130 public String getMainMethod() { 131 return "showSummaryForProject"; 133 } 134 135 public String getModuleDescription() { 136 return "vote module"; 137 } 138 139 public String getModuleDisplayName() { 140 return "Vote"; 141 } 142 143 public String canExecuteForRole(CoefficientContext ctx, String methodName, 144 Role usersHighestRole) { 145 return null; 146 } 147 148 public CoefficientContext castavote(CoefficientContext ctx) throws Exception { 149 Project project = ctx.getProject(); 150 HashMap map = new HashMap (); 151 StringBuffer sb = new StringBuffer ("Problem here"); 152 map.put(MODULE, this); 153 if (project != null) { 154 map.put(OP, InvokerFactory.getInvoker() 155 .invokeMethodOnModule("VoteAdmin", "getOp", 156 new Object []{ctx})); 157 map.put(PROJECT, project); 158 List polls = (List )InvokerFactory.getInvoker() 159 .invokeMethodOnModule("VoteAdmin", "getExistingOpenPolls", 160 new Object []{project}); 161 map.put(VOTES, polls); 162 String button = ctx.getParameter(BUTTON, ""); 163 164 if (button.toLowerCase().equals(VOTE)) { 166 processVotes(ctx, map); 167 polls = (List )InvokerFactory.getInvoker() 168 .invokeMethodOnModule("VoteAdmin", "getExistingOpenPolls", 169 new Object []{project}); 170 map.put(VOTES, polls); 171 sb = VelocityScreenUtil.getProcessedScreen(RESULTSPAGE, map); 172 ctx.setForward("project"); 173 } 174 } else { 175 map.put(ERROR, NOPROJECT); 176 sb = VelocityScreenUtil.getProcessedScreen(ERRORPAGE, map); 177 } 178 179 ctx.setModuleContent(sb.toString(), getModuleDisplayName()); 181 return ctx; 182 } 183 184 public CoefficientContext deleteAllForProject(CoefficientContext ctx) throws Exception { 185 Project project = ctx.getProject(); 186 if (project != null) { 187 List polls = (List )InvokerFactory.getInvoker() 188 .invokeMethodOnModule("VoteAdmin", "getExistingOpenPolls", 189 new Object []{project}); 190 int i; 191 192 for (i = 0; i < polls.size(); i++) { 193 try { 194 HibernateUtil.delete(polls.get(i)); 195 } catch (Throwable t) { 196 System.out.println("Hibernate can not delete " + t); 197 } 198 } 199 } 200 return ctx; 201 } 202 203 public String getSummaryForProject(CoefficientContext ctx) throws HibernateException { 204 HashMap map = new HashMap (); 205 Session sess = HibernateUtil.getSession(); 206 map.put("pollCnt", ((Integer )sess.iterate("select count(*) from " 207 + VoteData.class.getName() 208 + " as voteData where voteData.projectId = ?", 209 ctx.getProject().getId(), Hibernate.LONG).next())); 210 map.put("projectName", ProjectConstants.PROJECT_NAME); 211 212 HibernateUtil.finalizeSession(); 215 StringBuffer sb = VelocityScreenUtil 216 .getProcessedScreen("getSummaryForProject.vm", map); 217 return sb.toString(); 218 } 219 220 public CoefficientContext showSummaryForProject(CoefficientContext ctx) throws Exception { 221 Project project = ctx.getProject(); 222 HashMap map = new HashMap (); 223 StringBuffer sb = new StringBuffer ("Problem here"); 224 map.put(MODULE, this); 225 if (project != null) { 226 map.put(OP, InvokerFactory.getInvoker() 227 .invokeMethodOnModule("VoteAdmin", "getOp", 228 new Object []{ctx})); 229 map.put(PROJECT, project); 230 List polls = (List )InvokerFactory.getInvoker() 231 .invokeMethodOnModule("VoteAdmin", "getExistingOpenPolls", 232 new Object []{project}); 233 List unvoted; 234 if (ctx.getCurrentUser() == null) { 235 unvoted = (List )InvokerFactory.getInvoker() 236 .invokeMethodOnModule("VoteAdmin", "removePollsAlreadyVoted", 237 new Object []{polls, DEFAULTUSER}); 238 } else { 239 unvoted = (List )InvokerFactory.getInvoker() 240 .invokeMethodOnModule("VoteAdmin", "removePollsAlreadyVoted", 241 new Object []{polls, ctx.getCurrentUser().getUserName()}); 242 } 243 List allowed; 244 allowed = (List )InvokerFactory.getInvoker() 245 .invokeMethodOnModule("VoteAdmin", "removePollsNotAllowed", 246 new Object []{ctx, unvoted}); 247 map.put(VOTES, allowed); 248 sb = VelocityScreenUtil.getProcessedScreen(INDEXPAGE, map); 249 } else { 250 map.put(ERROR, NOPROJECT); 251 sb = VelocityScreenUtil.getProcessedScreen(ERRORPAGE, map); 252 } 253 254 ctx.setModuleContent(sb.toString(), getModuleDisplayName()); 256 return ctx; 257 } 258 259 private void processVotes(CoefficientContext ctx, HashMap map) { 260 int i; 261 List votes = new ArrayList (); 262 String choice; 263 264 for (i = 0; true; i++) { 265 choice = ctx.getParameter(CHOICE + i); 266 votes.add(choice); 267 if (choice == null) { 268 break; 269 } 270 } 271 272 StringTokenizer toks; 273 String id; 274 String pos; 275 VoteData vote; 276 for (i = 0; i < votes.size(); i++) { 277 if (votes.get(i) == null) { 278 continue; 279 } 280 toks = new StringTokenizer ((String ) votes.get(i), "-"); 281 List list = null; 282 try { 283 id = (String ) toks.nextElement(); 284 pos = (String ) toks.nextElement(); 285 list = 286 HibernateUtil.find("FROM " + VoteData.class.getName() 287 + " as vote " + "WHERE vote.id = ? ", new Long (id), 288 Hibernate.LONG); 289 vote = (VoteData) (list.get(0)); 290 String user; 291 if (ctx.getCurrentUser() == null) { 292 user = DEFAULTUSER; 293 } else { 294 user = ctx.getCurrentUser() 295 .getUserName(); 296 } 297 List voters = vote.getVoters(); 298 if (vote.getMinimumRole() 299 .getRoleValue() < SecurityUtil.getHighestRoleForUser( 300 ctx.getCurrentUser(), ctx.getProject()) 301 .getRoleValue()) { 302 } 303 if (!user.equals(DEFAULTUSER) && voters.contains(user)) { 304 map.put(ERROR, "You already voted on this issue!"); 305 } else if (vote.getMinimumRole() 306 .getRoleValue() < SecurityUtil.getHighestRoleForUser( 307 ctx.getCurrentUser(), ctx.getProject()) 308 .getRoleValue()) { 309 map.put(ERROR, 310 "You do not have permission to vote on this issue"); 311 } else { 312 vote.getVoters() 313 .add(user); 314 int k; 315 k = Integer.parseInt(pos); 316 Long cnt = (Long ) vote.getCounts() 317 .get(k); 318 long l = Long.parseLong("" + cnt); 319 l++; 320 List oldcounts = vote.getCounts(); 321 List counts = new ArrayList (); 322 int j; 323 for (j = 0; j < oldcounts.size(); j++) { 324 if (j == k) { 325 counts.add(new Long (l)); 326 } else { 327 counts.add(oldcounts.get(j)); 328 } 329 } 330 vote.setCounts(counts); 331 } 332 HibernateUtil.saveOrUpdate(vote); 333 } catch (Throwable t) { 334 System.out.println("Hibernate session throws " + t); 335 } 336 } 337 } 338 339 private void transferChoices(CoefficientContext ctx, VoteData data) { 340 int i; 341 String choice; 342 343 for (i = 1; true; i++) { 344 choice = ctx.getParameter(CHOICE + i); 345 if (choice == null) { 346 break; 347 } 348 data.getAnswers() 349 .add(choice); 350 data.getCounts() 351 .add(new Long (0)); 352 } 353 } 354 } 355 | Popular Tags |