BrightSide Workbench Full Report + Source Code
Newsletter.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.publication.entity;
20 
21 import java.io.IOException;
22 import java.io.Writer;
23 import java.util.ArrayList;
24 import java.util.Date;
25 import java.util.List;
26 import java.util.Set;
27 import java.util.TreeSet;
28 import javax.persistence.CascadeType;
29 import javax.persistence.Column;
30 import javax.persistence.Entity;
31 import javax.persistence.FetchType;
32 import javax.persistence.GeneratedValue;
33 import javax.persistence.GenerationType;
34 import javax.persistence.Id;
35 import javax.persistence.OneToMany;
36 import javax.persistence.Temporal;
37 import org.turro.string.Strings;
38 import org.turro.action.queue.NotificationCategory;
39 import org.turro.action.queue.Notifications;
40 import org.turro.elephant.entities.db.MailTracker;
41 import org.turro.elephant.util.DateFormats;
42 import org.turro.html.HTMLEntities;
43 import org.turro.jpa.entity.IDaoEntity;
44 import org.turro.newsletter.SendingChoice;
45 import org.turro.plugin.contacts.IContact;
46 import org.turro.reflection.MappingSet;
47 
52 @Entity
53 public class Newsletter implements java.io.Serializable, IDaoEntity {
54 
55  @Id
56  @GeneratedValue(strategy=GenerationType.IDENTITY)
57  @Column(name="IDENTIFIER")
58  private Long id;
59 
60  @Temporal(value = javax.persistence.TemporalType.TIMESTAMP)
61  private java.util.Date creation;
62 
63  private boolean accepted, staticContent;
64 
65  private String idCategory;
66 
67  @Temporal(value = javax.persistence.TemporalType.DATE)
68  @Column(name="NEWS_DATE")
69  private java.util.Date date;
70 
71  private String title, lang, template;
72 
73  private SendingChoice sendingChoice;
74 
75  @OneToMany(mappedBy = "newsletter", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval=true)
76  private Set<NewsSection> newsSections = new TreeSet<NewsSection>();
77 
78  public boolean isAccepted() {
79  return accepted;
80  }
81 
82  public void setAccepted(boolean accepted) {
83  this.accepted = accepted;
84  }
85 
86  public boolean isStaticContent() {
87  return staticContent;
88  }
89 
90  public void setStaticContent(boolean staticContent) {
91  this.staticContent = staticContent;
92  }
93 
94  public String getIdCategory() {
95  return idCategory;
96  }
97 
98  public void setIdCategory(String idCategory) {
99  this.idCategory = idCategory;
100  }
101 
102  public Date getCreation() {
103  return creation;
104  }
105 
106  public void setCreation(Date creation) {
107  this.creation = creation;
108  }
109 
110  public Date getDate() {
111  return date;
112  }
113 
114  public String getDateStr() {
115  return DateFormats.format(date, true);
116  }
117 
118  public void setDate(Date date) {
119  this.date = date;
120  }
121 
122  public Long getId() {
123  return id;
124  }
125 
126  public void setId(Long id) {
127  this.id = id;
128  }
129 
130  public String getTitle() {
131  return title;
132  }
133 
134  public void setTitle(String title) {
135  this.title = HTMLEntities.escape(title);
136  }
137 
138  public String getUnescapedTitle() {
139  return HTMLEntities.unescape(title);
140  }
141 
142  public String getLang() {
143  return lang;
144  }
145 
146  public void setLang(String lang) {
147  this.lang = lang;
148  }
149 
150  public String getTemplate() {
151  return template;
152  }
153 
154  public void setTemplate(String template) {
155  this.template = template;
156  }
157 
159  return sendingChoice;
160  }
161 
162  public void setSendingChoice(SendingChoice sendingChoice) {
163  this.sendingChoice = sendingChoice;
164  }
165 
166  public Set<NewsSection> getNewsSections() {
167  return newsSections;
168  }
169 
170  public void setNewsSections(Set<NewsSection> newsSections) {
171  this.newsSections = newsSections;
172  }
173 
174  /* IDaoEntity */
175 
176  @Override
177  public Object entityId() {
178  return id;
179  }
180 
181  @Override
182  public boolean isEmpty() {
183  return Strings.isBlank(title) || newsSections.isEmpty();
184  }
185 
188  public boolean trigger(IContact contact) {
189  if(newsSections.stream().anyMatch((ns) -> (ns.isUseAsTrigger()))) {
190  return newsSections.stream().anyMatch((ns) -> (ns.isUseAsTrigger() && ns.canShow(contact)));
191  } else {
192  return true;
193  }
194  }
195 
197  return idCategory != null ? Notifications.getCategory(idCategory) : null;
198  }
199 
200  public void setCategory(NotificationCategory category) {
201  idCategory = category != null ? category.getIdCategory() : null;
202  }
203 
204  public List<MailTracker> getTrackers() {
205  return new ArrayList<>();
206  }
207 
208  public void generateNewsletter(IContact contact, Writer writer) throws IOException {
209  writer.write("<#include \"/macros/site/*/buttonMacro.ftl\">\n");
210  writer.write("<#include \"/macros/site/*/summaryMacro.ftl\">\n");
211  writer.write("<#include \"/envelope/site/*/header" +
212  (Strings.isBlank(template) ? "" : template) +
213  "Template.html\">\n");
214 
215  writer.write("<@start_full_table \"2\" \"2\"/>\n");
216 
217  for(NewsSection ns : new TreeSet<>(newsSections)) {
218  if(ns.canShow(contact)) {
219  ns.generateSection(writer);
220  }
221  }
222 
223  writer.write("<@end_table/>\n");
224 
225  writer.write("<#include \"/envelope/site/*/footer" +
226  (Strings.isBlank(template) ? "" : template) +
227  "Template.html\">\n");
228  }
229 
230  public boolean checkStartRow(NewsSection newsSection) {
231  NewsSection ns = new TreeSet<>(newsSections).lower(newsSection);
232  return ns == null || !ns.isNewColumn();
233  }
234 
235  public boolean checkEndRow(NewsSection newsSection) {
236  NewsSection ns = new TreeSet<>(newsSections).higher(newsSection);
237  return ns == null || !ns.isNewColumn();
238  }
239 
240  public void resetSections() {
241  newsSections.forEach((ns) -> {
242  ns.reset();
243  });
244  }
245 
251  public void resetGlobalContext() {
252  newsSections.forEach((ns) -> {
253  ns.resetGlobalContext();
254  });
255  }
256 
257  /* XML Serializer */
258 
259  public MappingSet getSerializerMappings() {
260  MappingSet set = new MappingSet();
261  set.addMapping(Newsletter.class, 1,
262  new String[] { "id", "creation", "date", "accepted", "staticContent" },
263  new String[] { "title", "lang", "template", "idCategory", "sendingChoice",
264  "newsSections" });
265  set.addMapping(NewsSection.class, 2,
266  new String[] { "id", "newsOrder", "newColumn", "width", "useAsTrigger",
267  "hideIfEmpty", "type" },
268  new String[] { "body", "wiki", "banner" });
269  return set;
270  }
271 
272 }
static NotificationCategory getCategory(String id)
static final String format(Date d, boolean dateOnly)
static String escape(String html)
static String unescape(String html)
void setAccepted(boolean accepted)
Definition: Newsletter.java:82
void setIdCategory(String idCategory)
Definition: Newsletter.java:98
void setCategory(NotificationCategory category)
void setStaticContent(boolean staticContent)
Definition: Newsletter.java:90
boolean trigger(IContact contact)
void setNewsSections(Set< NewsSection > newsSections)
boolean checkEndRow(NewsSection newsSection)
void setSendingChoice(SendingChoice sendingChoice)
boolean checkStartRow(NewsSection newsSection)
void generateNewsletter(IContact contact, Writer writer)