BrightSide Workbench Full Report + Source Code
ContactServiceContentIterator.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.Arrays;
24 import java.util.Collection;
25 import org.turro.string.Strings;
26 import org.turro.attach.www.AttachCtrl;
27 import org.turro.contacts.ContactService;
28 import org.turro.contacts.ContactServiceRole;
29 import org.turro.contacts.db.ContactsPU;
30 import org.turro.contacts.service.ServiceContext;
31 import org.turro.elephant.context.Application;
32 import org.turro.elephant.context.ElephantContext;
33 import org.turro.elephant.context.IConstructor;
34 import org.turro.elephant.db.WhereClause;
35 import org.turro.elephant.impl.repository.Repository;
36 import org.turro.elephant.impl.repository.RepositoryFile;
37 import org.turro.elephant.view.ViewMode;
38 import org.turro.elephant.web.SocialImageMap;
39 import org.turro.elephant.web.SocialNet;
40 import org.turro.entities.Entities;
41 import org.turro.file.util.FileAttach;
42 import org.turro.indicator.Statistics;
43 import org.turro.jpa.content.EntityDaoContentIterator;
44 import org.turro.jpa.search.DaoHtmlSearch;
45 import org.turro.jpa.search.DaoSearchKey;
46 import org.turro.marker.ElephantMarker;
47 import org.turro.plugin.contacts.IContact;
48 import org.turro.polls.PollsCtrl;
49 import org.turro.util.Enums;
50 import org.turro.www.commentit.CommentItCtrl;
51 import org.turro.www.describeit.DescribeItCtrl;
52 import org.turro.www.starit.StarItCtrl;
53 import org.turro.www.voteit.VoteItCtrl;
54 
59 public class ContactServiceContentIterator extends EntityDaoContentIterator<ContactService, String> {
60 
61  private boolean withDate, active, lastEntries;
62  private ContactServiceRole role;
63 
64  private String type;
65  private final boolean all, allowQuestion;
66 
67  public ContactServiceContentIterator(IConstructor constructor, Writer writer, IContact contact, boolean mail, String pubPath) {
68  super(new ContactsPU(), constructor, writer, contact, mail, pubPath);
69  all = Application.getApplication().isInRole("contact:all");
71  }
72 
73  public boolean isWithDate() {
74  return withDate;
75  }
76 
77  public void setWithDate(boolean withDate) {
78  this.withDate = withDate;
79  }
80 
81  public boolean isActive() {
82  return active;
83  }
84 
85  public void setActive(boolean active) {
86  this.active = active;
87  }
88 
89  public boolean isLastEntries() {
90  return lastEntries;
91  }
92 
93  public void setLastEntries(boolean lastEntries) {
94  this.lastEntries = lastEntries;
95  }
96 
98  return role;
99  }
100 
101  public void setRole(ContactServiceRole role) {
102  this.role = role;
103  }
104 
105  public String getType() {
106  return type;
107  }
108 
109  public void setType(String type) {
110  this.type = type;
111  }
112 
113  public boolean isInBusiness(ContactService service, IContact contact) {
114  return all || service.getContact().isInBusiness(contact) ||
115  (ServiceContext.getAllowSingle() && service.getContact().getId().equals(contact.getId()));
116  }
117 
118  @Override
119  protected void preload() {
120  super.preload();
122  if(dhs != null && !dhs.hasAttribute(constructor, "roles")) {
123  dhs.setAttribute(constructor, "mode", ViewMode.values());
124  dhs.setAttribute(constructor, "roles", ContactServiceRole.values());
125  dhs.setAttribute(constructor, "types", dao.getResultList("select distinct service.type from ContactService as service order by 1"));
126  dhs.setAttribute(constructor, "thematics", dao.getResultList("select distinct service.thematic from ContactService as service order by 1"));
128  }
129  }
130 
131  @Override
133  WhereClause wc = new WhereClause();
134  wc.addClause("select distinct service from ContactService as service");
135  if(withDate) {
136  addCriteria(wc);
137  wc.addClause("order by service.startDate desc, service.title");
138  } else if(lastEntries) {
139  addCriteria(wc);
140  wc.addClause("order by service.creation desc, service.title");
141  } else {
142  wc = Statistics.load(constructor, "ServiceRanking", "ServiceContactMatching", wc)
143  .allowMatching(isAllowMatching())
144  .allowRanking(isAllowRanking())
145  .doMatching(isDoMatching())
146  .setContact(getContact())
147  .setEntityFields("service", "id")
148  .setDefaultSorting("service.title")
149  .onCriteria(w -> addCriteria(w))
150  .getClause();
151  }
152  return wc;
153  }
154 
155  @Override
157  WhereClause wc = new WhereClause();
158  wc.addClause("select count(distinct service) from ContactService as service");
159  addCriteria(wc);
160  return wc;
161  }
162 
163  @Override
164  protected void renderSummary(ElephantMarker marker, ContactService e, int page) {
165  if(e != null) {
166  marker.put("service", e);
167  prepareValues(e, page);
168  }
170  }
171 
172  @Override
173  protected void renderItem(ElephantMarker marker, ContactService e, int page) {
174  marker.put("service", e);
175  prepareValues(e, page);
177  }
178 
179  @Override
180  protected String entityRoot() {
181  return "service";
182  }
183 
184  @Override
185  protected ContactService entity(String value) {
186  WhereClause wc = new WhereClause();
187  wc.addClause("select distinct service from ContactService as service");
188  addCriteria(wc);
189  wc.addClause("and service.id = :id");
190  wc.addNamedValue("id", value);
192  }
193 
194  private void addCriteria(WhereClause wc) {
195  wc.addClause("where 1=1");
196  if(isSearchOption()) {
198  if(dhs != null) {
199  DaoSearchKey dsk = dhs.get("search-value");
200  if(dsk != null) {
201  String searchValue = dsk.getValue();
202  if(!Strings.isBlank(searchValue)) {
203  dsk.applyToQuery(wc, Arrays.asList("service.title", "service.thematic", "service.contact.name"), true);
204  }
205  }
206  dsk = dhs.get("role");
207  if(dsk != null) {
208  role = Enums.valueOf(ContactServiceRole.class, dsk.getValue(), null);
209  }
210  dsk = dhs.get("type");
211  if(dsk != null) {
212  type = dsk.getValue();
213  }
214  dsk = dhs.get("actives");
215  if(dsk != null) {
216  active = "true".equals(dsk.getValue());
217  }
218  dsk = dhs.get("with-date");
219  if(dsk != null) {
220  withDate = "true".equals(dsk.getValue());
221  }
222  }
223  }
224  if(role != null) {
225  wc.addClause("and service.role = :role");
226  wc.addNamedValue("role", role);
227  }
228  if(!Strings.isBlank(type) && !"all".equals(type)) {
229  wc.addClause("and service.type = :type");
230  wc.addNamedValue("type", type);
231  }
232  if(withDate) {
233  wc.addClause("and service.startDate is not null");
234  }
235  if(active) {
236  wc.addClause("and (service.endDate is null or service.endDate >= current_date)");
237  }
238  if(isRestricted() && !all) {
239  wc.addClause("and (service.contact = :contact");
240  wc.addClause("or exists (");
241  wc.addClause("select r from BusinessRelation as r");
242  wc.addClause("where r.business = service.contact");
243  wc.addClause("and r.contact = :contact");
244  wc.addClause("))");
245  wc.addNamedValue("contact", getContact().getContact());
246  }
247  if(!isRestricted()) {
248  wc.addClause("and service.timesSent > 0");
249  }
250  }
251 
252  @Override
253  protected boolean isValid(ContactService e) {
254  return super.isValid(e);
255  }
256 
257  private void prepareValues(final ContactService service, int page) {
258  prepareControls(service, page);
259  marker.put("allowQuestion", allowQuestion);
260  }
261 
262  @Override
263  protected String title(ContactService e) {
264  if(!Strings.isBlank(e.getTitle())) {
265  return e.getTitle();
266  }
267  return null;
268  }
269 
270  @Override
271  protected Collection<String> metas(ContactService e) {
272  ArrayList<RepositoryFile> files = new ArrayList<>();
273  String path = getItemLink(e);
274  if(!SocialImageMap.hasImage(path)) {
277  files.addAll(repository.getRepositoryFiles("*_social.png,*_social.jpg"));
278  files.addAll(repository.getRepositoryFiles("*.png,*.jpg"));
279  }
280  SocialNet sn;
281  sn = new SocialNet(path, e.getTitle(), e.getText(), files);
282  return sn.getMetas();
283  }
284 
285  @Override
286  protected String getTemplateRoot() {
287  return isMail() ? "content/newsletter/sections/services" : "service";
288  }
289 
290  @Override
291  protected Object doVotesCtrl(ContactService e) {
294  }
295 
296  @Override
297  protected Object doInterestCtrl(ContactService e) {
300  }
301 
302  @Override
303  protected Object doCommentsCtrl(ContactService e) {
306  }
307 
308  @Override
309  protected Object doAttachmentsCtrl(ContactService e) {
312  }
313 
314  @Override
315  protected Object doFilesCtrl(ContactService e) {
318  }
319 
320  @Override
321  protected Object doDescriptionsCtrl(ContactService e) {
324  }
325 
326  @Override
327  protected Object doPollsCtrl(ContactService e) {
330  }
331 
332  @Override
333  protected String getItemLink(ContactService e) {
334  return doItemLink(e, e.getId(), true);
335  }
336 
337  @Override
338  protected String getReadAllLink() {
339  String path = getContextPath();
340  if(Strings.isBlank(path)) {
341  path = ElephantContext.getEntityWebContext("/service");
342  }
343  if(Strings.isBlank(path)) {
344  return getRestrictedLink();
345  }
346  return path;
347  }
348 
349  @Override
350  protected String getRestrictedLink() {
351  return "/user/services";
352  }
353 
354 }
boolean isInBusiness(Contact worker)
Definition: Contact.java:648
boolean isInBusiness(ContactService service, IContact contact)
void renderSummary(ElephantMarker marker, ContactService e, int page)
void renderItem(ElephantMarker marker, ContactService e, int page)
ContactServiceContentIterator(IConstructor constructor, Writer writer, IContact contact, boolean mail, String pubPath)
static String getObjectPath(Object object)
Definition: ContactsPU.java:68
static boolean getAllowQuestion(IConstructor constructor)
static boolean getAllowNew(IConstructor constructor)
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
static Statistics load(IConstructor constructor, String rankingInstance, String matchingInstance, WhereClause wc)
Object getSingleResultOrNull(SqlClause sc)
Definition: Dao.java:419
String doItemLink(E entity, ID id, boolean obfuscated)
void setAttribute(IConstructor constructor, String attribute, Object value)
static DaoHtmlSearch getInstance(IConstructor constructor, String context)
boolean hasAttribute(IConstructor constructor, String attribute)
boolean applyToQuery(WhereClause wc, List< String > fields, boolean withSynonyms)
void process(String rootTmpl, String tmpl)
Object put(Object key, Object value)
Object configureCtrl(Object ctrl, IContact contact)