18 package org.turro.dossier.search;
20 import java.util.ArrayList;
21 import java.util.HashSet;
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;
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<>();
57 private String orderBy =
null;
78 ckOthers = app.
isInRole(
"dossier:all");
95 wc.
addClause(
"select count(distinct dossier)");
97 wc.
addClause(
"left outer join dossier.participants participant");
108 wc.
addClause(
"left outer join dossier.participants participant");
111 return new ArrayList(
new DossierSet(dao.getResultList(wc)));
117 wc.
addClause(
"select distinct new org.turro.dossier.search.DossierIds(dossier.id, dossier.category.id, dossier.category.fullDescription)");
119 wc.
addClause(
"left outer join dossier.participants participant");
122 return dao.getResultList(wc);
130 this.category = category;
146 this.status = status;
158 this.orderBy = orderBy;
162 boolean asParticipant =
166 if(Strings.isEmpty(dossierValue) || (!asParticipant && !app.
isInRole(
"dossier:all"))) {
171 if(byParticipant ==
null) {
178 "dossier.description"
179 }, (dossierValue ==
null ?
"" : dossierValue.replaceAll(
"\\*",
"%")));
181 wc.
addClause(
"or exists (select fieldValue from dossier.fieldValues fieldValue where 1=1");
184 }, (dossierValue ==
null ?
"" : dossierValue.replaceAll(
"\\*",
"%")));
186 wc.
addClause(
"or (fieldValue.value = 'true' and");
188 "fieldValue.fieldDef.labelKey"
189 }, (dossierValue ==
null ?
"" : dossierValue.replaceAll(
"\\*",
"%")));
192 wc.
addClause(
"or exists (select parsub from dossier.participants parsub where parsub.role = :subrole");
196 }, (dossierValue ==
null ?
"" : dossierValue.replaceAll(
"\\*",
"%")));
206 id = Long.valueOf(dossierValue);
211 }
catch(Exception ex) {}
215 if(asParticipant && !(ckOthers && app.
isInRole(
"dossier:all"))) {
218 wc.
addClause(
"participant.idContact = :idContact");
220 wc.
addClause(
"and (participant.role = :cpdrole1 or participant.role = :cpdrole2)");
234 wc.
addClause(
"dossier.status = :statusOpened");
240 wc.
addClause(
"dossier.status = :statusClosed");
246 wc.
addClause(
"dossier.status = :statusFrozen");
256 wc.
addClause(
"dossier.type = :typeDossier");
262 wc.
addClause(
"dossier.type = :typeElection");
268 wc.
addClause(
"dossier.type = :typeProject");
274 wc.
addClause(
"dossier.type = :typeProposal");
280 wc.
addClause(
"dossier.type = :typeSecElection");
286 wc.
addClause(
"dossier.type = :typeSecProposal");
291 if(subject !=
null) {
293 wc.
addClause(
"exists ( select subject from Participant as subject");
294 wc.
addClause(
"where subject.dossier = dossier");
295 wc.
addClause(
"and subject.idContact = :idSubject");
297 wc.
addClause(
"and subject.role = :roleSubject )");
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");
304 wc.
addClause(
"and csubject.role = :croleSubject )");
309 if(category !=
null) {
310 wc.
addClause(
"and (dossier.category.fullDescription = :decat");
312 wc.
addClause(
"or dossier.category.fullDescription like :dlcat)");
319 if(!Strings.isBlank(orderBy)) {
329 this.ckOthers = ckOthers;
337 this.dossierValue = dossierValue;
341 return byParticipant;
345 this.byParticipant = byParticipant;
353 this.subject = subject;
358 String sep, String
id, String dossier) {
360 wc.
addClause(
"select cp from CategoryParticipant as cp");
361 wc.
addClause(
"where cp.idContact = :idContact");
366 wc.
addClause(
"cp.category.fullDescription = " + dossier +
".category.fullDescription");
367 wc.
addClause(
"or " + dossier +
".category.fullDescription like concat(cp.category.fullDescription, '" + Chars.backward().spaced() +
"%')");
373 StringBuilder sb =
new StringBuilder(
374 " left outer join dossier.category.participants cparticipant");
376 sb.append(
" left outer join dossier.category");
377 for(
int c = 0; c < i; c++) {
378 sb.append(
".parent");
380 sb.append(
".participants cparticipant" + i);
382 return sb.toString();
387 String sep, String
id,
ParticipantRole participantRole, String
operator) {
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);
402 StringBuilder sb =
new StringBuilder(field);
403 for(
int ci = 0; ci < i; ci++) {
404 sb.append(
".parent");
406 wc.
addClause(sb.toString() +
" = :category" + i);
static IContact getIContact()
String getFullDescription()
static void addParticipantAffiliance(WhereClause wc, String sep, String idContact, String dossierField)
java.util.List< DossierIds > getDossierIdList()
void setOrderBy(String orderBy)
static final int CATEGORY_SQL_ITERATIONS
Set< DossierType > getType()
static void addCategoryAffiliance(WhereClause wc, Category category, String field)
void setCkOthers(boolean ckOthers)
java.util.List< Dossier > getDossierList()
Set< DossierStatus > getStatus()
static void addSubjectAffiliance(WhereClause wc, String sep, String id, String dossier)
void setCategory(Category category)
void setType(Set< DossierType > type)
static void addCategoryConditionals(WhereClause wc, String sep, String id, ParticipantRole participantRole, String operator)
Set< ParticipantRole > getRoles()
void setSubject(Contact subject)
void setRoles(Set< ParticipantRole > roles)
void addCriteria(WhereClause wc)
static String getCategoryJoins()
void setStatus(Set< DossierStatus > status)
void setDossierValue(String dossierValue)
void setByParticipant(IContact byParticipant)
IContact getByParticipant()
boolean isInRole(String role)
static Application getApplication()
void setPrefix(String prefix)
void addClause(String clause)
void addLikeFields(String[] fields, String value)
void addNamedValue(String name, Object value)
Object getSingleResult(WhereClause wc)