BrightSide Workbench Full Report + Source Code
AttachEntities.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2017 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.entities;
20 
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
30 import org.amic.util.file.FileUtil;
31 import org.turro.string.Strings;
32 import org.turro.action.AcceptanceSingleStatus;
33 import org.turro.annotation.ElephantEntity;
34 import org.turro.attach.db.AttachPU;
35 import org.turro.attach.entity.Attachment;
36 import org.turro.attach.zul.AttachmentUtil;
37 import org.turro.collections.KeyValueMap;
38 import org.turro.elephant.context.ElephantContext;
39 import org.turro.elephant.db.WhereClause;
40 import org.turro.file.FileWrapper;
41 import org.turro.i18n.I_;
42 import org.turro.jpa.Dao;
43 import org.turro.path.Path;
44 import org.turro.plugin.contacts.IContact;
45 import org.turro.wd.entities.AttachWd;
46 
51 @ElephantEntity
52 public class AttachEntities extends DefaultElephantEntity {
53 
54  public AttachEntities() {
55  }
56 
57  public AttachEntities(String entityPath) {
58  super(entityPath);
59  }
60 
61  public AttachEntities(Object entity) {
62  super(entity);
63  }
64 
65  @Override
66  public Object getEntity() {
67  if(entity == null) {
68  if(getPath() != null) {
69  if(path.getSize() == 2) {
70  if(null != path.getRoot()) switch (path.getRoot()) {
71  case "attachment":
72  if(isValidLongEntity()) {
73  entity = getDao().find(Attachment.class, path.getNodeAs(1, Long.class));
74  }
75  break;
76  case "file":
77  entity = new File(ElephantContext.getRealPath(path.getFrom(1)));
78  break;
79  default:
80  break;
81  }
82  }
83  }
84  }
85  return entity;
86  }
87 
88  @Override
89  public String getName() {
90  if(getEntity() != null) {
91  if(entity instanceof Attachment) {
92  return ((Attachment) entity).getFileName();
93  } else if(entity instanceof File) {
94  return ((File) entity).getName();
95  }
96  }
97  return null;
98  }
99 
100  @Override
101  public String getLabel() {
102  if(getPath() != null) {
103  String root = path.getRoot();
104  if(null != root) switch (root) {
105  case "attachment":
106  return I_.get("Attachment");
107  case "file":
108  return I_.get("File");
109  default:
110  break;
111  }
112  }
113  return null;
114  }
115 
116  @Override
117  public String getDescription() {
118  return null;
119  }
120 
121  @Override
122  public Object getLabelCtrl() {
123  return null;
124  }
125 
126  @Override
127  public String getImage() {
128  if(getPath() != null) {
129  String root = path.getRoot();
130  if(null != root) switch (root) {
131  case "attachment":
132  return "/_zul/images/clip.png";
133  case "file":
134  return "/_zul/images/clip.png";
135  default:
136  break;
137  }
138  }
139  return null;
140  }
141 
142  public String getMimeImage() {
143  if(getPath() != null) {
144  String root = path.getRoot();
145  if(null != root) switch (root) {
146  case "attachment":
147  return getMimeImage(entity);
148  case "file":
149  return getMimeImage(entity);
150  default:
151  break;
152  }
153  }
154  return null;
155  }
156 
157  @Override
158  public String getHierarchicalPath() {
159  if(getEntity() != null) {
160  if(entity instanceof Attachment) {
161  IElephantEntity iee = ((Attachment) entity).getOwnerEntity();
162  String hp = iee.getHierarchicalPath();
163  return (Strings.isBlank(hp) ? "" : hp + PATH_SEPARATOR) + getPath();
164  }
165  }
166  return getPath();
167  }
168 
169  @Override
171  if(getEntity() != null) {
172  if(entity instanceof Attachment) {
173  return ((Attachment) entity).getOwnerEntity();
174  }
175  }
176  return null;
177  }
178 
179  @Override
180  public List<IElephantEntity> getChildren() {
181  return Collections.EMPTY_LIST;
182  }
183 
184  @Override
185  public String getExtendedPath() {
187  }
188 
189  @Override
190  public File getFile() {
191  if(getEntity() != null) {
192  if(entity instanceof Attachment) {
193  try {
194  File file = File.createTempFile("attach_", "_mail");
196  return file;
197  } catch (IOException ex) {
198  Logger.getLogger(AttachEntities.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
199  }
200  } else if(entity instanceof File) {
201  return (File) entity;
202  }
203  }
204  return null;
205  }
206 
207  @Override
208  public boolean showPath() {
209  return false;
210  }
211 
212  @Override
213  public boolean showEntity() {
214  if(getEntity() != null) {
215  if(entity instanceof Attachment) {
217  return true;
218  } else if(entity instanceof File) {
219  new FileWrapper((File) entity).download();
220  return true;
221  }
222  }
223  return false;
224  }
225 
226  @Override
227  public boolean canEdit(IContact contact) {
228  if(getEntity() != null) {
229  if(entity instanceof Attachment) {
230  return contact.getPermissions().isInRole("attach:list");
231  } else if(entity instanceof File) {
232  return contact.getPermissions().isInRole("file-attach:list");
233  }
234  }
235  return false;
236  }
237 
238  @Override
239  public boolean canPublish(IContact contact) {
240  if(getEntity() != null) {
241  if(entity instanceof Attachment) {
242  Attachment attachment = (Attachment) entity;
243  return contact.getPermissions().isInRole("attach-show:all") ||
244  (
245  attachment.isPublishable() &&
246  ((
247  attachment.isRequiresAcceptance() &&
248  attachment.getSingleStatus(contact).equals(AcceptanceSingleStatus.SINGLE_ACCEPTED)
249  ) || !attachment.isRequiresAcceptance())
250  );
251  } else if(entity instanceof File) {
252  return true; // Controlled by owner entity
253  }
254  }
255  return false;
256  }
257 
258  @Override
259  public Collection getEntitites(String search, int maxResults, KeyValueMap kvm) {
260  ArrayList<Object> list = new ArrayList<>();
261  list.addAll(searchAttachments(search, maxResults, kvm));
262  return list;
263  }
264 
265  @Override
266  public Collection getEntitites(String root, String search, int maxResults, KeyValueMap kvm) {
267  ArrayList<Object> list = new ArrayList<>();
268  if(null != root) switch (root) {
269  case "attachment":
270  list.addAll(searchAttachments(search, maxResults, kvm));
271  break;
272  default:
273  break;
274  }
275  return list;
276  }
277 
278  @Override
279  public Object configureCtrl(Object ctrl, IContact contact) {
280  return ctrl;
281  }
282 
283  @Override
284  public boolean canShowInContext(IContact contact, Object relatedEntity) {
285  return true;
286  }
287 
288  @Override
289  public String getExtendedWebPath() {
290  return null;
291  }
292 
293  @Override
294  public String getUserContext() {
295  return null;
296  }
297 
298  @Override
299  protected Dao createDao() {
300  return new AttachPU();
301  }
302 
303  @Override
304  protected boolean itsMine(String rootPath) {
305  return super.itsMine(rootPath) || "file".equals(rootPath);
306  }
307 
308  @Override
309  protected boolean itsMine(Object entity) {
310  return (entity instanceof Attachment) || (entity instanceof File);
311  }
312 
313  @Override
314  public Collection<String> getAllowedRoots() {
315  return Arrays.asList("attachment", "file");
316  }
317 
318  @Override
320  return new AttachEntities(entityPath);
321  }
322 
323  @Override
325  return new AttachEntities(entity);
326  }
327 
328  @Override
329  protected Path createPath() {
330  return new Path(AttachPU.getObjectPath(entity));
331  }
332 
333  private List<Object> searchAttachments(String search, int maxResults, KeyValueMap kvm) {
334  WhereClause wc = new WhereClause();
335  wc.addClause("select c from Attachment c");
336  wc.addClause("where c.fileName like :search");
337  wc.addClause("order by c.fileName");
338  wc.addNamedValue("search", "%" + search + "%");
339  return getDao().getResultList(wc, maxResults);
340  }
341 
342  private String getMimeImage(Object object) {
343  if(object instanceof Attachment) {
344  Attachment attachment = (Attachment) object;
345  if(new File(ElephantContext.getRealPath("/_internal/system/mime/" + attachment.getFileExtension() + ".png")).exists()) {
346  return "/_internal/system/mime/" + attachment.getFileExtension() + ".png";
347  }
348  } else if(object instanceof File) {
349  File file = (File) object;
350  if(file.exists()) {
351  return "/_internal/system/mime/" + FileUtil.getExtension(file) + ".png";
352  }
353  }
354  return "/_internal/system/mime/empty.png";
355  }
356 
357 }
static String getObjectExtendedPath(Object object)
Definition: AttachPU.java:66
static String getObjectPath(Object object)
Definition: AttachPU.java:57
AcceptanceSingleStatus getSingleStatus()
static boolean download(Attachment attachment)
void addNamedValue(String name, Object value)
boolean canShowInContext(IContact contact, Object relatedEntity)
Collection getEntitites(String root, String search, int maxResults, KeyValueMap kvm)
List< IElephantEntity > getChildren()
Collection getEntitites(String search, int maxResults, KeyValueMap kvm)
IElephantEntity createInstance(Object entity)
Object configureCtrl(Object ctrl, IContact contact)
IElephantEntity createInstance(String entityPath)
Collection< String > getAllowedRoots()
boolean itsMine(String rootPath)
boolean canPublish(IContact contact)
boolean canEdit(IContact contact)
static String get(String msg)
Definition: I_.java:41
static void writeToFile(Attachment attachment, File file)
Definition: AttachWd.java:116