BrightSide Workbench Full Report + Source Code
PollsUtil.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2018 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program 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
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.polls;
20 
21 import java.util.Date;
22 import java.util.List;
23 import java.util.Set;
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;
35 
40 public class PollsUtil {
41 
42  private final IContact contact;
43 
44  public PollsUtil(IContact contact) {
45  this.contact = contact;
46  }
47 
48  public PollsUtil(String idContact) {
49  this.contact = Contacts.getContactById(idContact);
50  }
51 
52  public IContact getContact() {
53  return contact;
54  }
55 
56  public boolean vote(Long pollId, String pollData, Integer vote) {
57  return vote(getPollOption(pollId, pollData), vote);
58  }
59 
60  public synchronized boolean vote(PollOption pollOption, Integer vote) {
61  if(pollOption != null) {
62  PollVote pollVote = getPollVote(pollOption);
63  if(pollVote != null && pollVote.getVote() == vote) {
64  // Only 0/1, delete 0
65  getDao().deleteObject(pollVote);
66  return true;
67  }
68  if(pollVote == null) {
69  if(canVote(pollOption)) {
70  pollVote = new PollVote();
71  pollVote.setIdContact(contact.getId());
72  pollVote.setIdPoll(pollOption.getIdPoll());
73  pollVote.setPollData(pollOption.getPollData());
74  }
75  }
76  if(pollVote != null) {
77  pollVote.setVoteDate(new Date());
78  pollVote.setVote(vote);
79  getDao().saveObject(pollVote);
80  return true;
81  }
82  }
83  return false;
84  }
85 
86  public boolean isAllowedToVote(PollOption pollOption) {
87  if(pollOption != null) {
88  return getPollVote(pollOption) != null || canVote(pollOption);
89  }
90  return false;
91  }
92 
93  private boolean canVote(PollOption pollOption) {
94  if(!pollOption.getPoll().isParticipant(contact)) {
95  return false;
96  }
97  boolean canVote = true;
98  Poll poll = pollOption.getPoll();
99  if(poll.getDeadline() != null && poll.getDeadline().before(new Date())) {
100  canVote = false;
101  }
102  if(canVote) {
103  List<PollVote> votes = pollOption.getVotes();
104  if(poll.getMaxPerOption() > 0 && votes.size() >= poll.getMaxPerOption()) {
105  canVote = false;
106  }
107  }
108  if(canVote) {
109  List<PollVote> contactVotes = poll.getVotes(contact);
110  if(poll.getMaxVotedOptions() > 0 && contactVotes.size() >= poll.getMaxVotedOptions()) {
111  canVote = false;
112  }
113  }
114  return canVote;
115  }
116 
117  public PollOption getPollOption(Long pollId, String pollData) {
118  PollOptionPK pk = new PollOptionPK();
119  pk.setIdPoll(pollId);
120  pk.setPollData(pollData);
121  return getDao().find(PollOption.class, pk);
122  }
123 
124  public PollVote getPollVote(PollOption pollOption) {
125  return getPollVote(pollOption, contact);
126  }
127 
128  public PollVote getPollVote(PollOption pollOption, IContact contact) {
129  PollVotePK pk = new PollVotePK();
130  pk.setIdPoll(pollOption.getIdPoll());
131  pk.setPollData(pollOption.getPollData());
132  pk.setIdContact(contact.getId());
133  return getDao().find(PollVote.class, pk, null);
134  }
135 
136  public long getPollOptionCount(PollOption pollOption, int vote) {
137  WhereClause wc = new WhereClause();
138  wc.addClause("select count(vote) from PollVote vote");
139  wc.addClause("where vote.idPoll = :idPoll");
140  wc.addNamedValue("idPoll", pollOption.getIdPoll());
141  wc.addClause("and vote.pollData = :pollData");
142  wc.addNamedValue("pollData", pollOption.getPollData());
143  wc.addClause("and vote.vote = :vote");
144  wc.addNamedValue("vote", vote);
145  return (long) getDao().getSingleResultOrNull(wc);
146  }
147 
148  public static List<Poll> getPolls(String entityPath) {
149  Dao dao = new ElephantPU();
150  WhereClause wc = new WhereClause();
151  wc.addClause("select poll from Poll as poll");
152  wc.addClause("where poll.entityPath = :path");
153  wc.addNamedValue("path", entityPath);
154  return dao.getResultList(wc);
155  }
156 
157  public static Poll savePoll(Poll poll, List<PollOption> pos) {
158  Dao dao = new ElephantPU();
159  if(!poll.isEmpty()) {
160  poll = dao.saveObject(poll);
161  for(PollOption po : pos) {
162  if(!po.isEmpty()) {
163  dao.saveObject(po);
164  }
165  }
166  }
167  return poll;
168  }
169 
170  public static void removePoll(Poll poll) {
171  if(poll == null) return;
172  Dao dao = new ElephantPU();
173  WhereClause wc = new WhereClause();
174  wc.addClause("delete from Poll");
175  wc.addClause("where id = :idPoll");
176  wc.addNamedValue("idPoll", poll.getId());
177  dao.executeUpdate(wc);
178  wc = new WhereClause();
179  wc.addClause("delete from PollOption");
180  wc.addClause("where idPoll = :idPoll");
181  wc.addNamedValue("idPoll", poll.getId());
182  dao.executeUpdate(wc);
183  wc = new WhereClause();
184  wc.addClause("delete from PollVote");
185  wc.addClause("where idPoll = :idPoll");
186  wc.addNamedValue("idPoll", poll.getId());
187  dao.executeUpdate(wc);
188  }
189 
190  public static void removeOption(PollOption po) {
191  if(po == null) return;
192  Dao dao = new ElephantPU();
193  WhereClause wc = new WhereClause();
194  wc.addClause("delete from PollOption");
195  wc.addClause("where idPoll = :idPoll");
196  wc.addNamedValue("idPoll", po.getIdPoll());
197  wc.addClause("and pollData = :pollData");
198  wc.addNamedValue("pollData", po.getPollData());
199  dao.executeUpdate(wc);
200  wc = new WhereClause();
201  wc.addClause("delete from PollVote");
202  wc.addClause("where idPoll = :idPoll");
203  wc.addNamedValue("idPoll", po.getIdPoll());
204  wc.addClause("and pollData = :pollData");
205  wc.addNamedValue("pollData", po.getPollData());
206  dao.executeUpdate(wc);
207  }
208 
209  /* My polls */
210 
211  public static Set<Poll> getMyPolls(IContact contact) {
212  return getPolls(contact, false, true, 0);
213  }
214 
215  public static Set<Poll> getPolls(IContact contact, boolean active, boolean restricted, int maxResults) {
216  Dao dao = new ElephantPU();
217  TreeSet<Poll> polls = new TreeSet<>();
218  if(!active) {
219  WhereClause wc = new WhereClause();
220  wc.addClause("select distinct poll from Poll poll");
221  if(restricted) {
222  wc.addClause("join PollVote vote on vote.idPoll = poll.id");
223  wc.addClause("where vote.idContact = :contact");
224  wc.addNamedValue("contact", contact.getId());
225  }
226  wc.addClause("order by poll.deadline desc");
227  polls.addAll(dao.getResultList(wc, maxResults));
228  }
229  WhereClause wc = new WhereClause();
230  wc.addClause("select poll from Poll poll");
231  wc.addClause("where poll.deadline >= current_date");
232  if(restricted) {
233  for(Poll poll : (List<Poll>) dao.getResultList(wc)) {
234  if(poll.isParticipant(contact)) {
235  polls.add(poll);
236  }
237  }
238  }
239  return polls;
240  }
241 
242  /* Dao */
243 
244  private Dao _dao;
245 
246  private Dao getDao() {
247  if (_dao == null) {
248  _dao = new ElephantPU();
249  }
250  return _dao;
251  }
252 
253 }
static IContact getContactById(String id)
Definition: Contacts.java:72
void addNamedValue(String name, Object value)
void setPollData(String pollData)
Definition: PollVote.java:64
void setIdContact(String idContact)
Definition: PollVote.java:72
void deleteObject(Object obj)
Definition: Dao.java:162
int executeUpdate(String query)
Definition: Dao.java:463
Object getSingleResultOrNull(SqlClause sc)
Definition: Dao.java:419
PollVote getPollVote(PollOption pollOption, IContact contact)
Definition: PollsUtil.java:128
synchronized boolean vote(PollOption pollOption, Integer vote)
Definition: PollsUtil.java:60
PollsUtil(IContact contact)
Definition: PollsUtil.java:44
boolean vote(Long pollId, String pollData, Integer vote)
Definition: PollsUtil.java:56
static List< Poll > getPolls(String entityPath)
Definition: PollsUtil.java:148
long getPollOptionCount(PollOption pollOption, int vote)
Definition: PollsUtil.java:136
static Set< Poll > getMyPolls(IContact contact)
Definition: PollsUtil.java:211
PollsUtil(String idContact)
Definition: PollsUtil.java:48
static void removePoll(Poll poll)
Definition: PollsUtil.java:170
static Poll savePoll(Poll poll, List< PollOption > pos)
Definition: PollsUtil.java:157
static Set< Poll > getPolls(IContact contact, boolean active, boolean restricted, int maxResults)
Definition: PollsUtil.java:215
PollVote getPollVote(PollOption pollOption)
Definition: PollsUtil.java:124
PollOption getPollOption(Long pollId, String pollData)
Definition: PollsUtil.java:117
boolean isAllowedToVote(PollOption pollOption)
Definition: PollsUtil.java:86
static void removeOption(PollOption po)
Definition: PollsUtil.java:190