BrightSide Workbench Full Report + Source Code
PublicationIterator.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2013 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.www;
20 
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import org.turro.string.Strings;
24 import org.turro.auth.Authentication;
25 import org.turro.commentit.CommentItUtil;
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.navigation.DefaultNavigatorItem;
30 import org.turro.elephant.impl.repository.RepositoryFile;
31 import org.turro.elephant.web.SocialImageMap;
32 import org.turro.elephant.web.SocialNet;
33 import org.turro.file.util.FileAttach;
34 import org.turro.jpa.entity.EntityWebUrls;
35 import org.turro.jpa.iterator.DaoHtmlIterator;
36 import org.turro.jpa.search.DaoHtmlSearch;
37 import org.turro.jpa.search.DaoSearchKey;
38 import org.turro.mail.recipients.MailContact;
39 import org.turro.marker.ElephantMarker;
40 import org.turro.plugin.contacts.IContact;
41 import org.turro.plugin.contacts.SoftContact;
42 import org.turro.polls.PollsCtrl;
43 import org.turro.publication.db.PublicationPU;
44 import org.turro.publication.entity.Publication;
45 import org.turro.publication.entity.PublicationCategory;
46 import org.turro.tags.TagCloud;
47 import org.turro.www.commentit.CommentItCtrl;
48 import org.turro.www.starit.StarItCtrl;
49 
54 public class PublicationIterator extends DaoHtmlIterator<Publication, Long> {
55 
56  private String pubPath, contactId, actualParameters, uniqueId, lang;
57  private long groupId, categoryId;
58  private CategoryFilter categoryFilter;
59  private boolean blogger;
60 
61  public PublicationIterator(IConstructor constructor, String pubPath, String uniqueId) {
62  super(new PublicationPU(), new ElephantMarker(constructor));
63  this.pubPath = pubPath;
64  this.uniqueId = uniqueId;
65  }
66 
67  public void setContactId(String contactId) {
68  this.contactId = contactId;
69  }
70 
71  public void setGroupId(long groupId) {
72  this.groupId = groupId;
73  }
74 
75  public void setCategoryId(long categoryId) {
76  this.categoryId = categoryId;
77  }
78 
79  public void setLang(String lang) {
80  this.lang = lang;
81  }
82 
84  return categoryFilter;
85  }
86 
87  public void setCategoryFilter(CategoryFilter categoryFilter) {
88  this.categoryFilter = categoryFilter;
89  }
90 
91  public boolean isBlogger() {
92  return blogger;
93  }
94 
95  public void setBlogger(boolean blogger) {
96  this.blogger = blogger;
97  }
98 
99  public Collection<DefaultNavigatorItem> pubCategories() {
100  if(categoryFilter != null) {
101  return categoryFilter.pubCategories(getActualParameters(), groupId);
102  }
103  return null;
104  }
105 
106  public void renderCategories() {
107  if(categoryFilter != null) {
108  categoryFilter.renderCategories(getActualParameters(), groupId);
109  }
110  }
111 
112  @Override
113  protected String title(Publication e) {
114  return e.getTitle();
115  }
116 
117  @Override
118  protected Collection<String> metas(Publication e) {
119  ArrayList<RepositoryFile> files = new ArrayList<>();
120  String path = getItemLink(e, 0);
121  if(!SocialImageMap.hasImage(path)) {
123  files.addAll(fa.getPublishableRepository(constructor).getRepositoryFiles("/topmost", "*.png,*.jpg"));
124  files.addAll(fa.getPublishableRepository(constructor).getRepositoryFiles("/priorized", "*.png,*.jpg"));
125  files.addAll(fa.getPublishableRepository(constructor).getRepositoryFiles("/included", "*.png,*.jpg"));
126  files.addAll(fa.getPublishableRepository(constructor).getRepositoryFiles("/", "*.png,*.jpg"));
127  }
128  SocialNet sn = new SocialNet(path, e.getTitle(), e.getSummary(), files);
129  return sn.getMetas();
130  }
131 
132  @Override
134  WhereClause wc = new WhereClause();
135  wc.addClause("select distinct p from Publication as p");
136  addCriteria(wc);
137  wc.addClause("order by p.date desc");
138  return wc;
139  }
140 
141  @Override
143  WhereClause wc = new WhereClause();
144  wc.addClause("select count(p) from Publication as p");
145  addCriteria(wc);
146  return wc;
147  }
148 
149  public String getActualParameters() {
150  return Strings.isBlank(actualParameters) ? "?a=b" : actualParameters;
151  }
152 
153  public boolean isAnyBlogger() {
155  }
156 
157  @Override
158  protected void renderSummary(ElephantMarker marker, Publication e, int page) {
159  if(e != null) {
160  prepareValues(e, page, false);
161  }
162  marker.process("publication", getSummaryTemplate());
163  actualParameters = getPageLink(page);
164  }
165 
166  @Override
167  protected void renderItem(ElephantMarker marker, Publication e, int page) {
168 // if(!Strings.isBlank(e.getTitle())) {
169 // constructor.addOnLoadedJavaScript(
170 // "jQuery('title').html('" + e.getTitle().replace("'", "\\'") + "');"
171 // );
172 // }
173  prepareValues(e, page, false);
174  marker.process("publication", getFullTemplate());
175  e.updateReadings(dao);
176  actualParameters = getPageLink(page);
177  }
178 
179  @Override
180  protected String entityRoot() {
181  return "publication";
182  }
183 
184  @Override
185  protected Publication entity(Long value) {
186  return dao.find(Publication.class, value);
187  }
188 
189  @Override
190  protected Long getIdentifier() {
191  Long identifier = getIDFromURL("/publication");
192  if(identifier == null) {
193  identifier = super.getIdentifier();
194  }
195  return identifier;
196  }
197 
198  private void addCriteria(WhereClause wc) {
199  if(blogger) {
200  wc.addClause("where p.accepted = false");
201  IContact contact = Authentication.getIContact();
202  if(contact != null && contact.isWebUser()) {
203  wc.addClause("and ( exists (");
204  wc.addClause("select ps from PublicationBlogger ps");
205  wc.addClause("where ps.publicationCategory = p.publicationCategory");
206  wc.addClause("and ps.idContact = :blogger");
207  wc.addClause("and ps.administrator = TRUE");
208  wc.addClause(") or ( p.idContact = :blogger and exists (");
209  wc.addClause("select ps from PublicationBlogger ps");
210  wc.addClause("where ps.publicationCategory = p.publicationCategory");
211  wc.addClause("and ps.idContact = p.idContact");
212  wc.addClause("and ps.administrator = FALSE");
213  wc.addClause(")))");
214  wc.addNamedValue("blogger", contact.getId());
215  } else {
216  wc.addClause("and 1=2");
217  }
218  } else {
219  wc.addClause("where p.accepted = true");
220  }
221  if(!Strings.isBlank(pubPath) && isSearchOption()) {
222  DaoHtmlSearch dhs = DaoHtmlSearch.getInstance(constructor, ElephantContext.getContextVariable(constructor));
223  if(dhs != null) {
224  DaoSearchKey dsk = dhs.get("search-value");
225  if(dsk != null) {
226  String searchValue = dsk.getValue();
227  if(!Strings.isBlank(searchValue)) {
228  wc.addClause("and (p.title like :search01");
229  wc.addNamedValue("search01", "%" + searchValue + "%");
230  wc.addClause("or p.summary like :search02)");
231  wc.addNamedValue("search02", "%" + searchValue + "%");
232  }
233  }
234  }
235  }
236  if(groupId != 0) {
237  wc.addClause("and p.publicationGroup.id = :group");
238  wc.addNamedValue("group", groupId);
239  } else {
240  wc.addClause("and p.publicationGroup.publishable = TRUE");
241  }
242  if(categoryId != 0) {
243  wc.addClause("and p.publicationCategory.id = :category");
244  wc.addNamedValue("category", categoryId);
245  } else if(!blogger) {
246  wc.addClause("and p.publicationCategory.publishable = TRUE");
247  }
248  if(!Strings.isBlank(contactId)) {
249  wc.addClause("and p.idContact = :idContact");
250  wc.addNamedValue("idContact", contactId);
251  }
252  if(!Strings.isBlank(lang)) {
253  wc.addClause("and (p.lang is null or p.lang = :lang)");
254  wc.addNamedValue("lang", lang);
255  }
256  PublicationCategory pc = CategoryFilter.getCategory(constructor, uniqueId);
257  if(pc != null) {
258  wc.addClause("and p.publicationCategory = :selectedCat");
259  wc.addNamedValue("selectedCat", pc);
260  }
261  if(TagCloud.hasSelected(constructor, "publication")) {
262  wc.addIn("and", "p.id", TagCloud.getIdentifiersAsLong(constructor, "publication"));
263  }
264  }
265 
266  private void prepareValues(Publication pub, int page, boolean asMail) {
267 
268  marker.put("publication", pub);
269 
270  if(asMail && pubPath != null) {
271  marker.put("title", MailContact.createLink(pub.getTitle(), getItemLink(pub, page), false));
272  } else {
273  if(pubPath != null) {
274  marker.put("path", getItemLink(pub, page));
275  marker.put("all", getPageLink(page));
276  }
277  marker.put("title", pub.getTitle());
278  }
279 
280  if(pub.getPublicationGroup().isSomeDetailToShow()) {
281  if(pub.getPublicationGroup().isAllowComments() && pub.getId() != null) {
282  IContact contact = Authentication.getIContact();
283  SoftContact scontact = new SoftContact();
284  scontact.contact = contact;
285  scontact.author_ip = constructor.getRequest().getRemoteAddr();
286  marker.put("comments", CommentItUtil.count(pub, scontact) + "");
287  }
288  }
289 
290  if(pub.getPublicationGroup().isAllowInterest()) {
291  StarItCtrl sic = new StarItCtrl(constructor);
292  sic.setEntityPath(PublicationPU.getObjectPath(pub));
293  marker.put("sic", sic);
294  }
295  if(pub.getPublicationGroup().isAllowComments()) {
296  CommentItCtrl cic = new CommentItCtrl(constructor);
297  cic.setEntityPath(PublicationPU.getObjectPath(pub));
298  marker.put("cic", cic);
299  }
300  PollsCtrl pollc = new PollsCtrl(constructor);
301  pollc.setEntityPath(PublicationPU.getObjectPath(pub));
302  marker.put("pollc", pollc);
303 
304  marker.put("readall", getItemLink(pub, page));
305 
306  marker.put("files", new FileAttach(PublicationPU.getObjectPath(pub)));
307 
308  }
309 
310  private String getItemLink(Publication pub, int page) {
311  String ewu = EntityWebUrls.getUrlFromEntity(pub);
312  if(ewu != null) {
313  return pubPath + ewu;
314  } else {
315  return pubPath + "?item=" + pub.getId();
316  }
317  }
318 
319  private String getPageLink(int page) {
320  return pubPath + "?page=" + page;
321  }
322 
323 }
void addIn(String operator, String field, List values)
void addNamedValue(String name, Object value)
static boolean hasImage(String url)
Repository getPublishableRepository(IConstructor constructor)
Definition: FileAttach.java:47
void process(String rootTmpl, String tmpl)
Object put(Object key, Object value)
static String getObjectPath(Object object)
Collection< DefaultNavigatorItem > pubCategories(String parameters, long groupId)
void renderCategories(String parameters, long groupId)
Collection< String > metas(Publication e)
void renderItem(ElephantMarker marker, Publication e, int page)
PublicationIterator(IConstructor constructor, String pubPath, String uniqueId)
Collection< DefaultNavigatorItem > pubCategories()
void renderSummary(ElephantMarker marker, Publication e, int page)
void setCategoryFilter(CategoryFilter categoryFilter)