BrightSide Workbench Full Report + Source Code
ContactService.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.ArrayList;
22 import java.util.Collection;
23 import java.util.Date;
24 import javax.persistence.Column;
25 import javax.persistence.Entity;
26 import javax.persistence.GeneratedValue;
27 import javax.persistence.Id;
28 import javax.persistence.JoinColumn;
29 import javax.persistence.Lob;
30 import javax.persistence.ManyToOne;
31 import javax.persistence.Temporal;
32 import org.turro.string.Strings;
33 import org.turro.contacts.db.ContactsPU;
34 import org.turro.elephant.calendar.EventDates;
35 import org.turro.elephant.calendar.Periodicity;
36 import org.turro.fieldit.FieldItSet;
37 import org.turro.fieldit.FieldItUtil;
38 import org.turro.fieldit.IValueItEntity;
39 import org.turro.fieldit.ValueItSet;
40 import org.turro.file.util.FileAttach;
41 import org.turro.groupit.GroupItUtil;
42 import org.turro.html.HtmlContent;
43 import org.turro.i18n.I_;
44 import org.turro.jpa.entity.IDaoEntity;
45 import org.turro.plugin.contacts.IContact;
46 import org.turro.reflection.MappingSet;
47 
52 @Entity
53 @org.hibernate.annotations.GenericGenerator(name = "hibernate-uuid", strategy = "uuid")
54 public class ContactService implements java.io.Serializable, IDaoEntity, IValueItEntity {
55 
56  @Id
57  @GeneratedValue(generator = "hibernate-uuid")
58  @Column(name="IDENTIFIER")
59  private String id;
60 
61  private String title;
62 
63  private ContactServiceRole role;
64 
65  private String type;
66 
67  private String thematic;
68 
69  @Temporal(value = javax.persistence.TemporalType.TIMESTAMP)
70  private java.util.Date creation, startDate, endDate;
71 
72  @Lob
73  @Column(length=4096)
74  private String text;
75 
76  @Lob
77  @Column(length=4096)
78  private String wiki;
79 
80  @ManyToOne
81  @JoinColumn(name="CONTACT_FK")
82  private org.turro.contacts.Contact contact;
83 
84  @ManyToOne
85  @JoinColumn(name="RESPONSIBLE_FK")
86  private org.turro.contacts.Contact responsible;
87 
88  private int timesSent;
89 
90  private double price, tax;
91 
92  private Periodicity periodicity;
93 
94  private String relatedURL;
95 
96  public String getId() {
97  return id;
98  }
99 
100  protected void setId(String id) {
101  this.id = id;
102  }
103 
104  public String getTitle() {
105  return title;
106  }
107 
108  public void setTitle(String title) {
109  this.title = title;
110  }
111 
113  return role;
114  }
115 
116  public void setRole(ContactServiceRole role) {
117  this.role = role;
118  }
119 
120  public String getType() {
121  return type;
122  }
123 
124  public void setType(String type) {
125  this.type = type;
126  }
127 
128  public String getThematic() {
129  return thematic;
130  }
131 
132  public void setThematic(String thematic) {
133  this.thematic = thematic;
134  }
135 
136  public Date getCreation() {
137  return creation;
138  }
139 
140  public void setCreation(Date creation) {
141  this.creation = creation;
142  }
143 
144  public Date getStartDate() {
145  return startDate;
146  }
147 
148  public void setStartDate(Date startDate) {
149  this.startDate = startDate;
150  }
151 
152  public Date getEndDate() {
153  return endDate;
154  }
155 
156  public void setEndDate(Date endDate) {
157  this.endDate = endDate;
158  }
159 
160  public double getPrice() {
161  return price;
162  }
163 
164  public void setPrice(double price) {
165  this.price = price;
166  }
167 
168  public double getTax() {
169  return tax;
170  }
171 
172  public void setTax(double tax) {
173  this.tax = tax;
174  }
175 
177  return periodicity;
178  }
179 
180  public void setPeriodicity(Periodicity periodicity) {
181  this.periodicity = periodicity;
182  }
183 
184  public String getRelatedURL() {
185  return relatedURL;
186  }
187 
188  public void setRelatedURL(String relatedURL) {
189  this.relatedURL = relatedURL;
190  }
191 
192  public String getText() {
193  return text;
194  }
195 
196  public void setText(String text) {
197  this.text = text;
198  }
199 
200  public String getWiki() {
201  // Compatible with non wiki version
202  if(Strings.isEmpty(wiki) && !Strings.isEmpty(text)) {
203  wiki = text;
204  }
205  return wiki;
206  }
207 
208  public void setWiki(String wiki) {
209  this.wiki = wiki;
210  }
211 
212  public Contact getContact() {
213  return contact;
214  }
215 
216  public void setContact(Contact contact) {
217  this.contact = contact;
218  }
219 
221  return responsible;
222  }
223 
224  public void setResponsible(Contact responsible) {
225  this.responsible = responsible;
226  }
227 
228  public int getTimesSent() {
229  return timesSent;
230  }
231 
232  public void setTimesSent(int timesSent) {
233  this.timesSent = timesSent;
234  }
235 
236  /* IDaoEntity */
237 
238  @Override
239  public Object entityId() {
240  return id;
241  }
242 
243  @Override
244  public boolean isEmpty() {
245  return Strings.isBlank(title) || Strings.isBlank(text) || role == null ||
246  Strings.isBlank(type) || Strings.isBlank(thematic) || Strings.isBlank(relatedURL) ||
247  contact == null || responsible == null;
248  }
249 
250  /* Helpers */
251 
252  public String getPlainText() {
253  return HtmlContent.text(getText());
254  }
255 
256  public boolean isInBusiness(IContact worker) {
257  return contact.isInBusiness(worker);
258  }
259 
260  /* Contacts */
261 
263  return contact != null ? contact.getIContact() : null;
264  }
265 
266  public void setIContact(IContact contact) {
267  this.contact = contact != null ? (Contact) contact.getContact() : null;
268  }
269 
271  return responsible != null ? responsible.getIContact() : null;
272  }
273 
274  public void setIResponsible(IContact responsible) {
275  this.responsible = responsible != null ? (Contact) responsible.getContact() : null;
276  }
277 
278  /* Image */
279 
280  public String getImage() {
281  FileAttach fileAttach = new FileAttach(ContactsPU.getObjectPath(this));
282  String faceFile = fileAttach.getPublicFile("/profile/face.png", false);
283  return !Strings.isBlank(faceFile) ? faceFile : null;
284  }
285 
286  /* Event date */
287 
288  public String getEventDate() {
289  if(startDate != null && endDate != null) {
290  return EventDates.eventFormat(startDate, endDate);
291  } else if(startDate != null) {
292  return I_.get("Starts") + " " + EventDates.eventFormat(startDate);
293  } else if(endDate != null) {
294  return I_.get("Ends") + " " + EventDates.eventFormat(endDate);
295  }
296  return null;
297  }
298 
299  /* Fields */
300 
301  @Override
304  }
305 
306  @Override
309  }
310 
311  @Override
312  public Object getFieldValue(String label) {
313  return FieldItUtil.getFieldValue(label, ContactsPU.getObjectPath(this));
314  }
315 
316  @Override
317  public Collection<IValueItEntity> getRelated() {
318  Collection<String> paths = FieldItUtil.related(ContactsPU.getObjectPath(this));
319  return new ArrayList<>();
320  }
321 
322  /* XML Serializer */
323 
324  public MappingSet getSerializerMappings() {
325  MappingSet set = new MappingSet();
326  set.addMapping(ContactService.class, 1,
327  new String[] { "title", "creation", "startDate", "endDate", "role", "type" },
328  new String[] { "thematic", "text", "contact", "responsible", "relatedURL", "timesSent",
329  "price", "tax", "periodicity" });
330  set.addMapping(Contact.class, 2,
331  new String[] { "id", "name" },
332  null);
333  return set;
334  }
335 
336 }
Collection< IValueItEntity > getRelated()
void setResponsible(Contact responsible)
void setRelatedURL(String relatedURL)
void setIContact(IContact contact)
void setPeriodicity(Periodicity periodicity)
void setIResponsible(IContact responsible)
boolean isInBusiness(IContact worker)
void setRole(ContactServiceRole role)
boolean isInBusiness(Contact worker)
Definition: Contact.java:648
static String getObjectPath(Object object)
Definition: ContactsPU.java:68
static String eventFormat(Date date)
Definition: EventDates.java:37
static Collection< String > related(String path)
static Object getFieldValue(String label, String entityPath)
String getPublicFile(String file)
Definition: FileAttach.java:59
static ValueItSet getValues(String entityPath)
static FieldItSet getFields(String entityPath)
static String get(String msg)
Definition: I_.java:41