BrightSide Workbench Full Report + Source Code
AttachmentControl.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2015 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.attach.zul.control;
20 
21 import java.io.ByteArrayInputStream;
22 import java.io.File;
23 import java.nio.file.Paths;
24 import java.util.Collection;
25 import java.util.function.Consumer;
26 import org.turro.string.Strings;
27 import org.turro.action.IEntityHolder;
28 import org.turro.attach.db.AttachPU;
29 import org.turro.attach.entity.Attachment;
30 import org.turro.attach.search.AttachResults;
31 import org.turro.attach.zul.AttachmentUtil;
32 import org.turro.attach.zul.navigator.AttachFolder;
33 import org.turro.attach.zul.navigator.AttachNavigatorGrid;
34 import org.turro.attach.zul.navigator.AttachTree;
35 import org.turro.elephant.context.Application;
36 import org.turro.elephant.util.Messages;
37 import org.turro.entities.Entities;
38 import org.turro.entities.IElephantEntity;
39 import org.turro.i18n.I_;
40 import org.turro.wd.entities.AttachWd;
41 import org.turro.mail.recipients.SendAttachments;
42 import org.turro.zip.Compress;
43 import org.turro.zkoss.dialog.Windows;
44 import org.zkoss.zk.ui.Executions;
45 import org.zkoss.zk.ui.IdSpace;
46 import org.zkoss.zk.ui.event.UploadEvent;
47 import org.zkoss.zk.ui.ext.AfterCompose;
48 import org.zkoss.zk.ui.select.Selectors;
49 import org.zkoss.zk.ui.select.annotation.Listen;
50 import org.zkoss.zk.ui.select.annotation.Wire;
51 import org.zkoss.zul.Checkbox;
52 import org.zkoss.zul.Filedownload;
53 import org.zkoss.zul.Panel;
54 import org.zkoss.zul.Textbox;
55 import org.zkoss.zul.Toolbarbutton;
56 import org.zkoss.zul.West;
57 
62 public class AttachmentControl extends Panel implements IEntityHolder, IdSpace, AfterCompose {
63 
64  private static final String SECURED = "/#secure#/#forbidden#";
65 
66  private AttachResults results;
67  private boolean publicOnly, readOnly, treeCollapsed;
68  private AttachFolder currentFolder;
69  private String label, path;
70 
71  @Wire("#panelFolder")
72  private West panelFolder;
73 
74  @Wire("#searchValue")
75  private Textbox searchValue;
76 
77  @Wire("#search")
78  private Toolbarbutton search;
79 
80  @Wire("#validated")
81  private Checkbox validated;
82 
83  @Wire("#levelUp")
84  private Toolbarbutton levelUp;
85 
86  @Wire("#newFolder")
87  private Toolbarbutton newFolder;
88 
89  @Wire("#upload")
90  private Toolbarbutton upload;
91 
92  @Wire("#send")
93  private Toolbarbutton send;
94 
95  @Wire("#download")
96  private Toolbarbutton download;
97 
98  @Wire("#copy")
99  private Toolbarbutton copy;
100 
101  @Wire("#cut")
102  private Toolbarbutton cut;
103 
104  @Wire("#paste")
105  private Toolbarbutton paste;
106 
107  @Wire("#delete")
108  private Toolbarbutton delete;
109 
110  @Wire("#treeFolder")
111  private AttachTree treeFolder;
112 
113  @Wire("#attachmentGrid")
114  private AttachNavigatorGrid attachmentGrid;
115 
116  @Listen("onSelect = #treeFolder")
117  public void onSelectFolder() {
118  currentFolder = treeFolder.getSelectedFolder();
119  updateButtons();
120  attachmentGrid.setCurrentFolder(currentFolder);
121  }
122 
123  @Listen("onChange = #searchValue")
124  public void onSearchValue() {
125  results.setAttachValue(searchValue.getText());
126  }
127 
128  @Listen("onClick = #search; onOK = #searchValue")
129  public void onSearch() {
130  treeFolder.reloadContents();
131  selectFirst();
132  }
133 
134  @Listen("onCheck = #validated")
135  public void onValidated() {
136  results.setCkNonValidated(validated.isChecked());
137  treeFolder.reloadContents();
138  selectFirst();
139  }
140 
141  @Listen("onClick = #levelUp")
142  public void onLevelUp() throws Exception {
143  if(currentFolder != null && currentFolder.getParentItem() != null) {
144  currentFolder = (AttachFolder) currentFolder.getParentItem();
145  treeFolder.setSelectedItem(currentFolder);
146  attachmentGrid.setCurrentFolder(currentFolder);
147  updateButtons();
148  }
149  }
150 
151  @Listen("onClick = #newFolder")
152  public void onNewFolder() throws Exception {
153  if(currentFolder != null) {
154  currentFolder.doAddFolder();
155  attachmentGrid.setCurrentFolder(currentFolder);
156  }
157  }
158 
159  @Listen("onUpload = #upload")
160  public void onUpload(UploadEvent event) throws Exception {
161  if(currentFolder != null) {
162  //currentFolder.doUpload();
163  currentFolder.addMedias(event.getMedias());
164  attachmentGrid.setCurrentFolder(currentFolder);
165  }
166  }
167 
168  @Listen("onClick = #send")
169  public void onSend() throws Exception {
170  SendAttachments sa = new SendAttachments(I_.get("Attachment")) {
171  @Override
172  protected void fillAttachment(File attachment, Object entity) {
173  AttachWd.writeToFile((Attachment) entity, attachment);
174  }
175  };
176  AttachFolder af = attachmentGrid.getCurrentFolder();
177  if(af != null) {
178  for(Attachment a : af.getAttachments()) {
179  sa.addAttachment(a, AttachPU.getObjectPath(a), a.getFileName());
180  }
181  }
182  sa.sendAttachments(null);
183  }
184 
185  @Listen("onClick = #download")
186  public void onDownload() throws Exception {
187  AttachFolder af = attachmentGrid.getCurrentFolder();
188  if(af != null) {
189  File targetZip = File.createTempFile("attach_", "_folder.zip");
190  Compress.zip(af.getAllAttachments(), targetZip.toPath(),
191  a -> Paths.get(a.getPath(), a.getFileName()),
192  a -> new ByteArrayInputStream(a.getWithContent().getAttachContent().getFileContent()));
193  Filedownload.save(targetZip, "application/zip");
194  }
195  }
196 
197  @Listen("onClick = #copy")
198  public void onCopy() throws Exception {
199  AttachmentUtil.copyFolder(currentFolder.getPath());
200  }
201 
202  @Listen("onClick = #cut")
203  public void onCut() throws Exception {
204  AttachmentUtil.cutFolder(currentFolder.getLabel(), currentFolder.getPath());
205  }
206 
207  @Listen("onClick = #paste")
208  public void onPaste() throws Exception {
209  AttachmentUtil.paste(currentFolder.getPath());
210  treeFolder.setSelectedItem(currentFolder);
211  currentFolder.reloadContents();
212  attachmentGrid.setCurrentFolder(currentFolder);
213  paste.setDisabled(true);
214  }
215 
216  @Listen("onClick = #delete")
217  public void onDelete() throws Exception {
218  if(Application.getApplication().isInRole("attach:delete")) {
219  Messages.confirmDeletion().show(() -> {
220  Collection<Attachment> deleted = AttachmentUtil.delete(currentFolder.getPath());
221  if(treeFolder.getAttachListener() != null) {
222  for(Attachment a : deleted) {
223  treeFolder.getAttachListener().deleteDone(a);
224  }
225  }
226  treeFolder.setSelectedItem(currentFolder);
227  attachmentGrid.setCurrentFolder(currentFolder);
228  });
229  }
230  }
231 
232  @Listen("onUpload = #dropUpload")
233  public void onEvent(UploadEvent event) throws Exception {
234  if(currentFolder != null) {
235  currentFolder.addMedias(event.getMedias());
236  attachmentGrid.setCurrentFolder(currentFolder);
237  }
238  }
239 
240  public AttachmentControl() {
241  Executions.createComponents("/WEB-INF/_zul/bs/comps/attachment/attachmentControl.zul", this, null);
242  Selectors.wireComponents(this, this, false);
243  Selectors.wireEventListeners(this, this);
244  path = SECURED;
245  }
246 
247  public Boolean getPublicOnly() {
248  return publicOnly;
249  }
250 
251  public void setPublicOnly(Boolean publicOnly) {
252  this.publicOnly = publicOnly;
253  }
254 
255  public boolean isReadOnly() {
256  return readOnly;
257  }
258 
259  public void setReadOnly(boolean readOnly) {
260  this.readOnly = readOnly;
261  }
262 
263  public boolean isTreeCollapsed() {
264  return treeCollapsed;
265  }
266 
267  public void setTreeCollapsed(boolean treeCollapsed) {
268  this.treeCollapsed = treeCollapsed;
269  }
270 
271  public String getLabel() {
272  return label;
273  }
274 
275  public void setLabel(String label) {
276  this.label = label;
277  }
278 
279  public String getPath() {
280  return path;
281  }
282 
283  public void setPath(String path) {
284  this.path = path;
285  treeFolder.resetTree();
286  }
287 
288  @Override
289  public void setEntity(Object entity) {
290  IElephantEntity iee = Entities.getController(entity);
291  path = iee.getPath();
292  if(Strings.isBlank(path)) {
293  path = SECURED;
294  } else {
295  label = iee.getName();
296  }
297  treeFolder.setDoProcessors(false);
298  treeFolder.resetTree();
299  attachmentGrid.setCurrentFolder(null);
300  }
301 
302  @Override
303  public void refreshControls() {
304  afterCompose();
305  }
306 
308  return treeFolder;
309  }
310 
311  @Override
312  public void afterCompose() {
313  initResults();
314  initComponents();
315  panelFolder.setOpen(!treeCollapsed);
316  if(!SECURED.equals(path)) {
317  AttachFolder rootFolder = treeFolder.addFolder(label, path, true);
318  panelFolder.setTitle(label);
319  rootFolder.showContents();
320  selectFirst();
321  delete.setDisabled(!Application.getApplication().isInRole("attach:delete") || readOnly);
322  } else {
323  panelFolder.setTitle("");
324  treeFolder.resetTree();
325  attachmentGrid.setCurrentFolder(null);
326  newFolder.setDisabled(true);
327  upload.setDisabled(true);
328  send.setDisabled(true);
329  levelUp.setDisabled(true);
330  copy.setDisabled(true);
331  cut.setDisabled(true);
332  paste.setDisabled(true);
333  delete.setDisabled(true);
334  }
335  }
336 
337  private void initResults() {
338  results = new AttachResults();
339  results.markAll();
340  results.setCkOthers(false);
341  results.setPublicOnly(publicOnly);
342  }
343 
344  private void initComponents() {
345  treeFolder.setResults(results);
346  searchValue.setValue(results.getAttachValue());
347  validated.setChecked(results.isCkNonValidated());
348  attachmentGrid.setReadOnly(readOnly);
349  updateButtons();
350  }
351 
352  public void selectFirst() {
353  currentFolder = treeFolder.selectFirst();
354  updateButtons();
355  attachmentGrid.setCurrentFolder(currentFolder);
356  }
357 
358  private void updateButtons() {
359  newFolder.setDisabled(currentFolder == null || readOnly);
360  upload.setDisabled(currentFolder == null || readOnly);
361  send.setDisabled(currentFolder == null || !Application.getApplication().isInRole("attach:validate"));
362  download.setDisabled(currentFolder == null || !Application.getApplication().isInRole("attach:validate"));
363  levelUp.setDisabled(currentFolder == null || currentFolder.getParentItem() == null);
364  copy.setDisabled(currentFolder == null);
365  cut.setDisabled(currentFolder == null || readOnly);
366  paste.setDisabled(!AttachmentUtil.hasClipboard() || readOnly);
367  if(delete != null && Application.getApplication().isInRole("attach:delete")) {
368  delete.setDisabled(currentFolder == null || currentFolder.getParentFolder() == null || readOnly);
369  }
370  }
371 
372  /* Utils */
373 
374  public static void showFor(Object entity, Consumer<Windows> onClose) {
376  ac.setWidth("100%");
377  ac.setHeight("100%");
378  ac.setEntity(entity);
379  Windows.title(I_.get("Attachments"))
380  .width("80%")
381  .height("80%")
382  .addComponent(ac)
383  .onClose(onClose)
384  .closeable()
385  .show();
386  }
387 
388 }
static String getObjectPath(Object object)
Definition: AttachPU.java:57
void setAttachValue(String attachValue)
void setCkNonValidated(boolean ckNonValidated)
static void copyFolder(Object value)
static void cutFolder(String name, final Object value)
static Collection< Attachment > delete(String path)
static void paste(String toPath)
static void showFor(Object entity, Consumer< Windows > onClose)
Collection< Attachment > getAllAttachments()
AttachFolder addFolder(String label, String path)
Collection< Attachment > getAttachments()
static Messages confirmDeletion()
Definition: Messages.java:87
static IElephantEntity getController(String path)
Definition: Entities.java:78
static String get(String msg)
Definition: I_.java:41
void addAttachment(Object entity, String path, String name)
void sendAttachments(Collection< IContact > contacts)
static void writeToFile(Attachment attachment, File file)
Definition: AttachWd.java:116
static Windows title(String title)
Definition: Windows.java:138
Windows addComponent(HtmlBasedComponent component)
Definition: Windows.java:97
Windows width(String width)
Definition: Windows.java:61
Windows onClose(Consumer< Windows > onClose)
Definition: Windows.java:81
Windows height(String height)
Definition: Windows.java:66