BrightSide Workbench Full Report + Source Code
AttachFolder.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.navigator;
19 
20 import java.io.ByteArrayOutputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.nio.charset.Charset;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.Date;
27 import org.apache.commons.io.input.ReaderInputStream;
28 import org.turro.attach.db.AttachPU;
29 import org.turro.attach.entity.AttachContent;
30 import org.turro.attach.entity.Attachment;
31 import org.turro.attach.search.AttachResults;
32 import org.turro.attach.version.AttachVersionMap;
33 import org.turro.attach.zul.FolderNameCombobox;
34 import org.turro.collections.CollectionUtil;
35 import org.turro.command.Command;
36 import org.turro.command.Context;
37 import org.turro.elephant.context.Application;
38 import org.turro.elephant.impl.util.StringParser;
39 import org.turro.elephant.util.Messages;
40 import org.turro.entities.Entities;
41 import org.turro.i18n.I_;
42 import org.turro.log.SystemLogType;
43 import org.turro.log.SystemLogger;
44 import org.turro.plugin.attach.IAttachFolder;
45 import org.turro.zkoss.dialog.InputDialog;
46 import org.turro.zkoss.dialog.InputField;
47 import org.zkoss.lang.Strings;
48 import org.zkoss.util.media.Media;
49 import org.zkoss.zk.ui.HtmlBasedComponent;
50 import org.zkoss.zk.ui.event.DropEvent;
51 import org.zkoss.zk.ui.event.Event;
52 import org.zkoss.zk.ui.event.EventListener;
53 import org.zkoss.zk.ui.event.Events;
54 import org.zkoss.zk.ui.ext.AfterCompose;
55 import org.zkoss.zul.*;
56 
61 public class AttachFolder extends Treeitem implements IAttachFolder, AfterCompose {
62 
63  private Application app = Application.getApplication();
64 
65  private Treechildren children;
66  private String label, realLabel;
67  private String path;
68  private boolean loadOnDemand;
69 
70  public AttachFolder(String label, String path, boolean loadOnDemand) {
71  super();
72  this.label = label;
73  this.realLabel = label;
74  this.path = path;
75  this.loadOnDemand = loadOnDemand;
76  this.setTooltiptext(label);
77  }
78 
79  public AttachFolder(String label, Object value) {
80  throw new UnsupportedOperationException("Do not use");
81  }
82 
83  public AttachFolder(String label) {
84  throw new UnsupportedOperationException("Do not use");
85  }
86 
87  public AttachFolder() {
88  throw new UnsupportedOperationException("Do not use");
89  }
90 
91  @Override
92  public AttachTree getTree() {
93  return (AttachTree) super.getTree();
94  }
95 
96  public void doUpload() throws InterruptedException, IOException {
97  addMedias(Fileupload.get(-1, true));
98  }
99 
100  public void addMedias(Media[] medias) throws InterruptedException, IOException {
101  if(medias == null || medias.length == 0) return;
102  fillFolder();
103  setOpen(true);
104  for(Media media : medias) {
105  Attachment attachment = new Attachment();
106  attachment.setModification(new Date());
107  attachment.setPath(path);
108  attachment.setOwner(app.getConstructor().getUser().getId());
109  attachment.setOnlyOwner(false);
110  attachment.setComment("");
111  attachment.setShowKey(null);
112  attachment.setPublishable(true);
113  AttachContent ac = new AttachContent();
114  if(media.inMemory()) {
115  ac.setFileContent(media.isBinary() ? media.getByteData() : media.getStringData().getBytes());
116  } else {
117  byte[] buffer = new byte[102400];
118  InputStream is = media.isBinary() ? media.getStreamData() : new ReaderInputStream(media.getReaderData(), Charset.defaultCharset());
119  ByteArrayOutputStream baos = new ByteArrayOutputStream();
120  int r;
121  while((r = is.read(buffer)) != -1) {
122  baos.write(buffer, 0, r);
123  }
124  is.close();
125  ac.setFileContent(baos.toByteArray());
126  }
127  attachment.setAttachContent(ac);
128  attachment.setFileName(media.getName());
129  attachment.setFileContentType(media.getContentType());
130  attachment.setFileSize(ac.getFileContent().length);
131  attachment.setValidated(app.isInRole("attach:self-validate"));
132  attachment = new AttachPU().saveObject(attachment);
133  SystemLogger.getInstance().doLog(SystemLogType.LOG_INFO, attachment, "uploaded", null);
134  if(getTree().getAttachListener() != null) {
135  getTree().getAttachListener().uploadDone(attachment);
136  }
137  reloadContents();
138  }
139  }
140 
141  public void doAddFolder() throws InterruptedException {
143  getPage(),
144  I_.get("Folder"),
145  new InputField[] {
146  new InputField("Name", "", null, 0) {
147  @Override
148  protected HtmlBasedComponent createEditor() {
149  return new FolderNameCombobox(AttachFolder.this);
150  }
151  }
152  }, new Command() {
153  @Override
154  public Object execute(Context context) {
155  InputField[] fields = (InputField[]) context.get("fields");
156  if(fields.length > 0) {
157  for(InputField f : fields) {
158  if("Name".equals(f.getLabel())) {
159  fillFolder();
160  AttachFolder af = AttachFolder.this;
161  for(String s : ((String) f.getValue()).split("\\/")) {
162  if(!Strings.isBlank(s)) {
163  af.setOpen(true);
164  af = af.addFolder(s, af.getPath() + "/" + s);
165  }
166  }
167  }
168  }
169  }
170  return null;
171  }
172  });
173  }
174 
175  public AttachFolder addFolder(String label, String path) {
176  AttachFolder af = new AttachFolder(label, path, loadOnDemand);
177  if(children == null) {
178  children = new Treechildren();
179  appendChild(children);
180  }
181  children.appendChild(af);
182  af.afterCompose();
183  af.doProcessors();
184  return af;
185  }
186 
187  public void showContents() {
188  fillFolder();
189  setOpen(true);
190  }
191 
192  public void reloadContents() {
193  if(children != null) {
194  children.getChildren().clear();
195  showContents();
196  }
197  }
198 
199  private void addCells() {
200  Treerow row = new Treerow();
201  row.setDroppable("true");
202  row.addEventListener(Events.ON_DROP, new EventListener() {
203  @Override
204  public void onEvent(Event event) throws Exception {
205  final DropEvent dp = (DropEvent) event;
206  if(dp.getDragged() instanceof AttachmentRow) {
207  Messages.confirmMove().show(() -> {
208  fillFolder();
209  AttachmentRow row = (AttachmentRow) dp.getDragged();
210  for(Attachment a : row.getAttachmentSet()) {
211  a.setPath(Strings.isEmpty(getPath()) ? "/" : getPath());
212  a = new AttachPU().saveObject(a);
213  }
214  row.detach();
215  });
216  }
217  }
218  });
219  appendChild(row);
220  Treecell cell = new Treecell(label);
221  cell.setImage("/_zul/images/folder.png");
222  row.appendChild(cell);
223  }
224 
225  private void addChildrenSpace() {
226  if(!isLeaf()) {
227  children = new Treechildren();
228  appendChild(children);
229  if(!loadOnDemand) {
230  fillFolder();
231  setOpen(true);
232  }
233  }
234  }
235 
236  public boolean isLoadOnDemand() {
237  return loadOnDemand;
238  }
239 
240  public void setLoadOnDemand(boolean loadOnDemand) {
241  this.loadOnDemand = loadOnDemand;
242  }
243 
244  public String getPath() {
245  return path;
246  }
247 
248  public void setPath(String path) {
249  this.path = path;
250  }
251 
252  private void initLoadOnDemand() {
253  if(!loadOnDemand) return;
254 
255  setOpen(false);
256 
257  addEventListener(Events.ON_OPEN, new EventListener() {
258  @Override
259  public void onEvent(Event event) throws Exception {
260  // load files and add folders
261  if(isOpen()) {
262  fillFolder();
263  }
264  }
265  });
266  }
267 
268  private void fillFolder() {
269  if(children != null && children.getChildren().isEmpty()) {
270  for(String s : getFolders()) {
271  addFolder(s, path + "/" + s);
272  }
273  }
274  }
275 
276  @Override
278  return (IAttachFolder) getParentItem();
279  }
280 
281  @Override
282  public String getFolderPath() {
283  return path;
284  }
285 
286  @Override
287  public String getFolderLabel() {
288  return realLabel;
289  }
290 
291  @Override
292  public void setFolderLabel(String label) {
293  setLabel(label);
294  }
295 
296  public void doProcessors() {
297  String str = Entities.getController(getPath()).getTreeLabel();
298  if(str != null) {
299  setFolderLabel(StringParser.cutString(str, 50));
300  setTooltiptext(str);
301  }
302  }
303 
304  public boolean isLeaf() {
305  return getFolders().isEmpty();
306  }
307 
308  public Collection<String> getFolders() {
309  AttachResults results = getTree().getResults();
310  results.setAttachPath(path + "/_%");
311  results.setCkExactPath(false);
312  return results.getPathList(path);
313  }
314 
315  public Collection<AttachFolder> getAttachFolders() {
316  showContents();
317  return children != null ? CollectionUtil.from(children.getChildren()).<Collection<AttachFolder>>cast() : new ArrayList<>();
318  }
319 
320  public Collection<Attachment> getAttachments() {
321  AttachResults results = getTree().getResults();
322  results.setAttachPath(Strings.isEmpty(path) ? "/" : path);
323  results.setCkExactPath(true);
324  return results.getAttachmentList();
325  }
326 
329  for(Attachment a : getAttachments()) {
330  avm.addAttachment(a);
331  }
332  return avm;
333  }
334 
335  public Collection<Attachment> getAllAttachments() {
336  AttachResults results = getTree().getResults();
337  results.setAttachPath(Strings.isEmpty(path) ? "/" : path + "/%");
338  results.setCkExactPath(false);
339  return results.getAttachmentList();
340  }
341 
342  @Override
343  public void afterCompose() {
344  initLoadOnDemand();
345  addCells();
346  addChildrenSpace();
347  }
348 
349 }
void setFileContent(byte[] fileContent)
void setShowKey(String showKey)
void setOnlyOwner(boolean onlyOwner)
void setModification(Date modification)
void setFileContentType(String fileContentType)
void setValidated(boolean validated)
void setPublishable(boolean publishable)
void setComment(String comment)
void setFileName(String fileName)
void setAttachContent(AttachContent attachContent)
java.util.Set< String > getPathList(String currPath)
void setCkExactPath(boolean ckExactPath)
java.util.List< Attachment > getAttachmentList()
void setAttachPath(String attachPath)
void addAttachment(Attachment attachment)
Collection< Attachment > getAllAttachments()
Collection< AttachFolder > getAttachFolders()
AttachFolder addFolder(String label, String path)
AttachFolder(String label, String path, boolean loadOnDemand)
Collection< Attachment > getAttachments()
void setLoadOnDemand(boolean loadOnDemand)
AttachFolder(String label, Object value)
static String cutString(String value, int maxChars)
static Messages confirmMove()
Definition: Messages.java:99
static IElephantEntity getController(String path)
Definition: Entities.java:78
static String get(String msg)
Definition: I_.java:41
static ISystemLogger getInstance()
static void getInput(Page page, String title, String label, Object value, String format, int scale, final Command onOk)
void doLog(SystemLogType type, Object entity, String comment, Serializable data)