BrightSide Workbench Full Report + Source Code
DWIssueModel.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2016 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.dw;
20 
21 import java.util.Collection;
22 import java.util.Date;
23 import java.util.List;
24 import org.turro.action.Contacts;
25 import org.turro.dossier.db.DossierPU;
26 import org.turro.dossier.entity.IssueParticipantRole;
27 import org.turro.dossier.entity.IssuePriority;
28 import org.turro.dossier.entity.IssueResolution;
29 import org.turro.dossier.entity.IssueStatus;
30 import org.turro.dossier.entity.IssueType;
31 import org.turro.dossier.entity.ParticipantRole;
32 import org.turro.elephant.db.WhereClause;
33 import org.turro.jpa.Dao;
34 import org.turro.plugin.contacts.IContact;
35 import org.zkoss.lang.Strings;
36 
41 public class DWIssueModel {
42 
43  /* Filters */
44  private String participantId, categoryPath;
45  private Long dossierId, versionId;
46  private Date startDate, endDate;
47  private ParticipantRole participantRole;
48  private IssueType type;
49  private IssuePriority priority;
50  private IssueStatus status;
51  private IssueResolution resolution;
52 
53  public Long getCount() {
54  WhereClause wc = new WhereClause();
55  wc.addClause("select count(distinct issueId)");
56  wc.addClause("from DWIssue");
57  wc.addClause("where 1=1");
58  addCriteria(wc);
59  return (Long) getDao().getSingleResultOrNull(wc);
60  }
61 
62  public Long getOpenedCount() {
63  WhereClause wc = new WhereClause();
64  wc.addClause("select count(distinct issueId)");
65  wc.addClause("from DWIssue");
66  wc.addClause("where status <> :status");
68  addCriteria(wc);
69  return (Long) getDao().getSingleResultOrNull(wc);
70  }
71 
72  public Long getParticipantsCount() {
73  WhereClause wc = new WhereClause();
74  wc.addClause("select count(participantId)");
75  wc.addClause("from DWIssue");
76  wc.addClause("where 1=1");
77  addCriteria(wc);
78  return (Long) getDao().getSingleResultOrNull(wc);
79  }
80 
81  public Long getCommentCount() {
82  WhereClause wc = new WhereClause();
83  wc.addClause("select sum(commentCount)");
84  wc.addClause("from DWIssue");
85  wc.addClause("where 1=1");
86  addCriteria(wc);
87  return (Long) getDao().getSingleResultOrNull(wc);
88  }
89 
90  public List getByType() {
91  WhereClause wc = new WhereClause();
92  wc.addClause("select type, count(distinct issueId)");
93  wc.addClause("from DWIssue");
94  wc.addClause("where 1=1");
95  addCriteria(wc);
96  wc.addClause("group by type");
97  return getDao().getResultList(wc);
98  }
99 
100  public List getByStatus() {
101  WhereClause wc = new WhereClause();
102  wc.addClause("select status, count(distinct issueId)");
103  wc.addClause("from DWIssue");
104  wc.addClause("where 1=1");
105  addCriteria(wc);
106  wc.addClause("group by status");
107  return getDao().getResultList(wc);
108  }
109 
110  public List getByResolution() {
111  WhereClause wc = new WhereClause();
112  wc.addClause("select resolution, count(distinct issueId)");
113  wc.addClause("from DWIssue");
114  wc.addClause("where 1=1");
115  addCriteria(wc);
116  wc.addClause("group by resolution");
117  return getDao().getResultList(wc);
118  }
119 
120  public Collection<Long> getDossiersId() {
121  WhereClause wc = new WhereClause();
122  wc.addClause("select distinct dossierId");
123  wc.addClause("from DWIssue i");
124  addCriteria(wc);
125  return getDao().getResultList(wc);
126  }
127 
128  public List getResponsiblesId() {
129  WhereClause wc = new WhereClause();
130  wc.addClause("select distinct participantId, count(distinct issueId)");
131  wc.addClause(",(select count(distinct i2.issueId) from DWIssue i2 where i2.participantId = i.participantId and i2.status <> :status2 and i2.participantRole = :participantRole2)");
132  wc.addNamedValue("status2", IssueStatus.STATUS_CLOSED);
133  wc.addNamedValue("participantRole2", IssueParticipantRole.ISSUE_RESPONSIBLE);
134  wc.addClause("from DWIssue i");
135  wc.addClause("where participantRole = :participantRole");
137  addCriteriaParticipantExists(wc);
138  wc.addClause("group by participantId");
139  wc.addClause("order by 2 desc");
140  return getDao().getResultList(wc);
141  }
142 
143  public List getReportersId() {
144  WhereClause wc = new WhereClause();
145  wc.addClause("select distinct participantId, count(distinct issueId)");
146  wc.addClause(",(select count(distinct i2.issueId) from DWIssue i2 where i2.participantId = i.participantId and i2.status <> :status2 and i2.participantRole = :participantRole2)");
147  wc.addNamedValue("status2", IssueStatus.STATUS_CLOSED);
148  wc.addNamedValue("participantRole2", IssueParticipantRole.ISSUE_REPORTER);
149  wc.addClause("from DWIssue i");
150  wc.addClause("where participantRole = :participantRole");
151  wc.addNamedValue("participantRole", IssueParticipantRole.ISSUE_REPORTER);
152  addCriteriaParticipantExists(wc);
153  wc.addClause("group by participantId");
154  wc.addClause("order by 2 desc");
155  return getDao().getResultList(wc);
156  }
157 
158  public List getAssistantsId() {
159  WhereClause wc = new WhereClause();
160  wc.addClause("select distinct participantId, count(distinct issueId)");
161  wc.addClause(",(select count(distinct i2.issueId) from DWIssue i2 where i2.participantId = i.participantId and i2.status <> :status2 and i2.participantRole = :participantRole2)");
162  wc.addNamedValue("status2", IssueStatus.STATUS_CLOSED);
163  wc.addNamedValue("participantRole2", IssueParticipantRole.ISSUE_ASSISTANT);
164  wc.addClause("from DWIssue i");
165  wc.addClause("where participantRole = :participantRole");
166  wc.addNamedValue("participantRole", IssueParticipantRole.ISSUE_ASSISTANT);
167  addCriteriaParticipantExists(wc);
168  wc.addClause("group by participantId");
169  wc.addClause("order by 2 desc");
170  return getDao().getResultList(wc);
171  }
172 
173  public List getQAsId() {
174  WhereClause wc = new WhereClause();
175  wc.addClause("select distinct participantId, count(distinct issueId)");
176  wc.addClause(",(select count(distinct i2.issueId) from DWIssue i2 where i2.participantId = i.participantId and i2.status <> :status2 and i2.participantRole = :participantRole2)");
177  wc.addNamedValue("status2", IssueStatus.STATUS_CLOSED);
178  wc.addNamedValue("participantRole2", IssueParticipantRole.ISSUE_QA);
179  wc.addClause("from DWIssue i");
180  wc.addClause("where participantRole = :participantRole");
181  wc.addNamedValue("participantRole", IssueParticipantRole.ISSUE_QA);
182  addCriteriaParticipantExists(wc);
183  wc.addClause("group by participantId");
184  wc.addClause("order by 2 desc");
185  return getDao().getResultList(wc);
186  }
187 
188  /* Id to IContact for marker */
189  public IContact getIContact(String contactId) {
190  return Contacts.getContactById(contactId);
191  }
192 
193  /* Id to DWDossier for marker */
194  public DWDossier getDossier(Long dossierId) {
195  return getDao().find(DWDossier.class, dossierId);
196  }
197 
198  /* Id to DWIssue for marker */
199  public DWIssue getIssue(Long issueId) {
200  return getDao().find(DWIssue.class, issueId);
201  }
202 
203  public String getParticipantId() {
204  return participantId;
205  }
206 
207  public void setParticipantId(String participantId) {
208  this.participantId = participantId;
209  resetIContact();
210  }
211 
212  public String getCategoryPath() {
213  return categoryPath;
214  }
215 
216  public void setCategoryPath(String categoryPath) {
217  this.categoryPath = categoryPath;
218  }
219 
220  public Long getDossierId() {
221  return dossierId;
222  }
223 
224  public void setDossierId(Long dossierId) {
225  this.dossierId = dossierId;
226  }
227 
228  public Long getVersionId() {
229  return versionId;
230  }
231 
232  public void setVersionId(Long versionId) {
233  this.versionId = versionId;
234  }
235 
236  public Date getStartDate() {
237  return startDate;
238  }
239 
240  public void setStartDate(Date startDate) {
241  this.startDate = startDate;
242  }
243 
244  public Date getEndDate() {
245  return endDate;
246  }
247 
248  public void setEndDate(Date endDate) {
249  this.endDate = endDate;
250  }
251 
253  return participantRole;
254  }
255 
256  public void setParticipantRole(ParticipantRole participantRole) {
257  this.participantRole = participantRole;
258  }
259 
260  public IssueType getType() {
261  return type;
262  }
263 
264  public void setType(IssueType type) {
265  this.type = type;
266  }
267 
269  return priority;
270  }
271 
272  public void setPriority(IssuePriority priority) {
273  this.priority = priority;
274  }
275 
277  return status;
278  }
279 
280  public void setStatus(IssueStatus status) {
281  this.status = status;
282  }
283 
285  return resolution;
286  }
287 
288  public void setResolution(IssueResolution resolution) {
289  this.resolution = resolution;
290  }
291 
292  /* Dao */
293 
294  private Dao _dao;
295 
296  private Dao getDao() {
297  if(_dao == null) {
299  _dao = new DossierPU();
300  }
301  return _dao;
302  }
303 
304  private void addCriteria(WhereClause wc) {
305  if(!Strings.isBlank(participantId)) {
306  wc.addClause("and participantId = :participantId");
307  wc.addNamedValue("participantId", participantId);
308  if(participantRole != null) {
309  wc.addClause("and participantRole = :participantRole");
310  wc.addNamedValue("participantRole", participantRole);
311  }
312  }
313  if(!Strings.isBlank(categoryPath)) {
314  wc.addClause("and categoryPath = :categoryPath");
315  wc.addNamedValue("categoryPath", categoryPath);
316  }
317  if(dossierId != null && dossierId > 0) {
318  wc.addClause("and dossierId = :dossierId");
319  wc.addNamedValue("dossierId", dossierId);
320  }
321  if(versionId != null && versionId > 0) {
322  wc.addClause("and versionId = :versionId");
323  wc.addNamedValue("versionId", versionId);
324  }
325  }
326 
327  private void addCriteriaParticipantExists(WhereClause wc) {
328  if(!Strings.isBlank(participantId)) {
329  wc.addClause("and exists(");
330  wc.addClause("select i2 from DWIssue i2");
331  wc.addClause("where i2.issueId = i.issueId");
332  wc.addClause("and participantId = :participantId");
333  wc.addNamedValue("participantId", participantId);
334  if(participantRole != null) {
335  wc.addClause("and participantRole = :participantRole");
336  wc.addNamedValue("participantRole", participantRole);
337  }
338  wc.addClause(")");
339  }
340  if(!Strings.isBlank(categoryPath)) {
341  wc.addClause("and categoryPath = :categoryPath");
342  wc.addNamedValue("categoryPath", categoryPath);
343  }
344  if(dossierId != null && dossierId > 0) {
345  wc.addClause("and dossierId = :dossierId");
346  wc.addNamedValue("dossierId", dossierId);
347  }
348  if(versionId != null && versionId > 0) {
349  wc.addClause("and versionId = :versionId");
350  wc.addNamedValue("versionId", versionId);
351  }
352  }
353 
356  private transient IContact _contact;
357 
359  if(_contact == null) {
360  _contact = Contacts.getContactById(participantId);
361  }
362  return _contact;
363  }
364 
365  public void setIParticipant(IContact contact) {
366  _contact = contact;
367  participantId = _contact != null ? _contact.getId() : null;
368  }
369 
370  private void resetIContact() {
371  _contact = null;
372  }
373 
374 }
static IContact getContactById(String id)
Definition: Contacts.java:72
void setResolution(IssueResolution resolution)
void setIParticipant(IContact contact)
void setDossierId(Long dossierId)
void setPriority(IssuePriority priority)
void setParticipantId(String participantId)
void setStartDate(Date startDate)
DWIssue getIssue(Long issueId)
void setVersionId(Long versionId)
void setCategoryPath(String categoryPath)
Collection< Long > getDossiersId()
IContact getIContact(String contactId)
void setStatus(IssueStatus status)
void setParticipantRole(ParticipantRole participantRole)
DWDossier getDossier(Long dossierId)
void addNamedValue(String name, Object value)
Object getSingleResultOrNull(SqlClause sc)
Definition: Dao.java:419