19 package org.turro.polls;
21 import java.util.Date;
22 import java.util.List;
24 import java.util.TreeSet;
25 import org.turro.action.Contacts;
26 import org.turro.elephant.db.ElephantPU;
27 import org.turro.elephant.db.WhereClause;
28 import org.turro.elephant.entities.db.Poll;
29 import org.turro.elephant.entities.db.PollOption;
30 import org.turro.elephant.entities.db.PollOptionPK;
31 import org.turro.elephant.entities.db.PollVote;
32 import org.turro.elephant.entities.db.PollVotePK;
33 import org.turro.jpa.Dao;
34 import org.turro.plugin.contacts.IContact;
45 this.contact = contact;
56 public boolean vote(Long pollId, String pollData, Integer
vote) {
61 if(pollOption !=
null) {
63 if(pollVote !=
null && pollVote.
getVote() ==
vote) {
68 if(pollVote ==
null) {
69 if(canVote(pollOption)) {
76 if(pollVote !=
null) {
79 getDao().saveObject(pollVote);
87 if(pollOption !=
null) {
88 return getPollVote(pollOption) !=
null || canVote(pollOption);
93 private boolean canVote(
PollOption pollOption) {
97 boolean canVote =
true;
98 Poll poll = pollOption.
getPoll();
99 if(poll.getDeadline() !=
null && poll.getDeadline().before(
new Date())) {
103 List<PollVote> votes = pollOption.
getVotes();
104 if(poll.getMaxPerOption() > 0 && votes.size() >= poll.getMaxPerOption()) {
109 List<PollVote> contactVotes = poll.getVotes(contact);
110 if(poll.getMaxVotedOptions() > 0 && contactVotes.size() >= poll.getMaxVotedOptions()) {
133 return getDao().find(
PollVote.class, pk,
null);
138 wc.
addClause(
"select count(vote) from PollVote vote");
139 wc.
addClause(
"where vote.idPoll = :idPoll");
141 wc.
addClause(
"and vote.pollData = :pollData");
148 public static List<Poll>
getPolls(String entityPath) {
151 wc.
addClause(
"select poll from Poll as poll");
152 wc.
addClause(
"where poll.entityPath = :path");
154 return dao.getResultList(wc);
160 poll = dao.saveObject(poll);
171 if(poll ==
null)
return;
191 if(po ==
null)
return;
197 wc.
addClause(
"and pollData = :pollData");
204 wc.
addClause(
"and pollData = :pollData");
212 return getPolls(contact,
false,
true, 0);
215 public static Set<Poll>
getPolls(
IContact contact,
boolean active,
boolean restricted,
int maxResults) {
217 TreeSet<Poll> polls =
new TreeSet<>();
220 wc.
addClause(
"select distinct poll from Poll poll");
222 wc.
addClause(
"join PollVote vote on vote.idPoll = poll.id");
223 wc.
addClause(
"where vote.idContact = :contact");
226 wc.
addClause(
"order by poll.deadline desc");
227 polls.addAll(dao.getResultList(wc, maxResults));
230 wc.
addClause(
"select poll from Poll poll");
231 wc.
addClause(
"where poll.deadline >= current_date");
233 for(
Poll poll : (List<Poll>) dao.getResultList(wc)) {
234 if(poll.isParticipant(contact)) {
246 private Dao getDao() {
void addClause(String clause)
void addNamedValue(String name, Object value)
void setIdPoll(Long idPoll)
void setPollData(String pollData)
List< PollVote > getVotes()
void setIdPoll(Long idPoll)
void setPollData(String pollData)
void setIdContact(String idContact)
void setIdPoll(Long idPoll)
void setPollData(String pollData)
void setVoteDate(Date voteDate)
void setIdContact(String idContact)
void deleteObject(Object obj)
int executeUpdate(String query)
Object getSingleResultOrNull(SqlClause sc)
PollVote getPollVote(PollOption pollOption, IContact contact)
synchronized boolean vote(PollOption pollOption, Integer vote)
PollsUtil(IContact contact)
boolean vote(Long pollId, String pollData, Integer vote)
static List< Poll > getPolls(String entityPath)
long getPollOptionCount(PollOption pollOption, int vote)
static Set< Poll > getMyPolls(IContact contact)
PollsUtil(String idContact)
static void removePoll(Poll poll)
static Poll savePoll(Poll poll, List< PollOption > pos)
static Set< Poll > getPolls(IContact contact, boolean active, boolean restricted, int maxResults)
PollVote getPollVote(PollOption pollOption)
PollOption getPollOption(Long pollId, String pollData)
boolean isAllowedToVote(PollOption pollOption)
static void removeOption(PollOption po)