BrightSide Workbench Full Report + Source Code
AttachmentUtil.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.zul;
19 
20 import java.io.IOException;
21 import java.util.Collection;
22 import java.util.logging.Level;
23 import java.util.logging.Logger;
24 import org.turro.string.Strings;
25 import org.turro.attach.db.AttachPU;
26 import org.turro.attach.entity.AttachContent;
27 import org.turro.attach.entity.Attachment;
28 import org.turro.attach.version.AttachVersionSet;
29 import org.turro.elephant.util.Messages;
30 import org.turro.http.Respond;
31 import org.turro.jpa.Dao;
32 import org.turro.jpa.DaoTransaction;
33 import org.turro.log.SystemLogger;
34 import org.turro.log.WebLoggers;
35 import org.turro.zul.frame.Framework;
36 import org.zkoss.zul.Filedownload;
37 
42 public class AttachmentUtil {
43 
44  public static boolean download(Attachment attachment) {
45  boolean done = false;
46 // Dao dao = new AttachPU();
47 // dao.startTrans();
48  try(DaoTransaction transaction = new DaoTransaction(new AttachPU())) {
49 // attachment = dao.saveObjectWithTrans(attachment);
50  attachment = transaction.saveObject(attachment);
51  Filedownload.save(attachment.getAttachContent().getFileContent(), attachment.getFileContentType(), attachment.getFileName());
52  done = true;
53 // } finally {
54 // dao.endTrans();
55  SystemLogger.info().entity(attachment).comment("downloaded").log();
56  }
57  return done;
58  }
59 
60  public static Attachment withContent(Attachment attachment) {
61 // Dao dao = new AttachPU();
62 // dao.startTrans();
63  try(DaoTransaction transaction = new DaoTransaction(new AttachPU())) {
64  attachment = transaction.saveObject(attachment);
65  AttachContent content = attachment.getAttachContent();
66  content.getFileContent();
67 // } finally {
68 // dao.endTrans();
69  }
70  return attachment;
71  }
72 
73  public static Attachment withContent(Long id) {
74  try(DaoTransaction transaction = new DaoTransaction(new AttachPU())) {
75  Attachment attachment = transaction.find(Attachment.class, id);
76  AttachContent content = attachment.getAttachContent();
77  content.getFileContent();
78  return attachment;
79  }
80  }
81 
82  public static boolean respond(Respond respond, Long id) {
83  try {
84  Attachment attachment = withContent(id);
85  respond.name(attachment.getFileName())
86  .type(attachment.getFileContentType())
87  .lenght(attachment.getFileSize())
88  .flush(attachment.getAttachContent().getFileContent());
89  return true;
90  } catch (IOException ex) {
92  return false;
93  }
94  }
95 
96  public static void paste(String toPath) {
97  pasteAttachment(toPath);
98  pasteFolder(toPath);
100  }
101 
102  public static Collection<Attachment> delete(String path) {
103  Dao dao = new AttachPU();
104  Collection<Attachment> list = (Collection<Attachment>) dao.getResultList(
105  "select a from Attachment a where a.path like ? or a.path = ?",
106  new Object[] {path + "/%", path});
107  for(Attachment a : list) {
108  dao.deleteObject(a);
109  }
110  return list;
111  }
112 
113  public static void copyAttachment(Object value) {
114  clearClipboard();
115  Framework.getClipboard().put("bba_clip_attach_copy", value);
116  }
117 
118  public static void cutAttachment(String name, final Object value) {
119  Messages.confirmCutting().add(name).show(() -> {
120  clearClipboard();
121  Framework.getClipboard().put("bba_clip_attach_cut", value);
122  });
123  }
124 
125  private static void pasteAttachment(String toPath) {
126  boolean cut = false;
127  AttachVersionSet avs = (AttachVersionSet) Framework.getClipboard().get("bba_clip_attach_copy");
128  if (avs == null) {
129  avs = (AttachVersionSet) Framework.getClipboard().get("bba_clip_attach_cut");
130  cut = true;
131  }
132  if (avs != null) {
133  Dao dao = new AttachPU();
134  for (Attachment a : avs) {
135  if (cut) {
136  a.setPath(Strings.isEmpty(toPath) ? "/" : toPath);
137  dao.saveObject(a);
138  } else {
139  Attachment na = a.cloneEntity();
140  na.setPath(Strings.isEmpty(toPath) ? "/" : toPath);
141  dao.saveObject(na);
142  }
143  }
144  }
145  }
146 
147  public static void copyFolder(Object value) {
148  clearClipboard();
149  Framework.getClipboard().put("bba_clip_attach_folder_copy", value);
150  }
151 
152  public static void cutFolder(String name, final Object value) {
153  Messages.confirmCutting().add(name).show(() -> {
154  clearClipboard();
155  Framework.getClipboard().put("bba_clip_attach_folder_cut", value);
156  });
157  }
158 
159  private static void pasteFolder(String toPath) {
160  boolean cut = false;
161  String path = (String) Framework.getClipboard().get("bba_clip_attach_folder_copy");
162  if(Strings.isBlank(path)) {
163  path = (String) Framework.getClipboard().get("bba_clip_attach_folder_cut");
164  cut = true;
165  }
166  if(!Strings.isBlank(path)) {
167  Dao dao = new AttachPU();
168  for(Attachment a : (Collection<Attachment>) dao.getResultList("select a from Attachment a where a.path like ? or a.path = ?",
169  new Object[] {path + "/%", path})) {
170  if(cut) {
171  a.setPath(convertPath(path, a.getPath(), toPath));
172  dao.saveObject(a);
173  } else {
174  Attachment na = a.cloneEntity();
175  na.setPath(convertPath(path, a.getPath(), toPath));
176  dao.saveObject(na);
177  }
178  }
179  }
180  }
181 
182  public static boolean hasClipboard() {
183  return Framework.getClipboard().get("bba_clip_attach_folder_copy") != null ||
184  Framework.getClipboard().get("bba_clip_attach_folder_cut") != null ||
185  Framework.getClipboard().get("bba_clip_attach_copy") != null ||
186  Framework.getClipboard().get("bba_clip_attach_cut") != null;
187  }
188 
189  public static void clearClipboard() {
190  Framework.getClipboard().remove("bba_clip_attach_folder_copy");
191  Framework.getClipboard().remove("bba_clip_attach_folder_cut");
192  Framework.getClipboard().remove("bba_clip_attach_copy");
193  Framework.getClipboard().remove("bba_clip_attach_cut");
194  }
195 
196  private static String convertPath(String root, String path, String toPath) {
197  String reminder = path.substring(root.length());
198  return (Strings.isEmpty(toPath) ? "/" : toPath) + reminder;
199  }
200 
201  private AttachmentUtil() {
202  }
203 
204 }
static boolean respond(Respond respond, Long id)
static void copyAttachment(Object value)
static void copyFolder(Object value)
static Attachment withContent(Long id)
static boolean download(Attachment attachment)
static void cutFolder(String name, final Object value)
static void cutAttachment(String name, final Object value)
static Attachment withContent(Attachment attachment)
static void paste(String toPath)
static Messages confirmCutting()
Definition: Messages.java:91
Messages add(String word)
Definition: Messages.java:50
static ILogWrapper info()
static WebLoggers severe(Object entity)
Definition: WebLoggers.java:51
WebLoggers exception(Throwable throwable)
Definition: WebLoggers.java:29
static FrameClipboard getClipboard()
Definition: Framework.java:220
ILogWrapper comment(String comment)
ILogWrapper entity(Object entity)