BrightSide Workbench Full Report + Source Code
tree/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.tree;
19 
20 import java.io.ByteArrayOutputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.util.Date;
24 import java.util.Set;
25 import org.apache.commons.io.input.ReaderInputStream;
26 import org.turro.attach.db.AttachPU;
27 import org.turro.attach.entity.AttachContent;
28 import org.turro.attach.entity.Attachment;
29 import org.turro.attach.search.AttachResults;
30 import org.turro.attach.zul.FolderNameCombobox;
31 import org.turro.command.Command;
32 import org.turro.command.Context;
33 import org.turro.elephant.context.Application;
34 import org.turro.elephant.impl.util.StringParser;
35 import org.turro.elephant.util.ZkossUtils;
36 import org.turro.entities.Entities;
37 import org.turro.i18n.I_;
38 import org.turro.log.SystemLogType;
39 import org.turro.log.SystemLogger;
40 import org.turro.plugin.attach.IAttachFolder;
41 import org.turro.zkoss.dialog.InputDialog;
42 import org.turro.zkoss.dialog.InputField;
43 import org.zkoss.lang.Strings;
44 import org.zkoss.util.media.Media;
45 import org.zkoss.zk.ui.HtmlBasedComponent;
46 import org.zkoss.zk.ui.event.DropEvent;
47 import org.zkoss.zk.ui.event.Event;
48 import org.zkoss.zk.ui.event.EventListener;
49 import org.zkoss.zk.ui.event.Events;
50 import org.zkoss.zul.*;
51 
56 @Deprecated
57 public class AttachFolder extends Treeitem implements IAttachFolder {
58 
59  private Application app = Application.getApplication();
60 
61  private Treechildren children;
62  private String label, realLabel;
63  private String path;
64  private boolean loadOnDemand;
65  private Set<AttachColumn> columns;
66 
67  public AttachFolder(String label, String path, boolean loadOnDemand, Set<AttachColumn> columns) {
68  super();
69  this.label = label;
70  this.realLabel = label;
71  this.path = path;
72  this.loadOnDemand = loadOnDemand;
73  this.columns = columns;
74  initLoadOnDemand();
75  addCells();
76  addChildrenSpace();
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  Media[] medias = Fileupload.get(-1, true);
98  if(medias == null || medias.length == 0) return;
99  fillFolder();
100  setOpen(true);
101  for(Media media : medias) {
102  Attachment attachment = new Attachment();
103  attachment.setModification(new Date());
104  attachment.setPath(path);
105  attachment.setOwner(app.getConstructor().getUser().getId());
106  attachment.setOnlyOwner(false);
107  attachment.setComment("");
108  attachment.setShowKey(null);
109  AttachContent ac = new AttachContent();
110  if(media.inMemory()) {
111  ac.setFileContent(media.isBinary() ? media.getByteData() : media.getStringData().getBytes());
112  } else {
113  byte[] buffer = new byte[102400];
114  InputStream is = media.isBinary() ? media.getStreamData() : new ReaderInputStream(media.getReaderData());
115  ByteArrayOutputStream baos = new ByteArrayOutputStream();
116  int r;
117  while((r = is.read(buffer)) != -1) {
118  baos.write(buffer, 0, r);
119  }
120  is.close();
121  ac.setFileContent(baos.toByteArray());
122  }
123  attachment.setAttachContent(ac);
124  attachment.setFileName(media.getName());
125  attachment.setFileContentType(media.getContentType());
126  attachment.setFileSize(ac.getFileContent().length);
127  attachment.setValidated(app.isInRole("attach:self-validate"));
128  attachment = new AttachPU().saveObject(attachment);
129  SystemLogger.getInstance().doLog(SystemLogType.LOG_INFO, attachment, "uploaded", null);
130  AttachFile file = addFile(attachment);
131  if(getTree().getAttachListener() != null) {
133  }
134  }
135  }
136 
137  public void doAddFolder() throws InterruptedException {
139  getPage(),
140  I_.get("Folder"),
141  new InputField[] {
142  new InputField("Name", "", null, 0) {
143  @Override
144  protected HtmlBasedComponent createEditor() {
145  return new FolderNameCombobox(AttachFolder.this);
146  }
147  }
148  }, new Command() {
149  @Override
150  public Object execute(Context context) {
151  InputField[] fields = (InputField[]) context.get("fields");
152  if(fields.length > 0) {
153  for(InputField f : fields) {
154  if("Name".equals(f.getLabel())) {
155  fillFolder();
156  AttachFolder af = AttachFolder.this;
157  for(String s : ((String) f.getValue()).split("\\/")) {
158  if(!Strings.isBlank(s)) {
159  af.setOpen(true);
160  af = af.addFolder(s, af.getPath() + "/" + s);
161  }
162  }
163  }
164  }
165  }
166  return null;
167  }
168  });
169  }
170 
171  public AttachFolder addFolder(String label, String path) {
172  AttachFolder af = new AttachFolder(label, path, loadOnDemand, columns);
173  children.appendChild(af);
174  af.doProcessors();
175  return af;
176  }
177 
178  public AttachFile addFile(Attachment attachment) {
179  AttachFile af = new AttachFile(attachment, columns);
180  children.appendChild(af); //insertBefore(af, children.getFirstChild());
181  af.afterCompose();
182  return af;
183  }
184 
185  public void showContents() {
186  fillFolder();
187  setOpen(true);
188  }
189 
190  public void reloadContents() {
191  children.getChildren().clear();
192  showContents();
193  getTree().updateButtons();
194  }
195 
196  private void addCells() {
197  Treerow row = new Treerow();
198  row.setDroppable("true");
199  row.addEventListener(Events.ON_DROP, new EventListener() {
200  @Override
201  public void onEvent(Event event) throws Exception {
202  final DropEvent dp = (DropEvent) event;
203  if(dp.getDragged().getParent() instanceof AttachFile) {
204  ZkossUtils.confirmMove(null, new Command() {
205  @Override
206  public Object execute(Context context) {
207  fillFolder();
208  AttachFile af = (AttachFile) dp.getDragged().getParent();
209  Attachment a = af.getAttachment();
210  a.setPath(Strings.isEmpty(getPath()) ? "/" : getPath());
211  a = new AttachPU().saveObject(a);
212  children.insertBefore(af, children.getFirstChild());
213  return true;
214  }
215  });
216  }
217  }
218  });
219  appendChild(row);
220  Treecell cell = new Treecell(label);
221  row.appendChild(cell);
222  if(columns.contains(AttachColumn.ATTACH_SIZE)) {
223  row.appendChild(new Treecell(""));
224  }
225  if(columns.contains(AttachColumn.ATTACH_DATE)) {
226  row.appendChild(new Treecell(""));
227  }
228  if(columns.contains(AttachColumn.ATTACH_VALIDATED)) {
229  row.appendChild(new Treecell(""));
230  }
231  if(columns.contains(AttachColumn.ATTACH_OWNER)) {
232  row.appendChild(new Treecell(""));
233  }
234  }
235 
236  private void addChildrenSpace() {
237  children = new Treechildren();
238  appendChild(children);
239  if(!loadOnDemand) {
240  fillFolder();
241  setOpen(true);
242  }
243  }
244 
245  public boolean isLoadOnDemand() {
246  return loadOnDemand;
247  }
248 
249  public void setLoadOnDemand(boolean loadOnDemand) {
250  this.loadOnDemand = loadOnDemand;
251  }
252 
253  public String getPath() {
254  return path;
255  }
256 
257  public void setPath(String path) {
258  this.path = path;
259  }
260 
261  private void initLoadOnDemand() {
262  if(!loadOnDemand) return;
263 
264  setOpen(false);
265 
266  addEventListener(Events.ON_OPEN, new EventListener() {
267  @Override
268  public void onEvent(Event event) throws Exception {
269  // load files and add folders
270  if(isOpen()) {
271  fillFolder();
272  }
273  }
274  });
275  }
276 
277  private void fillFolder() {
278  if(children.getChildren().isEmpty()) {
279  AttachResults results = getTree().getResults();
280  results.setAttachPath(Strings.isEmpty(path) ? "/" : path);
281  results.setCkExactPath(true);
282  for(Object o : results.getVersionedAttachmentList()) {
283  addFile((Attachment) o);
284  }
285  results.setAttachPath(path + "/_%");
286  results.setCkExactPath(false);
287  for(String s : results.getPathList(path)) {
288  addFolder(s, path + "/" + s);
289  }
290  }
291  }
292 
293  @Override
295  return (IAttachFolder) getParentItem();
296  }
297 
298  @Override
299  public String getFolderPath() {
300  return path;
301  }
302 
303  @Override
304  public String getFolderLabel() {
305  return realLabel;
306  }
307 
308  @Override
309  public void setFolderLabel(String label) {
310  setLabel(label);
311  }
312 
313  public void doProcessors() {
314  String str = Entities.getController(getPath()).getTreeLabel();
315  if(str != null) {
316  setFolderLabel(StringParser.cutString(str, 50));
317  }
318  }
319 
320 }
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 setComment(String comment)
void setFileName(String fileName)
void setAttachContent(AttachContent attachContent)
AttachFolder(String label, String path, boolean loadOnDemand, Set< AttachColumn > columns)
void setLoadOnDemand(boolean loadOnDemand)
AttachFile addFile(Attachment attachment)
AttachFolder addFolder(String label, String path)
AttachFolder(String label, Object value)
static String cutString(String value, int maxChars)
static void confirmMove(String message, Command command)
Definition: ZkossUtils.java:82
static IElephantEntity getController(String path)
Definition: Entities.java:47
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)