BrightSide Workbench Full Report + Source Code
Category.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.entity;
19 
20 import java.util.ArrayList;
21 import java.util.HashSet;
22 import java.util.List;
23 import java.util.Set;
24 import javax.persistence.CascadeType;
25 import javax.persistence.Column;
26 import javax.persistence.Entity;
27 import javax.persistence.FetchType;
28 import javax.persistence.Id;
29 import javax.persistence.Index;
30 import javax.persistence.ManyToOne;
31 import javax.persistence.OneToMany;
32 import javax.persistence.OrderBy;
33 import javax.persistence.Table;
34 import org.turro.string.Strings;
35 import org.turro.action.Contacts;
36 import org.turro.collections.Hierarchy;
37 import org.turro.dossier.db.DossierPU;
38 import org.turro.dossier.dossier.ParticipantSet;
39 import org.turro.elephant.db.IdUtils;
40 import org.turro.elephant.db.WhereClause;
41 import org.turro.jpa.Dao;
42 import org.turro.jpa.entity.IDaoEntity;
43 import org.turro.plugin.contacts.IContact;
44 import org.turro.util.Chars;
45 
50 @Entity
51 @Table(indexes={
52  @Index(name="PathIndex", columnList="uniquePath")
53 })
54 public class Category implements java.io.Serializable, IDaoEntity {
55 
56  @Id
57  @Column(name="IDENTIFIER")
58  private Long id;
59 
60  private String description;
61 
62  private String fullDescription, uniquePath;
63 
64  private boolean knowledgeBase, ownsProjects;
65 
66  @ManyToOne
67  private Category parent;
68 
69  @OneToMany(mappedBy = "parent", fetch = FetchType.EAGER)
70  @OrderBy(value="description")
71  private Set<Category> children = new HashSet<Category>();
72 
73  @OneToMany(mappedBy = "category", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval=true)
74  @OrderBy(value="name")
75  private Set<CategoryParticipant> participants = new HashSet<CategoryParticipant>();
76 
77  @OneToMany(mappedBy = "category", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval=true)
78  @OrderBy(value="orderNumber")
79  private Set<DescriptorDefinition> descriptors = new HashSet<DescriptorDefinition>();
80 
81  @OneToMany(mappedBy = "category", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval=true)
82  private Set<WorthDefinition> worths = new HashSet<WorthDefinition>();
83 
84  public Set<Category> getChildren() {
85  return children;
86  }
87 
88  public void setChildren(Set<Category> children) {
89  this.children = children;
90  }
91 
92  public String getDescription() {
93  return description;
94  }
95 
96  public void setDescription(String description) {
97  this.description = description;
98  }
99 
100  public Long getId() {
101  return id;
102  }
103 
104  public void setId(Long id) {
105  this.id = id;
106  }
107 
108  public Category getParent() {
109  return parent;
110  }
111 
112  public void setParent(Category parent) {
113  this.parent = parent;
114  }
115 
116  public Set<CategoryParticipant> getParticipants() {
117  Dao dao = new DossierPU();
118  if(id != null && id > 0 && dao.isNotLoaded(participants)) {
119  participants = dao.lazyLoader(Category.class, this, "participants").participants;
120  }
121  return participants;
122  }
123 
124  public void setParticipants(Set<CategoryParticipant> participants) {
125  this.participants = participants;
126  }
127 
128  public String getFullDescription() {
129  return fullDescription;
130  }
131 
132  public void setFullDescription(String fullDescription) {
133  this.fullDescription = fullDescription;
134  }
135 
136  public String getUniquePath() {
137  return uniquePath;
138  }
139 
140  public void setUniquePath(String uniquePath) {
141  this.uniquePath = uniquePath;
142  }
143 
144  public boolean isKnowledgeBase() {
145  return knowledgeBase;
146  }
147 
148  public void setKnowledgeBase(boolean knowledgeBase) {
149  this.knowledgeBase = knowledgeBase;
150  }
151 
152  public boolean isOwnsProjects() {
153  return ownsProjects;
154  }
155 
156  public void setOwnsProjects(boolean ownsProjects) {
157  this.ownsProjects = ownsProjects;
158  }
159 
160  public Set<DescriptorDefinition> getDescriptors() {
161  return descriptors;
162  }
163 
164  public void setDescriptors(Set<DescriptorDefinition> descriptors) {
165  this.descriptors = descriptors;
166  }
167 
168  public Set<WorthDefinition> getWorths() {
169  return worths;
170  }
171 
172  public void setWorths(Set<WorthDefinition> worths) {
173  this.worths = worths;
174  }
175 
176  /* IDaoEntity */
177 
178  @Override
179  public Object entityId() {
180  return id;
181  }
182 
183  @Override
184  public boolean isEmpty() {
185  return Strings.isBlank(description);
186  }
187 
188  @Override
189  public void prepareSave() {
190  IDaoEntity.super.prepareSave();
191  if(id == null || id == 0) {
192  id = IdUtils.getNewLongIdFromLong(new DossierPU(), "Category", "id");
193  } else {
194  if(Hierarchy.checkLoop(this, (c) -> c.id, (c) -> c.parent)) {
195  parent = null;
196  }
197  }
198  }
199 
200  /* Helpers */
201 
203  if(contact == null) return null;
205  p.setCategory(this);
206  p.setIdContact(contact.getId());
207  p.setName(contact.getName());
209  getParticipants().add(p);
210  return p;
211  }
212 
214  if(contact == null) return null;
216  p.setCategory(this);
217  p.setIdContact(contact.getId());
218  p.setName(contact.getName());
220  getParticipants().add(p);
221  return p;
222  }
223 
224  public boolean isDirectOwner(IContact contact) {
225  for(CategoryParticipant p : getParticipants()) {
226  if(p.getRole().equals(ParticipantRole.PARTICIPANT_OWNER)) {
227  if(contact.getId().equals(p.getIdContact())) {
228  return true;
229  }
230  }
231  }
232  return false;
233  }
234 
235  public boolean isBranchAdmin(IContact contact) {
236  for(CategoryParticipant p : getParticipants()) {
237  if(p.getRole().equals(ParticipantRole.PARTICIPANT_OWNER) &&
238  contact.getId().equals(p.getIdContact()) &&
239  p.isAdmin()) {
240  return true;
241  }
242  }
243  return false;
244  }
245 
246  public List<IDossierParticipant> getDirectOwners() {
247  List<IDossierParticipant> list = new ArrayList<>();
248  for(CategoryParticipant p : getParticipants()) {
249  if(p.getRole().equals(ParticipantRole.PARTICIPANT_OWNER)) {
250  list.add(p);
251  }
252  }
253  return list;
254  }
255 
256  public String getPath() {
257  if(!Strings.isBlank(fullDescription)) {
258  return "/" + fullDescription.replaceAll("\\s*\\" + Chars.backward() + "\\s*", "\\/");
259  }
260  return null;
261  }
262 
263  public static String getPathToFullDescription(String path) {
264  if(!Strings.isBlank(path)) {
265  return path.substring(1).replaceAll("\\/", " \\" + Chars.backward() + " ");
266  }
267  return null;
268  }
269 
271  ParticipantSet<IDossierParticipant> set = new ParticipantSet<>(getParticipants());
272  Category c = getParent();
273  while(c != null) {
274  set.addAll(c.getParticipants());
275  c = c.getParent();
276  }
277  return set;
278  }
279 
281  for(CategoryParticipant p : getParticipants()) {
282  if(p.getRole().equals(role)) {
283  return Contacts.getContactById(p.getIdContact());
284  }
285  }
286  if(getParent() != null) {
287  return getParent().getDefaultResponsible(role);
288  }
289  return null;
290  }
291 
292  public List<Dossier> getDossiers() {
293  WhereClause wc = new WhereClause();
294  wc.addClause("select d from Dossier d");
295  wc.addClause("where d.category = :category");
296  wc.addNamedValue("category", this);
297  Dao dao = new DossierPU();
298  return dao.getResultList(Dossier.class, wc);
299  }
300 
301 }
static IContact getContactById(String id)
Definition: Contacts.java:72
boolean isBranchAdmin(IContact contact)
Definition: Category.java:235
Set< CategoryParticipant > getParticipants()
Definition: Category.java:116
static String getPathToFullDescription(String path)
Definition: Category.java:263
Set< WorthDefinition > getWorths()
Definition: Category.java:168
void setParticipants(Set< CategoryParticipant > participants)
Definition: Category.java:124
void setOwnsProjects(boolean ownsProjects)
Definition: Category.java:156
boolean isDirectOwner(IContact contact)
Definition: Category.java:224
ParticipantSet< IDossierParticipant > getFullParticipants()
Definition: Category.java:270
void setDescription(String description)
Definition: Category.java:96
void setParent(Category parent)
Definition: Category.java:112
void setWorths(Set< WorthDefinition > worths)
Definition: Category.java:172
void setUniquePath(String uniquePath)
Definition: Category.java:140
CategoryParticipant addAssistant(IContact contact)
Definition: Category.java:213
List< IDossierParticipant > getDirectOwners()
Definition: Category.java:246
void setFullDescription(String fullDescription)
Definition: Category.java:132
Set< DescriptorDefinition > getDescriptors()
Definition: Category.java:160
IContact getDefaultResponsible(ParticipantRole role)
Definition: Category.java:280
void setChildren(Set< Category > children)
Definition: Category.java:88
void setKnowledgeBase(boolean knowledgeBase)
Definition: Category.java:148
void setDescriptors(Set< DescriptorDefinition > descriptors)
Definition: Category.java:164
CategoryParticipant addOwner(IContact contact)
Definition: Category.java:202
static long getNewLongIdFromLong(Dao dao, String table, String field)
Definition: IdUtils.java:33
void addNamedValue(String name, Object value)
boolean isNotLoaded(Object o, String attribute)
Definition: Dao.java:216