BrightSide Workbench Full Report + Source Code
AttachmentButton.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2019 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.ByteArrayOutputStream;
22 import java.io.InputStream;
23 import java.nio.charset.Charset;
24 import java.util.Date;
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.elephant.context.Application;
30 import org.turro.entities.Entities;
31 import org.turro.log.SystemLogType;
32 import org.turro.log.SystemLogger;
33 import org.zkoss.util.media.Media;
34 import org.zkoss.zk.ui.event.EventListener;
35 import org.zkoss.zk.ui.event.Events;
36 import org.zkoss.zk.ui.event.UploadEvent;
37 import org.zkoss.zul.Button;
38 
43 public class AttachmentButton extends Button implements EventListener<UploadEvent> {
44 
45  private Object entity;
46  private String entityPath;
47 
48  public AttachmentButton() {
49  addEventListener(Events.ON_UPLOAD, this);
50  }
51 
52  public void setEntity(Object entity) {
53  this.entity = entity;
55  }
56 
57  public void setEntityPath(String entityPath) {
58  this.entityPath = entityPath;
59  }
60 
61  @Override
62  public void onEvent(UploadEvent event) throws Exception {
64  Media[] medias = event.getMedias();
65  if(medias == null || medias.length == 0) return;
66  for(Media media : medias) {
67  Attachment attachment = new Attachment();
68  attachment.setModification(new Date());
69  attachment.setPath(entityPath);
70  attachment.setOwner(app.getConstructor().getUser().getId());
71  attachment.setOnlyOwner(false);
72  attachment.setComment("");
73  attachment.setShowKey(null);
74  attachment.setPublishable(true);
75  AttachContent ac = new AttachContent();
76  if(media.inMemory()) {
77  ac.setFileContent(media.isBinary() ? media.getByteData() : media.getStringData().getBytes());
78  } else {
79  byte[] buffer = new byte[102400];
80  InputStream is = media.isBinary() ? media.getStreamData() : new ReaderInputStream(media.getReaderData(), Charset.defaultCharset());
81  ByteArrayOutputStream baos = new ByteArrayOutputStream();
82  int r;
83  while((r = is.read(buffer)) != -1) {
84  baos.write(buffer, 0, r);
85  }
86  is.close();
87  ac.setFileContent(baos.toByteArray());
88  }
89  attachment.setAttachContent(ac);
90  attachment.setFileName(media.getName());
91  attachment.setFileContentType(media.getContentType());
92  attachment.setFileSize(ac.getFileContent().length);
93  attachment.setValidated(app.isInRole("attach:self-validate"));
94  attachment = new AttachPU().saveObject(attachment);
95  SystemLogger.getInstance().doLog(SystemLogType.LOG_INFO, attachment, "uploaded", null);
96  }
97  }
98 
99 }
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)
static IElephantEntity getController(String path)
Definition: Entities.java:78
static ISystemLogger getInstance()
void doLog(SystemLogType type, Object entity, String comment, Serializable data)