18 package org.turro.attach.entity;
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;
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")
60 public class Attachment implements java.io.Serializable {
63 @GeneratedValue(strategy=GenerationType.IDENTITY)
64 @Column(name=
"IDENTIFIER")
67 @Column(name="ATTACH_PATH")
70 private String relatedPath;
74 private String showKey;
76 private
boolean validated;
78 private
boolean locked;
80 private String locker;
82 private
boolean publishable;
84 @Temporal(value = javax.persistence.TemporalType.TIMESTAMP)
85 private java.util.Date lockDate;
87 @Temporal(value = javax.persistence.TemporalType.TIMESTAMP)
88 private java.util.Date lockCommit;
92 private String lockComment;
94 private
boolean onlyOwner;
96 @Temporal(value = javax.persistence.TemporalType.TIMESTAMP)
97 private java.util.Date modification;
101 private String comment;
103 private String fileName;
105 private String fileContentType;
107 private
long fileSize;
109 private
boolean requiresAcceptance;
111 private
boolean watermark;
113 @OneToOne(fetch=FetchType.LAZY,cascade={CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REMOVE}, orphanRemoval=
true)
117 return attachContent;
121 this.attachContent = attachContent;
145 this.onlyOwner = onlyOwner;
153 this.lockComment = lockComment;
161 this.lockCommit = lockCommit;
169 this.lockDate = lockDate;
177 this.locked = locked;
185 this.locker = locker;
194 while(path.indexOf(
"//") > -1) {
195 path = path.replaceAll(
"//",
"/");
197 if(path.endsWith(
"/")) {
198 path = path.substring(0, path.length() - 1);
200 if(!path.startsWith(
"/")) {
212 this.relatedPath = relatedPath;
220 this.modification = modification;
228 this.comment = comment;
232 return fileContentType;
236 this.fileContentType = fileContentType;
244 this.fileName = fileName;
252 this.fileSize = fileSize;
260 this.showKey = showKey;
268 this.validated = validated;
276 this.publishable = publishable;
280 return requiresAcceptance;
284 this.requiresAcceptance = requiresAcceptance;
292 this.watermark = watermark;
298 if(fileName !=
null) {
299 int p = fileName.lastIndexOf(
".");
301 return fileName.substring(p + 1).toLowerCase();
311 if(path.toUpperCase().startsWith(root.toUpperCase())) {
312 return path.substring(root.length());
342 modification =
new CheckDate(modification).setDayMonthYear(value).getDate();
346 modification =
new CheckDate(modification).setHourMinute(value).getDate();
357 if(isWatermark() && isPDF()) {
358 if(contact !=
null && contact.isValid()) {
361 ByteArrayOutputStream output =
new ByteArrayOutputStream((
int) getFileSize());
365 return output.toByteArray();
367 return new byte[] {};
374 return fileName.toLowerCase().endsWith(
".pdf");
377 private String getContactName(
IContact contact) {
378 String name = contact.
getName();
380 if(business !=
null && business.
isValid()) {
381 name = name +
"\n" + business.
getName();
389 if(!Strings.isBlank(owner)) {
409 public boolean canShow(HashSet<String> restrictedFolders) {
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)) {
448 return new BytesFormatter(getFileSize()).toString();
452 return DateFormats.format(getModification(),
453 DateFormat.SHORT,
I_.
api().used());
static< T > T loadImplementation(Class< T > jclass)
static String getObjectPath(Object object)
void setFileContent(byte[] fileContent)
void setOwner(String owner)
void setTimeModification(Date value)
void setLockCommit(Date lockCommit)
void setShowKey(String showKey)
void setWatermark(boolean watermark)
String getDateString(IConstructor constructor)
void setOnlyOwner(boolean onlyOwner)
IElephantEntity getOwnerEntity()
void setModification(Date modification)
void setRelatedPath(String relatedPath)
void setLockDate(Date lockDate)
void setFileSize(long fileSize)
String getShowURL(IConstructor constructor)
void setLocker(String locker)
void setFileContentType(String fileContentType)
Attachment getWithContent()
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)
AttachContent getAttachContent()
boolean isRequiresAcceptance()
String getFileContentType()
byte[] getContentModified(IContact contact)
void setDateModification(Date value)
void setRequiresAcceptance(boolean requiresAcceptance)
void setPath(String path)
String getFileExtension()
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 IContact getIContact()
boolean isInRole(String role)
static Application getApplication()
static IElephantEntity getController(String path)
static I18nApiWrapper api()
void addWatermark(InputStream src, OutputStream dest, WatermarkCfg watermark)
boolean isInRole(String role)
AcceptanceStatus getStatusFor(String path)