BrightSide Workbench Full Report + Source Code
NewsSection.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.TreeSet;
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.ManyToOne;
31 import org.turro.string.Strings;
32 import org.turro.action.content.ContentContext;
33 import org.turro.action.content.ContentProviders;
34 import org.turro.action.content.ContentRenderer;
35 import org.turro.jpa.entity.IDaoEntity;
36 import org.turro.plugin.contacts.IContact;
37 import org.turro.util.CompareUtil;
38 
43 @Entity
44 public class NewsSection implements java.io.Serializable, IDaoEntity, Comparable<NewsSection> {
45 
46  @Id
47  @GeneratedValue(strategy=GenerationType.IDENTITY)
48  @Column(name="IDENTIFIER")
49  private Long id;
50 
51  @ManyToOne
52  private Newsletter newsletter;
53 
54  private int newsOrder;
55 
56  private boolean newColumn, useAsTrigger, hideIfEmpty;
57 
58  private String width;
59 
60  private NewsSectionType type;
61 
62  @Lob
63  @Column(length=4096)
64  private String banner;
65 
66  @Lob
67  @Column(length=4096)
68  private String body;
69 
70  @Lob
71  @Column(length=4096)
72  private String wiki;
73 
74  public Long getId() {
75  return id;
76  }
77 
78  public void setId(Long id) {
79  this.id = id;
80  }
81 
83  return newsletter;
84  }
85 
86  public void setNewsletter(Newsletter newsletter) {
87  this.newsletter = newsletter;
88  }
89 
90  public int getNewsOrder() {
91  return newsOrder;
92  }
93 
94  public void setNewsOrder(int newsOrder) {
95  this.newsOrder = newsOrder;
96  }
97 
98  public boolean isNewColumn() {
99  return newColumn;
100  }
101 
102  public void setNewColumn(boolean newColumn) {
103  this.newColumn = newColumn;
104  }
105 
106  public boolean isUseAsTrigger() {
107  return useAsTrigger;
108  }
109 
110  public void setUseAsTrigger(boolean useAsTrigger) {
111  this.useAsTrigger = useAsTrigger;
112  }
113 
114  public boolean isHideIfEmpty() {
115  return hideIfEmpty;
116  }
117 
118  public void setHideIfEmpty(boolean hideIfEmpty) {
119  this.hideIfEmpty = hideIfEmpty;
120  }
121 
122  public String getWidth() {
123  return width;
124  }
125 
126  public void setWidth(String width) {
127  this.width = width;
128  }
129 
130  public String getBanner() {
131  return banner;
132  }
133 
134  public void setBanner(String banner) {
135  this.banner = banner;
136  }
137 
139  return type;
140  }
141 
142  public void setType(NewsSectionType type) {
143  this.type = type;
144  }
145 
146  public String getBody() {
147  return body;
148  }
149 
150  public void setBody(String body) {
151  this.body = body;
152  }
153 
154  public String getWiki() {
155  return wiki;
156  }
157 
158  public void setWiki(String wiki) {
159  this.wiki = wiki;
160  }
161 
162  /* IDaoEntity */
163 
164  @Override
165  public Object entityId() {
166  return id;
167  }
168 
169  @Override
170  public boolean isEmpty() {
171  return Strings.isBlank(body);
172  }
173 
176  public boolean canShow(IContact contact) {
177  loadBody(contact);
178  return !(isHideIfEmpty() && isEmptyContent());
179  }
180 
181  public boolean isEmptyContent() {
182  if(NewsSectionType.NEWS_SECTION_EXTERNAL.equals(type)) {
183  return content != null ? content.getIterator().isEmpty() : true;
184  }
185  return isEmpty();
186  }
187 
188  public void generateSection(Writer template) throws IOException {
189  boolean firstColumn = false;
190  boolean lastColumn = false;
191  if(isNewColumn()) {
192  firstColumn = newsletter.checkStartRow(this);
193  lastColumn = newsletter.checkEndRow(this);
194  if(firstColumn) {
195  template.write("<@start_row/>\n");
196  template.write("<@start_column/>\n");
197  template.write("<@start_full_table \"0\" \"0\"/>\n");
198  template.write("<@start_row/>\n");
199  }
200  template.write("<@start_stackable style=\"width:" + getColumnWidth(this) + "\"/>\n");
201  } else {
202  template.write("<@start_row/>\n");
203  template.write("<@start_column/>\n");
204  }
205 
206  if(!Strings.isBlank(banner)) {
207  if(NewsSectionType.NEWS_SECTION_IMAGE.equals(type)) {
208  template.write("<a target='_blank' href='" + banner + "'>");
209  } else {
210  template.write(banner);
211  }
212  }
213 
214  if(NewsSectionType.NEWS_SECTION_EXTERNAL.equals(type)) {
215  template.write("${newsSection" + newsOrder + ".generateBody()}\n");
216  } else {
217  template.write("${newsSection" + newsOrder + ".body}\n");
218  }
219 
220  if(!Strings.isBlank(banner)) {
221  if(NewsSectionType.NEWS_SECTION_IMAGE.equals(type)) {
222  template.write("</a>");
223  }
224  }
225 
226  if(isNewColumn() && !lastColumn) {
227  template.write("<@stackable_divider/>");
228  }
229 
230  template.write("<@end_column/>\n");
231 
232  if(isNewColumn()) {
233  if(lastColumn) {
234  template.write("<@end_row/>\n");
235  template.write("<@end_table/>\n");
236  template.write("<@end_column/>\n");
237  template.write("<@end_row/>\n");
238  template.write("<@row_summary_divider height=\"20px\"/>");
239  }
240  } else {
241  template.write("<@end_row/>\n");
242  template.write("<@row_summary_divider height=\"20px\"/>");
243  }
244  }
245 
248  private transient ContentRenderer content;
249 
250  public void loadBody(IContact contact) {
251  if(content == null && NewsSectionType.NEWS_SECTION_EXTERNAL.equals(type)) {
252  content = ContentProviders.loadCommand(body, contact, getUniqueId());
253  }
254  }
255 
256  public String generateBody() {
257  if(NewsSectionType.NEWS_SECTION_EXTERNAL.equals(type)) {
258  return content != null ? content.execute() : "";
259  }
260  return "";
261  }
262 
263  public void reset() {
264  content = null;
265  }
266 
267  public void resetGlobalContext() {
269  }
270 
271  public String getUniqueId() {
272  return "NewsSection" + getId();
273  }
274 
277  @Override
278  public int compareTo(NewsSection o) {
279  int result = CompareUtil.compare(newsOrder, o.newsOrder);
280  if(result == 0 && id != null && o.id != null) {
281  result = CompareUtil.compare(id, o.id);
282  }
283  return result;
284  }
285 
286  private String getColumnWidth(NewsSection section) {
287  if(!Strings.isBlank(section.getWidth())) {
288  return section.getWidth();
289  }
290  TreeSet<NewsSection> sections = new TreeSet(newsletter.getNewsSections());
291  int count = 0;
292  boolean iAmIn = false;
293  for(NewsSection ns : sections) {
294  if(ns.isNewColumn()) {
295  if(ns.getNewsOrder() == section.getNewsOrder()) {
296  iAmIn = true;
297  }
298  count++;
299  } else {
300  if(iAmIn) {
301  return (100 / count) + "%";
302  }
303  count = 0;
304  }
305  }
306  if(iAmIn && count > 0) {
307  return (100 / count) + "%";
308  }
309  return "100%";
310  }
311 
312 }
static void removeContent(String context)
static ContentRenderer loadCommand(String command, IContact contact, String idContext)
void setHideIfEmpty(boolean hideIfEmpty)
void setUseAsTrigger(boolean useAsTrigger)
void setNewsletter(Newsletter newsletter)
void setType(NewsSectionType type)