BrightSide Workbench Full Report + Source Code
DossierViewpoint.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.zul.viewer;
19 
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.List;
23 import java.util.Map;
24 import org.amic.util.string.Strings;
25 import org.turro.auth.Authentication;
26 import org.turro.dossier.db.DossierPU;
27 import org.turro.dossier.entity.Category;
28 import org.turro.dossier.entity.Dossier;
29 import org.turro.dossier.entity.DossierStatus;
30 import org.turro.dossier.entity.ParticipantRole;
31 import org.turro.dossier.search.DossierResults;
32 import org.turro.elephant.db.WhereClause;
33 import org.turro.plugin.contacts.IContact;
34 import org.turro.plugin.contacts.IContactNameComparator;
35 import org.turro.plugin.contacts.IContactTreeMap;
36 
41 @Deprecated
42 public class DossierViewpoint {
43 
44  private Category category;
45  private ParticipantRole viewpoint;
46  private IContact contact;
47 
48  public DossierViewpoint() {
49  }
50 
51  public Category getCategory() {
52  return category;
53  }
54 
55  public void setCategory(Category category) {
56  this.category = category;
57  }
58 
59  public IContact getContact() {
60  return contact;
61  }
62 
63  public void setContact(IContact contact) {
64  this.contact = contact;
65  }
66 
68  return viewpoint;
69  }
70 
71  public void setViewpoint(ParticipantRole viewpoint) {
72  this.viewpoint = viewpoint;
73  }
74 
75  public List<Category> getPossibleCategories() {
76  WhereClause wc = new WhereClause();
77  wc.addClause("select distinct dossier.category from Dossier as dossier");
78  wc.addClause("left join dossier.participants participant");
80  wc.addClause("where (");
81  wc.addClause("(");
82  wc.addClause("participant.idContact = :idContact");
83  wc.addNamedValue("idContact", contact.getId());
84  if(viewpoint.equals(ParticipantRole.PARTICIPANT_OWNER)) {
85  wc.addClause("and participant.role <> :roleContact");
87  } else {
88  wc.addClause("and participant.role = :roleContact");
89  wc.addNamedValue("roleContact", viewpoint);
90  }
91  wc.addClause(")");
92  if(viewpoint.equals(ParticipantRole.PARTICIPANT_OWNER)) {
93  DossierResults.addCategoryConditionals(wc, "or", contact.getId(),
95  } else {
96  DossierResults.addCategoryConditionals(wc, "or", contact.getId(),
97  viewpoint, "=");
98  }
99  wc.addClause(")");
100  return new DossierPU().getResultList(wc);
101  }
102 
103  public Map<IContact, List<Dossier>> getDossiersMap(String searchValue, boolean onlyActive) {
105  new IContactNameComparator());
106  if(contact != null) {
107  List<Dossier> ld = getDossiers(searchValue, onlyActive);
108  if(viewpoint.equals(ParticipantRole.PARTICIPANT_SUBJECT)) {
109  if(!ld.isEmpty()) {
110  dossiers.put(contact, ld);
111  }
112  for(IContact r : contact.getRelations("Dossiers")) {
113  ld = getDossiers(r, searchValue, onlyActive);
114  if(!ld.isEmpty()) {
115  dossiers.put(r, ld);
116  }
117  }
118  } else {
119  for(Dossier d : ld) {
120  String cId;
121  if(d.getSubject() == null) {
122  cId = Authentication.getIContact().getId();
123  } else {
124  cId = d.getSubject().getIdContact();
125  }
126  List<Dossier> tld = dossiers.getById(cId);
127  if(tld == null) {
128  tld = new ArrayList<Dossier>();
129  tld.add(d);
130  dossiers.putById(cId, tld);
131  } else {
132  tld.add(d);
133  }
134  }
135  }
136  }
137  return dossiers;
138  }
139 
140  public List<Dossier> getDossiers(String searchValue, boolean onlyActive) {
141  return getDossiers(contact, searchValue, onlyActive);
142  }
143 
144  private List<Dossier> getDossiers(IContact contact, String searchValue, boolean onlyActive) {
145  if(Strings.isBlank(searchValue)) return Collections.EMPTY_LIST;
146  WhereClause wc = new WhereClause();
147  wc.addClause("select distinct dossier from Dossier as dossier");
148  wc.addClause("left join dossier.participants participant");
150  //wc.addClause("left outer join dossier.category.participants cparticipant");
151  //wc.addClause("left outer join dossier.fieldValues fieldValue");
152  wc.addClause("where (");
153  wc.addClause("(");
154  wc.addClause("participant.idContact = :idContact");
155  wc.addNamedValue("idContact", contact.getId());
156  if(viewpoint.equals(ParticipantRole.PARTICIPANT_OWNER)) {
157  wc.addClause("and participant.role <> :roleContact");
159  } else {
160  wc.addClause("and participant.role = :roleContact");
161  wc.addNamedValue("roleContact", viewpoint);
162  }
163  wc.addClause(")");
164  if(viewpoint.equals(ParticipantRole.PARTICIPANT_OWNER)) {
165  DossierResults.addCategoryConditionals(wc, "or", contact.getId(), ParticipantRole.PARTICIPANT_SUBJECT, "<>");
166  } else {
167  DossierResults.addCategoryConditionals(wc, "or", contact.getId(), viewpoint, "=");
168  }
169 // wc.addClause(") or (");
170 // wc.addClause("cparticipant.idContact = :cidContact");
171 // wc.addNamedValue("cidContact", contact.getId());
172 // if(viewpoint.equals(ParticipantRole.PARTICIPANT_OWNER)) {
173 // wc.addClause("and cparticipant.role <> :croleContact");
174 // wc.addNamedValue("croleContact", ParticipantRole.PARTICIPANT_SUBJECT);
175 // } else {
176 // wc.addClause("and cparticipant.role = :croleContact");
177 // wc.addNamedValue("croleContact", viewpoint);
178 // }
179 // wc.addClause(")");
180  wc.addClause(")");
181  if(onlyActive) {
182  wc.addClause("and dossier.status = :status");
183  wc.addNamedValue("status", DossierStatus.DOSSIER_OPENED);
184  }
185  if(category != null) {
186  wc.addClause("and (");
187  DossierResults.addCategoryAffiliance(wc, category, "dossier.category");
188  wc.addClause(")");
189 // wc.addClause("and dossier.category = :category");
190 // wc.addNamedValue("category", category);
191  }
192 // wc.addLikeFields(new String[] {
193 // "dossier.description",
194 // "fieldValue.value"
195 // }, searchValue);
196  wc.addClause("and (");
197  wc.setPrefix("");
198  wc.addLikeFields(new String[] {
199  "dossier.description"
200  }, searchValue);
201  wc.setPrefix(null);
202  wc.addClause("or exists (select fieldValue from dossier.fieldValues fieldValue where 1=1");
203  wc.addLikeFields(new String[] {
204  "fieldValue.value"
205  }, searchValue);
206  wc.addClause("))");
207  return new DossierPU().getResultList(wc);
208  }
209 
210 }
static void addCategoryConditionals(WhereClause wc, String sep, String id, ParticipantRole participantRole, String operator)
List< Dossier > getDossiers(String searchValue, boolean onlyActive)
Map< IContact, List< Dossier > > getDossiersMap(String searchValue, boolean onlyActive)
void setViewpoint(ParticipantRole viewpoint)
void addLikeFields(String[] fields, String value)
void addNamedValue(String name, Object value)
List< IContact > getRelations(String id)