BrightSide Workbench Full Report + Source Code
AttachFile.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.File;
21 import java.text.DateFormat;
22 import java.util.Set;
23 import org.turro.action.Contacts;
24 import org.turro.action.Plugins;
25 import org.turro.attach.db.AttachPU;
26 import org.turro.attach.entity.Attachment;
27 import org.turro.attach.zul.PreviewButton;
28 import org.turro.command.Command;
29 import org.turro.command.Context;
30 import org.turro.elephant.context.ElephantContext;
31 import org.turro.elephant.util.ZkossUtils;
32 import org.turro.i18n.I_;
33 import org.turro.jpa.DaoTransaction;
34 import org.turro.log.SystemLogType;
35 import org.turro.log.SystemLogger;
36 import org.turro.plugin.contacts.IContact;
37 import org.turro.plugin.tagit.TagItCtrl;
38 import org.turro.zkoss.dialog.InputDialog;
39 import org.turro.zkoss.dialog.InputField;
40 import org.zkoss.zk.ui.Component;
41 import org.zkoss.zk.ui.ext.AfterCompose;
42 import org.zkoss.zul.Filedownload;
43 import org.zkoss.zul.Space;
44 import org.zkoss.zul.Treecell;
45 import org.zkoss.zul.Treeitem;
46 import org.zkoss.zul.Treerow;
47 import org.turro.plugin.acceptance.IAcceptanceCtrl;
48 
53 @Deprecated
54 public class AttachFile extends Treeitem implements AfterCompose {
55 
56  private Attachment attachment;
57  private Treerow row;
58  private Set<AttachColumn> columns;
59 
60  public AttachFile(Attachment attachment, Set<AttachColumn> columns) {
61  super();
62  this.attachment = attachment;
63  this.columns = columns;
64  }
65 
66  @Override
67  public void afterCompose() {
68  addCells(columns);
69  setImage(getMimeImage());
70  }
71 
72  public AttachFile(String label, Object value) {
73  throw new UnsupportedOperationException("Do not use");
74  }
75 
76  public AttachFile(String label) {
77  throw new UnsupportedOperationException("Do not use");
78  }
79 
80  public AttachFile() {
81  throw new UnsupportedOperationException("Do not use");
82  }
83 
84  @Override
85  public AttachTree getTree() {
86  return (AttachTree) super.getTree();
87  }
88 
89  public void doDownload() {
90  try(DaoTransaction transaction = new DaoTransaction(new AttachPU())) {
91  attachment = transaction.saveObject(attachment);
92  Filedownload.save(attachment.getAttachContent().getFileContent(), attachment.getFileContentType(), attachment.getFileName());
93  if(getTree().getAttachListener() != null) {
95  }
96  SystemLogger.getInstance().doLog(SystemLogType.LOG_INFO, attachment, "downloaded", null);
97  }
98  }
99 
100  public void doEdit() {
102  getPage(),
103  I_.get("Attachment") + " " + attachment.getId(),
104  new InputField[] {
105  new InputField("Name", attachment.getFileName(), null, 0),
106  new InputField("Comment", attachment.getComment(), null, 3),
107  new InputField("Show key", attachment.getShowKey(), null, 0),
108  new InputField("Validated", attachment.isValidated(), null, 0)
109  }, new Command() {
110  @Override
111  public Object execute(Context context) {
112  InputField[] fields = (InputField[]) context.get("fields");
113  if(fields.length > 0) {
114  for(InputField f : fields) {
115  if("Name".equals(f.getLabel())) {
116  attachment.setFileName((String) f.getValue());
117  } else if("Comment".equals(f.getLabel())) {
118  attachment.setComment((String) f.getValue());
119  } else if("Show key".equals(f.getLabel())) {
120  attachment.setShowKey((String) f.getValue());
121  } else if("Validated".equals(f.getLabel())) {
122  attachment.setValidated((Boolean) f.getValue());
123  }
124  }
125  attachment = new AttachPU().saveObject(attachment);
126  addCells(getTree().getColumns());
127  setImage(getMimeImage());
128  }
129  return null;
130  }
131  });
132  }
133 
134  public void doDelete() throws InterruptedException {
135  ZkossUtils.confirmDeletion(null, new Command() {
136  @Override
137  public Object execute(Context context) {
138  new AttachPU().deleteObject(attachment);
139  if(getTree().getAttachListener() != null) {
141  }
142  TagItCtrl tagIt = Plugins.loadImplementation(TagItCtrl.class, "button");//(TagItCtrl) PluginChecker.get("tagitctrl");
143  tagIt.setEntity(attachment);
144  tagIt.deleteEntity();
145  detach();
146  return true;
147  }
148  });
149  }
150 
152  return attachment;
153  }
154 
155  private String getMimeImage() {
156  if(!new File(ElephantContext.getRealPath("/_internal/system/mime/" + attachment.getFileExtension() + ".png")).exists()) {
157  return "/_internal/system/mime/empty.png";
158  } else {
159  return "/_internal/system/mime/" + attachment.getFileExtension() + ".png";
160  }
161  }
162 
163  private void addCells(Set<AttachColumn> columns) {
164  if(row == null) {
165  row = new Treerow();
166  row.setDraggable("true");
167  appendChild(row);
168  } else {
169  row.getChildren().clear();
170  }
171  Treecell cell = new Treecell(attachment.getFileName());
172  PreviewButton ap = new PreviewButton(attachment);
173  cell.appendChild(ap);
174  if(!attachment.isValidated()) {
175  cell.setStyle("font-style:oblique;color:gray");
176  }
177  if(!getTree().isOnlyDownload()) {
178  cell.appendChild(new Space());
179  IAcceptanceCtrl accept = (IAcceptanceCtrl) Plugins.loadImplementation(IAcceptanceCtrl.class);//PluginChecker.get("acceptctrl");
180  accept.setEntity(attachment);
181  cell.appendChild((Component) accept);
182  cell.appendChild(new Space());
183  TagItCtrl tagIt = Plugins.loadImplementation(TagItCtrl.class, "button");//(TagItCtrl) PluginChecker.get("tagitctrl");
184  tagIt.setEntity(attachment);
185  if(tagIt instanceof AfterCompose) {
186  ((AfterCompose) tagIt).afterCompose();
187  }
188  cell.appendChild((Component) tagIt);
189  }
190  row.appendChild(cell);
191  if(columns.contains(AttachColumn.ATTACH_SIZE)) {
192  row.appendChild(new Treecell(new org.amic.util.string.BytesFormatter(attachment.getFileSize()).toString()));
193  }
194  if(columns.contains(AttachColumn.ATTACH_DATE)) {
195  DateFormat df = DateFormat.getDateInstance();
196  row.appendChild(new Treecell(df.format(attachment.getModification())));
197  }
198  if(columns.contains(AttachColumn.ATTACH_VALIDATED)) {
199  if(attachment.isValidated()) {
200  row.appendChild(new Treecell("", "/_internal/system/images/actions/ok.png"));
201  } else {
202  row.appendChild(new Treecell(""));
203  }
204  }
205  if(columns.contains(AttachColumn.ATTACH_OWNER)) {
206  IContact contact = Contacts.getContact();
207  if(contact != null) {
208  contact.loadByLogin(attachment.getOwner());
209  row.appendChild(new Treecell(contact.getName()));
210  }
211  }
212  }
213 
214 }
static< T > T loadImplementation(Class< T > jclass)
Definition: Plugins.java:58
void setShowKey(String showKey)
void setValidated(boolean validated)
void setComment(String comment)
void setFileName(String fileName)
AttachFile(String label, Object value)
Definition: AttachFile.java:72
AttachFile(Attachment attachment, Set< AttachColumn > columns)
Definition: AttachFile.java:60
static void confirmDeletion(String message, Command command)
Definition: ZkossUtils.java:58
static String get(String msg)
Definition: I_.java:41
void deleteObject(Object obj)
Definition: Dao.java:151
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)
void setEntity(Object entity)