BrightSide Workbench Full Report + Source Code
PublicationWeb.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.www;
19 
20 import com.sun.syndication.feed.synd.SyndContent;
21 import com.sun.syndication.feed.synd.SyndContentImpl;
22 import com.sun.syndication.feed.synd.SyndEntry;
23 import com.sun.syndication.feed.synd.SyndEntryImpl;
24 import java.io.IOException;
25 import java.io.PrintWriter;
26 import java.util.ArrayList;
27 import java.util.List;
28 import javax.servlet.ServletException;
29 import org.turro.string.ObjectString;
30 import org.turro.string.Strings;
31 import org.turro.collections.Tree;
32 import org.turro.elephant.context.ElephantContext;
33 import org.turro.elephant.context.IElement;
34 import org.turro.elephant.feed.IRSSable;
35 import org.turro.elephant.impl.abstracts.AbstractElement;
36 import org.turro.elephant.layout.IManageable;
37 import org.turro.elephant.layout.IRenderable;
38 import org.turro.elephant.search.FoundList;
39 import org.turro.elephant.search.ISearchable;
40 import org.turro.elephant.search.SearchFormatter;
41 import org.turro.elephant.security.IDefendable;
42 import org.turro.elephant.web.ElContext;
43 import org.turro.i18n.I_;
44 import org.turro.jpa.entity.EntityWebUrls;
45 import org.turro.jpa.search.DaoHtmlSearch;
46 import org.turro.publication.db.PublicationPU;
47 import org.turro.publication.entity.Publication;
48 import org.turro.publication.entity.PublicationCategory;
49 import org.turro.publication.util.PublicationCategories;
50 import org.turro.publication.zul.portal.PublicationContent;
51 
57 
58  protected String sclass, contactId, onTop, onBottom, fullTemplate, summaryTemplate;
59  protected long groupId, categoryId, publicationId;
60  protected int count;
61  protected boolean showTitle, showDetails, showSummary, showBody, navigate, blogger;
63 
64  public PublicationWeb() {
65  super();
66  }
67 
68  @Override
69  public void loadData() throws ServletException, IOException {
70  sclass = getAttributes().getAttributeValue("attrib:sclass", "");
71  fullTemplate = getAttributes().getAttributeValue("attrib:fullTemplate", "");
72  summaryTemplate = getAttributes().getAttributeValue("attrib:summaryTemplate", "");
73  contactId = getAttributes().getAttributeValue("attrib:contactId", null);
74  groupId = getAttributes().getAttributeIntegerValue("attrib:groupId", 0);
75  categoryId = getAttributes().getAttributeIntegerValue("attrib:categoryId", 0);
76  publicationId = getAttributes().getAttributeIntegerValue("attrib:publicationId", 0);
77  count = getAttributes().getAttributeIntegerValue("attrib:count", 10);
78  showTitle = getAttributes().getAttributeBooleanValue("attrib:showTitle", true);
79  showDetails = getAttributes().getAttributeBooleanValue("attrib:showDetails", true);
80  showSummary = getAttributes().getAttributeBooleanValue("attrib:showSummary", true);
81  showBody = getAttributes().getAttributeBooleanValue("attrib:showBody", false);
82  navigate = getAttributes().getAttributeBooleanValue("attrib:navigate", false);
83  onTop = getAttributes().getAttributeValue("attrib:onTop", null);
84  onBottom = getAttributes().getAttributeValue("attrib:onBottom", null);
85  blogger = getAttributes().getAttributeBooleanValue("attrib:blogger", false);
86 
87  if(navigate) {
88  } else if(publicationId > 0) {
89  } else {
90  pi = new PublicationIterator(getConstructor(), getContext().getFullPath(), getId());
91  pi.setFullTemplate(fullTemplate);
92  pi.setSummaryTemplate(summaryTemplate);
93  pi.setPage(count);
94  pi.setContactId(contactId);
96  pi.setCategoryId(categoryId);
97  pi.setBlogger(blogger);
98  pi.setLang("_" + I_.api().used().getLanguage());
99  pi.load();
100  setTitle(pi.getTitle());
101  if(pi.isItem()) {
102  setMetas(pi.getMetas());
103  } else {
104  setDefaultMetas();
105  }
106  }
107  }
108 
109  @Override
110  public void setRootCategories(ElContext root) {
111  Tree<PublicationCategory, String> tree = new Tree<PublicationCategory, String>() {
112  @Override
113  protected String getId(PublicationCategory entity) {
114  return entity.getName();
115  }
116 
117  @Override
118  protected String getParentId(PublicationCategory entity) {
119  return null;
120  }
121 
122  @Override
123  protected PublicationCategory getEntity(String id) {
124  return null;
125  }
126  };
127  tree.setPath("");
128  tree.addLeaf(null, I_.get("All categories"));
130  tree.addLeaf(pc, pc.getName());
131  }
132  root.generateCategoriesChildren(tree, (t) -> {
133  return "?category=" + (t.getValue() == null ? "0" : ((PublicationCategory) t.getValue()).getId());
134  });
135  }
136 
137  @Override
138  public void startConstruction() throws ServletException, IOException {
139  PrintWriter out = getConstructor().getOut();
140  out.print(getStart());
141  out.print(getStartBody());
142 
143  if(!Strings.isBlank(onTop)) {
145  }
146 
147  long fullPub = (Long) ObjectString.parseNativeString(getConstructor().getParameter("pubid"), Long.class, true);
148 
149  if(navigate) {
150  getConstructor().includeContent("/WEB-INF/_zul/www/publication-viewer.zul?groupId=" + groupId + "&categoryId=" + categoryId + "&fullPub=" + fullPub);
151  } else if(publicationId > 0) {
152  Publication pub = new PublicationPU().find(Publication.class, publicationId);
154  pub, sclass, showTitle, showDetails, showSummary, showBody, "", false, null, getConstructor()));
155  } else {
156  CategoryFilter cf = null;
157  if(categoryId == 0) {
158  cf = new CategoryFilter(getConstructor(), getId());
159  cf.setBlogger(blogger);
160  cf.processParameters();
161  }
162  if(cf != null) {
164  pi.setSearchEngine(dhs);
165  pi.setCategoryFilter(cf);
166  }
167  pi.render();
168  }
169 
170  if(!Strings.isBlank(onBottom)) {
171  getConstructor().includeContent(onBottom);
172  }
173 
174  out.print(getEndBody());
175  out.print(getEnd());
176  }
177 
178  @Override
179  public String getStart() {
180  return "<div class='" + sclass + "publication-web'>";
181  }
182 
183  @Override
184  public String getEnd() {
185  return "</div>";
186  }
187 
188  private boolean checkItFits(Publication pub) {
189  return pub != null &&
190  (publicationId == 0 || publicationId == pub.getId()) &&
191  (groupId == 0 || groupId == pub.getPublicationGroup().getId()) &&
192  (categoryId == 0 || categoryId == pub.getPublicationCategory().getId()) &&
193  (Strings.isBlank(contactId) || contactId.equals(pub.getIdContact()));
194  }
195 
196  /* ISearchable */
197 
198  @Override
199  public FoundList search(String value, boolean ignoreCase) {
200  FoundList fl = new FoundList();
201 
202  String extraSql =
203  "and (" + SearchFormatter.queryToSQL("publication.publicationCategory.name", value, ignoreCase) +
204  "or " + SearchFormatter.queryToSQL("publication.title", value, ignoreCase) +
205  "or " + SearchFormatter.queryToSQL("publication.summary", value, ignoreCase) +
206  "or " + SearchFormatter.queryToSQL("publication.body", value, ignoreCase) +
207  ")";
208 
209  List<Publication> list = PublicationCategories.getPublicationsBy(
210  0, groupId, categoryId, contactId, true, extraSql);
211 
212  for(Publication pub : list) {
213  SearchFormatter sf = new SearchFormatter(value, ignoreCase);
214  sf.processLine(pub.getTitle());
216  sf.processLine(pub.getSummary());
217  sf.processLine(pub.getBody());
218  if(sf.wasFound()) {
219  fl.addItem(
220  getId() == null ? "" : getId(),
221  context.getPath(),
222  "<img src='" + ElephantContext.getRootResourcePath() + "/_internal/system/images/blog.gif'/>",
223  pub.getTitle(),
224  sf.getResult(),
225  (double)sf.getFound(),
226  value
227  );
228  }
229  }
230 
231  return fl;
232  }
233 
234  /* End of ISearchable */
235 
236  /* IRSSable */
237 
238  @Override
239  public List<SyndEntry> getSyndEntries() {
240  contactId = getAttributes().getAttributeValue("attrib:contactId", null);
241  groupId = getAttributes().getAttributeIntegerValue("attrib:groupId", 0);
242  categoryId = getAttributes().getAttributeIntegerValue("attrib:categoryId", 0);
243  SyndEntry entry;
244  SyndContent description;
245  List<SyndEntry> entries = new ArrayList<>();
246  int rsscount = this.getAttributes().getAttributeIntegerValue("attrib:rss_count", 10);
247  List<Publication> list = PublicationCategories.getPublicationsBy(
248  rsscount, groupId, categoryId, contactId, true);
249  String serverBase = ElephantContext.getServerBase("http");
250  for(Publication pub : list) {
251  entry = new SyndEntryImpl();
252  entry.setTitle(pub.getTitle());
253  entry.setLink(serverBase + context.getFullPath() + getItemLink(pub));
254  entry.setPublishedDate(pub.getDate());
255  entry.setAuthor(pub.getAuthor());
256  description = new SyndContentImpl();
257  description.setType("text/html");
258  String summary = pub.getSummary();
259  if(!Strings.isBlank(summary)) {
260  summary = summary.replaceAll("href=\\'(?![a-z]+:)(\\/?)\\/?([^\\']*)\\'", "href=\\'" + serverBase + "$1$2\\'")
261  .replaceAll("src=\\'(?![a-z]+:)(\\/?)\\/?([^\\']*)\\'", "src=\\'" + serverBase + "$1$2\\'")
262  .replaceAll("href=\\\"(?![a-z]+:)(\\/?)\\/?([^\\\"]*)\\\"", "href=\\\"" + serverBase + "$1$2\\\"")
263  .replaceAll("src=\\\"(?![a-z]+:)(\\/?)\\/?([^\\\"]*)\\\"", "src=\\\"" + serverBase + "$1$2\\\"");
264  description.setValue(summary);
265  }
266  entry.setDescription(description);
267  entries.add(entry);
268  }
269  return entries;
270  }
271 
272  private String getItemLink(Publication pub) {
273  String ewu = EntityWebUrls.getUrlFromEntity(pub);
274  if(ewu != null) {
275  return ewu;
276  } else {
277  return "?pubid=" + pub.getId() + "&item=" + pub.getId();
278  }
279  }
280 
281  private void renderRSSSynd() throws IOException {
282  PrintWriter out = getConstructor().getOut();
283  out.print("<div class='rss_feed'>");
284  out.print("<a href='" + ElephantContext.getRootResourcePath() + "/print_" + context.getPath() + "?rss'>");
285  out.print("<img border='0' src='" + ElephantContext.getRootResourcePath() + "/_internal/system/images/64/feed.png'/>");
286  out.print("</a>");
287  out.print("</div>");
288  }
289 
290  /* End of IRSSable */
291 
292 }
static String getServerBase(String scheme)
static String getContextVariable(IConstructor constructor)
void addItem(String id, String link, String image, String title, String value, double similarity, String query)
Definition: FoundList.java:66
static String queryToSQL(String field, String query, boolean ignoreCase)
void generateCategoriesChildren(Tree tree, Function< Item, String > onParam)
Definition: ElContext.java:491
static String get(String msg)
Definition: I_.java:41
static I18nApiWrapper api()
Definition: I_.java:65
static String getUrlFromEntity(Object entity)
void setFullTemplate(String fullTemplate)
void setSearchEngine(DaoHtmlSearch searchEngine)
void setSummaryTemplate(String summaryTemplate)
static DaoHtmlSearch getInstance(IConstructor constructor, String context)
PublicationCategory getPublicationCategory()
static Collection< PublicationCategory > getPublicCategories()
static List< Publication > getPublicationsBy(int max, long group, long category, String idContact, boolean accepted)
void setCategoryFilter(CategoryFilter categoryFilter)
FoundList search(String value, boolean ignoreCase)
static String getPublicationStr(Publication publication, String sclass, boolean title, boolean details, boolean summary, boolean body, String path, boolean asMail, String readAll, IConstructor constructor)
void setMetas(Collection< String > metas)