BrightSide Workbench Full Report + Source Code
FileDocument.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.elephant.documents;
19 
20 import java.io.File;
21 import java.io.UnsupportedEncodingException;
22 import java.net.URLEncoder;
23 import java.util.Properties;
24 import java.util.Set;
25 import java.util.TreeSet;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
28 import org.amic.util.string.Cipher;
29 import org.turro.elephant.context.ElephantContext;
30 
35 public class FileDocument extends File {
36  private DocumentsRoot docsRoot = null;
37  private FileDocument parentDoc;
38  private FileDocument[] children = null;
39  private int level = 0;
40 
42  public FileDocument(DocumentsRoot docsRoot, FileDocument parentDoc, String pathName) {
43  super(pathName);
44  this.docsRoot = docsRoot;
45  this.parentDoc = parentDoc;
46  this.level = (parentDoc != null ? parentDoc.getLevel() + 1 : 0);
47  }
48 
50  return docsRoot;
51  }
52 
54  return parentDoc;
55  }
56 
57  public int getLevel() {
58  return level;
59  }
60 
61  public boolean getCanNew() {
62  return docsRoot.getCanNew();
63  }
64 
65  public boolean getCanDelete() {
66  return docsRoot.getCanDelete();
67  }
68 
69  public boolean getCanNewFolder() {
70  return docsRoot.getCanNewFolder();
71  }
72 
73  public boolean getCanDelFolder() {
74  return docsRoot.getCanDelFolder();
75  }
76 
78  FileDocument tmp;
79  if(children == null) {
80  Set s = new TreeSet(new FileComparator());
81  if(exists()) {
82  File[] fs = listFiles();
83  for(int i = 0; i < fs.length; i++) {
84  if(!fs[i].getName().startsWith(".")) {
85  tmp = new FileDocument(docsRoot, this, fs[i].getPath());
86  String role = tmp.readConfig("root.role", false);
87  if((role == null || docsRoot.getDocumentsBean().getConstructor().isInRole(role)) && tmp.getSomethingToShow()) {
88  s.add(tmp);
89  }
90  }
91  }
92  }
93  children = (FileDocument[]) s.toArray(new FileDocument[0]);
94  }
95  return children;
96  }
97 
99  FileDocument tmp;
100  Set s = new TreeSet(new FileComparator());
101  if(exists()) {
102  File[] fs = listFiles();
103  for(int i = 0; i < fs.length; i++) {
104  if(!fs[i].getName().startsWith(".") && fs[i].isDirectory()) {
105  tmp = new FileDocument(docsRoot, this, fs[i].getPath());
106  String role = tmp.readConfig("root.role", false);
107  if(role == null || docsRoot.getDocumentsBean().getConstructor().isInRole(role)) {
108  s.add(tmp);
109  }
110  }
111  }
112  }
113  return (FileDocument[]) s.toArray(new FileDocument[0]);
114  }
115 
117  FileDocument tmp;
118  Set s = new TreeSet(new FileComparator());
119  if(exists()) {
120  File[] fs = listFiles();
121  for(int i = 0; i < fs.length; i++) {
122  if(fs[i].isDirectory() || (fs[i].isHidden() && !".docs.properties".equals(fs[i].getName()))) {
123  tmp = new FileDocument(docsRoot, this, fs[i].getPath());
124  String role = tmp.readConfig("root.role", false);
125  if(role == null || docsRoot.getDocumentsBean().getConstructor().isInRole(role)) {
126  s.add(tmp);
127  }
128  }
129  }
130  }
131  return (FileDocument[]) s.toArray(new FileDocument[0]);
132  }
133 
134  public String getFormattedName() {
135  String result = isFile() ? null : readConfig("root.name", false);
136  if(result == null) {
137  result = "";
138  String tmp = getName();
139  if(tmp.startsWith(".")) tmp = tmp.substring(1);
140  int p = tmp.lastIndexOf(".");
141  if(p > -1) tmp = tmp.substring(0, p);
142  String s[] = tmp.split("\\_");
143  for(int i = 0; i < s.length; i++) {
144  // append an space if there is already content
145  if(result.length() > 0) result += " - ";
146  // this part could be the period phase
147  if(s[i].matches("[0-9]*")) {
148  switch(s[i].length()) {
149  case 12:
150  result += docsRoot.getDocumentsBean().getLocalizer().parseString("date", "yyyyMMddHHmm", "date_time", s[i]);
151  break;
152  case 8:
153  result += docsRoot.getDocumentsBean().getLocalizer().parseString("date", "yyyyMMdd", "full", s[i]);
154  break;
155  case 6:
156  result += docsRoot.getDocumentsBean().getLocalizer().parseString("date", "yyyyMM", "month_year", s[i]);
157  break;
158  case 4:
159  result += docsRoot.getDocumentsBean().getLocalizer().parseString("date", "yyyy", "year", s[i]);
160  break;
161  case 2:
162  result += docsRoot.getDocumentsBean().getLocalizer().parseString("date", "yy", "year", s[i]);
163  break;
164  }
165  }
166  // otherwise name phase
167  else {
168  tmp = readConfig("name." + s[i]);
169  result += (tmp == null ? s[i] : tmp);
170  }
171  }
172  }
173  if(result == null) result = getName();
174  if(isFile() && "true".equals(readConfig("file.size"))) {
175  result += " [" + docsRoot.getDocumentsBean().getLocalizer().parseValue("bytes", null, new Long(length())) + "]";
176  }
177  return result;
178  }
179 
180  public String getFormattedPath() {
181  String result = "";
182  FileDocument p = isFile() ? getParentDoc() : this;
183  while(p != null) {
184  result = p.getFormattedName() + " / " + result;
185  p = p.getParentDoc();
186  }
187  return result;
188  }
189 
190  public String getRelativeURL() {
191  try {
192  return docsRoot.getDownloadURL() + "&down_file=" + URLEncoder.encode(docsRoot.getRelativePath(getPath()), "UTF-8");
193  } catch (UnsupportedEncodingException ex) {
194  Logger.getLogger(FileDocument.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
195  }
196  return null;
197  }
198 
199  public String getUniqueId() {
200  return Cipher.digest(getPath(), 40);
201  }
202 
203  public String getExtension() {
204  String name = getName();
205  int p = name.lastIndexOf(".");
206  if(p > -1) return name.substring(p + 1).toLowerCase();
207  return "bin";
208  }
209 
210  public boolean getSomethingToShow() {
211  return isFile() || getListDocuments().length > 0;
212  }
213 
214  public String readConfig(String property) {
215  return readConfig(property, true);
216  }
217 
218  public String readConfig(String property, boolean moveUp) {
219  String result = null;
220  File startPoint = this;
221  if(isFile()) {
222  startPoint = getParentDoc();
223  }
224  Properties props = docsRoot.getDocumentsBean().getPropertiesFile(startPoint.getAbsolutePath() + "/.docs.properties");
225  if(props != null) {
226  result = props.getProperty(property);
227  }
228  if(moveUp && result == null) {
229  if(getParentDoc() != null) {
230  result = getParentDoc().readConfig(property);
231  }
232  else {
233  result = docsRoot.readRootConfig(property);
234  }
235  }
236  return result;
237  }
238 
239 }
String readConfig(String property, boolean moveUp)
FileDocument(DocumentsRoot docsRoot, FileDocument parentDoc, String pathName)
String parseValue(String type, String outFormat, Object value)
Definition: Localizer.java:96
String parseString(String type, String inFormat, String outFormat, String value)
Definition: Localizer.java:39