BrightSide Workbench Full Report + Source Code
ConvocationContentIterator.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.contacts.content;
20 
21 import java.io.Writer;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import org.turro.string.Strings;
25 import org.turro.attach.www.AttachCtrl;
26 import org.turro.contacts.Convocation;
27 import org.turro.contacts.db.ContactsPU;
28 import org.turro.elephant.context.ElephantContext;
29 import org.turro.elephant.context.IConstructor;
30 import org.turro.elephant.db.WhereClause;
31 import org.turro.elephant.impl.repository.Repository;
32 import org.turro.elephant.impl.repository.RepositoryFile;
33 import org.turro.elephant.web.SocialImageMap;
34 import org.turro.elephant.web.SocialNet;
35 import org.turro.entities.Entities;
36 import org.turro.file.util.FileAttach;
37 import org.turro.jpa.content.EntityDaoContentIterator;
38 import org.turro.jpa.search.DaoHtmlSearch;
39 import org.turro.jpa.search.DaoSearchKey;
40 import org.turro.marker.ElephantMarker;
41 import org.turro.plugin.contacts.IContact;
42 import org.turro.polls.PollsCtrl;
43 import org.turro.www.commentit.CommentItCtrl;
44 import org.turro.www.convocation.ConvocationCtrl;
45 import org.turro.www.describeit.DescribeItCtrl;
46 import org.turro.www.starit.StarItCtrl;
47 import org.turro.www.voteit.VoteItCtrl;
48 
53 public class ConvocationContentIterator extends EntityDaoContentIterator<Convocation, String> {
54 
55  private boolean publishable;
56  private String mode;
57 
58  public ConvocationContentIterator(IConstructor constructor, Writer writer, IContact contact, boolean mail, String contextPath) {
59  super(new ContactsPU(), constructor, writer, contact, mail, contextPath);
60  }
61 
62  public boolean isPublishable() {
63  return publishable;
64  }
65 
66  public void setPublishable(boolean publishable) {
67  this.publishable = publishable;
68  }
69 
70  public String getMode() {
71  return mode;
72  }
73 
74  public void setMode(String mode) {
75  this.mode = mode;
76  }
77 
78  public boolean isAssistant(Convocation convocation, IContact contact) {
79  return convocation.isAssistant(contact);
80  }
81 
82  @Override
84  WhereClause wc = new WhereClause();
85  wc.addClause("select distinct convocation from Convocation as convocation");
86  wc.addClause("left outer join convocation.attendees attendee");
87  addCriteria(wc);
88  if("open".equals(mode)) {
89  wc.addClause("order by convocation.callDate");
90  } else {
91  wc.addClause("order by convocation.callDate desc");
92  }
93  return wc;
94  }
95 
96  @Override
98  WhereClause wc = new WhereClause();
99  wc.addClause("select count(distinct convocation) from Convocation as convocation");
100  wc.addClause("left outer join convocation.attendees attendee");
101  addCriteria(wc);
102  return wc;
103  }
104 
105  @Override
106  protected void renderSummary(ElephantMarker marker, Convocation e, int page) {
107  if(e != null) {
108  marker.put("convocation", e);
109  ConvocationCtrl cc = new ConvocationCtrl();
112  marker.put("actions", cc);
113  prepareValues(e, page);
114  }
116  }
117 
118  @Override
119  protected void renderItem(ElephantMarker marker, Convocation e, int page) {
120  marker.put("convocation", e);
121  ConvocationCtrl cc = new ConvocationCtrl();
124  marker.put("actions", cc);
125  prepareValues(e, page);
127  }
128 
129  @Override
130  protected String entityRoot() {
131  return "convocation";
132  }
133 
134  @Override
135  protected Convocation entity(String value) {
136  WhereClause wc = new WhereClause();
137  wc.addClause("select distinct convocation from Convocation as convocation");
138  wc.addClause("left outer join convocation.attendees attendee");
139  addCriteria(wc);
140  wc.addClause("and convocation.id = :id");
141  wc.addNamedValue("id", value);
143  }
144 
145  private void addCriteria(WhereClause wc) {
146  wc.addClause("where 1=1");
147  if(isSearchOption()) {
149  if(dhs != null) {
150  DaoSearchKey dsk = dhs.get("search-value");
151  if(dsk != null) {
152  String searchValue = dsk.getValue();
153  if(!Strings.isBlank(searchValue)) {
154  wc.addClause("and convocation.name like :search01");
155  wc.addNamedValue("search01", "%" + searchValue + "%");
156  }
157  }
158  }
159  }
160  if(isRestricted() && !getContact().getPermissions().isInRole("convocation:all")) {
161  wc.addClause("and ((");
162  wc.addClause("convocation.organizer.id = :idContact");
163  wc.addClause("or attendee.contact.id = :idContact");
164  wc.addClause(") or convocation.publishable = TRUE");
165  wc.addClause(")");
166  wc.addNamedValue("idContact", getContact().getId());
167  }
168  if(publishable) {
169  wc.addClause("and convocation.publishable = TRUE");
170  }
171  if("open".equals(mode)) {
172  wc.addClause("and convocation.endDate >= current_timestamp");
173  } else if("closed".equals(mode)) {
174  wc.addClause("and convocation.endDate < current_timestamp");
175  }
176  }
177 
178  @Override
179  protected boolean isValid(Convocation e) {
180  return super.isValid(e);
181  }
182 
183  private void prepareValues(final Convocation e, int page) {
184  prepareControls(e, page);
185  }
186 
187  @Override
188  protected String title(Convocation e) {
189  if(!Strings.isBlank(e.getName())) {
190  return e.getName();
191  }
192  return null;
193  }
194 
195  @Override
196  protected Collection<String> metas(Convocation e) {
197  ArrayList<RepositoryFile> files = new ArrayList<>();
198  String path = getItemLink(e);
199  if(!SocialImageMap.hasImage(path)) {
202  files.addAll(repository.getRepositoryFiles("*_social.png,*_social.jpg"));
203  files.addAll(repository.getRepositoryFiles("*.png,*.jpg"));
204  }
205  SocialNet sn;
206  sn = new SocialNet(path, e.getName(), e.getName(), files);
207  return sn.getMetas();
208  }
209 
210  @Override
211  protected String getTemplateRoot() {
212  return isMail() ? "content/newsletter/sections/convocations" : "convocation";
213  }
214 
215  @Override
216  protected Object doVotesCtrl(Convocation e) {
219  }
220 
221  @Override
222  protected Object doInterestCtrl(Convocation e) {
225  }
226 
227  @Override
228  protected Object doCommentsCtrl(Convocation e) {
231  }
232 
233  @Override
234  protected Object doAttachmentsCtrl(Convocation e) {
237  }
238 
239  @Override
240  protected Object doFilesCtrl(Convocation e) {
243  }
244 
245  @Override
246  protected Object doDescriptionsCtrl(Convocation e) {
249  }
250 
251  @Override
252  protected Object doPollsCtrl(Convocation e) {
255  }
256 
257  @Override
258  protected String getItemLink(Convocation e) {
259  return doItemLink(e, e.getId(), true);
260  }
261 
262  @Override
263  protected String getReadAllLink() {
264  String path = getContextPath();
265  if(Strings.isBlank(path)) {
266  path = ElephantContext.getEntityWebContext("/convocation");
267  }
268  if(Strings.isBlank(path)) {
269  return getRestrictedLink();
270  }
271  return path;
272  }
273 
274  @Override
275  protected String getRestrictedLink() {
276  return "/user/convocations";
277  }
278 
279 }
ConvocationContentIterator(IConstructor constructor, Writer writer, IContact contact, boolean mail, String contextPath)
void renderSummary(ElephantMarker marker, Convocation e, int page)
boolean isAssistant(Convocation convocation, IContact contact)
void renderItem(ElephantMarker marker, Convocation e, int page)
static String getObjectPath(Object object)
Definition: ContactsPU.java:68
static String getContextVariable(IConstructor constructor)
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
Object getSingleResultOrNull(SqlClause sc)
Definition: Dao.java:419
String doItemLink(E entity, ID id, boolean obfuscated)
static DaoHtmlSearch getInstance(IConstructor constructor, String context)
void process(String rootTmpl, String tmpl)
Object put(Object key, Object value)
Object configureCtrl(Object ctrl, IContact contact)