BrightSide Workbench Full Report + Source Code
ForumContentIterator.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2020 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.forum.content;
20 
21 import java.io.Writer;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.Collection;
25 import java.util.Date;
26 import org.turro.string.Strings;
27 import org.turro.action.IEntityCtrl;
28 import org.turro.elephant.context.ElephantContext;
29 import org.turro.elephant.context.IConstructor;
30 import org.turro.elephant.db.ElephantPU;
31 import org.turro.elephant.db.WhereClause;
32 import org.turro.elephant.entities.db.Topic;
33 import org.turro.elephant.impl.repository.RepositoryFile;
34 import org.turro.elephant.web.SocialNet;
35 import org.turro.forum.Forums;
36 import org.turro.forum.www.ForumCtrl;
37 import org.turro.jpa.content.EntityDaoContentIterator;
38 import org.turro.jpa.content.SeenStatusCheck;
39 import org.turro.jpa.search.DaoHtmlSearch;
40 import org.turro.jpa.search.DaoSearchKey;
41 import org.turro.marker.ElephantMarker;
42 import org.turro.participation.ParticipationReason;
43 import org.turro.plugin.contacts.IContact;
44 
49 public class ForumContentIterator extends EntityDaoContentIterator<Topic, Long> {
50 
51  private Date since = null;
52 
53  public ForumContentIterator(IConstructor constructor, Writer writer, IContact contact, boolean mail, String pubPath) {
54  super(new ElephantPU(), constructor, writer, contact, mail, pubPath);
55  }
56 
57  public void setSince(Date since) {
58  this.since = since;
59  }
60 
61  @Override
63  WhereClause wc = new WhereClause();
64  wc.addClause("select distinct topic from Topic as topic");
65  wc.addClause("left join Post as post");
66  wc.addClause("on post.uniquePath like concat(topic.uniquePath, '%')");
67  addCriteria(wc);
68  wc.addClause("order by topic.creation desc");
69  return wc;
70  }
71 
72  @Override
74  WhereClause wc = new WhereClause();
75  wc.addClause("select count(distinct topic) from Topic as topic");
76  wc.addClause("left join Post as post");
77  wc.addClause("on post.uniquePath like concat(topic.uniquePath, '%')");
78  addCriteria(wc);
79  return wc;
80  }
81 
82  @Override
83  protected void renderSummary(ElephantMarker marker, Topic e, int page) {
84  if(e != null) {
85  marker.put("topic", e);
86  prepareValues(e, page);
87  }
89  }
90 
91  @Override
92  protected void renderItem(ElephantMarker marker, Topic e, int page) {
93  marker.put("topic", e);
94  prepareValues(e, page);
97  }
98 
99  @Override
100  protected void renderForbidden(ElephantMarker marker, int page) {
101  marker.put("topic", null);
103  }
104 
105  @Override
106  protected String entityRoot() {
107  return "topic";
108  }
109 
110  @Override
111  protected Topic entity(Long value) {
112  WhereClause wc = new WhereClause();
113  wc.addClause("select distinct topic from Topic as topic");
114  wc.addClause("left join Post as post");
115  wc.addClause("on post.uniquePath like concat(topic.uniquePath, '%')");
116  addCriteria(wc);
117  wc.addClause("and topic.id = :id");
118  wc.addNamedValue("id", value);
119  return (Topic) dao.getSingleResultOrNull(wc);
120  }
121 
122  @Override
123  protected boolean canShow(Topic e) {
124  return super.canShow(e) && (isRestricted() || e.getEntity().canPublish(getContact()));
125  }
126 
127  private void addCriteria(WhereClause wc) {
128  wc.addClause("where 1=1");
129  if(isSearchOption()) {
131  if(dhs != null) {
132  DaoSearchKey dsk = dhs.get("search-value");
133  if(dsk != null) {
134  dsk.applyToQuery(wc, Arrays.asList("topic.text", "post.text"), false);
135  }
136  }
137  }
138  if(isRestricted()) {
139  wc.addClause("and (");
140  wc.addClause("topic.authorId = :idContact");
141  wc.addClause("or post.authorId = :idContact");
142  wc.addClause(")");
143  wc.addNamedValue("idContact", getContact().getId());
144  }
145  if(since != null) {
146  wc.addClause("and (");
147  wc.addClause("topic.creation >= :since");
148  wc.addClause("or post.creation >= :since");
149  wc.addClause(")");
150  wc.addNamedValue("since", since);
151  }
152  if(!getSeenStatus().equals(SeenStatusCheck.DONT_CHECK)) {
153  if(getSeenStatus().equals(SeenStatusCheck.CHECK_UNSEEN)) {
154  wc.addClause("and not exists(");
155  } else {
156  wc.addClause("and exists(");
157  }
158  wc.addClause("select ep.entityPath from EntityParticipation ep");
159  wc.addClause("where ep.entityPath = concat('/topic/', topic.id)");
160  wc.addClause("and ep.participatorPath = concat('/contact/', :participatorId)");
161  wc.addNamedValue("participatorId", getContact().getId());
162  wc.addClause("and ep.reason = :reason");
163  wc.addNamedValue("reason", ParticipationReason.REASON_SEEN);
164  wc.addClause(")");
165  }
166  }
167 
168  private void prepareValues(final Topic topic, int page) {
169  prepareControls(topic, page);
170  IEntityCtrl iec = new ForumCtrl();
171  iec.setConstructor(constructor);
172  iec.setEntityPath(ElephantPU.getObjectPath(topic));
173  marker.put("actions", iec);
174  }
175 
176  @Override
177  protected String title(Topic e) {
178  if(!Strings.isBlank(e.getText())) {
179  return Strings.truncateAndWarn(e.getText(), 100);
180  }
181  return null;
182  }
183 
184  @Override
185  protected Collection<String> metas(Topic e) {
186  ArrayList<RepositoryFile> files = new ArrayList<>();
187  String path = getItemLink(e);
188 // if(!SocialImageMap.hasImage(path)) {
189 // FileAttach fa = new FileAttach(StudentsPU.getObjectPath(e));
190 // Repository repository = fa.getPublishableRepository(constructor);
191 // files.addAll(repository.getRepositoryFiles("*_social.png,*_social.jpg"));
192 // files.addAll(repository.getRepositoryFiles("*.png,*.jpg"));
193 // }
194  SocialNet sn;
195  sn = new SocialNet(path, Strings.truncateAndWarn(e.getText(), 100), e.getText(), files);
196  return sn.getMetas();
197  }
198 
199  @Override
200  protected String getTemplateRoot() {
201  return isMail() ? "content/newsletter/sections/forums" : "forum";
202  }
203 
204  @Override
205  protected Object doVotesCtrl(Topic e) {
206  return null;
207  }
208 
209  @Override
210  protected Object doInterestCtrl(Topic e) {
211  return null;
212  }
213 
214  @Override
215  protected Object doCommentsCtrl(Topic e) {
216  return null;
217  }
218 
219  @Override
220  protected Object doAttachmentsCtrl(Topic e) {
221  return null;
222  }
223 
224  @Override
225  protected Object doFilesCtrl(Topic e) {
226  return null;
227  }
228 
229  @Override
230  protected Object doDescriptionsCtrl(Topic e) {
231  return null;
232  }
233 
234  @Override
235  protected Object doPollsCtrl(Topic e) {
236  return null;
237  }
238 
239  @Override
240  protected String getItemLink(Topic e) {
241  return doItemLink(e, e.getId(), true);
242  }
243 
244  @Override
245  protected String getReadAllLink() {
246  String path = getContextPath();
247  if(Strings.isBlank(path)) {
248  path = ElephantContext.getEntityWebContext("/forum");
249  }
250  if(Strings.isBlank(path)) {
251  return getRestrictedLink();
252  }
253  return path;
254  }
255 
256  @Override
257  protected String getRestrictedLink() {
258  return "/user/forums";
259  }
260 
261 }
static String getContextVariable(IConstructor constructor)
static String getEntityWebContext(String path)
void addNamedValue(String name, Object value)
static void markAsSeenFor(ITreeEntity entity, IContact contact)
Definition: Forums.java:61
ForumContentIterator(IConstructor constructor, Writer writer, IContact contact, boolean mail, String pubPath)
void renderForbidden(ElephantMarker marker, int page)
void renderSummary(ElephantMarker marker, Topic e, int page)
void renderItem(ElephantMarker marker, Topic e, int page)
Object getSingleResultOrNull(SqlClause sc)
Definition: Dao.java:419
String doItemLink(E entity, ID id, boolean obfuscated)
static DaoHtmlSearch getInstance(IConstructor constructor, String context)
boolean applyToQuery(WhereClause wc, List< String > fields, boolean withSynonyms)
void process(String rootTmpl, String tmpl)
Object put(Object key, Object value)
boolean canPublish(IContact contact)