BrightSide Workbench Full Report + Source Code
Attachment.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.attach.entity;
19 
20 import com.lowagie.text.DocumentException;
21 import java.io.ByteArrayInputStream;
22 import java.io.ByteArrayOutputStream;
23 import java.io.IOException;
24 import java.text.DateFormat;
25 import java.util.Date;
26 import java.util.HashSet;
27 import javax.persistence.*;
28 import org.amic.util.date.CheckDate;
29 import org.amic.util.date.DateFormats;
30 import org.turro.formatter.BytesFormatter;
31 import org.turro.string.Strings;
32 import org.turro.action.AcceptanceSingleStatus;
33 import org.turro.action.Contacts;
34 import org.turro.action.IAcceptances;
35 import org.turro.action.Plugins;
36 import org.turro.attach.db.AttachPU;
37 import org.turro.attach.www.AttachDownload;
38 import org.turro.attach.zul.AttachmentUtil;
39 import org.turro.auth.Authentication;
40 import org.turro.elephant.context.Application;
41 import org.turro.elephant.context.IConstructor;
42 import org.turro.entities.Entities;
43 import org.turro.entities.IElephantEntity;
44 import org.turro.i18n.I_;
45 import org.turro.path.Path;
46 import org.turro.pdf.Watermark;
47 import org.turro.pdf.WatermarkCfg;
48 import org.turro.plugin.contacts.IContact;
49 
50 @Entity
51 @NamedQueries({
52  @NamedQuery(name="attachmentByPartialFileName", query="select a from Attachment a where UCASE(a.path) like UCASE(:path) and UCASE(a.fileName) like UCASE(:fileName) order by path, fileName, modification"),
53  @NamedQuery(name="attachmentByPartialFileNameNV", query="select a from Attachment a where UCASE(a.path) like UCASE(:path) and UCASE(a.fileName) like UCASE(:fileName) and validated = false order by path, fileName, modification"),
54  @NamedQuery(name="attachmentByPartialFileNameV", query="select a from Attachment a where UCASE(a.path) like UCASE(:path) and UCASE(a.fileName) like UCASE(:fileName) and validated = true order by path, fileName, modification"),
55  @NamedQuery(name="attachmentByPartialPath", query="select a from Attachment a where UCASE(a.path) like UCASE(:path) order by path, fileName, modification"),
56  @NamedQuery(name="attachmentByPath", query="select a from Attachment a where a.path = :path order by path, fileName, modification"),
57  @NamedQuery(name="pathByRoot", query="select distinct a.path from Attachment a where UCASE(a.path) like UCASE(:root) order by a.path DESC"),
58  @NamedQuery(name="pathByRootNV", query="select distinct a.path from Attachment a where UCASE(a.path) like UCASE(:root) and validated = false order by a.path DESC")
59 })
60 public class Attachment implements java.io.Serializable {
61 
62  @Id
63  @GeneratedValue(strategy=GenerationType.IDENTITY)
64  @Column(name="IDENTIFIER")
65  private Long id;
66 
67  @Column(name="ATTACH_PATH")
68  private String path;
69 
70  private String relatedPath;
71 
72  private String owner;
73 
74  private String showKey;
75 
76  private boolean validated;
77 
78  private boolean locked;
79 
80  private String locker;
81 
82  private boolean publishable;
83 
84  @Temporal(value = javax.persistence.TemporalType.TIMESTAMP)
85  private java.util.Date lockDate;
86 
87  @Temporal(value = javax.persistence.TemporalType.TIMESTAMP)
88  private java.util.Date lockCommit;
89 
90  @Lob
91  @Column(length=4096)
92  private String lockComment;
93 
94  private boolean onlyOwner;
95 
96  @Temporal(value = javax.persistence.TemporalType.TIMESTAMP)
97  private java.util.Date modification;
98 
99  @Lob
100  @Column(length=4096)
101  private String comment;
102 
103  private String fileName;
104 
105  private String fileContentType;
106 
107  private long fileSize;
108 
109  private boolean requiresAcceptance;
110 
111  private boolean watermark;
112 
113  @OneToOne(fetch=FetchType.LAZY,cascade={CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REMOVE}, orphanRemoval=true)
114  private AttachContent attachContent;
115 
117  return attachContent;
118  }
119 
120  public void setAttachContent(AttachContent attachContent) {
121  this.attachContent = attachContent;
122  }
123 
124  public Long getId() {
125  return id;
126  }
127 
128  public void setId(Long id) {
129  this.id = id;
130  }
131 
132  public String getOwner() {
133  return owner;
134  }
135 
136  public void setOwner(String owner) {
137  this.owner = owner;
138  }
139 
140  public boolean isOnlyOwner() {
141  return onlyOwner;
142  }
143 
144  public void setOnlyOwner(boolean onlyOwner) {
145  this.onlyOwner = onlyOwner;
146  }
147 
148  public String getLockComment() {
149  return lockComment;
150  }
151 
152  public void setLockComment(String lockComment) {
153  this.lockComment = lockComment;
154  }
155 
156  public Date getLockCommit() {
157  return lockCommit;
158  }
159 
160  public void setLockCommit(Date lockCommit) {
161  this.lockCommit = lockCommit;
162  }
163 
164  public Date getLockDate() {
165  return lockDate;
166  }
167 
168  public void setLockDate(Date lockDate) {
169  this.lockDate = lockDate;
170  }
171 
172  public boolean isLocked() {
173  return locked;
174  }
175 
176  public void setLocked(boolean locked) {
177  this.locked = locked;
178  }
179 
180  public String getLocker() {
181  return locker;
182  }
183 
184  public void setLocker(String locker) {
185  this.locker = locker;
186  }
187 
188  public String getPath() {
189  return path;
190  }
191 
192  public void setPath(String path) {
193  if(path != null) {
194  while(path.indexOf("//") > -1) {
195  path = path.replaceAll("//", "/");
196  }
197  if(path.endsWith("/")) {
198  path = path.substring(0, path.length() - 1);
199  }
200  if(!path.startsWith("/")) {
201  path = "/" + path;
202  }
203  }
204  this.path = path;
205  }
206 
207  public String getRelatedPath() {
208  return relatedPath;
209  }
210 
211  public void setRelatedPath(String relatedPath) {
212  this.relatedPath = relatedPath;
213  }
214 
215  public Date getModification() {
216  return modification;
217  }
218 
219  public void setModification(Date modification) {
220  this.modification = modification;
221  }
222 
223  public String getComment() {
224  return comment;
225  }
226 
227  public void setComment(String comment) {
228  this.comment = comment;
229  }
230 
231  public String getFileContentType() {
232  return fileContentType;
233  }
234 
235  public void setFileContentType(String fileContentType) {
236  this.fileContentType = fileContentType;
237  }
238 
239  public String getFileName() {
240  return fileName;
241  }
242 
243  public void setFileName(String fileName) {
244  this.fileName = fileName;
245  }
246 
247  public long getFileSize() {
248  return fileSize;
249  }
250 
251  public void setFileSize(long fileSize) {
252  this.fileSize = fileSize;
253  }
254 
255  public String getShowKey() {
256  return showKey;
257  }
258 
259  public void setShowKey(String showKey) {
260  this.showKey = showKey;
261  }
262 
263  public boolean isValidated() {
264  return validated;
265  }
266 
267  public void setValidated(boolean validated) {
268  this.validated = validated;
269  }
270 
271  public boolean isPublishable() {
272  return publishable;
273  }
274 
275  public void setPublishable(boolean publishable) {
276  this.publishable = publishable;
277  }
278 
279  public boolean isRequiresAcceptance() {
280  return requiresAcceptance;
281  }
282 
283  public void setRequiresAcceptance(boolean requiresAcceptance) {
284  this.requiresAcceptance = requiresAcceptance;
285  }
286 
287  public boolean isWatermark() {
288  return watermark;
289  }
290 
291  public void setWatermark(boolean watermark) {
292  this.watermark = watermark;
293  }
294 
295  /* Helpers */
296 
297  public String getFileExtension() {
298  if(fileName != null) {
299  int p = fileName.lastIndexOf(".");
300  if(p > 0) {
301  return fileName.substring(p + 1).toLowerCase();
302  }
303  }
304  return "bin";
305  }
306 
307  public String getRelativePath(String root) {
308  if(root == null) {
309  return path;
310  }
311  if(path.toUpperCase().startsWith(root.toUpperCase())) {
312  return path.substring(root.length());
313  }
314  return "";
315  }
316 
319  Attachment na = new Attachment();
320  na.setComment(a.comment);
321  na.setFileContentType(a.fileContentType);
322  na.setFileName(a.fileName);
323  na.setFileSize(a.fileSize);
324  na.setLockComment(a.lockComment);
325  na.setLockCommit(a.lockCommit);
326  na.setLockDate(a.lockDate);
327  na.setLocked(a.locked);
328  na.setLocker(a.locker);
329  na.setModification(a.modification);
330  na.setOnlyOwner(a.onlyOwner);
331  na.setOwner(a.owner);
332  na.setPath(a.path);
333  na.setShowKey(a.showKey);
334  na.setValidated(a.validated);
335  AttachContent nac = new AttachContent();
336  nac.setFileContent(a.attachContent.getFileContent());
337  na.setAttachContent(nac);
338  return na;
339  }
340 
341  public void setDateModification(Date value) {
342  modification = new CheckDate(modification).setDayMonthYear(value).getDate();
343  }
344 
345  public void setTimeModification(Date value) {
346  modification = new CheckDate(modification).setHourMinute(value).getDate();
347  }
348 
349  /* Content help */
350 
352  return AttachmentUtil.withContent(this);
353  }
354 
355  public byte[] getContentModified(IContact contact) throws IOException, DocumentException {
356  AttachContent ac = getWithContent().getAttachContent();
357  if(isWatermark() && isPDF()) {
358  if(contact != null && contact.isValid()) {
359  WatermarkCfg cfg = new WatermarkCfg(getContactName(contact), 45.0F);
360  Watermark mark = new Watermark();
361  ByteArrayOutputStream output = new ByteArrayOutputStream((int) getFileSize());
362  mark.addWatermark(
363  new ByteArrayInputStream(ac.getFileContent()),
364  output, cfg);
365  return output.toByteArray();
366  } else {
367  return new byte[] {};
368  }
369  }
370  return ac.getFileContent();
371  }
372 
373  public boolean isPDF() {
374  return fileName.toLowerCase().endsWith(".pdf");
375  }
376 
377  private String getContactName(IContact contact) {
378  String name = contact.getName();
379  IContact business = contact.getBusiness();
380  if(business != null && business.isValid()) {
381  name = name + "\n" + business.getName();
382  }
383  return name;
384  }
385 
386  /* Contact help */
387 
388  public IContact getIOwner() {
389  if(!Strings.isBlank(owner)) {
390  IContact contact = Contacts.getContactById(owner);
391  if(!contact.isValid()) {
392  contact.loadByLogin(owner);
393  }
394  if(contact.isValid()) {
395  return contact;
396  }
397  }
398  return null;
399  }
400 
401  /* Entity help */
402 
404  return Entities.getController(new Path(path).getTill(2));
405  }
406 
407  /* Restricted folders */
408 
409  public boolean canShow(HashSet<String> restrictedFolders) {
410  return canShow(restrictedFolders, Application.getApplication().isInRole("attach-show:all"));
411  }
412 
413  public boolean canShowTo(IContact contact, HashSet<String> restrictedFolders) {
414  return canShow(restrictedFolders, contact.getPermissions().isInRole("attach-show:all"));
415  }
416 
417  private boolean canShow(HashSet<String> restrictedFolders, boolean allPermission) {
418  if((restrictedFolders != null && restrictedFolders.contains("*")) || allPermission) return true;
419  Path p = new Path(getPath());
420  for(String node : p.getNodes()) {
421  if(!Strings.isBlank(node) && node.startsWith("#")) {
422  boolean allowed = false;
423  for(String restrictedFolder : restrictedFolders) {
424  if(("#" + restrictedFolder).equalsIgnoreCase(node)) {
425  allowed = true;
426  break;
427  }
428  }
429  if(!allowed) {
430  return false;
431  }
432  }
433  }
434  return true;
435  }
436 
437  /* WWW */
438 
439  public String getShowURL(IConstructor constructor) {
440  return AttachDownload.createURL(constructor, this);
441  }
442 
443  public String getROShowURL(IConstructor constructor) {
444  return AttachDownload.createROURL(constructor, this);
445  }
446 
447  public String getByteString() {
448  return new BytesFormatter(getFileSize()).toString();
449  }
450 
451  public String getDateString(IConstructor constructor) {
452  return DateFormats.format(getModification(),
453  DateFormat.SHORT, I_.api().used());
454  }
455 
456  /* Requires acceptation */
457 
459  return getSingleStatus(Authentication.getIContact());
460  }
461 
464  return acceptances.getStatusFor(AttachPU.getObjectPath(this), contact, getIOwner());
465  }
466 
467 }
static IContact getContactById(String id)
Definition: Contacts.java:72
static< T > T loadImplementation(Class< T > jclass)
Definition: Plugins.java:57
static String getObjectPath(Object object)
Definition: AttachPU.java:57
void setFileContent(byte[] fileContent)
void setLockCommit(Date lockCommit)
void setShowKey(String showKey)
void setWatermark(boolean watermark)
String getDateString(IConstructor constructor)
void setOnlyOwner(boolean onlyOwner)
void setModification(Date modification)
void setRelatedPath(String relatedPath)
String getShowURL(IConstructor constructor)
void setFileContentType(String fileContentType)
void setLockComment(String lockComment)
void setValidated(boolean validated)
AcceptanceSingleStatus getSingleStatus()
boolean canShowTo(IContact contact, HashSet< String > restrictedFolders)
void setPublishable(boolean publishable)
String getROShowURL(IConstructor constructor)
void setComment(String comment)
void setFileName(String fileName)
byte[] getContentModified(IContact contact)
void setRequiresAcceptance(boolean requiresAcceptance)
boolean canShow(HashSet< String > restrictedFolders)
void setAttachContent(AttachContent attachContent)
String getRelativePath(String root)
AcceptanceSingleStatus getSingleStatus(IContact contact)
void setLocked(boolean locked)
static String createROURL(IConstructor constructor, Attachment attachment)
static String createURL(IConstructor constructor, Attachment attachment)
static Attachment withContent(Attachment attachment)
static IElephantEntity getController(String path)
Definition: Entities.java:78
static I18nApiWrapper api()
Definition: I_.java:65
void addWatermark(InputStream src, OutputStream dest, WatermarkCfg watermark)
Definition: Watermark.java:41
AcceptanceStatus getStatusFor(String path)
Object loadByLogin(String login)