BrightSide Workbench Full Report + Source Code
Publication.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 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 package org.turro.publication.entity;
19 
20 import java.util.Date;
21 import javax.persistence.Column;
22 import javax.persistence.Entity;
23 import javax.persistence.GeneratedValue;
24 import javax.persistence.GenerationType;
25 import javax.persistence.Id;
26 import javax.persistence.Lob;
27 import javax.persistence.ManyToOne;
28 import javax.persistence.Temporal;
29 import org.turro.string.Strings;
30 import org.turro.action.Contacts;
31 import org.turro.elephant.context.Application;
32 import org.turro.elephant.impl.util.Parser;
33 import org.turro.elephant.util.DateFormats;
34 import org.turro.html.HTMLEntities;
35 import org.turro.html.HtmlContent;
36 import org.turro.jpa.Dao;
37 import org.turro.jpa.entity.IDaoEntity;
38 import org.turro.parser.wiki.WikiCompiler;
39 import org.turro.plugin.contacts.IContact;
40 import org.turro.reflection.MappingSet;
41 
46 @Entity
47 public class Publication implements java.io.Serializable, IDaoEntity {
48 
49  @Id
50  @GeneratedValue(strategy=GenerationType.IDENTITY)
51  @Column(name="IDENTIFIER")
52  private Long id;
53 
54  @Temporal(value = javax.persistence.TemporalType.TIMESTAMP)
55  private java.util.Date creation;
56 
57  private String idContact;
58 
59  private boolean accepted, sent;
60 
61  @ManyToOne
62  private PublicationGroup publicationGroup;
63 
64  @ManyToOne
65  private PublicationCategory publicationCategory;
66 
67  private String author;
68 
69  @Temporal(value = javax.persistence.TemporalType.DATE)
70  @Column(name="PUB_DATE")
71  private java.util.Date date;
72 
73  @Temporal(value = javax.persistence.TemporalType.TIMESTAMP)
74  private java.util.Date retainTill;
75 
76  private String title, lang;
77 
78  private String senderId;
79 
80  private long readings;
81 
82  @Lob
83  @Column(length=4096)
84  private String summary;
85 
86  @Lob
87  @Column(length=4096)
88  private String body;
89 
90  @Lob
91  @Column(length=4096)
92  private String wikiSummary;
93 
94  @Lob
95  @Column(length=4096)
96  private String wikiBody;
97 
98  public boolean isAccepted() {
99  return accepted;
100  }
101 
102  public void setAccepted(boolean accepted) {
103  this.accepted = accepted;
104  }
105 
106  public String getAuthor() {
107  return author;
108  }
109 
110  public void setAuthor(String author) {
111  this.author = author;
112  }
113 
114  public String getBody() {
115  return body;
116  }
117 
118  public void setBody(String body) {
119  this.body = body;
120  }
121 
122  public Date getCreation() {
123  return creation;
124  }
125 
126  public void setCreation(Date creation) {
127  this.creation = creation;
128  }
129 
130  public Date getDate() {
131  return date;
132  }
133 
134  public String getDateStr() {
135  return DateFormats.format(date, true);
136  }
137 
138  public void setDate(Date date) {
139  this.date = date;
140  }
141 
142  public Date getRetainTill() {
143  return retainTill;
144  }
145 
146  public void setRetainTill(Date retainTill) {
147  this.retainTill = retainTill;
148  }
149 
150  public Long getId() {
151  return id;
152  }
153 
154  public void setId(Long id) {
155  this.id = id;
156  }
157 
158  public String getIdContact() {
159  return idContact;
160  }
161 
162  public void setIdContact(String idContact) {
163  this.idContact = idContact;
164  resetIContact();
165  }
166 
168  return publicationCategory;
169  }
170 
171  public void setPublicationCategory(PublicationCategory publicationCategory) {
172  this.publicationCategory = publicationCategory;
173  }
174 
176  return publicationGroup;
177  }
178 
179  public void setPublicationGroup(PublicationGroup publicationGroup) {
180  this.publicationGroup = publicationGroup;
181  }
182 
183  public long getReadings() {
184  return readings;
185  }
186 
187  public void setReadings(long readings) {
188  this.readings = readings;
189  }
190 
191  public boolean isSent() {
192  return sent;
193  }
194 
195  public void setSent(boolean sent) {
196  this.sent = sent;
197  }
198 
199  public String getSummary() {
200  return summary;
201  }
202 
203  public void setSummary(String summary) {
204  this.summary = summary;
205  }
206 
207  public String getTitle() {
208  return title;
209  }
210 
211  public void setTitle(String title) {
212  this.title = HTMLEntities.escape(title);
213  }
214 
215  public String getUnescapedTitle() {
216  return HTMLEntities.unescape(title);
217  }
218 
219  public String getLang() {
220  return lang;
221  }
222 
223  public void setLang(String lang) {
224  this.lang = lang;
225  }
226 
227  public String getWikiSummary() {
228  if(Strings.isBlank(wikiSummary) && !Strings.isBlank(summary)) {
229  wikiSummary = "$no_wiki\n" + summary;
230  }
231  return wikiSummary;
232  }
233 
234  public void setWikiSummary(String wikiSummary) {
235  this.wikiSummary = wikiSummary;
236  this.summary = WikiCompiler.source(wikiSummary).html();
237  }
238 
239  public String getWikiBody() {
240  if(Strings.isBlank(wikiBody) && !Strings.isBlank(body)) {
241  wikiBody = "$no_wiki\n" + body;
242  }
243  return wikiBody;
244  }
245 
246  public void setWikiBody(String wikiBody) {
247  this.wikiBody = wikiBody;
248  this.body = WikiCompiler.source(wikiBody).html();
249  }
250 
251  public String getSenderId() {
252  return senderId;
253  }
254 
255  public void setSenderId(String senderId) {
256  this.senderId = senderId;
257  }
258 
261  public void updateReadings(Dao dao) {
262  dao.executeUpdate(
263  "update Publication set readings = readings + 1 where id = ? and accepted = TRUE",
264  new Object[] { id });
265  }
266 
267  /* IDaoEntity */
268 
269  @Override
270  public Object entityId() {
271  return id;
272  }
273 
274  @Override
275  public boolean isEmpty() {
276  return publicationCategory == null || publicationGroup == null || Strings.isBlank(title) ||
277  (Strings.isBlank(summary) && Strings.isBlank(wikiSummary));
278  }
279 
282  public String getContent() {
283  return (Strings.isBlank(summary) ? "" : summary) +
284  (Strings.isBlank(body) ? "" : body);
285  }
286 
287  public String getSummary(int max) {
288  return HtmlContent.truncate(summary, max);
289  }
290 
291  public String getPlainSummary(int max) {
292  return HtmlContent.plain(summary, max);
293  }
294 
297  private transient IContact _contact;
298 
300  if(_contact == null) {
301  _contact = Contacts.getContactById(idContact);
302  }
303  return _contact;
304  }
305 
306  public void setContactAuthor(IContact contact) {
307  _contact = contact;
308  idContact = _contact != null ? _contact.getId() : null;
309  author = _contact != null ? _contact.getName() : null;
310  }
311 
312  private void resetIContact() {
313  _contact = null;
314  }
315 
316  private transient IContact _sender;
317 
318  public IContact getSender() {
319  if(_sender == null) {
320  _sender = Contacts.getContactById(senderId);
321  }
322  return _sender;
323  }
324 
325  public void setSender(IContact contact) {
326  _sender = contact;
327  senderId = _sender != null ? _sender.getId() : null;
328  }
329 
330  /* XML Serializer */
331 
332  public MappingSet getSerializerMappings() {
333  MappingSet set = new MappingSet();
334  set.addMapping(Publication.class, 1,
335  new String[] { "id", "creation", "date", "retainTill", "idContact",
336  "author", "senderId", "accepted", "sent" },
337  new String[] { "title", "lang", "summary", "body", "wikiSummary",
338  "wikiBody", "publicationGroup", "publicationCategory" });
339  set.addMapping(PublicationGroup.class, 2,
340  new String[] { "id", "name" },
341  null);
342  set.addMapping(PublicationCategory.class, 2,
343  new String[] { "id", "name" },
344  null);
345  return set;
346  }
347 
348 }
static IContact getContactById(String id)
Definition: Contacts.java:72
static final String format(Date d, boolean dateOnly)
static String escape(String html)
static String unescape(String html)
int executeUpdate(String query)
Definition: Dao.java:463
void setPublicationGroup(PublicationGroup publicationGroup)
void setWikiSummary(String wikiSummary)
PublicationCategory getPublicationCategory()
void setPublicationCategory(PublicationCategory publicationCategory)