BrightSide Workbench Full Report + Source Code
EntityDocumentation.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 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 
19 package org.turro.attach.entity;
20 
21 import java.io.IOException;
22 import java.io.Serializable;
23 import java.util.Collections;
24 import java.util.Date;
25 import java.util.HashSet;
26 import java.util.List;
27 import java.util.Set;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
30 import javax.persistence.ElementCollection;
31 import javax.persistence.Entity;
32 import javax.persistence.FetchType;
33 import javax.persistence.Id;
34 import javax.persistence.IdClass;
35 import javax.persistence.Temporal;
36 import javax.persistence.TemporalType;
37 import org.turro.action.Contacts;
38 import org.turro.auth.Authentication;
39 import org.turro.documentation.DocumentDefinition;
40 import org.turro.documentation.DocumentDefinitions;
41 import org.turro.elephant.context.ElephantContext;
42 import org.turro.entities.Entities;
43 import org.turro.entities.IElephantEntity;
44 import org.turro.file.Document;
45 import org.turro.file.Folder;
46 import org.turro.plugin.contacts.IContact;
47 
52 @Entity
53 @IdClass(EntityDocumentationPK.class)
54 public class EntityDocumentation implements Serializable {
55 
56  @Id private String entityPath;
57  @Id private String contactId;
58  @Id private String documentation;
59 
60  private String requesterId;
61  private EntityDocumentationStatus status;
62  private String comment;
63 
64  @Temporal(TemporalType.TIMESTAMP)
65  private Date deadline;
66 
67  @ElementCollection(fetch = FetchType.EAGER)
68  private Set<String> assistantIds = new HashSet<>();
69 
70  public String getEntityPath() {
71  return entityPath;
72  }
73 
74  public void setEntityPath(String entityPath) {
75  this.entityPath = entityPath;
76  }
77 
78  public String getContactId() {
79  return contactId;
80  }
81 
82  public void setContactId(String contactId) {
83  this.contactId = contactId;
84  }
85 
86  public String getDocumentation() {
87  return documentation;
88  }
89 
90  public void setDocumentation(String documentation) {
91  this.documentation = documentation;
92  }
93 
94  public String getRequesterId() {
95  return requesterId;
96  }
97 
98  public void setRequesterId(String requesterId) {
99  this.requesterId = requesterId;
100  }
101 
103  return status;
104  }
105 
106  public void setStatus(EntityDocumentationStatus status) {
107  this.status = status;
108  }
109 
110  public String getComment() {
111  return comment;
112  }
113 
114  public void setComment(String comment) {
115  this.comment = comment;
116  }
117 
118  public Date getDeadline() {
119  return deadline;
120  }
121 
122  public void setDeadline(Date deadline) {
123  this.deadline = deadline;
124  }
125 
126  public Set<String> getAssistantIds() {
127  return assistantIds;
128  }
129 
130  public void setAssistantIds(Set<String> assistantIds) {
131  this.assistantIds = assistantIds;
132  }
133 
134  /* Utils */
135 
136  public boolean isValid(List<EntityDocumentation> invalids) {
137  if(getDefinition() == null) {
138  invalids.add(this);
139  return false;
140  }
141  return true;
142  }
143 
144  public void reviewStatus() {
145  if(getFolder().count() < getDefinition().getMin()) {
147  } else if(!EntityDocumentationStatus.validationStatus().contains(status)) {
149  }
150  }
151 
152  public boolean allowsUpload() throws IOException {
153  return (!EntityDocumentationStatus.VALIDATED.equals(status) &&
154  (getDefinition().isUploadControl() || getFolder().count() < getDefinition().getMax())) ||
156  }
157 
158  public boolean allowsValidation() {
159  return EntityDocumentationStatus.PENDING_VALIDATION.equals(status);
160  }
161 
162  public boolean allowsDeny() {
163  return allowsValidation() || (Authentication.isAdministrator() &&
164  EntityDocumentationStatus.VALIDATED.equals(status));
165  }
166 
167  public boolean allowsDelete() throws IOException {
168  return !(getDefinition().isUploadControl() || EntityDocumentationStatus.VALIDATED.equals(status)) ||
170  }
171 
172  public boolean allowsDeleteDef() throws IOException {
173  return getFolder().isEmpty() || Authentication.isAdministrator();
174  }
175 
176  public List<Document> getDocuments() {
177  Folder folder = getFolder();
178  if(folder.exists()) try {
179  return folder.documents();
180  } catch (IOException ex) {
181  Logger.getLogger(EntityDocumentation.class.getName()).log(Level.SEVERE, null, ex);
182  }
183  return Collections.EMPTY_LIST;
184  }
185 
186  public Folder getFolder() {
187  return Folder.from(ElephantContext.getRealPath(getFolderString()));
188  }
189 
190  public String getFolderString() {
191  DocumentDefinition dd = getDefinition();
192  return "/WEB-INF/files/" + entityPath + "/documentation/" +
193  dd.getFolderName() + "/" + contactId;
194  }
195 
197  return DocumentDefinitions.instance().get(documentation);
198  }
199 
201  return Entities.getController(entityPath);
202  }
203 
204  public IContact getContact() {
205  return Contacts.getContactById(contactId);
206  }
207 
209  return Contacts.getContactById(requesterId);
210  }
211 
212  public List<IContact> getAssistants() {
213  return assistantIds.stream().map(id -> Contacts.getContactById(id)).toList();
214  }
215 
216 }
static IContact getContactById(String id)
Definition: Contacts.java:72
void setAssistantIds(Set< String > assistantIds)
void setStatus(EntityDocumentationStatus status)
boolean isValid(List< EntityDocumentation > invalids)
static IElephantEntity getController(String path)
Definition: Entities.java:78
static EnumSet< EntityDocumentationStatus > validationStatus()