BrightSide Workbench Full Report + Source Code
Convocation.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2017 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.contacts;
20 
21 import java.util.Date;
22 import java.util.HashSet;
23 import java.util.Set;
24 import java.util.TreeSet;
25 import java.util.concurrent.TimeUnit;
26 import javax.persistence.CascadeType;
27 import javax.persistence.Column;
28 import javax.persistence.Embedded;
29 import javax.persistence.Entity;
30 import javax.persistence.FetchType;
31 import javax.persistence.GeneratedValue;
32 import javax.persistence.Id;
33 import javax.persistence.JoinColumn;
34 import javax.persistence.Lob;
35 import javax.persistence.ManyToOne;
36 import javax.persistence.OneToMany;
37 import javax.persistence.Temporal;
38 import org.turro.string.Strings;
39 import org.turro.assistant.Assistant;
40 import org.turro.assistant.AssistantConstants;
41 import org.turro.assistant.AssistantSet;
42 import org.turro.assistant.Assistants;
43 import org.turro.auth.Authentication;
44 import org.turro.contacts.db.ContactsPU;
45 import org.turro.elephant.calendar.EventDates;
46 import org.turro.elephant.db.WhereClause;
47 import org.turro.entities.Entities;
48 import org.turro.entities.IElephantEntity;
49 import org.turro.html.HtmlContent;
50 import org.turro.i18n.I_;
51 import org.turro.jpa.entity.IDaoEntity;
52 import org.turro.parser.wiki.WikiCompiler;
53 import org.turro.plugin.contacts.IContact;
54 import org.turro.reflection.MappingSet;
55 import org.turro.util.CompareUtil;
56 import org.turro.ws.embeddables.WsServers;
57 import org.turro.zul.convocation.AttendeeComparator;
58 
63 @Entity
64 @org.hibernate.annotations.GenericGenerator(name = "hibernate-uuid", strategy = "uuid")
65 public class Convocation implements java.io.Serializable, IDaoEntity {
66 
67  @Id
68  @GeneratedValue(generator = "hibernate-uuid")
69  @Column(name="IDENTIFIER")
70  private String id;
71 
72  private String entityPath;
73 
74  private boolean online, publishable;
75 
76  private String name, location, onlineUrl;
77 
78  @Temporal(value = javax.persistence.TemporalType.TIMESTAMP)
79  private java.util.Date callDate, endDate;
80 
81  @Lob
82  @Column(length=4096)
83  private String text;
84 
85  @Lob
86  @Column(length=4096)
87  private String wiki;
88 
89  @ManyToOne
90  @JoinColumn(name="CONTACT_FK")
91  private Contact organizer;
92 
93  private String icalIdentifier;
94 
95  @OneToMany(mappedBy = "convocation", fetch = FetchType.EAGER, cascade=CascadeType.ALL, orphanRemoval=true)
96  private Set<Attendee> attendees = new HashSet<>();
97 
98  @Embedded
99  private WsServers servers;
100 
101  public String getId() {
102  return id;
103  }
104 
105  public void setId(String id) {
106  this.id = id;
107  }
108 
109  public String getEntityPath() {
110  return entityPath;
111  }
112 
113  public void setEntityPath(String entityPath) {
114  this.entityPath = entityPath;
115  }
116 
117  public boolean isOnline() {
118  return online;
119  }
120 
121  public void setOnline(boolean online) {
122  this.online = online;
123  }
124 
125  public boolean isPublishable() {
126  return publishable;
127  }
128 
129  public void setPublishable(boolean publishable) {
130  this.publishable = publishable;
131  }
132 
133  public String getName() {
134  return name;
135  }
136 
137  public void setName(String name) {
138  this.name = name;
139  }
140 
141  public Date getCallDate() {
142  return callDate;
143  }
144 
145  public String getLocation() {
146  return location;
147  }
148 
149  public void setLocation(String location) {
150  this.location = location;
151  }
152 
153  public String getOnlineUrl() {
154  return onlineUrl;
155  }
156 
157  public void setOnlineUrl(String onlineUrl) {
158  this.onlineUrl = onlineUrl;
159  }
160 
161  public void setCallDate(Date callDate) {
162  this.callDate = callDate;
163  }
164 
165  public Date getEndDate() {
166  return endDate;
167  }
168 
169  public void setEndDate(Date endDate) {
170  this.endDate = endDate;
171  }
172 
173  public String getText() {
174  return text;
175  }
176 
177  public void setText(String text) {
178  this.text = text;
179  }
180 
181  public String getWiki() {
182  // Compatible with non wiki version
183  if(Strings.isEmpty(wiki) && !Strings.isEmpty(text)) {
184  wiki = text;
185  }
186  return wiki;
187  }
188 
189  public void setWiki(String wiki) {
190  this.wiki = wiki;
191  this.text = WikiCompiler.source(this.wiki).html();
192  }
193 
195  return organizer;
196  }
197 
198  public void setOrganizer(Contact organizer) {
199  this.organizer = organizer;
200  }
201 
202  public String getIcalIdentifier() {
203  return icalIdentifier;
204  }
205 
206  public void setIcalIdentifier(String icalIdentifier) {
207  this.icalIdentifier = icalIdentifier;
208  }
209 
210  public Set<Attendee> getAttendees() {
211  return attendees;
212  }
213 
214  public void setAttendees(Set<Attendee> attendees) {
215  this.attendees = attendees;
216  }
217 
218  public WsServers getServers() {
219  return servers;
220  }
221 
222  public void setServers(WsServers servers) {
223  this.servers = servers;
224  }
225 
226  /* Event date */
227 
228  public String getEventDate() {
229  if(callDate != null && endDate != null) {
230  return EventDates.eventFormat(callDate, endDate);
231  } else if(callDate != null) {
232  return I_.get("Starts") + " " + EventDates.eventFormat(callDate);
233  } else if(endDate != null) {
234  return I_.get("Ends") + " " + EventDates.eventFormat(endDate);
235  }
236  return null;
237  }
238 
239  /* IDaoEntity */
240 
241  @Override
242  public Object entityId() {
243  return id;
244  }
245 
246  @Override
247  public boolean isEmpty() {
248  return Strings.isBlank(name) || Strings.isBlank(text) || callDate == null ||
249  endDate == null || organizer == null;
250  }
251 
252  /* Helpers */
253 
254  public String getPlainText() {
255  return HtmlContent.text(getText());
256  }
257 
259  return Entities.getController(entityPath);
260  }
261 
262  public void addAttendeesByEntity(boolean deep) {
263  AssistantSet as = new AssistantSet();
264  Assistants.addAssistants(entityPath, deep, as, deep ? AssistantConstants.all() : null);
265  for(Assistant a : as) {
266  addContact((Contact) a.contact.getContact());
267  }
268  }
269 
270  public void addContact(Contact contact) {
271  if(contact != null) {
272  for(Attendee a : getAttendees()) {
273  if(a.exists(contact)) return;
274  }
275  Attendee a = new Attendee();
276  a.setContact(contact);
277  a.setConvocation(this);
278  getAttendees().add(a);
279  }
280  }
281 
282  public void attended() {
284  }
285 
286  public void attended(IContact contact) {
287  WhereClause wc = new WhereClause();
288  wc.addClause("update from Attendee a");
289  wc.addClause("set a.attended = TRUE");
290  wc.addClause("where a.convocation.id = :id");
291  wc.addNamedValue("id", id);
292  wc.addClause("and a.contact.id = :cid");
293  wc.addNamedValue("cid", contact.getId());
294  new ContactsPU().executeUpdate(wc);
295  }
296 
297  public boolean hasAttended() {
299  }
300 
301  public boolean hasAttended(IContact contact) {
302  for(Attendee a : getAttendees()) {
303  if(a.isAttended() && a.getIContact().getId().equals(contact.getId())) return true;
304  }
305  return false;
306  }
307 
308  public boolean hasPassed() {
309  return endDate.before(new Date());
310  }
311 
312  public long getDaysLeft() {
313  return TimeUnit.DAYS.convert(callDate.getTime() - new Date().getTime(), TimeUnit.MILLISECONDS);
314  }
315 
316  public boolean isAssistant() {
318  }
319 
320  public boolean isAssistant(IContact contact) {
321  if(contact != null && contact.isValid()) {
322  return isAssistant((Contact) contact.getContact());
323  }
324  return false;
325  }
326 
327  public boolean isAssistant(Contact contact) {
328  if(contact != null) {
329  if(organizer != null) {
330  if(CompareUtil.compare(organizer.getId(), contact.getId()) == 0) {
331  return true;
332  }
333  }
334  for(Attendee a : getAttendees()) {
335  if(a.exists(contact)) return true;
336  }
337  }
338  return false;
339  }
340 
341  public TreeSet<Attendee> getSortedAttendees() {
342  TreeSet<Attendee> set = new TreeSet(new AttendeeComparator());
343  set.addAll(attendees);
344  return set;
345  }
346 
347  /* XML Serializer */
348 
349  public MappingSet getSerializerMappings() {
350  MappingSet set = new MappingSet();
351  set.addMapping(Convocation.class, 1,
352  new String[] { "id", "entityPath", "name", "onLine", "publishable",
353  "callDate", "endDate", "organizer" },
354  new String[] { "text", "location", "onLineUrl", "icalIdentifier", "attendees" });
355  set.addMapping(Attendee.class, 2,
356  new String[] { "id" },
357  new String[] { "contact" });
358  set.addMapping(Contact.class, 3,
359  new String[] { "id" },
360  null);
361  return set;
362  }
363 
364 }
static void addAssistants(String role, AssistantSet list, Object data)
Definition: Assistants.java:35
void setConvocation(Convocation convocation)
Definition: Attendee.java:67
void setContact(Contact contact)
Definition: Attendee.java:75
void setIcalIdentifier(String icalIdentifier)
boolean hasAttended(IContact contact)
void attended(IContact contact)
void setOnline(boolean online)
void setOnlineUrl(String onlineUrl)
void setEndDate(Date endDate)
void setOrganizer(Contact organizer)
void setServers(WsServers servers)
boolean isAssistant(IContact contact)
Set< Attendee > getAttendees()
void setAttendees(Set< Attendee > attendees)
boolean isAssistant(Contact contact)
void addContact(Contact contact)
TreeSet< Attendee > getSortedAttendees()
void setEntityPath(String entityPath)
void addAttendeesByEntity(boolean deep)
void setPublishable(boolean publishable)
void setLocation(String location)
void setCallDate(Date callDate)
static String eventFormat(Date date)
Definition: EventDates.java:37
void addNamedValue(String name, Object value)
static IElephantEntity getController(String path)
Definition: Entities.java:78
static String get(String msg)
Definition: I_.java:41
int executeUpdate(String query)
Definition: Dao.java:463