BrightSide Workbench Full Report + Source Code
NewsletterDashboard.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.newsletter;
20 
21 import com.sun.syndication.feed.synd.SyndContent;
22 import com.sun.syndication.feed.synd.SyndContentImpl;
23 import com.sun.syndication.feed.synd.SyndEntry;
24 import com.sun.syndication.feed.synd.SyndEntryImpl;
25 import java.io.IOException;
26 import java.io.PrintWriter;
27 import java.util.ArrayList;
28 import java.util.List;
29 import javax.servlet.ServletException;
30 import org.turro.string.ObjectString;
31 import org.turro.string.Strings;
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.jpa.entity.EntityWebUrls;
43 import org.turro.jpa.search.DaoHtmlSearch;
44 import org.turro.publication.db.PublicationPU;
45 import org.turro.publication.entity.Publication;
46 import org.turro.publication.util.PublicationCategories;
47 import org.turro.publication.www.CategoryFilter;
48 import org.turro.publication.www.PublicationIterator;
49 
55 
56  protected String sclass, contactId, fullTemplate, summaryTemplate;
57  protected long groupId, categoryId, publicationId;
58  protected int count;
60 
62  super();
63  }
64 
65  @Override
66  public void loadData() throws ServletException, IOException {
67  sclass = getAttributes().getAttributeValue("attrib:sclass", "");
68  fullTemplate = getAttributes().getAttributeValue("attrib:fullTemplate", "");
69  summaryTemplate = getAttributes().getAttributeValue("attrib:summaryTemplate", "");
70  contactId = getAttributes().getAttributeValue("attrib:contactId", null);
71  groupId = getAttributes().getAttributeIntegerValue("attrib:groupId", 0);
72  categoryId = getAttributes().getAttributeIntegerValue("attrib:categoryId", 0);
73  publicationId = getAttributes().getAttributeIntegerValue("attrib:publicationId", 0);
74  count = getAttributes().getAttributeIntegerValue("attrib:count", 10);
75 
76  if(publicationId > 0) {
77  } else {
78  pi = new PublicationIterator(getConstructor(), getContext().getFullPath(), getId());
79  pi.setFullTemplate(fullTemplate);
80  pi.setSummaryTemplate(summaryTemplate);
81  pi.setPage(count);
82  pi.setContactId(contactId);
84  pi.setCategoryId(categoryId);
85  pi.load();
86  setTitle(pi.getTitle());
87  if(pi.isItem()) {
88  setMetas(pi.getMetas());
89  } else {
91  }
92  }
93  }
94 
95  @Override
96  public void startConstruction() throws ServletException, IOException {
97  PrintWriter out = getConstructor().getOut();
98  out.print(getStart());
99  out.print(getStartBody());
100 
101  long fullPub = (Long) ObjectString.parseNativeString(getConstructor().getParameter("pubid"), Long.class, true);
102 
103  if(publicationId > 0) {
104  Publication pub = new PublicationPU().find(Publication.class, publicationId);
105  out.print("");
106  } else {
107  CategoryFilter cf = null;
108  if(categoryId == 0) {
109  cf = new CategoryFilter(getConstructor(), getId());
110  cf.processParameters();
111  }
112  if(cf != null) {
114  pi.setSearchEngine(dhs);
115  pi.setCategoryFilter(cf);
116  }
117  pi.render();
118  }
119 
120  out.print(getEndBody());
121  out.print(getEnd());
122  }
123 
124  @Override
125  public String getStart() {
126  return "<div class='" + sclass + "publication-web'>";
127  }
128 
129  @Override
130  public String getEnd() {
131  return "</div>";
132  }
133 
134  /* ISearchable */
135 
136  @Override
137  public FoundList search(String value, boolean ignoreCase) {
138  FoundList fl = new FoundList();
139 
140  String extraSql =
141  "and (" + SearchFormatter.queryToSQL("publication.publicationCategory.name", value, ignoreCase) +
142  "or " + SearchFormatter.queryToSQL("publication.title", value, ignoreCase) +
143  "or " + SearchFormatter.queryToSQL("publication.summary", value, ignoreCase) +
144  "or " + SearchFormatter.queryToSQL("publication.body", value, ignoreCase) +
145  ")";
146 
147  List<Publication> list = PublicationCategories.getPublicationsBy(
148  0, groupId, categoryId, contactId, true, extraSql);
149 
150  for(Publication pub : list) {
151  SearchFormatter sf = new SearchFormatter(value, ignoreCase);
152  sf.processLine(pub.getTitle());
153  sf.processLine(pub.getPublicationCategory().getName());
154  sf.processLine(pub.getSummary());
155  sf.processLine(pub.getBody());
156  if(sf.wasFound()) {
157  fl.addItem(
158  getId() == null ? "" : getId(),
159  context.getPath(),
160  "<img src='" + ElephantContext.getRootResourcePath() + "/_internal/system/images/blog.gif'/>",
161  pub.getTitle(),
162  sf.getResult(),
163  (double)sf.getFound(),
164  value
165  );
166  }
167  }
168 
169  return fl;
170  }
171 
172  /* End of ISearchable */
173 
174  /* IRSSable */
175 
176  @Override
177  public List<SyndEntry> getSyndEntries() {
178  contactId = getAttributes().getAttributeValue("attrib:contactId", null);
179  groupId = getAttributes().getAttributeIntegerValue("attrib:groupId", 0);
180  categoryId = getAttributes().getAttributeIntegerValue("attrib:categoryId", 0);
181  SyndEntry entry;
182  SyndContent description;
183  List<SyndEntry> entries = new ArrayList<>();
184  int rsscount = this.getAttributes().getAttributeIntegerValue("attrib:rss_count", 10);
185  List<Publication> list = PublicationCategories.getPublicationsBy(
186  rsscount, groupId, categoryId, contactId, true);
187  String serverBase = ElephantContext.getServerBase("http");
188  for(Publication pub : list) {
189  entry = new SyndEntryImpl();
190  entry.setTitle(pub.getTitle());
191  entry.setLink(serverBase + context.getFullPath() + getItemLink(pub));
192  entry.setPublishedDate(pub.getDate());
193  entry.setAuthor(pub.getAuthor());
194  description = new SyndContentImpl();
195  description.setType("text/html");
196  String summary = pub.getSummary();
197  if(!Strings.isBlank(summary)) {
198  summary = summary.replaceAll("href=\\'(?![a-z]+:)(\\/?)\\/?([^\\']*)\\'", "href=\\'" + serverBase + "$1$2\\'")
199  .replaceAll("src=\\'(?![a-z]+:)(\\/?)\\/?([^\\']*)\\'", "src=\\'" + serverBase + "$1$2\\'")
200  .replaceAll("href=\\\"(?![a-z]+:)(\\/?)\\/?([^\\\"]*)\\\"", "href=\\\"" + serverBase + "$1$2\\\"")
201  .replaceAll("src=\\\"(?![a-z]+:)(\\/?)\\/?([^\\\"]*)\\\"", "src=\\\"" + serverBase + "$1$2\\\"");
202  description.setValue(summary);
203  }
204  entry.setDescription(description);
205  entries.add(entry);
206  }
207  return entries;
208  }
209 
210  private String getItemLink(Publication pub) {
211  String ewu = EntityWebUrls.getUrlFromEntity(pub);
212  if(ewu != null) {
213  return ewu;
214  } else {
215  return "?pubid=" + pub.getId() + "&item=" + pub.getId();
216  }
217  }
218 
219  /* End of IRSSable */
220 
221 }
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)
static String getUrlFromEntity(Object entity)
void setFullTemplate(String fullTemplate)
void setSearchEngine(DaoHtmlSearch searchEngine)
void setSummaryTemplate(String summaryTemplate)
static DaoHtmlSearch getInstance(IConstructor constructor, String context)
FoundList search(String value, boolean ignoreCase)
static List< Publication > getPublicationsBy(int max, long group, long category, String idContact, boolean accepted)
void setCategoryFilter(CategoryFilter categoryFilter)
void setMetas(Collection< String > metas)