BrightSide Workbench Full Report + Source Code
PublicationContent.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.zul.portal;
19 
20 import java.io.IOException;
21 import java.io.PrintWriter;
22 import java.io.StringWriter;
23 import java.util.Arrays;
24 import java.util.List;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27 import javax.servlet.ServletException;
28 import org.turro.string.ObjectString;
29 import org.turro.string.Strings;
30 import org.jdom.Element;
31 import org.turro.elephant.context.Application;
32 import org.turro.elephant.context.ElephantContext;
33 import org.turro.elephant.context.IConstructor;
34 import org.turro.elephant.impl.util.Parser;
35 import org.turro.mail.recipients.MailContact;
36 import org.turro.publication.db.PublicationPU;
37 import org.turro.publication.entity.Publication;
38 import org.turro.publication.util.PublicationCategories;
39 import org.turro.zul.portal.IPortalContent;
40 import org.zkoss.zul.Html;
41 import org.zkoss.zul.Style;
42 import org.zkoss.zul.Window;
43 
48 public class PublicationContent extends Window implements IPortalContent {
49 
50  private Html html;
51  private long groupId, categoryId, publicationId;
52  private boolean accepted, title = true, details = true, summary = true, body = true;
53  private int max;
54  private String sclass, contactId;
55 
56  @Override
57  public void setConfiguration(Element root) {
58  if(contactId != null) {
59  root.setAttribute("contactId", contactId);
60  } else {
61  root.removeAttribute("contactId");
62  }
63  root.setAttribute("groupId", groupId + "");
64  root.setAttribute("categoryId", categoryId + "");
65  root.setAttribute("publicationId", publicationId + "");
66  root.setAttribute("accepted", Boolean.toString(accepted));
67  root.setAttribute("title", Boolean.toString(title));
68  root.setAttribute("details", Boolean.toString(details));
69  root.setAttribute("summary", Boolean.toString(summary));
70  root.setAttribute("body", Boolean.toString(body));
71  root.setAttribute("max", max + "");
72  root.setAttribute("sclass", sclass);
73  }
74 
75  @Override
76  public void getConfiguration(Element root) {
77  contactId = root.getAttributeValue("contactId");
78  groupId = Long.valueOf(root.getAttributeValue("groupId"));
79  categoryId = Long.valueOf(root.getAttributeValue("categoryId"));
80  publicationId = (Long) ObjectString.parseNativeString(root.getAttributeValue("publicationId"), Long.class, true);
81  accepted = Boolean.valueOf(root.getAttributeValue("accepted"));
82  title = Boolean.valueOf(root.getAttributeValue("title"));
83  details = Boolean.valueOf(root.getAttributeValue("details"));
84  summary = Boolean.valueOf(root.getAttributeValue("summary"));
85  body = Boolean.valueOf(root.getAttributeValue("body"));
86  max = Integer.valueOf(root.getAttributeValue("max"));
87  sclass = root.getAttributeValue("sclass");
88 
89  if(publicationId != 0) {
90  Publication pub = new PublicationPU().find(Publication.class, publicationId);
91  setPublication(pub);
92  } else {
94  max, groupId, categoryId, contactId, accepted));
95  }
96  }
97 
98  public void setPublication(Publication publication) {
99  setPublications(Arrays.asList(new Publication[] { publication }));
100  }
101 
102  public void setPublications(List<Publication> publications) {
103  addDefaults();
104  html.setContent(getPublicationsStr(publications, sclass, title, details, summary, body,
105  null, false, null, Application.getApplication().getConstructor()));
106  }
107 
108  public void setPublication(String publication) {
109  addDefaults();
110  html.setContent(getPublicationStr(publication, sclass, Application.getApplication().getConstructor()));
111  }
112 
113  public static String getPublicationTemplateStr(String template, Publication publication,
114  String sclass, boolean title, boolean details,
115  boolean summary, boolean body, String path, boolean asMail, String readAll,
116  IConstructor constructor) {
117  return getPublicationsTemplateStr(template, Arrays.asList(new Publication[] { publication }),
118  sclass, title, details, summary, body, path, asMail, readAll, constructor);
119  }
120 
121  public static String getPublicationsTemplateStr(String template, List<Publication> publications,
122  String sclass, boolean title, boolean details,
123  boolean summary, boolean body, String path, boolean asMail, String readAll,
124  IConstructor constructor) {
125  StringBuilder sb = new StringBuilder();
126  boolean firstOne = false;
127  for(Publication pub : publications) {
128  if(!firstOne) {
129  firstOne = true;
130  } else {
131  sb.append("<div class='divider'></div>");
132  }
133  writePublication(template, sb, pub, sclass, title, details, summary, body, path, asMail, readAll, constructor);
134  }
135  return sb.toString();
136  }
137 
138  public static String getPublicationStr(Publication publication,
139  String sclass, boolean title, boolean details,
140  boolean summary, boolean body, String path, boolean asMail, String readAll,
141  IConstructor constructor) {
142  return getPublicationsStr(Arrays.asList(new Publication[] { publication }),
143  sclass, title, details, summary, body, path, asMail, readAll, constructor);
144  }
145 
146  public static String getPublicationsStr(List<Publication> publications,
147  String sclass, boolean title, boolean details,
148  boolean summary, boolean body, String path, boolean asMail, String readAll,
149  IConstructor constructor) {
150  StringBuilder sb = new StringBuilder();
151  boolean firstOne = false;
152  for(Publication pub : publications) {
153  if(!firstOne) {
154  firstOne = true;
155  } else {
156  sb.append("<div class='divider'></div>");
157  }
158  writePublication(sb, pub, sclass, title, details, summary, body, path, asMail, readAll, constructor);
159  }
160  return sb.toString();
161  }
162 
163  public static String getPublicationStr(String publication,
164  String sclass, IConstructor constructor) {
165  StringBuilder sb = new StringBuilder();
166  writeAsPublication(sb, publication, sclass, constructor);
167  return sb.toString();
168  }
169 
170  public static String getPublicationTemplateStr(String publication,
171  String sclass, IConstructor constructor) {
172  StringBuilder sb = new StringBuilder();
173  writeAsPublication(sb, publication, sclass, constructor);
174  return sb.toString();
175  }
176 
177  private static void writeAsPublication(StringBuilder sb, String publication,
178  String sclass, IConstructor constructor) {
179  Publication pub = new Publication();
180  pub.setSummary(publication);
181  writePublication(sb, pub, sclass, false, false, true, false, null, false, null, constructor);
182  }
183 
184  private static void writePublication(StringBuilder sb, Publication pub,
185  String sclass, boolean title, boolean details,
186  boolean summary, boolean body, String path, boolean asMail, String readAll,
187  IConstructor constructor) {
188  sb.append("<div class='publication" +
189  (Strings.isBlank(sclass) ? "" : " " + sclass) +
190  "'>");
191 
192  if(title) {
193  sb.append("<h1 class='title'>");
194  if(asMail && path != null) {
195  sb.append(MailContact.createLink(pub.getTitle(), path + "?pubid=" + pub.getId() + "&item=" + pub.getId(), false));
196  } else {
197  if(path != null) {
198  sb.append("<a href='" + path + "?pubid=" + pub.getId() + "&item=" + pub.getId() + "'>");
199  }
200  sb.append(pub.getTitle());
201  if(path != null) {
202  sb.append("</a>");
203  }
204  }
205  sb.append("</h1>");
206  }
207 
208  if(details && pub.getPublicationGroup().isSomeDetailToShow()) {
209  sb.append("<div class='details'>");
210  if(pub.getPublicationGroup().isShowCategory()) {
211  sb.append("<span class='category'>" + pub.getPublicationCategory().getName() + "</span>");
212  }
213  if(pub.getPublicationGroup().isShowDate()) {
214  sb.append("<span class='date'>" + pub.getDateStr() + "</span>");
215  }
216  if(pub.getPublicationGroup().isShowAuthor()) {
217  sb.append("<span class='author'>" + pub.getAuthor() + "</span>");
218  }
219  if(pub.getPublicationGroup().isShowReads()) {
220  sb.append("<span class='reads'>" + pub.getReadings() + "</span>");
221  }
222  sb.append("</div>");
223  }
224 
225  if(summary && !Strings.isBlank(pub.getSummary())) sb.append("<div class='summary'>" + processOutput(constructor, pub.getSummary()) + "</div>");
226 
227  if(body && !Strings.isBlank(pub.getBody())) sb.append("<div class='body'>" + processOutput(constructor, pub.getBody()) + "</div>");
228 
229  if(!body && path != null && readAll != null) {
230  sb.append("<div class='readallpub'><a href='" + path + "?pubid=" + pub.getId() + "&item=" + pub.getId() + "'>");
231  sb.append(readAll);
232  sb.append("</a>");
233  }
234 
235  sb.append("</div>");
236  }
237 
238  private static void writePublication(String template, StringBuilder sb, Publication pub,
239  String sclass, boolean title, boolean details,
240  boolean summary, boolean body, String path, boolean asMail, String readAll,
241  IConstructor constructor) {
242 
243  if(asMail && path != null) {
244  template = template.replaceAll("#title", Parser.escapeGrouping(MailContact.createLink(pub.getTitle(),
245  path + "?pubid=" + pub.getId() + "&item=" + pub.getId(), false)));
246  } else {
247  if(path != null) {
248  template = template.replaceAll("#path", path + "?pubid=" + pub.getId() + "&item=" + pub.getId());
249  }
250  template = template.replaceAll("#title", Parser.escapeGrouping(pub.getTitle()));
251  }
252 
253  if(details && pub.getPublicationGroup().isSomeDetailToShow()) {
254  template = template.replaceAll("#category", Parser.escapeGrouping(pub.getPublicationCategory().getName()));
255  if(pub.getPublicationGroup().isShowDate()) {
256  template = template.replaceAll("#date", pub.getDateStr());
257  }
258  if(pub.getPublicationGroup().isShowAuthor()) {
259  template = template.replaceAll("#author", Parser.escapeGrouping(pub.getAuthor()));
260  }
261  if(pub.getPublicationGroup().isShowReads()) {
262  template = template.replaceAll("#readings", pub.getReadings() + "");
263  }
264  }
265 
266  if(summary && !Strings.isBlank(pub.getSummary())) {
267  template = template.replaceAll("#summary", Parser.escapeGrouping(processOutput(constructor, pub.getSummary())));
268  }
269 
270  if(body && !Strings.isBlank(pub.getBody())) {
271  template = template.replaceAll("#body", Parser.escapeGrouping(processOutput(constructor, pub.getBody())));
272  }
273 
274  template = template.replaceAll("#title", "");
275  template = template.replaceAll("#path", "");
276  template = template.replaceAll("#category", "");
277  template = template.replaceAll("#date", "");
278  template = template.replaceAll("#author", "");
279  template = template.replaceAll("#readings", "");
280  template = template.replaceAll("#comments", "");
281  template = template.replaceAll("#summary", "");
282  template = template.replaceAll("#body", "");
283 
284  sb.append(template);
285 
286  }
287 
288  private void addDefaults() {
289  appendChild(new Style("/_internal/css/default.css"));
290  appendChild(new Style("/_internal/css/default-site.css"));
291  appendChild(new Style("/_internal/css/publication.css"));
292  appendChild(new Style("/_internal/css/publication-site.css"));
293  html = new Html();
294  html.setWidth("100%");
295  html.setHeight("100%");
296  appendChild(html);
297  setContentStyle("overflow:auto");
298  }
299 
300  private static String processOutput(IConstructor constructor, String string) {
301  try {
302  StringWriter sw = new StringWriter();
303  PrintWriter pw = new PrintWriter(sw);
304  constructor.processOutput(pw, string);
305  return sw.toString();
306  } catch (ServletException | IOException ex) {
307  Logger.getLogger(PublicationContent.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
308  }
309  return string;
310  }
311 
312 }
static List< Publication > getPublicationsBy(int max, long group, long category, String idContact, boolean accepted)
static String getPublicationTemplateStr(String template, Publication publication, String sclass, boolean title, boolean details, boolean summary, boolean body, String path, boolean asMail, String readAll, IConstructor constructor)
static String getPublicationsStr(List< Publication > publications, String sclass, boolean title, boolean details, boolean summary, boolean body, String path, boolean asMail, String readAll, IConstructor constructor)
void setPublications(List< Publication > publications)
static String getPublicationsTemplateStr(String template, List< Publication > publications, String sclass, boolean title, boolean details, boolean summary, boolean body, String path, boolean asMail, String readAll, IConstructor constructor)
static String getPublicationStr(Publication publication, String sclass, boolean title, boolean details, boolean summary, boolean body, String path, boolean asMail, String readAll, IConstructor constructor)
static String getPublicationTemplateStr(String publication, String sclass, IConstructor constructor)
static String getPublicationStr(String publication, String sclass, IConstructor constructor)