BrightSide Workbench Full Report + Source Code
DossierResults.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.ArrayList;
21 import java.util.HashSet;
22 import java.util.Set;
23 import org.turro.string.Strings;
24 import org.turro.auth.Authentication;
25 import org.turro.contacts.Contact;
26 import org.turro.dossier.db.DossierPU;
27 import org.turro.dossier.dossier.DossierSet;
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.DossierType;
32 import org.turro.dossier.entity.ParticipantRole;
33 import org.turro.elephant.context.Application;
34 import org.turro.elephant.db.WhereClause;
35 import org.turro.jpa.Dao;
36 import org.turro.plugin.contacts.IContact;
37 import org.turro.util.Chars;
38 
43 public class DossierResults {
44 
45  public static final int CATEGORY_SQL_ITERATIONS = 5;
46 
47  private final Application app = Application.getApplication();
48 
49  private String dossierValue = "*";
50  private boolean ckOthers = false;
51  private Set<ParticipantRole> roles = new HashSet<>();
52  private Set<DossierStatus> status = new HashSet<>();
53  private Set<DossierType> type = new HashSet<>();
54  private Category category;
55  private IContact byParticipant;
56  private Contact subject = null;
57  private String orderBy = null;
58 
59  public DossierResults() {
62 
63  status.add(DossierStatus.DOSSIER_OPENED);
64 
65  type.add(DossierType.TYPE_DOSSIER);
66  type.add(DossierType.TYPE_ELECTION);
67  type.add(DossierType.TYPE_PROJECT);
68  type.add(DossierType.TYPE_PROPOSAL);
71  }
72 
73  public void markAll() {
74 
77 
78  ckOthers = app.isInRole("dossier:all");
79 
80  status.add(DossierStatus.DOSSIER_OPENED);
81  status.add(DossierStatus.DOSSIER_CLOSED);
82  status.add(DossierStatus.DOSSIER_FROZEN);
83 
84  type.add(DossierType.TYPE_DOSSIER);
85  type.add(DossierType.TYPE_ELECTION);
86  type.add(DossierType.TYPE_PROJECT);
87  type.add(DossierType.TYPE_PROPOSAL);
90  }
91 
92  public long getDossierCount() {
93  Dao dao = new DossierPU();
94  WhereClause wc = new WhereClause();
95  wc.addClause("select count(distinct dossier)");
96  wc.addClause("from Dossier as dossier");
97  wc.addClause("left outer join dossier.participants participant");
98  wc.addClause("where 1=1");
99  addCriteria(wc);
100  return (Long) dao.getSingleResult(wc);
101  }
102 
103  public java.util.List<Dossier> getDossierList() {
104  Dao dao = new DossierPU();
105  WhereClause wc = new WhereClause();
106  wc.addClause("select distinct dossier");
107  wc.addClause("from Dossier as dossier");
108  wc.addClause("left outer join dossier.participants participant");
109  wc.addClause("where 1=1");
110  addCriteria(wc);
111  return new ArrayList(new DossierSet(dao.getResultList(wc)));
112  }
113 
114  public java.util.List<DossierIds> getDossierIdList() {
115  Dao dao = new DossierPU();
116  WhereClause wc = new WhereClause();
117  wc.addClause("select distinct new org.turro.dossier.search.DossierIds(dossier.id, dossier.category.id, dossier.category.fullDescription)");
118  wc.addClause("from Dossier as dossier");
119  wc.addClause("left outer join dossier.participants participant");
120  wc.addClause("where 1=1");
121  addCriteria(wc);
122  return dao.getResultList(wc);
123  }
124 
126  return category;
127  }
128 
129  public void setCategory(Category category) {
130  this.category = category;
131  }
132 
133  public Set<ParticipantRole> getRoles() {
134  return roles;
135  }
136 
137  public void setRoles(Set<ParticipantRole> roles) {
138  this.roles = roles;
139  }
140 
141  public Set<DossierStatus> getStatus() {
142  return status;
143  }
144 
145  public void setStatus(Set<DossierStatus> status) {
146  this.status = status;
147  }
148 
149  public Set<DossierType> getType() {
150  return type;
151  }
152 
153  public void setType(Set<DossierType> type) {
154  this.type = type;
155  }
156 
157  public void setOrderBy(String orderBy) {
158  this.orderBy = orderBy;
159  }
160 
161  public void addCriteria(WhereClause wc) {
162  boolean asParticipant =
163  roles.contains(ParticipantRole.PARTICIPANT_OWNER) ||
164  roles.contains(ParticipantRole.PARTICIPANT_ASSISTANT);
165 
166  if(Strings.isEmpty(dossierValue) || (!asParticipant && !app.isInRole("dossier:all"))) {
167  wc.addClause("and 0=1");
168  return;
169  }
170 
171  if(byParticipant == null) {
172  byParticipant = Authentication.getIContact();
173  }
174 
175  wc.addClause("and ((");
176  wc.setPrefix("");
177  wc.addLikeFields(new String[] {
178  "dossier.description"
179  }, (dossierValue == null ? "" : dossierValue.replaceAll("\\*", "%")));
180  wc.setPrefix(null);
181  wc.addClause("or exists (select fieldValue from dossier.fieldValues fieldValue where 1=1");
182  wc.addLikeFields(new String[] {
183  "fieldValue.value"
184  }, (dossierValue == null ? "" : dossierValue.replaceAll("\\*", "%")));
185  wc.setPrefix("");
186  wc.addClause("or (fieldValue.value = 'true' and");
187  wc.addLikeFields(new String[] {
188  "fieldValue.fieldDef.labelKey"
189  }, (dossierValue == null ? "" : dossierValue.replaceAll("\\*", "%")));
190  wc.setPrefix(null);
191  wc.addClause("))");
192  wc.addClause("or exists (select parsub from dossier.participants parsub where parsub.role = :subrole");
194  wc.addLikeFields(new String[] {
195  "parsub.name"
196  }, (dossierValue == null ? "" : dossierValue.replaceAll("\\*", "%")));
197  wc.addClause("))");
198 
199 // wc = SQLHelper.getWhereClause(new String[] {
200 // "dossier.description",
201 // "fieldValue.value"
202 // }, (dossierValue == null ? "" : dossierValue.replaceAll("\\*", "%")));
203 
204  long id;
205  try {
206  id = Long.valueOf(dossierValue);
207  if(id > 0) {
208  wc.addClause("or dossier.id = :id");
209  wc.addNamedValue("id", id);
210  }
211  } catch(Exception ex) {}
212 
213  wc.addClause(")");
214 
215  if(asParticipant && !(ckOthers && app.isInRole("dossier:all"))) {
216  wc.addClause("and (");
217  wc.addClause("(");
218  wc.addClause("participant.idContact = :idContact");
219  wc.addNamedValue("idContact", byParticipant.getId());
220  wc.addClause("and (participant.role = :cpdrole1 or participant.role = :cpdrole2)");
223  wc.addClause(")");
224  CategoryResults.addParticipantAffiliance(wc, "or", byParticipant.getId(), "dossier");
225  //addCategoryConditionals(wc, sep, byParticipant.getId(), ParticipantRole.PARTICIPANT_OWNER, "=");
226  wc.addClause(")");
227  }
228 
229  String sep = "";
230  wc.addClause("and (");
231  if(status.contains(DossierStatus.DOSSIER_OPENED)) {
232  wc.addClause(sep);
233  sep = "or";
234  wc.addClause("dossier.status = :statusOpened");
235  wc.addNamedValue("statusOpened", DossierStatus.DOSSIER_OPENED);
236  }
237  if(status.contains(DossierStatus.DOSSIER_CLOSED)) {
238  wc.addClause(sep);
239  sep = "or";
240  wc.addClause("dossier.status = :statusClosed");
241  wc.addNamedValue("statusClosed", DossierStatus.DOSSIER_CLOSED);
242  }
243  if(status.contains(DossierStatus.DOSSIER_FROZEN)) {
244  wc.addClause(sep);
245  sep = "or";
246  wc.addClause("dossier.status = :statusFrozen");
247  wc.addNamedValue("statusFrozen", DossierStatus.DOSSIER_FROZEN);
248  }
249  wc.addClause(")");
250 
251  sep = "";
252  wc.addClause("and (");
253  if(type.contains(DossierType.TYPE_DOSSIER)) {
254  wc.addClause(sep);
255  sep = "or";
256  wc.addClause("dossier.type = :typeDossier");
257  wc.addNamedValue("typeDossier", DossierType.TYPE_DOSSIER);
258  }
259  if(type.contains(DossierType.TYPE_ELECTION)) {
260  wc.addClause(sep);
261  sep = "or";
262  wc.addClause("dossier.type = :typeElection");
263  wc.addNamedValue("typeElection", DossierType.TYPE_ELECTION);
264  }
265  if(type.contains(DossierType.TYPE_PROJECT)) {
266  wc.addClause(sep);
267  sep = "or";
268  wc.addClause("dossier.type = :typeProject");
269  wc.addNamedValue("typeProject", DossierType.TYPE_PROJECT);
270  }
271  if(type.contains(DossierType.TYPE_PROPOSAL)) {
272  wc.addClause(sep);
273  sep = "or";
274  wc.addClause("dossier.type = :typeProposal");
275  wc.addNamedValue("typeProposal", DossierType.TYPE_PROPOSAL);
276  }
277  if(type.contains(DossierType.TYPE_SECRET_ELECTION)) {
278  wc.addClause(sep);
279  sep = "or";
280  wc.addClause("dossier.type = :typeSecElection");
281  wc.addNamedValue("typeSecElection", DossierType.TYPE_SECRET_ELECTION);
282  }
283  if(type.contains(DossierType.TYPE_SECRET_PROPOSAL)) {
284  wc.addClause(sep);
285  sep = "or";
286  wc.addClause("dossier.type = :typeSecProposal");
287  wc.addNamedValue("typeSecProposal", DossierType.TYPE_SECRET_PROPOSAL);
288  }
289  wc.addClause(")");
290 
291  if(subject != null) {
292  wc.addClause("and ((");
293  wc.addClause("exists ( select subject from Participant as subject");
294  wc.addClause("where subject.dossier = dossier");
295  wc.addClause("and subject.idContact = :idSubject");
296  wc.addNamedValue("idSubject", subject.getId());
297  wc.addClause("and subject.role = :roleSubject )");
299  wc.addClause(") or (");
300  wc.addClause("exists ( select csubject from CategoryParticipant as csubject");
301  wc.addClause("where csubject.category = dossier.category");
302  wc.addClause("and csubject.idContact = :cidSubject");
303  wc.addNamedValue("cidSubject", subject.getId());
304  wc.addClause("and csubject.role = :croleSubject )");
306  wc.addClause("))");
307  }
308 
309  if(category != null) {
310  wc.addClause("and (dossier.category.fullDescription = :decat");
311  wc.addNamedValue("decat", category.getFullDescription());
312  wc.addClause("or dossier.category.fullDescription like :dlcat)");
313  wc.addNamedValue("dlcat", category.getFullDescription() + Chars.backward().spaced() + "%");
314 // wc.addClause("and (");
315 // addCategoryAffiliance(wc, category, "dossier.category");
316 // wc.addClause(")");
317  }
318 
319  if(!Strings.isBlank(orderBy)) {
320  wc.addClause(orderBy);
321  }
322  }
323 
324  public boolean isCkOthers() {
325  return ckOthers;
326  }
327 
328  public void setCkOthers(boolean ckOthers) {
329  this.ckOthers = ckOthers;
330  }
331 
332  public String getDossierValue() {
333  return dossierValue;
334  }
335 
336  public void setDossierValue(String dossierValue) {
337  this.dossierValue = dossierValue;
338  }
339 
341  return byParticipant;
342  }
343 
344  public void setByParticipant(IContact byParticipant) {
345  this.byParticipant = byParticipant;
346  }
347 
348  public Contact getSubject() {
349  return subject;
350  }
351 
352  public void setSubject(Contact subject) {
353  this.subject = subject;
354  }
355 
356  @Deprecated
357  public static void addSubjectAffiliance(WhereClause wc,
358  String sep, String id, String dossier) {
359  wc.addClause(sep + " exists (");
360  wc.addClause("select cp from CategoryParticipant as cp");
361  wc.addClause("where cp.idContact = :idContact");
362  wc.addNamedValue("idContact", id);
363  wc.addClause("and cp.role = :cprole");
365  wc.addClause("and (");
366  wc.addClause("cp.category.fullDescription = " + dossier + ".category.fullDescription");
367  wc.addClause("or " + dossier + ".category.fullDescription like concat(cp.category.fullDescription, '" + Chars.backward().spaced() + "%')");
368  wc.addClause("))");
369  }
370 
371  @Deprecated
372  public static String getCategoryJoins() {
373  StringBuilder sb = new StringBuilder(
374  " left outer join dossier.category.participants cparticipant");
375  for(int i = 0; i < CATEGORY_SQL_ITERATIONS; i++) {
376  sb.append(" left outer join dossier.category");
377  for(int c = 0; c < i; c++) {
378  sb.append(".parent");
379  }
380  sb.append(".participants cparticipant" + i);
381  }
382  return sb.toString();
383  }
384 
385  @Deprecated
386  public static void addCategoryConditionals(WhereClause wc,
387  String sep, String id, ParticipantRole participantRole, String operator) {
388  for(int i = 0; i < CATEGORY_SQL_ITERATIONS; i++) {
389  wc.addClause(sep);
390  wc.addClause("(");
391  wc.addClause("cparticipant" + i + ".idContact = :idContact" + participantRole.name() + i);
392  wc.addNamedValue("idContact" + participantRole.name() + i, id);
393  wc.addClause("and cparticipant" + i + ".role " + operator + " :" + participantRole.name() + i);
394  wc.addNamedValue(participantRole.name() + i, participantRole);
395  wc.addClause(")");
396  }
397  }
398 
399  @Deprecated
400  public static void addCategoryAffiliance(WhereClause wc, Category category, String field) {
401  for(int i = 0; i < CATEGORY_SQL_ITERATIONS; i++) {
402  StringBuilder sb = new StringBuilder(field);
403  for(int ci = 0; ci < i; ci++) {
404  sb.append(".parent");
405  }
406  wc.addClause(sb.toString() + " = :category" + i);
407  wc.addNamedValue("category" + i, category);
408  if(i < CATEGORY_SQL_ITERATIONS - 1) wc.addClause("or");
409  }
410  }
411 
412 }
static void addParticipantAffiliance(WhereClause wc, String sep, String idContact, String dossierField)
java.util.List< DossierIds > getDossierIdList()
static void addCategoryAffiliance(WhereClause wc, Category category, String field)
java.util.List< Dossier > getDossierList()
static void addSubjectAffiliance(WhereClause wc, String sep, String id, String dossier)
void setType(Set< DossierType > type)
static void addCategoryConditionals(WhereClause wc, String sep, String id, ParticipantRole participantRole, String operator)
void setRoles(Set< ParticipantRole > roles)
void setStatus(Set< DossierStatus > status)
void setDossierValue(String dossierValue)
void setByParticipant(IContact byParticipant)
void addLikeFields(String[] fields, String value)
void addNamedValue(String name, Object value)
Object getSingleResult(WhereClause wc)
Definition: Dao.java:380