BrightSide Workbench Full Report + Source Code
FileWrapper.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.file;
19 
20 import java.io.*;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.Properties;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 import org.amic.util.file.FileUtil;
27 import org.turro.command.Command;
28 import org.turro.command.Context;
29 import org.turro.elephant.context.ElephantContext;
30 import org.turro.file.action.*;
31 import org.turro.file.zul.tree.FileItem;
32 import org.turro.log.SystemLogger;
33 import org.turro.upload.Medias;
34 import org.zkoss.util.media.ContentTypes;
35 import org.zkoss.util.media.Media;
36 import org.zkoss.zk.ui.event.EventListener;
37 import org.zkoss.zk.ui.event.UploadEvent;
38 import org.zkoss.zul.Filedownload;
39 import org.zkoss.zul.Fileupload;
40 
45 public class FileWrapper {
46 
47  protected File file;
48  protected FileItem fileItem;
50  protected List<FileAction> actions;
51 
52  public FileWrapper(File file) {
53  this.file = file;
54  actions = new ArrayList<>();
55  actions.add(new DeleteAction(this));
56  actions.add(new RenameAction(this));
57  if(file.isDirectory()) {
58  actions.add(new UploadAction(this));
59  } else if(file.isFile()) {
60  actions.add(new DownloadAction(this));
61  if(EditAction.fits(file)) {
62  actions.add(new EditAction(this));
63  }
64  }
65  }
66 
67  public File getFile() {
68  return file;
69  }
70 
71  public String getBaseName() {
72  return FileUtil.getBaseName(file);
73  }
74 
75  public String getExtension() {
76  return FileUtil.getExtension(file);
77  }
78 
79  public List<FileAction> getActions() {
80  return actions;
81  }
82 
83  public FileAction getAction(String label) {
84  for(FileAction fa : getActions()) {
85  if(fa.getLabel().equals(label)) {
86  return fa;
87  }
88  }
89  return null;
90  }
91 
92  public Properties getProperties() throws FileNotFoundException, IOException {
93  if(propertiesFile == null) {
95  }
97  }
98 
99  public FileItem getFileItem() {
100  return fileItem;
101  }
102 
104  this.fileItem = fileItem;
105  }
106 
107  public void delete() {
108  FileUtil.deleteFile(file);
109  }
110 
111  public void copyTo(File destination) throws IOException {
112  if(destination.isDirectory()) {
113  if(file.isDirectory()) {
114  FileUtil.copyFolder(file, destination.getAbsolutePath());
115  } else if(file.isFile()) {
116  FileUtil.copyFile(file, destination.getAbsolutePath());
117  }
118  }
119  }
120 
121  public void download() {
122  try {
123  Filedownload.save(file, ContentTypes.getContentType(FileUtil.getExtension(file)));
125  .entityPath("/file" + ElephantContext.getRelativePath(file.getAbsolutePath()))
126  .entityName(file.getName()).comment("downloaded")
127  .log();
128  } catch (Exception ex) {
129  Logger.getLogger(FileWrapper.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
130  }
131  }
132 
133  public void upload(final Command command) {
134  if(!file.exists()) {
135  file.mkdirs();
136  }
137  Fileupload.get(100, new EventListener<UploadEvent>() {
138  @Override
139  public void onEvent(UploadEvent event) {
140  ArrayList<File> list = new ArrayList<>();
141  Media[] medias = event.getMedias();
142  if(medias == null || medias.length == 0) return;
143  for(Media media : medias) {
144  File newFile = new File(file.getAbsolutePath() + "/" + Document.correctName(media.getName()));
145  Medias.toFile(media, newFile);
146  list.add(newFile);
147  }
148  if(command != null) {
149  try {
150  Context context = new Context();
151  context.put("files", list);
152  command.execute(context);
153  } catch (Exception ex) {
154  Logger.getLogger(FileWrapper.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
155  }
156  }
157  }
158  });
159  }
160 
161  public void uploadContent(final Command command) {
162  if(!file.getParentFile().exists()) {
163  file.getParentFile().mkdirs();
164  }
165  Fileupload.get(1, new EventListener<UploadEvent>() {
166  @Override
167  public void onEvent(UploadEvent event) throws Exception {
168  Media[] medias = event.getMedias();
169  if(medias == null || medias.length == 0) return;
170  for(Media media : medias) {
171  Medias.toFile(media, file);
172  }
173  if(command != null) {
174  try {
175  Context context = new Context();
176  context.put("file", file);
177  command.execute(context);
178  } catch (Exception ex) {
179  Logger.getLogger(FileWrapper.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
180  }
181  }
182  }
183  });
184  }
185 
186  public static FileWrapper getFileByType(File file) {
187  if(FolderFile.isMyType(file)) {
188  return new FolderFile(file);
189  } else if(ZipFile.isMyType(file)) {
190  return new ZipFile(file);
191  } else if(VideoFile.isMyType(file)) {
192  return new VideoFile(file);
193  } else if(ImageFile.isMyType(file)) {
194  return new ImageFile(file);
195  } else if(JasperFile.isMyType(file)) {
196  return new JasperFile(file);
197  } else if(DefaultFile.isMyType(file)) {
198  return new DefaultFile(file);
199  }
200  return null;
201  }
202 
203 }
static boolean isMyType(File file)
static FileWrapper getFileByType(File file)
FileAction getAction(String label)
void uploadContent(final Command command)
List< FileAction > actions
void copyTo(File destination)
void setFileItem(FileItem fileItem)
List< FileAction > getActions()
PropertiesFile propertiesFile
void upload(final Command command)
static boolean isMyType(File file)
Definition: FolderFile.java:50
static boolean isMyType(File file)
Definition: ImageFile.java:44
static boolean isMyType(File file)
Definition: JasperFile.java:52
static boolean isMyType(File file)
Definition: VideoFile.java:33
static boolean isMyType(File file)
Definition: ZipFile.java:40
static boolean fits(File file)
Definition: EditAction.java:68
static ILogWrapper info()
static void toFile(Media media, File file)
Definition: Medias.java:66
ILogWrapper comment(String comment)
ILogWrapper entityPath(String path)
ILogWrapper entityName(String name)