BrightSide Workbench Full Report + Source Code
elephant-attach/src/main/java/org/turro/documentation/DocumentDefinition.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.documentation;
20 
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Objects;
26 import java.util.TreeMap;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29 import org.turro.string.Strings;
30 import org.turro.attach.db.AttachPU;
31 import org.turro.elephant.context.ElephantContext;
32 import org.turro.file.Document;
33 import org.turro.file.Folder;
34 import org.turro.plugin.contacts.IContact;
35 import org.turro.sql.SqlClause;
36 import org.turro.util.CompareUtil;
37 
42 public class DocumentDefinition implements Comparable<DocumentDefinition> {
43 
44  public final String name;
45  public final DocumentType type;
46  public String description, allowedRoot;
47  public boolean allowMultiple, uploadControl;
48  public int min, max;
49 
51  this.name = name;
52  this.type = type;
53  this.min = 1;
54  this.max = 1;
55  }
56 
57  public DocumentDefinition allowedRoot(String root) {
58  this.allowedRoot = root;
59  return this;
60  }
61 
62  public DocumentDefinition description(String text) {
63  this.description = text;
64  return this;
65  }
66 
68  this.allowMultiple = true;
69  return this;
70  }
71 
73  this.uploadControl = true;
74  return this;
75  }
76 
77  public DocumentDefinition min(int min) {
78  this.min = min;
79  return this;
80  }
81 
82  public DocumentDefinition max(int max) {
83  this.max = max;
84  return this;
85  }
86 
87  public String getName() {
88  return name;
89  }
90 
91  public DocumentType getType() {
92  return type;
93  }
94 
95  public void setAllowedRoot(String allowedRoot) {
96  this.allowedRoot = allowedRoot;
97  }
98 
99  public String getAllowedRoot() {
100  return allowedRoot;
101  }
102 
103  public void setDescription(String description) {
104  this.description = description;
105  }
106 
107  public String getDescription() {
108  return description;
109  }
110 
111  public boolean isAllowMultiple() {
112  return allowMultiple;
113  }
114 
115  public void setAllowMultiple(boolean allowMultiple) {
116  this.allowMultiple = allowMultiple;
117  }
118 
119  public boolean isUploadControl() {
120  return uploadControl;
121  }
122 
123  public void setUploadControl(boolean uploadControl) {
124  this.uploadControl = uploadControl;
125  }
126 
127  public void setMin(int min) {
128  this.min = min;
129  }
130 
131  public int getMin() {
132  return min;
133  }
134 
135  public void setMax(int max) {
136  this.max = max;
137  }
138 
139  public int getMax() {
140  return max;
141  }
142 
143  /* Utils */
144 
145  public boolean isInformation() {
146  return !Strings.isBlank(description) || !getFolder().isEmpty();
147  }
148 
149  public boolean isFiltered() {
150  return !"*".equals(Strings.isBlank(allowedRoot, "*"));
151  }
152 
153  public boolean matches(String root) {
154  return !isFiltered() || (CompareUtil.compare(root, allowedRoot) == 0);
155  }
156 
157  public Map<String, List<Document>> getDocumentsMap() {
158  Map<String, List<Document>> docs = new TreeMap<>();
159  Folder folder = getFolder();
160  try {
161  if(folder.exists()) docs.put("*", folder.documents());
162  for(Folder child : folder.folders()) {
163  docs.put(child.name(), child.documents());
164  }
165  } catch (IOException ex) {
166  Logger.getLogger(DocumentDefinition.class.getName()).log(Level.SEVERE, null, ex);
167  }
168  return docs;
169  }
170 
171  public List<Document> getDocumentsFor(IContact contact) {
172  List<Document> docs = new ArrayList<>();
173  Folder folder = getFolder();
174  try {
175  if(folder.exists()) docs.addAll(folder.documents());
176  Folder selfDocs = folder.child(contact.getId());
177  if(selfDocs.exists()) docs.addAll(selfDocs.documents());
178  IContact parent = contact.getBusiness();
179  if(parent != null) {
180  Folder parentDocs = folder.child(parent.getId());
181  if(parentDocs.exists()) docs.addAll(parentDocs.documents());
182  }
183  } catch (IOException ex) {
184  Logger.getLogger(DocumentDefinition.class.getName()).log(Level.SEVERE, null, ex);
185  }
186  return docs;
187  }
188 
189  public Folder getFolder() {
190  return Folder.from(ElephantContext.getRealPath(getFolderString()));
191  }
192 
193  public String getFolderString() {
194  return "/WEB-INF/files/doc-references/" + getFolderName();
195  }
196 
197  public String getFolderName() {
198  return "hash-" + hashCode() + "-def";
199  }
200 
201  public boolean canDelete() {
202  return SqlClause.select("count(ed)")
203  .from("EntityDocumentation ed")
204  .where().equal("ed.documentation", name)
205  .dao(new AttachPU())
206  .singleResult(Long.class) == 0L;
207  }
208 
209  @Override
210  public int hashCode() {
211  int hash = 7;
212  hash = 73 * hash + Objects.hashCode(this.name);
213  return hash;
214  }
215 
216  @Override
217  public boolean equals(Object obj) {
218  if (this == obj) {
219  return true;
220  }
221  if (obj == null) {
222  return false;
223  }
224  if (getClass() != obj.getClass()) {
225  return false;
226  }
227  final DocumentDefinition other = (DocumentDefinition) obj;
228  return Objects.equals(this.name, other.name);
229  }
230 
231  @Override
233  return CompareUtil.compare(name, o.name);
234  }
235 
236 }