BrightSide Workbench Full Report + Source Code
PublicationContentIterator.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.content;
20 
21 import java.io.Writer;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Date;
25 import org.turro.string.Strings;
26 import org.turro.elephant.context.ElephantContext;
27 import org.turro.elephant.context.IConstructor;
28 import org.turro.elephant.db.WhereClause;
29 import org.turro.elephant.impl.repository.RepositoryFile;
30 import org.turro.elephant.web.SocialImageMap;
31 import org.turro.elephant.web.SocialNet;
32 import org.turro.entities.Entities;
33 import org.turro.file.util.FileAttach;
34 import org.turro.jpa.content.EntityDaoContentIterator;
35 import org.turro.jpa.search.DaoHtmlSearch;
36 import org.turro.jpa.search.DaoSearchKey;
37 import org.turro.marker.ElephantMarker;
38 import org.turro.plugin.contacts.IContact;
39 import org.turro.polls.PollsCtrl;
40 import org.turro.publication.db.PublicationPU;
41 import org.turro.publication.entity.Publication;
42 import org.turro.publication.entity.PublicationCategory;
43 import org.turro.publication.www.CategoryFilter;
44 import org.turro.www.commentit.CommentItCtrl;
45 import org.turro.www.starit.StarItCtrl;
46 
51 public class PublicationContentIterator extends EntityDaoContentIterator<Publication, Long> {
52 
53  private String contactId, lang;
54  private long groupId, categoryId;
55  private CategoryFilter categoryFilter;
56  private boolean blogger, notSent, future;
57 
58  public PublicationContentIterator(IConstructor constructor, Writer writer, IContact contact, boolean mail, String pubPath) {
59  super(new PublicationPU(), constructor, writer, contact, mail, pubPath);
60  }
61 
62  public void setContactId(String contactId) {
63  this.contactId = contactId;
64  }
65 
66  public void setGroupId(long groupId) {
67  this.groupId = groupId;
68  }
69 
70  public void setCategoryId(long categoryId) {
71  this.categoryId = categoryId;
72  }
73 
74  public void setLang(String lang) {
75  this.lang = lang;
76  }
77 
79  return categoryFilter;
80  }
81 
82  public void setCategoryFilter(CategoryFilter categoryFilter) {
83  this.categoryFilter = categoryFilter;
84  }
85 
86  public boolean isBlogger() {
87  return blogger;
88  }
89 
90  public void setBlogger(boolean blogger) {
91  this.blogger = blogger;
92  }
93 
94  public boolean isNotSent() {
95  return notSent;
96  }
97 
98  public void setNotSent(boolean notSent) {
99  this.notSent = notSent;
100  }
101 
102  public boolean isFuture() {
103  return future;
104  }
105 
106  public void setFuture(boolean future) {
107  this.future = future;
108  }
109 
110  @Override
111  protected String title(Publication e) {
112  return e.getTitle();
113  }
114 
115  @Override
116  protected Collection<String> metas(Publication e) {
117  ArrayList<RepositoryFile> files = new ArrayList<>();
118  String path = getItemLink(e);
119  if(!SocialImageMap.hasImage(path)) {
121  files.addAll(fa.getPublishableRepository(constructor).getRepositoryFiles("/topmost", "*.png,*.jpg"));
122  files.addAll(fa.getPublishableRepository(constructor).getRepositoryFiles("/priorized", "*.png,*.jpg"));
123  files.addAll(fa.getPublishableRepository(constructor).getRepositoryFiles("/included", "*.png,*.jpg"));
124  files.addAll(fa.getPublishableRepository(constructor).getRepositoryFiles("/", "*.png,*.jpg"));
125  }
126  SocialNet sn = new SocialNet(path, e.getTitle(), e.getSummary(), files);
127  return sn.getMetas();
128  }
129 
130  @Override
132  WhereClause wc = new WhereClause();
133  wc.addClause("select distinct p from Publication as p");
134  addCriteria(wc);
135  wc.addClause("order by p.date desc");
136  return wc;
137  }
138 
139  @Override
141  WhereClause wc = new WhereClause();
142  wc.addClause("select count(p) from Publication as p");
143  addCriteria(wc);
144  return wc;
145  }
146 
147  public boolean isAnyBlogger() {
149  }
150 
151  @Override
152  protected void renderSummary(ElephantMarker marker, Publication e, int page) {
153  if(e != null) {
154  prepareValues(e, page);
155  }
157  }
158 
159  @Override
160  protected void renderItem(ElephantMarker marker, Publication e, int page) {
161  prepareValues(e, page);
163  e.updateReadings(dao);
164  }
165 
166  @Override
167  protected String entityRoot() {
168  return "publication";
169  }
170 
171  @Override
172  protected Publication entity(Long value) {
173  return dao.find(Publication.class, value);
174  }
175 
176  @Override
177  protected Long getIdentifier() {
178  Long identifier = getIDFromURL("/publication");
179  if(identifier == null) {
180  identifier = super.getIdentifier();
181  }
182  return identifier;
183  }
184 
185  private void addCriteria(WhereClause wc) {
186  if(blogger) {
187  wc.addClause("where p.accepted = false");
188  if(getContact() != null && getContact().isWebUser()) {
189  wc.addClause("and ( exists (");
190  wc.addClause("select ps from PublicationBlogger ps");
191  wc.addClause("where ps.publicationCategory = p.publicationCategory");
192  wc.addClause("and ps.idContact = :blogger");
193  wc.addClause("and ps.administrator = TRUE");
194  wc.addClause(") or ( p.idContact = :blogger and exists (");
195  wc.addClause("select ps from PublicationBlogger ps");
196  wc.addClause("where ps.publicationCategory = p.publicationCategory");
197  wc.addClause("and ps.idContact = p.idContact");
198  wc.addClause("and ps.administrator = FALSE");
199  wc.addClause(")))");
200  wc.addNamedValue("blogger", getContact().getId());
201  } else {
202  wc.addClause("and 1=2");
203  }
204  } else {
205  wc.addClause("where p.accepted = true");
206  }
207  if(!Strings.isBlank(getContextPath()) && isSearchOption()) {
208  DaoHtmlSearch dhs = DaoHtmlSearch.getInstance(constructor, ElephantContext.getContextVariable(constructor));
209  if(dhs != null) {
210  DaoSearchKey dsk = dhs.get("search-value");
211  if(dsk != null) {
212  String searchValue = dsk.getValue();
213  if(!Strings.isBlank(searchValue)) {
214  wc.addClause("and (p.title like :search01");
215  wc.addNamedValue("search01", "%" + searchValue + "%");
216  wc.addClause("or p.summary like :search02)");
217  wc.addNamedValue("search02", "%" + searchValue + "%");
218  }
219  }
220  }
221  }
222  if(notSent) {
223  wc.addClause("and p.sent = FALSE");
224  wc.addClause("and (p.retainTill is null or p.retainTill <= :now)");
225  wc.addNamedValue("now", new Date());
226  }
227  if(future) {
228  wc.addClause("and p.date > :future");
229  wc.addNamedValue("future", new Date());
230  }
231  if(groupId != 0) {
232  wc.addClause("and p.publicationGroup.id = :group");
233  wc.addNamedValue("group", groupId);
234  } else {
235  wc.addClause("and p.publicationGroup.publishable = TRUE");
236  }
237  if(categoryId != 0) {
238  wc.addClause("and p.publicationCategory.id = :category");
239  wc.addNamedValue("category", categoryId);
240  } else {
241  wc.addClause("and p.publicationCategory.publishable = TRUE");
242  }
243  if(!Strings.isBlank(contactId)) {
244  wc.addClause("and p.idContact = :idContact");
245  wc.addNamedValue("idContact", contactId);
246  }
247  if(!Strings.isBlank(lang)) {
248  wc.addClause("and (p.lang is null or p.lang = :lang)");
249  wc.addNamedValue("lang", lang);
250  }
251  PublicationCategory pc = CategoryFilter.getCategory(constructor, getId());
252  if(pc != null) {
253  wc.addClause("and p.publicationCategory = :selectedCat");
254  wc.addNamedValue("selectedCat", pc);
255  }
256  }
257 
258  private void prepareValues(Publication pub, int page) {
259  prepareControls(pub, page);
260 
261  marker.put("publication", pub);
262 
263  setAllowInterest(blogger);
264  setAllowComments(pub.getPublicationGroup().isAllowComments());
265  }
266 
267  @Override
268  protected String getTemplateRoot() {
269  return isMail() ? "content/newsletter/sections/publications" : "publication";
270  }
271 
272  @Override
273  protected Object doVotesCtrl(Publication e) {
274  throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
275  }
276 
277  @Override
278  protected Object doInterestCtrl(Publication e) {
281  }
282 
283  @Override
284  protected Object doCommentsCtrl(Publication e) {
287  }
288 
289  @Override
290  protected Object doAttachmentsCtrl(Publication e) {
291  throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
292  }
293 
294  @Override
295  protected Object doFilesCtrl(Publication e) {
298  }
299 
300  @Override
301  protected Object doDescriptionsCtrl(Publication e) {
302  throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
303  }
304 
305  @Override
306  protected Object doPollsCtrl(Publication e) {
309  }
310 
311  @Override
312  protected String getItemLink(Publication e) {
313  return doItemLink(e, e.getId(), true);
314  }
315 
316  @Override
317  protected String getReadAllLink() {
318  String path = getContextPath();
319  if(Strings.isBlank(path)) {
320  path = ElephantContext.getEntityWebContext("/publication");
321  }
322  return path;
323  }
324 
325  @Override
326  protected String getRestrictedLink() {
327  return null;
328  }
329 
330 }
static String getEntityWebContext(String path)
void addNamedValue(String name, Object value)
static boolean hasImage(String url)
static IElephantEntity getController(String path)
Definition: Entities.java:78
Repository getPublishableRepository(IConstructor constructor)
Definition: FileAttach.java:47
String doItemLink(E entity, ID id, boolean obfuscated)
void setAllowInterest(boolean allowInterest)
void setAllowComments(boolean allowComments)
void process(String rootTmpl, String tmpl)
Object put(Object key, Object value)
void renderItem(ElephantMarker marker, Publication e, int page)
void renderSummary(ElephantMarker marker, Publication e, int page)
PublicationContentIterator(IConstructor constructor, Writer writer, IContact contact, boolean mail, String pubPath)
static String getObjectPath(Object object)
Object configureCtrl(Object ctrl, IContact contact)