BrightSide Workbench Full Report + Source Code
Poll.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.elephant.entities.db;
20 
21 import java.util.Date;
22 import java.util.List;
23 import java.util.Objects;
24 import javax.persistence.Column;
25 import javax.persistence.Entity;
26 import javax.persistence.GeneratedValue;
27 import javax.persistence.GenerationType;
28 import javax.persistence.Id;
29 import javax.persistence.Lob;
30 import javax.persistence.Temporal;
31 import org.turro.string.Strings;
32 import org.turro.assistant.AssistantConstants;
33 import org.turro.assistant.AssistantSet;
34 import org.turro.assistant.Assistants;
35 import org.turro.auth.Authentication;
36 import org.turro.elephant.context.Application;
37 import org.turro.elephant.context.ElephantContext;
38 import org.turro.elephant.db.ElephantPU;
39 import org.turro.elephant.db.WhereClause;
40 import org.turro.elephant.impl.util.Parser;
41 import org.turro.entities.Entities;
42 import org.turro.entities.IElephantEntity;
43 import org.turro.jpa.entity.IDaoEntity;
44 import org.turro.parser.wiki.WikiCompiler;
45 import org.turro.plugin.contacts.IContact;
46 import org.turro.util.CompareUtil;
47 
52 @Entity
53 public class Poll implements java.io.Serializable, IDaoEntity, Comparable<Poll> {
54 
55  @Id
56  @GeneratedValue(strategy=GenerationType.IDENTITY)
57  @Column(name="IDENTIFIER")
58  private Long id;
59 
60  private String title, location, entityPath;
61 
62  @Temporal(javax.persistence.TemporalType.TIMESTAMP)
63  private Date deadline;
64 
65  @Lob
66  @Column(length=4096)
67  private String wiki, text;
68 
69  private int maxPerOption, maxVotedOptions;
70 
71  private boolean showParticipants, showVotes, allowInsertions, allowNegatives;
72 
73  private String optionClass, optionStart, optionEnd;
74 
75  public Long getId() {
76  return id;
77  }
78 
79  public void setId(Long id) {
80  this.id = id;
81  }
82 
83  public String getTitle() {
84  return title;
85  }
86 
87  public void setTitle(String title) {
88  this.title = title;
89  }
90 
91  public String getLocation() {
92  return location;
93  }
94 
95  public void setLocation(String location) {
96  this.location = location;
97  }
98 
99  public String getEntityPath() {
100  return entityPath;
101  }
102 
103  public void setEntityPath(String entityPath) {
104  this.entityPath = entityPath;
105  }
106 
107  public Date getDeadline() {
108  return deadline;
109  }
110 
111  public void setDeadline(Date deadline) {
112  this.deadline = deadline;
113  }
114 
115  public String getWiki() {
116  return wiki;
117  }
118 
119  public void setWiki(String wiki) {
120  this.wiki = wiki;
121  text = WikiCompiler.source(wiki).html();
122  }
123 
124  public String getText() {
125  return text == null ? null : text.replaceAll("\\{site\\}", ElephantContext.getSiteName());
126  }
127 
128  public void setText(String text) {
129  this.text = text;
130  }
131 
132  public int getMaxPerOption() {
133  return maxPerOption;
134  }
135 
136  public void setMaxPerOption(int maxPerOption) {
137  this.maxPerOption = maxPerOption;
138  }
139 
140  public int getMaxVotedOptions() {
141  return maxVotedOptions;
142  }
143 
144  public void setMaxVotedOptions(int maxVotedOptions) {
145  this.maxVotedOptions = maxVotedOptions;
146  }
147 
148  public boolean isShowParticipants() {
149  return showParticipants;
150  }
151 
152  public void setShowParticipants(boolean showParticipants) {
153  this.showParticipants = showParticipants;
154  }
155 
156  public boolean isShowVotes() {
157  return showVotes;
158  }
159 
160  public void setShowVotes(boolean showVotes) {
161  this.showVotes = showVotes;
162  }
163 
164  public boolean isAllowInsertions() {
165  return allowInsertions;
166  }
167 
168  public void setAllowInsertions(boolean allowInsertions) {
169  this.allowInsertions = allowInsertions;
170  }
171 
172  public boolean isAllowNegatives() {
173  return allowNegatives;
174  }
175 
176  public void setAllowNegatives(boolean allowNegatives) {
177  this.allowNegatives = allowNegatives;
178  }
179 
180  public String getOptionClass() {
181  return optionClass;
182  }
183 
184  public void setOptionClass(String optionClass) {
185  this.optionClass = optionClass;
186  }
187 
188  public String getOptionStart() {
189  return optionStart;
190  }
191 
192  public void setOptionStart(String optionStart) {
193  this.optionStart = optionStart;
194  }
195 
196  public String getOptionEnd() {
197  return optionEnd;
198  }
199 
200  public void setOptionEnd(String optionEnd) {
201  this.optionEnd = optionEnd;
202  }
203 
204  /* IDaoEntity */
205 
206  @Override
207  public Object entityId() {
208  return id;
209  }
210 
211  @Override
212  public boolean isEmpty() {
213  return Strings.isBlank(title) || deadline == null;
214  }
215 
216  /* Helpers */
217 
218  public boolean canVote() {
219  return !isEmpty() && deadline.after(new Date());
220  }
221 
223  return Entities.getController(entityPath);
224  }
225 
226  public List<PollOption> getOptions() {
227  WhereClause wc = new WhereClause();
228  wc.addClause("select opt from PollOption opt");
229  wc.addClause("where opt.idPoll = :idPoll");
230  wc.addNamedValue("idPoll", this.getId());
231  wc.addClause("order by opt.ordering, opt.pollData");
232  return new ElephantPU().getResultList(wc);
233  }
234 
235  public List<PollVote> getVotes() {
236  WhereClause wc = new WhereClause();
237  wc.addClause("select vote from PollVote vote");
238  wc.addClause("where vote.idPoll = :idPoll");
239  wc.addNamedValue("idPoll", this.getId());
240  wc.addClause("order by vote.idPoll, vote.pollData");
241  return new ElephantPU().getResultList(wc);
242  }
243 
244  public List<PollVote> getVotes(IContact contact) {
245  WhereClause wc = new WhereClause();
246  wc.addClause("select vote from PollVote vote");
247  wc.addClause("where vote.idPoll = :idPoll");
248  wc.addNamedValue("idPoll", this.getId());
249  wc.addClause("and vote.idContact = :contact");
250  wc.addNamedValue("contact", contact.getId());
251  wc.addClause("order by vote.idPoll, vote.pollData");
252  return new ElephantPU().getResultList(wc);
253  }
254 
255  /* Participants */
256 
257  private transient AssistantSet assistantSet;
258 
260  if(assistantSet == null) {
261  assistantSet = new AssistantSet();
262  Assistants.addAssistants(entityPath, true, assistantSet, AssistantConstants.all());
263  }
264  return assistantSet;
265  }
266 
267  public boolean isParticipant() {
269  }
270 
271  public boolean isParticipant(IContact contact) {
272  return getParticipants().exists(contact);
273  }
274 
275  public boolean isFullyVoted() {
277  }
278 
279  public boolean isFullyVoted(IContact contact) {
280  for(PollOption po : getOptions()) {
281  if(po.getPollVote(contact) == null) {
282  return false;
283  }
284  }
285  return true;
286  }
287 
288  /* Set */
289 
290  @Override
291  public int hashCode() {
292  int hash = 5;
293  hash = 17 * hash + Objects.hashCode(this.id);
294  return hash;
295  }
296 
297  @Override
298  public boolean equals(Object obj) {
299  if (this == obj) {
300  return true;
301  }
302  if (obj == null) {
303  return false;
304  }
305  if (getClass() != obj.getClass()) {
306  return false;
307  }
308  final Poll other = (Poll) obj;
309  if (!Objects.equals(this.id, other.id)) {
310  return false;
311  }
312  return true;
313  }
314 
315  @Override
316  public int compareTo(Poll o) {
317  int result = CompareUtil.compare(o.deadline, deadline);
318  if(result == 0) {
319  CompareUtil.compare(o.id, id);
320  }
321  return result;
322  }
323 
324 }
boolean exists(IContact contact)
static void addAssistants(String role, AssistantSet list, Object data)
Definition: Assistants.java:35
void addNamedValue(String name, Object value)
void setTitle(String title)
Definition: Poll.java:87
IElephantEntity getEntity()
Definition: Poll.java:222
void setMaxPerOption(int maxPerOption)
Definition: Poll.java:136
boolean equals(Object obj)
Definition: Poll.java:298
boolean isFullyVoted(IContact contact)
Definition: Poll.java:279
void setOptionStart(String optionStart)
Definition: Poll.java:192
List< PollVote > getVotes(IContact contact)
Definition: Poll.java:244
void setAllowNegatives(boolean allowNegatives)
Definition: Poll.java:176
void setMaxVotedOptions(int maxVotedOptions)
Definition: Poll.java:144
List< PollOption > getOptions()
Definition: Poll.java:226
void setShowParticipants(boolean showParticipants)
Definition: Poll.java:152
void setEntityPath(String entityPath)
Definition: Poll.java:103
void setLocation(String location)
Definition: Poll.java:95
void setShowVotes(boolean showVotes)
Definition: Poll.java:160
void setOptionClass(String optionClass)
Definition: Poll.java:184
List< PollVote > getVotes()
Definition: Poll.java:235
void setOptionEnd(String optionEnd)
Definition: Poll.java:200
void setDeadline(Date deadline)
Definition: Poll.java:111
boolean isParticipant(IContact contact)
Definition: Poll.java:271
void setAllowInsertions(boolean allowInsertions)
Definition: Poll.java:168
static IElephantEntity getController(String path)
Definition: Entities.java:78