BrightSide Workbench Full Report + Source Code
DWDossierModel.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.Date;
22 import java.util.List;
23 import org.turro.action.Contacts;
24 import org.turro.dossier.db.DossierPU;
25 import org.turro.dossier.entity.DossierStatus;
26 import org.turro.dossier.entity.DossierType;
27 import org.turro.dossier.entity.ParticipantRole;
28 import org.turro.elephant.db.WhereClause;
29 import org.turro.jpa.Dao;
30 import org.turro.plugin.contacts.IContact;
31 import org.zkoss.lang.Strings;
32 
37 public class DWDossierModel {
38 
39  /* Filters */
40  private String participantId, categoryPath;
41  private Long dossierId, versionId;
42  private Date startDate, endDate;
43  private DossierType type;
44  private DossierStatus status;
45 
46  public Long getCount() {
47  WhereClause wc = new WhereClause();
48  wc.addClause("select count(distinct dossierId)");
49  wc.addClause("from DWDossier");
50  wc.addClause("where 1=1");
51  addCriteria(wc);
52  return (Long) getDao().getSingleResultOrNull(wc);
53  }
54 
55  public Long getProjectCount() {
56  WhereClause wc = new WhereClause();
57  wc.addClause("select count(distinct dossierId)");
58  wc.addClause("from DWDossier");
59  wc.addClause("where type=:type");
61  addCriteria(wc);
62  return (Long) getDao().getSingleResultOrNull(wc);
63  }
64 
65  public Long getOpenedCount() {
66  WhereClause wc = new WhereClause();
67  wc.addClause("select count(distinct dossierId)");
68  wc.addClause("from DWDossier");
69  wc.addClause("where status <> :status");
71  addCriteria(wc);
72  return (Long) getDao().getSingleResultOrNull(wc);
73  }
74 
75  public List getByStatus() {
76  WhereClause wc = new WhereClause();
77  wc.addClause("select status, count(distinct dossierId)");
78  wc.addClause("from DWDossier");
79  wc.addClause("where 1=1");
80  addCriteria(wc);
81  wc.addClause("group by status");
82  return getDao().getResultList(wc);
83  }
84 
85  public List getByPhase() {
86  WhereClause wc = new WhereClause();
87  wc.addClause("select phase, count(distinct dossierId)");
88  wc.addClause("from DWDossier");
89  wc.addClause("where phase is not null");
90  addCriteria(wc);
91  wc.addClause("group by phase");
92  return getDao().getResultList(wc);
93  }
94 
95  public Long getParticipantsCount() {
96  WhereClause wc = new WhereClause();
97  wc.addClause("select count(participantId)");
98  wc.addClause("from DWDossier");
99  wc.addClause("where 1=1");
100  addCriteria(wc);
101  return (Long) getDao().getSingleResultOrNull(wc);
102  }
103 
104  /* Dao */
105 
106  private Dao _dao;
107 
108  private Dao getDao() {
109  if(_dao == null) {
111  _dao = new DossierPU();
112  }
113  return _dao;
114  }
115 
116  private void addCriteria(WhereClause wc) {
117  if(!Strings.isBlank(participantId)) {
118  wc.addClause("and (participantId = :participantId");
119  wc.addNamedValue("participantId", participantId);
120  wc.addClause("or exists(");
121  wc.addClause("select cp from CategoryParticipant cp");
122  wc.addClause("where cp.role <> :role");
123  wc.addNamedValue("role", ParticipantRole.PARTICIPANT_SUBJECT);
124  wc.addClause("and cp.idContact = :participantId");
125  wc.addClause("and categoryPath like concat(cp.category.fullDescription, '%')");
126  wc.addClause("))");
127  }
128  if(!Strings.isBlank(categoryPath)) {
129  wc.addClause("and categoryPath = :categoryPath");
130  wc.addNamedValue("categoryPath", categoryPath);
131  }
132  if(versionId != null && versionId > 0) {
133  wc.addClause("and versionId = :versionId");
134  wc.addNamedValue("versionId", versionId);
135  }
136  }
137 
140  private transient IContact _contact;
141 
143  if(_contact == null) {
144  _contact = Contacts.getContactById(participantId);
145  }
146  return _contact;
147  }
148 
149  public void setIParticipant(IContact contact) {
150  _contact = contact;
151  participantId = _contact != null ? _contact.getId() : null;
152  }
153 
154  private void resetIContact() {
155  _contact = null;
156  }
157 
158 }
static IContact getContactById(String id)
Definition: Contacts.java:72
void setIParticipant(IContact contact)
void addNamedValue(String name, Object value)
Object getSingleResultOrNull(SqlClause sc)
Definition: Dao.java:419