BrightSide Workbench Full Report + Source Code
IssueContentIterator.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2018 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.dossier.content;
20 
21 import java.io.Writer;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.List;
25 import org.turro.string.Strings;
26 import org.turro.attach.www.AttachCtrl;
27 import org.turro.dossier.db.DossierPU;
28 import org.turro.dossier.entity.Category;
29 import org.turro.dossier.entity.Dossier;
30 import org.turro.dossier.entity.DossierStatus;
31 import org.turro.dossier.entity.Issue;
32 import org.turro.dossier.entity.IssueResolution;
33 import org.turro.dossier.entity.IssueStatus;
34 import org.turro.dossier.entity.IssueType;
35 import org.turro.dossier.issue.IssueWrapper;
36 import org.turro.dossier.www.IssueOrdering;
37 import org.turro.elephant.context.IConstructor;
38 import org.turro.elephant.db.WhereClause;
39 import org.turro.elephant.impl.repository.Repository;
40 import org.turro.elephant.impl.repository.RepositoryFile;
41 import org.turro.elephant.web.SocialImageMap;
42 import org.turro.elephant.web.SocialNet;
43 import org.turro.entities.Entities;
44 import org.turro.file.util.FileAttach;
45 import org.turro.jpa.content.EntityDaoContentIterator;
46 import org.turro.marker.ElephantMarker;
47 import org.turro.plugin.contacts.IContact;
48 import org.turro.www.commentit.CommentItCtrl;
49 import org.turro.www.describeit.DescribeItCtrl;
50 import org.turro.www.starit.StarItCtrl;
51 import org.turro.www.voteit.VoteItCtrl;
52 
57 public class IssueContentIterator extends EntityDaoContentIterator<Issue, Long> {
58 
59  private long categoryId, dossierId, lastDosierId = -1, lastCategoryId = -1;
60  private List<String> issueTypes, issueStatus, dossierStatus;
61  private boolean showSubject, all;
63 
64  public IssueContentIterator(IConstructor constructor, Writer writer, IContact contact, boolean mail, String pubPath) {
65  super(new DossierPU(), constructor, writer, contact, mail, pubPath);
66  this.all = contact.getPermissions().isInRole("issue:all");
67  }
68 
69  public void setCategoryId(long categoryId) {
70  this.categoryId = categoryId;
71  }
72 
73  public void setDossierId(long dossierId) {
74  this.dossierId = dossierId;
75  }
76 
77  public void setDossierStatus(List<String> dossierStatus) {
78  this.dossierStatus = dossierStatus;
79  }
80 
81  public void setIssueTypes(List<String> issueTypes) {
82  this.issueTypes = issueTypes;
83  }
84 
85  public void setIssueStatus(List<String> issueStatus) {
86  this.issueStatus = issueStatus;
87  }
88 
89  public boolean isShowSubject() {
90  return showSubject;
91  }
92 
93  public void setShowSubject(boolean showSubject) {
94  this.showSubject = showSubject;
95  }
96 
98  return orderBy;
99  }
100 
101  public void setOrderBy(IssueOrdering orderBy) {
102  this.orderBy = orderBy;
103  }
104 
105  @Override
107  WhereClause wc = new WhereClause();
108  wc.addClause("select distinct i from Issue as i");
109  wc.addClause("join i.participants as p");
110  addCriteria(wc);
111  wc.addClause(IssueOrdering.ORDER_BY_ACTIVITY.getOrderBy());
112  return wc;
113  }
114 
115  @Override
117  WhereClause wc = new WhereClause();
118  wc.addClause("select count(distinct i) from Issue as i");
119  wc.addClause("join i.participants as p");
120  addCriteria(wc);
121  return wc;
122  }
123 
124  @Override
125  protected void renderSummary(ElephantMarker marker, Issue e, int page) {
126  if(e != null) {
127  marker.put("issue", e);
128  marker.put("newCategory", e.getDossier().getCategory().getId() != lastCategoryId);
129  marker.put("newDossier", e.getDossier().getId() != lastDosierId);
130  lastCategoryId = e.getDossier().getCategory().getId();
131  lastDosierId = e.getDossier().getId();
132  prepareValues(e, page);
133  marker.put("restricted", isRestricted());
134  }
136  }
137 
138  @Override
139  protected void renderItem(ElephantMarker marker, Issue e, int page) {
140  marker.put("issue", e);
141  prepareValues(e, page);
142  marker.put("restricted", isRestricted());
144  }
145 
146  @Override
147  protected String entityRoot() {
148  return "issue";
149  }
150 
151  @Override
152  protected Issue entity(Long value) {
153  WhereClause wc = new WhereClause();
154  wc.addClause("select distinct i from Issue as i");
155  wc.addClause("join i.participants as p");
156  addCriteria(wc);
157  wc.addClause("and i.id = :id");
158  wc.addNamedValue("id", value);
159  return (Issue) dao.getSingleResultOrNull(wc);
160  }
161 
162  private void addCriteria(WhereClause wc) {
163  wc.addClause("where i.publishable = TRUE");
164  if(categoryId > 0) {
165  wc.addClause("and i.dossier.category.id in (" + getCategories(categoryId) + ")");
166  } else if(dossierId > 0) {
167  wc.addClause("and i.dossier.id = " + dossierId);
168  }
169  if(dossierStatus != null && dossierStatus.size() > 0) {
170  wc.addClause("and (");
171  String sep = "";
172  for(String s : dossierStatus) {
173  DossierStatus ds = DossierStatus.valueOf(s);
174  if(ds != null) {
175  String par = "status" + wc.getUniqueSuffix();
176  wc.addClause(sep + "i.dossier.status = :" + par);
177  wc.addNamedValue(par, ds);
178  sep = " or ";
179  }
180  }
181  wc.addClause(")");
182  }
183  if(issueStatus != null && issueStatus.size() > 0) {
184  wc.addClause("and (");
185  String sep = "";
186  for(String s : issueStatus) {
187  IssueStatus is = IssueStatus.valueOf(s);
188  if(is != null) {
189  String par = "status" + wc.getUniqueSuffix();
190  wc.addClause(sep + "i.status = :" + par);
191  wc.addNamedValue(par, is);
192  sep = " or ";
193  }
194  }
195  wc.addClause(")");
196  }
197  if(issueTypes != null && issueTypes.size() > 0) {
198  wc.addClause("and (");
199  String sep = "";
200  for(String s : issueTypes) {
201  IssueType it = IssueType.valueOf(s);
202  if(it != null) {
203  String par = "type" + wc.getUniqueSuffix();
204  wc.addClause(sep + "i.type = :" + par);
205  wc.addNamedValue(par, it);
206  sep = " or ";
207  }
208  }
209  wc.addClause(")");
210  }
211  }
212 
213  @Override
214  protected boolean isValid(Issue e) {
215  if(isRestricted() && e != null) {
216  return all || new IssueWrapper(e).isFullParticipant();
217  } else {
218  return super.isValid(e);
219  }
220  }
221 
222  private String getCategories(long categoryId) {
223  String result = null;
224  Category category = dao.find(Category.class, Long.valueOf(categoryId));
225  return addCategory(category, result);
226  }
227 
228  private String addCategory(Category category, String result) {
229  if(category != null) {
230  result = (String) (((result == null) ? "" : result + ",") + category.getId());
231  for(Category c : category.getChildren()) {
232  result = addCategory(c, result);
233  }
234  }
235  return result;
236  }
237 
238  private void prepareValues(Issue issue, int page) {
239  prepareControls(issue, page);
240 
241  marker.put("issueClass", getIssueClass(issue));
242  Dossier dossier = issue.getDossier();
243  marker.put("dossierClass", "dossier" +
244  (dossier.getStatus().equals(DossierStatus.DOSSIER_OPENED) ? " openned" : "") +
245  (dossier.getStatus().equals(DossierStatus.DOSSIER_CLOSED) ? " closed" : "") +
246  (dossier.getStatus().equals(DossierStatus.DOSSIER_FROZEN) ? " frozen" : ""));
247 
248  }
249 
250  private String getIssueClass(Issue issue) {
251  return"issue" +
252  (issue.getStatus().equals(IssueStatus.STATUS_NEW) ? " new" : "") +
253  (issue.getStatus().equals(IssueStatus.STATUS_STARTED) ? " started" : "") +
254  (issue.getStatus().equals(IssueStatus.STATUS_INCOMPLETE) ? " incomplete" : "") +
255  (issue.getStatus().equals(IssueStatus.STATUS_REOPENED) ? " reopened" : "") +
256  (issue.getStatus().equals(IssueStatus.STATUS_REUNION) ? " reunion" : "") +
257  (issue.getStatus().equals(IssueStatus.STATUS_FROZEN) ? " frozen" : "") +
258  (issue.getStatus().equals(IssueStatus.STATUS_RESOLVED) ? " resolved" : "") +
259  (issue.getStatus().equals(IssueStatus.STATUS_VERIFIED) ? " verified" : "") +
260  (issue.getStatus().equals(IssueStatus.STATUS_CLOSED) ? " closed" : "") +
261 
262  (issue.getType().equals(IssueType.TYPE_DEFECT) ? " defect" : "") +
263  (issue.getType().equals(IssueType.TYPE_ENHANCEMENT) ? " enhancement" : "") +
264  (issue.getType().equals(IssueType.TYPE_FEATURE) ? " feature" : "") +
265  (issue.getType().equals(IssueType.TYPE_NONCONFORMITY) ? " nonconformity" : "") +
266  (issue.getType().equals(IssueType.TYPE_PATCH) ? " patch" : "") +
267  (issue.getType().equals(IssueType.TYPE_PREVENTION) ? " prevention" : "") +
268  (issue.getType().equals(IssueType.TYPE_QUESTION) ? " question" : "") +
269  (issue.getType().equals(IssueType.TYPE_TASK) ? " task" : "") +
270  (issue.getType().equals(IssueType.TYPE_DOCUMENT) ? " document" : "") +
271  (issue.getType().equals(IssueType.TYPE_BESTPRACTICE) ? " bestpractice" : "") +
272  (issue.getType().equals(IssueType.TYPE_PROCESS) ? " process" : "") +
273  (issue.getType().equals(IssueType.TYPE_PROPOSITION) ? " proposition" : "") +
274 
275  (issue.getResolution().equals(IssueResolution.RESOLUTION_DUPLICATED) ? " duplicated" : "") +
276  (issue.getResolution().equals(IssueResolution.RESOLUTION_FIXED) ? " fixed" : "") +
277  (issue.getResolution().equals(IssueResolution.RESOLUTION_INVALID) ? " invalid" : "") +
278  (issue.getResolution().equals(IssueResolution.RESOLUTION_MOVED) ? " moved" : "") +
279  (issue.getResolution().equals(IssueResolution.RESOLUTION_NONE) ? " none" : "") +
280  (issue.getResolution().equals(IssueResolution.RESOLUTION_WONTFIX) ? " wontfix" : "") +
281  (issue.getResolution().equals(IssueResolution.RESOLUTION_WORKSFORME) ? " worksforme" : "");
282  }
283 
284  @Override
285  protected String title(Issue e) {
286  if(!Strings.isBlank(e.getFullDescription(showSubject))) {
287  return e.getFullDescription(showSubject);
288  }
289  return null;
290  }
291 
292  @Override
293  protected Collection<String> metas(Issue e) {
294  ArrayList<RepositoryFile> files = new ArrayList<>();
295  String path = getItemLink(e);
296  if(!SocialImageMap.hasImage(path)) {
299  files.addAll(repository.getRepositoryFiles("*_social.png,*_social.jpg"));
300  files.addAll(repository.getRepositoryFiles("*.png,*.jpg"));
301  }
302  SocialNet sn = new SocialNet(path, e.getDescription(), e.getFullDescription(), files);
303  return sn.getMetas();
304  }
305 
306  @Override
307  protected String getTemplateRoot() {
308  return isMail() ? "content/newsletter/sections/issues" : "issue";
309  }
310 
311  @Override
312  protected Object doVotesCtrl(Issue e) {
315  }
316 
317  @Override
318  protected Object doInterestCtrl(Issue e) {
321  }
322 
323  @Override
324  protected Object doCommentsCtrl(Issue e) {
327  }
328 
329  @Override
330  protected Object doAttachmentsCtrl(Issue e) {
333  }
334 
335  @Override
336  protected Object doFilesCtrl(Issue e) {
339  }
340 
341  @Override
342  protected Object doDescriptionsCtrl(Issue e) {
345  }
346 
347  @Override
348  protected Object doPollsCtrl(Issue e) {
349  throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
350  }
351 
352  @Override
353  protected String getItemLink(Issue e) {
354  return doItemLink(e, e.getId(), true);
355  }
356 
357  @Override
358  protected String getReadAllLink() {
359  String path = getContextPath();
360  if(Strings.isBlank(path)) {
361  return getRestrictedLink();
362  }
363  return path;
364  }
365 
366  @Override
367  protected String getRestrictedLink() {
368  return "/user/myissues";
369  }
370 
371 }
void setDossierStatus(List< String > dossierStatus)
void renderItem(ElephantMarker marker, Issue e, int page)
IssueContentIterator(IConstructor constructor, Writer writer, IContact contact, boolean mail, String pubPath)
void renderSummary(ElephantMarker marker, Issue e, int page)
static String getObjectPath(Object object)
Definition: DossierPU.java:66
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)
void process(String rootTmpl, String tmpl)
Object put(Object key, Object value)
Object configureCtrl(Object ctrl, IContact contact)