BrightSide Workbench Full Report + Source Code
elephant-attach/src/main/java/org/turro/file/zul/repository/RepositoryItem.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2018 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.file.zul.repository;
20 
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import org.turro.string.Strings;
26 import org.turro.collections.CollectionUtil;
27 import org.turro.command.Command;
28 import org.turro.command.Context;
29 import org.turro.elephant.context.ElephantContext;
30 import org.turro.elephant.impl.util.StringParser;
31 import org.turro.entities.Entities;
32 import org.turro.file.Document;
33 import org.turro.file.Folder;
34 import org.turro.file.zul.control.FileResults;
35 import org.turro.i18n.I_;
36 import org.turro.path.Path;
37 import org.turro.upload.Medias;
38 import org.turro.zkoss.dialog.InputDialog;
39 import org.turro.zkoss.dialog.InputField;
40 import org.turro.zkoss.text.RepositoriesTree;
41 import org.turro.zkoss.text.RepositoryFilter;
42 import org.zkoss.util.media.Media;
43 import org.zkoss.zk.ui.HtmlBasedComponent;
44 import org.zkoss.zul.Textbox;
45 import org.zkoss.zul.Treechildren;
46 import org.zkoss.zul.Treeitem;
47 
52 public class RepositoryItem extends Treeitem {
53 
54  private final File file;
55  private final String path;
56 
57  public RepositoryItem(File file, String name, String path) {
58  super(name);
59  this.file = file;
60  this.path = path;
61  if(doProcessors(file)) {
62  setOpen(false);
63  fillFolder();
64  }
65  }
66 
67  public File getRepositoryFile() {
68  return new File(file, path);
69  }
70 
71  public boolean isFolder(String folder) {
72  return ElephantContext.getRelativePath(file.getAbsolutePath()).equals(folder);
73  }
74 
76  return (RepositoryItem) getParentItem();
77  }
78 
79  public Collection<RepositoryItem> getFileFolders() {
81  Treechildren children = getTreechildren();
82  return children != null ? CollectionUtil.from(children.getChildren()).<Collection<RepositoryItem>>cast() : new ArrayList<>();
83  }
84 
85  public void reloadContents() {
86  Treechildren children = getTreechildren();
87  if(children != null) {
88  children.getChildren().clear();
89  fillFolder();
90  }
91  }
92 
93  @Override
95  return (RepositoryTree) super.getTree();
96  }
97 
98  public File getRealFolder() {
99  return getRealFolder(false);
100  }
101 
102  public File getRealFolder(boolean create) {
103  File folder = getRepositoryFile();
104  if(!folder.exists()) {
105  folder.mkdirs();
106  }
107  return folder;
108  }
109 
110  public Collection<File> getFilesFrom(File folder) {
111  FileResults fr = getTree().getResults();
112  return fr.getFileList(folder);
113  }
114 
115  public Collection<File> getFiles() {
116  File folder = getRealFolder(true);
117  FileResults fr = getTree().getResults();
118  return fr.getFiles(folder);
119  }
120 
121  public String getPath() {
122  return path;
123  }
124 
125  public void addMedias(Media[] medias) throws InterruptedException, IOException {
126  if(medias == null || medias.length == 0) return;
127  fillFolder();
128  setOpen(true);
129  for(Media media : medias) {
130  Medias.toFolder(media, getRealFolder());
131  }
132  reloadContents();
133  }
134 
135  public void doAddFolder() throws InterruptedException {
137  getPage(),
138  I_.get("Folder"),
139  new InputField[] {
140  new InputField("Name", "", null, 0) {
141  @Override
142  protected HtmlBasedComponent createEditor() {
143  return new Textbox();
144  }
145  }
146  }, new Command() {
147  @Override
148  public Object execute(Context context) {
149  InputField[] fields = (InputField[]) context.get("fields");
150  if(fields.length > 0) {
151  for(InputField f : fields) {
152  if("Name".equals(f.getLabel())) {
153  fillFolder();
154  RepositoryItem af = RepositoryItem.this;
155  for(String s : ((String) f.getValue()).split("\\/")) {
156  if(!Strings.isBlank(s)) {
157  af.setOpen(true);
158  af = af.addFolder(s, s);
159  }
160  }
161  }
162  }
163  }
164  return null;
165  }
166  });
167  }
168 
169  public RepositoryItem addFolder(String label, String path) {
170  File folder = new File(file, path);
171  folder.mkdir();
172  RepositoryItem af = new RepositoryItem(folder, label, "");
173  checkChildren().appendChild(af);
174  return af;
175  }
176 
177  private void fillFolder() {
178  if(!Strings.isBlank(path)) { // Add also repository subfolders
179  File repositoryFile = getRepositoryFile();
180  if(repositoryFile.exists()) {
181  for(File child : repositoryFile.listFiles(new RepositoryFilter(""))) {
182  checkChildren().appendChild(new RepositoryItem(child, child.getName(), ""));
183  }
184  }
185  }
186  if(file.exists()) {
187  for(File child : file.listFiles(new RepositoryFilter(path))) {
188  checkChildren().appendChild(new RepositoryItem(child, child.getName(), path));
189  }
190  }
191  }
192 
193  private boolean doProcessors(File file) {
194  if(RepositoriesTree.isPublishable(file)) {
195  if(Folder.from(file).removeEmpties()) {
196  RepositoryItem.this.detach();
197  return false;
198  } else {
199  Path p = new Path(RepositoriesTree.getEntityPath(file));
200  if(p.getSize() > 0 && p.getSize() < 3) {
201  String str = Entities.getController(p.getPath()).getTreeLabel();
202  if(str != null) {
203  setLabel(StringParser.cutString(str, 50));
204  setTooltiptext(str);
205  } else if(p.getSize() == 2) {
206  Document.from(file).delete();
207  RepositoryItem.this.detach();
208  return false;
209  }
210  }
211  }
212  }
213  return true;
214  }
215 
216  private Treechildren checkChildren() {
217  Treechildren children = getTreechildren();
218  if(children == null) {
219  children = new Treechildren();
220  appendChild(children);
221  }
222  return children;
223  }
224 
225 }
Collection< File > getFiles(File folder)
Collection< File > getFileList(File folder)
static String get(String msg)
Definition: I_.java:41
static void toFolder(Media media, Folder folder)
Definition: Medias.java:50
static void getInput(Page page, String title, String label, Object value, String format, int scale, final Command onOk)