BrightSide Workbench Full Report + Source Code
CategoryWrapper.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.List;
22 import org.turro.auth.Authentication;
23 import org.turro.dossier.db.DossierPU;
24 import org.turro.dossier.entity.Category;
25 import org.turro.dossier.entity.IDossierParticipant;
26 import org.turro.dossier.entity.ParticipantRole;
27 import org.turro.jpa.Dao;
28 import org.turro.jpa.entity.EntityCollections;
29 import org.turro.plugin.contacts.IContact;
30 
35 public class CategoryWrapper {
36 
37  private List<ParticipantRole> role;
38 
39  private Category category;
40  private IContact contact;
41 
42  public CategoryWrapper(Category category) {
43  this.category = category;
44  contact = Authentication.getIContact();
45  }
46 
47  public boolean isChildrenParticipant() {
48  boolean result = isParticipant();
49  if(!result) {
50  for(Category c : category.getChildren()) {
52  return true;
53  }
54  }
55  }
56  return result;
57  }
58 
59  public boolean isParticipant() {
60  if(category == null) return false;
61  return getRole() != null && !getRole().isEmpty();
62  }
63 
64  public boolean isOwner() {
65  if(category == null) return false;
66  return getRole().contains(ParticipantRole.PARTICIPANT_OWNER);
67  }
68 
69  public boolean isSubject() {
70  if(category == null) return false;
71  return getRole().contains(ParticipantRole.PARTICIPANT_SUBJECT);
72  }
73 
74  public boolean isAssistant() {
75  if(category == null) return false;
76  return getRole().contains(ParticipantRole.PARTICIPANT_ASSISTANT);
77  }
78 
79  public boolean canDelete() {
80  Dao dao = new DossierPU();
81  List l = dao.getResultList(
82  "select d from Dossier d where d.category = ?",
83  new Object[] { category });
84  if(!l.isEmpty()) {
85  return false;
86  }
87  l = dao.getResultList(
88  "select c from Category c where c.parent = ?",
89  new Object[] { category });
90  if(!l.isEmpty()) {
91  return false;
92  }
93  return true;
94  }
95 
96  public void delete() {
97  Dao dao = new DossierPU();
99  dao.deleteObject(category);
100  }
101 
102  public boolean getCanShowAllAttachments() {
103  if(isOwner()) {
104  return true;
105  } else if(isParticipant()) {
106  for(IDossierParticipant p : category.getFullParticipants()) {
107  if(p.getIdContact().equals(contact.getId()) && p.isShowAllAttachments()) {
108  return true;
109  }
110  }
111  }
112  return false;
113  }
114 
115  private List<ParticipantRole> getRole() {
116  if(role == null && category != null) {
117  role = new ArrayList<ParticipantRole>();
118  for(IDossierParticipant p : category.getFullParticipants()) {
119  if(p.getIdContact() != null && p.getIdContact().equals(contact.getId())) {
120  role.add(p.getRole());
121  }
122  }
123  }
124  return role;
125  }
126 
127 }
Set< Category > getChildren()
Definition: Category.java:84
Set< CategoryParticipant > getParticipants()
Definition: Category.java:116
ParticipantSet< IDossierParticipant > getFullParticipants()
Definition: Category.java:270
void deleteObject(Object obj)
Definition: Dao.java:162
static EntityCollections entities(Collection values)