BrightSide Workbench Full Report + Source Code
DossierWrapper.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.dossier;
19 
20 import java.util.ArrayList;
21 import java.util.Date;
22 import java.util.EnumSet;
23 import java.util.List;
24 import org.turro.string.Strings;
25 import org.turro.action.Contacts;
26 import org.turro.auth.Authentication;
27 import org.turro.dossier.db.DossierPU;
28 import org.turro.dossier.dw.DossierDWReport;
29 import org.turro.dossier.entity.DescriptorDefinition;
30 import org.turro.dossier.entity.DescriptorValue;
31 import org.turro.dossier.entity.Dossier;
32 import org.turro.dossier.entity.DossierType;
33 import org.turro.dossier.entity.IDossierParticipant;
34 import org.turro.dossier.entity.IssueParticipantRole;
35 import org.turro.dossier.entity.Participant;
36 import org.turro.dossier.entity.ParticipantRole;
37 import org.turro.dossier.entity.Project;
38 import org.turro.dossier.issue.IssueWrapper;
39 import org.turro.dossier.search.CategoryResults;
40 import org.turro.dossier.search.IssueResults;
41 import org.turro.elephant.context.Application;
42 import org.turro.elephant.db.WhereClause;
43 import org.turro.i18n.I_;
44 import org.turro.jpa.Dao;
45 import org.turro.jpa.entity.DaoEntity;
46 import org.turro.jpa.entity.EntityCollections;
47 import org.turro.jpa.entity.EntityWebUrls;
48 import org.turro.plugin.contacts.IContact;
49 import org.turro.related.RelatedItem;
50 import org.turro.related.Relateds;
51 import org.turro.util.CompareUtil;
52 import org.turro.util.PhraseBuilder;
53 
58 public class DossierWrapper extends DaoEntity<Dossier, Long> {
59 
60  private IContact contact = Authentication.getIContact();
61  private List<ParticipantRole> role;
62  private List<IssueWrapper> issueAlarms;
63  private String issueParticipant;
64  private DossierDWReport report;
65 
67  super(entity);
68  }
69 
70  @Override
71  protected Dao createDao() {
72  return new DossierPU();
73  }
74 
75  @Override
76  public Dossier save() {
77  RelatedItem rpi = entity.getRelated();
78  if(entity.getCreation() == null) {
79  entity.setCreation(new Date());
80  }
81  if(entity.getType() == null) {
83  }
84  EntityCollections.entities(entity.getParticipants()).removeEmpties();
85  for(Participant p : entity.getParticipants()) {
86  p.setRole(p.getRole()); //ensure properties for owner
87  }
89  EntityCollections.entities(entity.getDescriptors()).removeEmpties();
90  if(DossierType.TYPE_PROJECT.equals(entity.getType())) {
91  Project project = entity.getProject();
92  if(project == null) {
93  project = new Project();
94  project.setPhase(0);
95  project.setProjectTitle(entity.getDescription());
96  entity.setProject(project);
97  project.setDossier(entity);
98  }
99  PhraseBuilder pb = new PhraseBuilder();
100  pb.addWord(project.getProjectCode());
101  pb.addPendingSeparator(" - ");
102  pb.addWord(project.getProjectTitle());
103  if(!pb.isBlank()) {
104  entity.setDescription(pb.toString());
105  }
106  } else {
107  entity.setProject(null);
108  }
109  entity = super.save();
111  if(!Strings.isBlank(rpi.getRelatedPath())) {
114  rpi.getDescription());
115  }
116  if(entity.getId() != null) {
117  String url = Strings.unpunctuateKeepPath(entity.getCategory().getPath()).toLowerCase() +
118  "/" + entity.getId() +
119  "/" + Strings.unpunctuate(entity.getDescription()).toLowerCase();
120  EntityWebUrls.addWebUrl("/dossier/" + entity.getId(), url);
121  }
122  return entity;
123  }
124 
125  @Override
126  public boolean delete() {
127  EntityCollections.entities(entity.getParticipants()).removeEmpties();
129  return super.delete();
130  }
131 
132  @Override
133  protected boolean shouldLog() {
134  return true;
135  }
136 
137  public String getTabLabel() {
138  return getSelfLabel() + " #" + (entity.getId() == null ? "" : entity.getId());
139  }
140 
141  public String getSelfLabel() {
142  if(DossierType.TYPE_PROJECT.equals(entity.getType())) {
143  return I_.get("Project");
144  } else {
145  return I_.get("Dossier");
146  }
147  }
148 
149  public String getTooltiptext() {
150  try {
151  return entity.getFullDescription();
152  } catch(Exception ex) {
153  return null;
154  }
155  }
156 
157  public void setContact(IContact contact) {
158  this.contact = contact;
159  }
160 
161  public boolean isParticipant() {
162  if(entity == null) return false;
163  return getRole() != null && !getRole().isEmpty();
164  }
165 
166  public boolean isOwner() {
167  if(entity == null) return false;
168  return getRole().contains(ParticipantRole.PARTICIPANT_OWNER);
169  }
170 
171  public boolean isSubject() {
172  if(entity == null) return false;
173  return getRole().contains(ParticipantRole.PARTICIPANT_SUBJECT);
174  }
175 
176  public boolean isAssistant() {
177  if(entity == null) return false;
178  return getRole().contains(ParticipantRole.PARTICIPANT_ASSISTANT);
179  }
180 
181  public boolean getCanShowParticipants() {
182  if(entity == null) return false;
183  if(Application.getApplication().isInRole("dossier:participants")) {
184  return true;
185  } else if(isOwner()) {
186  return true;
187  } else if(isParticipant()) {
188  for(IDossierParticipant p : getEntity().getFullParticipants()) {
189  if(p.getIdContact().equals(contact.getId()) && p.isShowParticipants()) {
190  return true;
191  }
192  }
193  }
194  return false;
195  }
196 
197  public boolean getCanShowAllAttachments() {
198  if(entity == null) return false;
199  if(isOwner()) {
200  return true;
201  } else if(isParticipant()) {
202  for(IDossierParticipant p : getEntity().getFullParticipants()) {
203  if(p.getIdContact().equals(contact.getId()) && p.isShowAllAttachments()) {
204  return true;
205  }
206  }
207  }
208  return false;
209  }
210 
211  public boolean getCanShowAllIssues() {
212  if(entity == null) return false;
213  if(isOwner() || Application.getApplication().isInRole("issue:all")) {
214  return true;
215  } else if(isParticipant()) {
216  for(IDossierParticipant p : getEntity().getFullParticipants()) {
217  if(p.getIdContact().equals(contact.getId()) && p.isShowAllIssues()) {
218  return true;
219  }
220  }
221  }
222  return false;
223  }
224 
225  public boolean getReceiveAllEmails() {
226  if(entity == null || !getCanShowAllIssues()) return false;
227  if(isParticipant()) {
228  for(IDossierParticipant p : getEntity().getFullParticipants()) {
229  if(p.getIdContact().equals(contact.getId()) && p.isReceiveAllEmails()) {
230  return true;
231  }
232  }
233  }
234  return false;
235  }
236 
237  public String getIssueParticipantName() {
238  IContact c = Contacts.getContactById(issueParticipant);
239  if(c.isValid()) {
240  return c.getName();
241  }
242  return null;
243  }
244 
245  public String getIssueParticipant() {
246  return issueParticipant;
247  }
248 
249  public void setIssueParticipant(String issueParticipant) {
250  this.issueParticipant = issueParticipant;
251  }
252 
253  public List getIssueAlarms() {
254  if(issueAlarms == null) createIssueAlarms();
255  return issueAlarms;
256  }
257 
259  IssueResults ir = new IssueResults();
260 
261  ir.setDossierId(entity.getId());
262  ir.setIssueValue("*");
263  ir.markAll();
264  if(issueParticipant != null) {
265  ir.setByParticipantId(issueParticipant);
267  ir.setCkOthers(false);
268  }
269 
270  return ir;
271  }
272 
274  if(report == null) {
275  report = new DossierDWReport();
276  report.setReportValues(issueParticipant, IssueParticipantRole.ISSUE_RESPONSIBLE, entity.getId());
277  }
278  return report;
279  }
280 
281  public List<DossierWrapper> getResponsibles() {
282  Dao dao = new DossierPU();
283  WhereClause wc = new WhereClause();
284  wc.addClause("select distinct participant.idContact");
285  wc.addClause("from IssueParticipant as participant");
286  wc.addClause("where participant.issue.dossier = :dossier");
287  wc.addClause("and participant.role = :role");
288  wc.addClause("order by participant.name");
289  wc.addNamedValue("dossier", entity);
291  List<DossierWrapper> l = new ArrayList<>();
292  for(String s : (List<String>) dao.getResultList(wc)) {
294  dw.setIssueParticipant(s);
295  l.add(dw);
296  }
297  return l;
298  }
299 
300  public long getIssueCount() {
301  Dao dao = new DossierPU();
302  WhereClause wc = new WhereClause();
303  wc.addClause("select count(issue) from Issue as issue");
304  wc.addClause("where dossier = :dossier");
305  wc.addNamedValue("dossier", entity);
306  return (Long) dao.getSingleResult(wc);
307  }
308 
309  public Date getIssueLastModification() {
310  Dao dao = new DossierPU();
311  WhereClause wc = new WhereClause();
312  wc.addClause("select max(issue.modification)");
313  wc.addClause("from Issue as issue");
314  wc.addClause("where dossier = :dossier");
315  wc.addNamedValue("dossier", entity);
316  return (Date) dao.getSingleResult(wc);
317  }
318 
319  private void createIssueAlarms() {
320  issueAlarms = new ArrayList<>();
321  for(IssueWrapper wissue : getIssueSearch().getIssueList()) {
322  if(wissue.getIssue().getStatus().isFinished()) continue;
323  if(wissue.hasAlarm()) {
324  issueAlarms.add(wissue);
325  }
326  }
327  }
328 
329  private List<ParticipantRole> getRole() {
330  if(role == null && getEntity() != null) {
331  role = new ArrayList<>();
332  for(IDossierParticipant p : getEntity().getFullParticipants()) {
333  if(p.getIdContact() != null && p.getIdContact().equals(contact.getId())) {
334  role.add(p.getRole());
335  }
336  }
337  }
338  return role;
339  }
340 
341  public boolean isEmptyDescriptors(String grouping, boolean required) {
342  for(DescriptorDefinition dd : entity.getDescriptorSet()) {
343  if("all".equals(grouping) || CompareUtil.compare(grouping, dd.getGrouping()) == 0) {
344  DescriptorValue dv = dd.getValue();
345  if(required && dd.isRequired() && (dv == null || dv.isEmpty())) {
346  return true;
347  }
348  }
349  }
350  return false;
351  }
352 
353 }
static IContact getContactById(String id)
Definition: Contacts.java:72
static String getObjectPath(Object object)
Definition: DossierPU.java:66
boolean isEmptyDescriptors(String grouping, boolean required)
void setIssueParticipant(String issueParticipant)
void setIssueValue(String issueValue)
void setRoles(Set< IssueParticipantRole > roles)
void addNamedValue(String name, Object value)
static String get(String msg)
Definition: I_.java:41
Object getSingleResult(WhereClause wc)
Definition: Dao.java:380
boolean equals(Object obj)
Definition: DaoEntity.java:154
static EntityCollections entities(Collection values)
static EntityWebUrl addWebUrl(String entityPath, String url)
Relateds removeAny(String removePath)
Definition: Relateds.java:100
static Relateds from(String entityPath)
Definition: Relateds.java:145
static Relateds empty()
Definition: Relateds.java:149
Related addDestination(String destination, String description)
Definition: Relateds.java:41