BrightSide Workbench Full Report + Source Code
IssueResults.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 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 package org.turro.dossier.search;
19 
20 import java.util.*;
21 import org.turro.action.Contacts;
22 import org.turro.auth.Authentication;
23 import org.turro.dossier.db.DossierPU;
24 import org.turro.dossier.entity.*;
25 import org.turro.dossier.issue.IssueSet;
26 import org.turro.dossier.issue.IssueWrapper;
27 import org.turro.elephant.context.Application;
28 import org.turro.elephant.db.WhereClause;
29 import org.turro.jpa.Dao;
30 import org.turro.plugin.contacts.IContact;
31 import org.turro.util.Chars;
32 import org.zkoss.lang.Strings;
33 
38 public class IssueResults {
39 
40  private Application app = Application.getApplication();
41 
42  private String issueValue = "*";
43  private boolean
44  ckStartPosible = false,
45  ckOthers = false;
46  private IContact byParticipant, byResponsible, byReporter, byQA;
47  private Long dossierId;
48  private Dossier dossier;
49  private Set<IssueParticipantRole> roles = new HashSet<IssueParticipantRole>();
50  private Set<IssueStatus> status = new HashSet<IssueStatus>();
51  private Set<IssueResolution> resolutions = new HashSet<IssueResolution>();
52  private Set<IssueType> types = new HashSet<IssueType>();
53  private Category category;
54  private IContact subject;
55 
56  public IssueResults() {
57  roles.addAll(Arrays.asList(IssueParticipantRole.values()));
58  types.addAll(Arrays.asList(IssueType.values()));
59  status.addAll(Arrays.asList(IssueStatus.values()));
60  status.remove(IssueStatus.STATUS_CLOSED);
61  resolutions.addAll(Arrays.asList(IssueResolution.values()));
62  }
63 
64  public void setApplication(Application app) {
65  this.app = app;
66  }
67 
68  public void markAll() {
69  ckStartPosible = true;
70 
71  ckOthers = app.isInRole("issue:all");
72 
73  roles.addAll(Arrays.asList(IssueParticipantRole.values()));
74  types.addAll(Arrays.asList(IssueType.values()));
75  status.addAll(Arrays.asList(IssueStatus.values()));
76  resolutions.addAll(Arrays.asList(IssueResolution.values()));
77  }
78 
79  public long getIssueCount() {
80  Dao dao = new DossierPU();
81  WhereClause wc = new WhereClause();
82  wc.addClause("select count(distinct issue) from Issue as issue");
83  wc.addClause("left outer join issue.participants participant");
84  wc.addClause("left outer join issue.comments comment");
85  wc.addClause("where 1=1");
86  addCriteria(wc);
87  return (Long) dao.getSingleResult(wc);
88  }
89 
90  public java.util.List<IssueWrapper> getIssueList() {
91  Dao dao = new DossierPU();
92  WhereClause wc = new WhereClause();
93  wc.addClause("select distinct issue from Issue as issue");
94  wc.addClause("left outer join issue.participants participant");
95  wc.addClause("left outer join issue.comments comment");
96  wc.addClause("where 1=1");
97  addCriteria(wc);
98  addClause(wc);
99  return new ArrayList(new IssueSet(dao.getResultList(wc), ckStartPosible, byParticipant));
100  }
101 
103  return category;
104  }
105 
106  public void setCategory(Category category) {
107  this.category = category;
108  }
109 
110  public Set<IssueResolution> getResolutions() {
111  return resolutions;
112  }
113 
114  public void setResolutions(Set<IssueResolution> resolutions) {
115  this.resolutions = resolutions;
116  }
117 
118  public Set<IssueParticipantRole> getRoles() {
119  return roles;
120  }
121 
122  public void setRoles(Set<IssueParticipantRole> roles) {
123  this.roles = roles;
124  }
125 
126  public Set<IssueStatus> getStatus() {
127  return status;
128  }
129 
130  public void setStatus(Set<IssueStatus> status) {
131  this.status = status;
132  }
133 
134  public IContact getSubject() {
135  return subject;
136  }
137 
138  public void setSubject(IContact subject) {
139  this.subject = subject;
140  }
141 
142  public Set<IssueType> getTypes() {
143  return types;
144  }
145 
146  public void setTypes(Set<IssueType> types) {
147  this.types = types;
148  }
149 
150  public void setType(IssueType type) {
151  this.types = EnumSet.of(type);
152  }
153 
154  public void addCriteria(WhereClause wc) {
155  boolean asParticipant =
156  roles.contains(IssueParticipantRole.ISSUE_REPORTER) ||
157  roles.contains(IssueParticipantRole.ISSUE_RESPONSIBLE) ||
158  roles.contains(IssueParticipantRole.ISSUE_QA) ||
159  roles.contains(IssueParticipantRole.ISSUE_ASSISTANT);
160 
161  if(Strings.isEmpty(issueValue) || (!asParticipant && !app.isInRole("issue:all"))) {
162  wc.addClause("and 1=0");
163  return;
164  }
165 
166  if(byParticipant == null) {
167  byParticipant = Authentication.getIContact();
168  }
169 
170  long id = 0;
171  try {
172  id = Long.valueOf(issueValue);
173  if(id > 0) {
174  wc.addClause("and ((issue.id = :id)");
175  wc.addNamedValue("id", id);
176  wc.setPrefix("or");
177  }
178  } catch(Exception ex) {}
179 
180  wc.addLikeFields(new String[] {
181  "issue.description",
182  "comment.comment"
183  }, (issueValue == null ? "" : issueValue.replaceAll("\\*", "%")));
184 
185  if(id > 0) {
186  wc.addClause(")");
187  wc.setPrefix(null);
188  }
189 
190  if(ckStartPosible) {
191  wc.addClause("and (issue.startDate >= :startDate or issue.startDate is null)");
192  wc.addNamedValue("startDate", new Date());
193  }
194 
195  if(asParticipant && !(ckOthers && app.isInRole("issue:all"))) {
196  String sep = "";
197  wc.addClause("and (");
198  for(IssueParticipantRole ii : IssueParticipantRole.values()) {
199  if(roles.contains(ii)) {
200  wc.addClause(sep);
201  sep = "or";
202  wc.addClause("(");
203  wc.addClause("participant.idContact = :idContact");
204  wc.addNamedValue("idContact", byParticipant.getId());
205  wc.addClause("and participant.role = :" + ii.toString());
206  wc.addNamedValue(ii.toString(), ii);
207  wc.addClause(")");
208  }
209  }
210  wc.addClause(")");
211  }
212 
213  if(byResponsible != null && byResponsible.getId() != null) {
214  wc.addClause("and (");
215  wc.addClause("participant.idContact = :idRes");
216  wc.addNamedValue("idRes", byResponsible.getId());
217  wc.addClause("and participant.role = :byRes");
219  wc.addClause(")");
220  }
221 
222  if(byReporter != null && byReporter.getId() != null) {
223  wc.addClause("and (");
224  wc.addClause("participant.idContact = :idInf");
225  wc.addNamedValue("idInf", byReporter.getId());
226  wc.addClause("and participant.role = :byInf");
228  wc.addClause(")");
229  }
230 
231  if(byQA != null && byQA.getId() != null) {
232  wc.addClause("and (");
233  wc.addClause("participant.idContact = :idQA");
234  wc.addNamedValue("idQA", byQA.getId());
235  wc.addClause("and participant.role = :byQA");
237  wc.addClause(")");
238  }
239 
240  String sep = "";
241  wc.addClause("and (");
242  for(IssueStatus ii : IssueStatus.values()) {
243  if(status.contains(ii)) {
244  wc.addClause(sep);
245  sep = "or";
246  wc.addClause("issue.status = :" + ii.toString());
247  wc.addNamedValue(ii.toString(), ii);
248  }
249  }
250  wc.addClause(")");
251 
252  sep = "";
253  wc.addClause("and (");
254  for(IssueResolution ii : IssueResolution.values()) {
255  if(resolutions.contains(ii)) {
256  wc.addClause(sep);
257  sep = "or";
258  wc.addClause("issue.resolution = :" + ii.toString());
259  wc.addNamedValue(ii.toString(), ii);
260  }
261  }
262  wc.addClause(")");
263 
264  sep = "";
265  wc.addClause("and (");
266  for(IssueType ii : IssueType.values()) {
267  if(types.contains(ii)) {
268  wc.addClause(sep);
269  sep = "or";
270  wc.addClause("issue.type = :" + ii.toString());
271  wc.addNamedValue(ii.toString(), ii);
272  }
273  }
274  wc.addClause(")");
275 
276  if(subject != null) {
277  wc.addClause("and exists ( select subject from Participant as subject");
278  wc.addClause("where subject.dossier = issue.dossier");
279  wc.addClause("and subject.idContact = :idSubject");
280  wc.addNamedValue("idSubject", subject.getId());
281  wc.addClause("and subject.role = :roleSubject )");
283  }
284 
285  if(dossier != null) {
286  wc.addClause("and issue.dossier = :dossier");
287  wc.addNamedValue("dossier", dossier);
288  } else if(dossierId != null && dossierId > 0) {
289  wc.addClause("and issue.dossier.id = :dossierId");
290  wc.addNamedValue("dossierId", dossierId);
291  } else {
292  wc.addClause("and issue.dossier.status = :dstatus");
294  }
295 
296  if(category != null) {
297  wc.addClause("and (issue.dossier.category.fullDescription = :decat");
298  wc.addNamedValue("decat", category.getFullDescription());
299  wc.addClause("or issue.dossier.category.fullDescription like :dlcat)");
300  wc.addNamedValue("dlcat", category.getFullDescription() + Chars.backward().spaced() + "%");
301  //DossierResults.addExistsCategoryAffiliance(wc, category, "issue.dossier");
302  }
303 
304  }
305 
306  public boolean isCkOthers() {
307  return ckOthers;
308  }
309 
310  public void setCkOthers(boolean ckOthers) {
311  this.ckOthers = ckOthers;
312  }
313 
314  public boolean isCkStartPosible() {
315  return ckStartPosible;
316  }
317 
318  public void setCkStartPosible(boolean ckStartPosible) {
319  this.ckStartPosible = ckStartPosible;
320  }
321 
322  public Dossier getDossier() {
323  return dossier;
324  }
325 
326  public void setDossier(Dossier dossier) {
327  this.dossier = dossier;
328  }
329 
330  public Long getDossierId() {
331  return dossierId;
332  }
333 
334  public void setDossierId(Long dossierId) {
335  this.dossierId = dossierId;
336  }
337 
338  public String getIssueValue() {
339  return issueValue;
340  }
341 
342  public void setIssueValue(String issueValue) {
343  this.issueValue = issueValue;
344  }
345 
346  public IContact getByQA() {
347  return byQA;
348  }
349 
350  public void setByQA(IContact byQA) {
351  this.byQA = byQA;
352  }
353 
355  return byResponsible;
356  }
357 
358  public void setByResponsible(IContact byResponsible) {
359  this.byResponsible = byResponsible;
360  }
361 
363  return byReporter;
364  }
365 
366  public void setByReporter(IContact byReporter) {
367  this.byReporter = byReporter;
368  }
369 
371  return byParticipant;
372  }
373 
374  public void setByParticipant(IContact byParticipant) {
375  this.byParticipant = byParticipant;
376  }
377 
378  public void setByParticipantId(String id) {
379  this.byParticipant = Contacts.getContactById(id);
380  }
381 
382  protected void addClause(WhereClause wc) {
383  // Do nothing
384  }
385 
386 }
static IContact getContactById(String id)
Definition: Contacts.java:72
void setIssueValue(String issueValue)
Set< IssueResolution > getResolutions()
void setByResponsible(IContact byResponsible)
void setTypes(Set< IssueType > types)
java.util.List< IssueWrapper > getIssueList()
void setResolutions(Set< IssueResolution > resolutions)
void setByReporter(IContact byReporter)
void setCategory(Category category)
Set< IssueParticipantRole > getRoles()
void setCkStartPosible(boolean ckStartPosible)
void setRoles(Set< IssueParticipantRole > roles)
void setApplication(Application app)
void setStatus(Set< IssueStatus > status)
void setByParticipant(IContact byParticipant)
void addLikeFields(String[] fields, String value)
void addNamedValue(String name, Object value)
Object getSingleResult(WhereClause wc)
Definition: Dao.java:380