BrightSide Workbench Full Report + Source Code
PracticalWorkContentIterator.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2021 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.students.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.List;
26 import java.util.stream.Collectors;
27 import org.turro.string.Strings;
28 import org.turro.attach.www.AttachCtrl;
29 import org.turro.contacts.db.ContactsPU;
30 import org.turro.contacts.profile.Profile;
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.web.SocialImageMap;
38 import org.turro.elephant.web.SocialNet;
39 import org.turro.entities.Entities;
40 import org.turro.file.util.FileAttach;
41 import org.turro.indicator.Statistics;
42 import org.turro.jobboard.PracticalWorkContext;
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.students.db.StudentsPU;
50 import org.turro.students.entities.PracticalWork;
51 import org.turro.students.entities.PracticalWorkRole;
52 import org.turro.util.Enums;
53 import org.turro.www.commentit.CommentItCtrl;
54 import org.turro.www.describeit.DescribeItCtrl;
55 import org.turro.www.starit.StarItCtrl;
56 import org.turro.www.voteit.VoteItCtrl;
57 
62 public class PracticalWorkContentIterator extends EntityDaoContentIterator<PracticalWork, Long> {
63 
64  private boolean withDate, active, lastEntries;
65  private PracticalWorkRole role;
66 
67  private String type;
68  private final boolean all, allowQuestion;
69 
70  public PracticalWorkContentIterator(IConstructor constructor, Writer writer, IContact contact, boolean mail, String pubPath) {
71  super(new StudentsPU(), constructor, writer, contact, mail, pubPath);
72  all = Application.getApplication().isInRole("contact:all");
74  }
75 
76  public boolean isWithDate() {
77  return withDate;
78  }
79 
80  public void setWithDate(boolean withDate) {
81  this.withDate = withDate;
82  }
83 
84  public boolean isActive() {
85  return active;
86  }
87 
88  public void setActive(boolean active) {
89  this.active = active;
90  }
91 
92  public boolean isLastEntries() {
93  return lastEntries;
94  }
95 
96  public void setLastEntries(boolean lastEntries) {
97  this.lastEntries = lastEntries;
98  }
99 
101  return role;
102  }
103 
104  public void setRole(PracticalWorkRole role) {
105  this.role = role;
106  }
107 
108  public String getType() {
109  return type;
110  }
111 
112  public void setType(String type) {
113  this.type = type;
114  }
115 
116  public boolean isInBusiness(PracticalWork practicalWork, IContact contact) {
117  return all || practicalWork.getIContact().isInBusiness(contact) ||
118  (PracticalWorkContext.getAllowSingle() && practicalWork.getIContact().getId().equals(contact.getId()));
119  }
120 
121  @Override
123  WhereClause wc = new WhereClause();
124  wc.addClause("select distinct practicalwork from PracticalWork as practicalwork");
125  if(withDate) {
126  addCriteria(wc);
127  wc.addClause("order by practicalwork.startDate desc, practicalwork.title");
128  } else if(lastEntries) {
129  addCriteria(wc);
130  wc.addClause("order by practicalwork.creation desc, practicalwork.title");
131  } else {
132  wc = Statistics.load(constructor, "PracticalWorkRanking", "PracticalWorkContactMatching", wc)
133  .allowMatching(isAllowMatching())
134  .allowRanking(isAllowRanking())
135  .doMatching(isDoMatching())
136  .setContact(getContact())
137  .setEntityFields("practicalwork", "id")
138  .setDefaultSorting("practicalwork.title")
139  .onCriteria(w -> addCriteria(w))
140  .getClause();
141  }
142  return wc;
143  }
144 
145  @Override
147  WhereClause wc = new WhereClause();
148  wc.addClause("select count(distinct practicalwork) from PracticalWork as practicalwork");
149  addCriteria(wc);
150  return wc;
151  }
152 
153  @Override
154  protected void renderSummary(ElephantMarker marker, PracticalWork e, int page) {
155  if(e != null) {
156  marker.put("practicalWork", e);
157  prepareValues(e, page);
158  }
160  }
161 
162  @Override
163  protected void renderItem(ElephantMarker marker, PracticalWork e, int page) {
164  marker.put("practicalWork", e);
165  prepareValues(e, page);
167  }
168 
169  @Override
170  protected String entityRoot() {
171  return "practicalwork";
172  }
173 
174  @Override
175  protected PracticalWork entity(Long value) {
176  WhereClause wc = new WhereClause();
177  wc.addClause("select distinct practicalwork from PracticalWork as practicalwork");
178  addCriteria(wc);
179  wc.addClause("and practicalwork.id = :id");
180  wc.addNamedValue("id", value);
182  }
183 
184  private void addCriteria(WhereClause wc) {
185  wc.addClause("where 1=1");
186  if(isSearchOption()) {
188  if(dhs != null) {
189  DaoSearchKey dsk = dhs.get("search-value");
190  if(dsk != null) {
191  String searchValue = dsk.getValue();
192  if(!Strings.isBlank(searchValue)) {
193  dsk.applyToQuery(wc, Arrays.asList("practicalwork.title"), true);
194  }
195  }
196  dsk = dhs.get("role");
197  if(dsk != null) {
198  role = Enums.valueOf(PracticalWorkRole.class, dsk.getValue(), null);
199  }
200  dsk = dhs.get("type");
201  if(dsk != null) {
202  type = dsk.getValue();
203  }
204  dsk = dhs.get("actives");
205  if(dsk != null) {
206  active = "true".equals(dsk.getValue());
207  }
208  dsk = dhs.get("with-date");
209  if(dsk != null) {
210  withDate = "true".equals(dsk.getValue());
211  }
212  }
213  }
214  if(role != null) {
215  wc.addClause("and practicalwork.role = :role");
216  wc.addNamedValue("role", role);
217  }
218  if(!Strings.isBlank(type) && !"all".equals(type)) {
219  wc.addClause("and practicalwork.type = :type");
220  wc.addNamedValue("type", type);
221  }
222  if(withDate) {
223  wc.addClause("and practicalwork.startDate is not null");
224  }
225  if(active) {
226  wc.addClause("and (practicalwork.endDate is null or practicalwork.endDate >= current_date)");
227  }
228  if(isRestricted() && !all) {
229  wc.addClause("and (practicalwork.idContact = :idContact");
230  wc.addIn("or", "practicalwork.idContact", getBusinessIds(getContact()));
231  wc.addClause(")");
232  wc.addNamedValue("idContact", getContact().getId());
233  }
234  if(!isRestricted()) {
235  wc.addClause("and practicalwork.published = TRUE");
236  }
237  }
238 
239  private List<String> getBusinessIds(IContact contact) {
240  Profile profile = (Profile) contact.getProfile();
241  return profile.getCurrentCompanies().stream()
242  .map(r -> r.getBusiness().getContact().getId()).collect(Collectors.toList());
243  }
244 
245  @Override
246  protected boolean isValid(PracticalWork e) {
247  return super.isValid(e);
248  }
249 
250  private void prepareValues(final PracticalWork practicalWork, int page) {
251  prepareControls(practicalWork, page);
252  marker.put("allowQuestion", allowQuestion);
253  }
254 
255  @Override
256  protected String title(PracticalWork e) {
257  if(!Strings.isBlank(e.getTitle())) {
258  return e.getTitle();
259  }
260  return null;
261  }
262 
263  @Override
264  protected Collection<String> metas(PracticalWork e) {
265  ArrayList<RepositoryFile> files = new ArrayList<>();
266  String path = getItemLink(e);
267  if(!SocialImageMap.hasImage(path)) {
270  files.addAll(repository.getRepositoryFiles("*_social.png,*_social.jpg"));
271  files.addAll(repository.getRepositoryFiles("*.png,*.jpg"));
272  }
273  SocialNet sn;
274  sn = new SocialNet(path, e.getTitle(), e.getText(), files);
275  return sn.getMetas();
276  }
277 
278  @Override
279  protected String getTemplateRoot() {
280  return isMail() ? "content/newsletter/sections/practicalworks" : "practicalwork";
281  }
282 
283  @Override
284  protected Object doVotesCtrl(PracticalWork e) {
287  }
288 
289  @Override
290  protected Object doInterestCtrl(PracticalWork e) {
293  }
294 
295  @Override
296  protected Object doCommentsCtrl(PracticalWork e) {
299  }
300 
301  @Override
302  protected Object doAttachmentsCtrl(PracticalWork e) {
305  }
306 
307  @Override
308  protected Object doFilesCtrl(PracticalWork e) {
311  }
312 
313  @Override
314  protected Object doDescriptionsCtrl(PracticalWork e) {
317  }
318 
319  @Override
320  protected Object doPollsCtrl(PracticalWork e) {
323  }
324 
325  @Override
326  protected String getItemLink(PracticalWork e) {
327  return doItemLink(e, e.getId(), true);
328  }
329 
330  @Override
331  protected String getReadAllLink() {
332  String path = getContextPath();
333  if(Strings.isBlank(path)) {
334  path = ElephantContext.getEntityWebContext("/practicalwork");
335  }
336  if(Strings.isBlank(path)) {
337  return getRestrictedLink();
338  }
339  return path;
340  }
341 
342  @Override
343  protected String getRestrictedLink() {
344  return "/user/mypracticalworks";
345  }
346 
347 }
static String getObjectPath(Object object)
Definition: ContactsPU.java:68
static String getContextVariable(IConstructor constructor)
static String getEntityWebContext(String path)
void addIn(String operator, String field, List values)
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)
static boolean getAllowQuestion(IConstructor constructor)
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)
PracticalWorkContentIterator(IConstructor constructor, Writer writer, IContact contact, boolean mail, String pubPath)
void renderItem(ElephantMarker marker, PracticalWork e, int page)
boolean isInBusiness(PracticalWork practicalWork, IContact contact)
void renderSummary(ElephantMarker marker, PracticalWork e, int page)
Object configureCtrl(Object ctrl, IContact contact)
boolean isInBusiness(IContact worker)