BrightSide Workbench Full Report + Source Code
Directory.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.wd.files;
19 
20 import java.io.File;
21 import java.text.ParseException;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 import org.apache.pivot.collections.ArrayList;
27 import org.apache.pivot.collections.Collection;
28 import org.apache.pivot.serialization.BinarySerializer;
29 import org.apache.pivot.web.GetQuery;
30 import org.apache.pivot.web.QueryException;
31 import org.jdom.Element;
32 import org.turro.pivot.dialog.ProgressSheet;
33 import org.turro.wd.entities.ServerFile;
34 
39 public class Directory implements Comparable<Directory> {
40 
41  private Directory parent;
42  private String serverPath, name;
43  private DirectorySet dirs = new DirectorySet();
44  private WorkFileSet files = new WorkFileSet();
45  private int nUpdates, nCommits, nFiles;
46 
47  public DirectorySet getDirs() {
48  return dirs;
49  }
50 
51  public void setDirs(DirectorySet dirs) {
52  this.dirs = dirs;
53  }
54 
55  public WorkFileSet getFiles() {
56  return files;
57  }
58 
59  public void setFiles(WorkFileSet files) {
60  this.files = files;
61  }
62 
63  public String getName() {
64  return name;
65  }
66 
67  public void setName(String name) {
68  this.name = name;
69  }
70 
71  public Directory getParent() {
72  return parent;
73  }
74 
75  public void setParent(Directory parent) {
76  this.parent = parent;
77  }
78 
79  public String getAbsolutePath() {
80  String ap = null;
81  Directory current = this;
82  while (current != null) {
83  if (current.getPath() != null) {
84  ap = current.getPath() + (ap == null ? "" : "/" + ap);
85  }
86  current = current.getParent();
87  }
88  return ap;
89  }
90 
91  public String getPath() {
92  if (getServerPath() != null) {
93  return getServerPath();
94  } else {
95  return getName();
96  }
97  }
98 
99  public String getServerPath() {
100  return serverPath;
101  }
102 
103  public void setServerPath(String serverPath) {
104  this.serverPath = serverPath;
105  }
106 
107  public String getSystemName() {
108  return getName()
109  .replaceAll("\\|", "-")
110  .replaceAll("\\<", "-")
111  .replaceAll("\\>", "-")
112  .replaceAll("\\:", "-")
113  .replaceAll("\"", "'")
114  .replaceAll("\\\\", "-")
115  .replaceAll("\\/", "-")
116  .replaceAll("\\*", "-")
117  .replaceAll("\\&", "i")
118  .replaceAll("\\%", "-")
119  .replaceAll("\\?", "-");
120  }
121 
122  public String getSystemPath() {
123  String sp = null;
124  Directory current = this;
125  while (current != null) {
126  if (current.getSystemName() != null) {
127  sp = current.getSystemName() + (sp == null ? "" : "/" + sp);
128  current = current.getParent();
129  }
130  }
131  return sp;
132  }
133 
134  public File getSystemFile() {
135  String sp = getSystemPath();
136  if (sp != null) {
137  return new File(sp);
138  }
139  return null;
140  }
141 
142  public String getHost() {
143  Directory current = this;
144  while (!(current instanceof WorkingDirectory)) {
145  current = current.getParent();
146  }
147  return ((WorkingDirectory) current).getBbHost();
148  }
149 
150  public Integer getPort() {
151  Directory current = this;
152  while (!(current instanceof WorkingDirectory)) {
153  current = current.getParent();
154  }
155  String port = ((WorkingDirectory) current).getBbPort();
156  return port == null ? 80 : new Integer(port);
157  }
158 
159  public String getContext() {
160  Directory current = this;
161  while (!(current instanceof WorkingDirectory)) {
162  current = current.getParent();
163  }
164  return ((WorkingDirectory) current).getBbContext();
165  }
166 
167  public String getUser() {
168  Directory current = this;
169  while (!(current instanceof WorkingDirectory)) {
170  current = current.getParent();
171  }
172  return ((WorkingDirectory) current).getBbUser();
173  }
174 
175  public boolean isRoot() {
176  return parent instanceof WorkingDirectory;
177  }
178 
179  public boolean isWorkingDirectory() {
180  return this instanceof WorkingDirectory;
181  }
182 
183  public boolean isExistsOnServer() {
184  return !isWorkingDirectory() && getServerPath() != null;
185  }
186 
187  public boolean isUpdatable() {
188  return !isWorkingDirectory()
189  && ((isRoot() && getServerPath() != null)
190  || !isRoot() && parent.isUpdatable());
191  }
192 
193  public Directory addDirectory(String name) {
194  Directory wdir = new Directory();
195  wdir.setParent(this);
196  wdir.setName(name == null ? "wdtmp" : name);
197  dirs.add(wdir);
198  return wdir;
199  }
200 
201  public WorkFile addFile(String name) {
202  WorkFile wfile = new WorkFile();
203  wfile.setParent(this);
204  wfile.setName(name == null ? "wdtmp" : name);
205  files.add(wfile);
206  return wfile;
207  }
208 
209  public int getnCommits() {
210  return nCommits;
211  }
212 
213  public int getnFiles() {
214  return nFiles;
215  }
216 
217  public int getnUpdates() {
218  return nUpdates;
219  }
220 
221  protected void countFiles() throws ParseException {
222  nUpdates = nCommits = nFiles = 0;
223  for(Directory d : getDirs()) {
224  d.countFiles();
225  nUpdates += d.nUpdates;
226  nCommits += d.nCommits;
227  nFiles += d.nFiles;
228  }
229  for(WorkFile wf : getFiles()) {
230  nFiles++;
231  if(wf.isDownloadable()) {
232  nUpdates++;
233  } else if(wf.isUploadable()) {
234  nCommits++;
235  }
236  }
237  }
238 
239  protected void readXML(Element root) throws ParseException {
240  if (root.getAttributeValue("name") != null) {
241  setName(root.getAttributeValue("name"));
242  }
243  if (root.getAttributeValue("serverPath") != null) {
244  setServerPath(decryptString(root.getAttributeValue("serverPath")));
245  return;
246  }
247  for (Element el : (List<Element>) root.getChildren("wdir")) {
248  Directory wdir = addDirectory(null);
249  wdir.readXML(el);
250  }
251  for (Element el : (List<Element>) root.getChildren("wfile")) {
252  WorkFile wfile = addFile(null);
253  wfile.readXML(el);
254  }
255  }
256 
257  protected void writeXML(Element root) {
258  Element el;
259  if (!isWorkingDirectory()) {
260  el = new Element("wdir");
261  el.setAttribute("name", getName());
262  if (getServerPath() != null) {
263  el.setAttribute("serverPath", encryptString(getServerPath()));
264  }
265  root.addContent(el);
266  if (getServerPath() != null) return;
267  } else {
268  el = root;
269  }
270  for (Directory d : dirs) {
271  d.writeXML(el);
272  }
273  for (WorkFile f : files) {
274  f.writeXML(el);
275  }
276  }
277 
278  protected boolean checkExisting() {
279  if(!(isRoot() || isWorkingDirectory())) return false;
280  File dir = getSystemFile();
281  ProgressSheet.nextProgress(dir.getName());
282  if (dir != null && dir.exists()) {
283  Iterator<Directory> itd = getDirs().iterator();
284  while (itd.hasNext()) {
285  if (!itd.next().checkExisting()) {
286  itd.remove();
287  }
288  }
289 // Iterator<WorkFile> itwf = getFiles().iterator();
290 // while (itwf.hasNext()) {
291 // if (!itwf.next().checkExisting()) {
292 // itwf.remove();
293 // }
294 // }
295  return true;
296  } else {
297  return false;
298  }
299  }
300 
301  protected void loadSystemFiles() {
302  File dir = getSystemFile();
303  for (File file : dir.listFiles()) {
304  if(file.getName().startsWith(".") ||
305  file.getName().endsWith("~") ||
306  file.isHidden()) {
307  continue;
308  }
309  ProgressSheet.nextProgress(file.getName());
310  if (file.isDirectory()) {
311  Directory d = getDirs().getDir(file.getName());
312  if (d == null) {
313  d = addDirectory(file.getName());
314  }
315  d.loadSystemFiles();
316  } else if (file.isFile()) {
317  WorkFile wf = getFiles().getFile(file.getName());
318  if (wf == null) {
319  wf = addFile(file.getName());
320  }
321  wf.setFile(file);
322  }
323  }
324  }
325 
326  protected void loadServerFiles() {
327  for (Directory d : getDirs()) {
328  d.loadServerFiles();
329  }
330  for (String folder : doQueryFolders()) {
331  ProgressSheet.nextProgress(folder);
332  if (!getDirs().containsDir(folder)) {
333  Directory nd = addDirectory(folder);
334  nd.loadServerFiles();
335  }
336  }
337  for (ServerFile sf : doQueryFiles()) {
338  WorkFile wf = getFiles().getFile(sf.getFileName());
339  if (wf == null) {
340  wf = addFile(sf.getFileName());
341  }
342  ProgressSheet.nextProgress(sf.getFileName());
343  wf.setDownModification(sf.getModification());
344  wf.setServerOwner(sf.getOwner());
345  wf.setServerSize(sf.getFileSize());
346  wf.setServerLocked(sf.isLocked());
347  wf.setServerLocker(sf.getLocker());
348  }
349  }
350 
351  public int compareTo(Directory o) {
352  if (getName() == null || o.getName() == null) {
353  return 0;
354  }
355  return getName().compareTo(o.getName());
356  }
357 
358  public Collection<String> doQueryFolders() {
359  if (isUpdatable()) {
360  try {
361  GetQuery getQuery = new GetQuery(
362  getHost(), getPort(),
363  getContext() + "/pservice/queryFolders", false);
364  getQuery.getParameters().put("user", getUser());
365  getQuery.getParameters().put("path", getAbsolutePath());
366  getQuery.setSerializer(new BinarySerializer());
367  return (Collection<String>) getQuery.execute();
368  } catch (QueryException ex) {
369  Logger.getLogger(Directory.class.getName()).log(Level.SEVERE, null, ex);
370  }
371  }
372  return new ArrayList<String>();
373  }
374 
375  public Collection<ServerFile> doQueryFiles() {
376  if (isUpdatable()) {
377  try {
378  GetQuery getQuery = new GetQuery(
379  getHost(), getPort(),
380  getContext() + "/pservice/queryFiles", false);
381  getQuery.getParameters().put("user", getUser());
382  getQuery.getParameters().put("path", getAbsolutePath());
383  getQuery.setSerializer(new BinarySerializer());
384  return (Collection<ServerFile>) getQuery.execute();
385  } catch (QueryException ex) {
386  Logger.getLogger(Directory.class.getName()).log(Level.SEVERE, null, ex);
387  }
388  }
389  return new ArrayList<ServerFile>();
390  }
391 
392  private static String encryptString(String str) {
393  StringBuilder sb = new StringBuilder();
394  for(byte b : str.getBytes()) {
395  sb.append(String.format("%03d", b));
396  }
397  return sb.toString();
398  }
399 
400  private static String decryptString(String str) {
401  byte[] b = new byte[str.length() / 3];
402  for(int i = 0; i < str.length(); i+=3) {
403  b[i / 3] = new Byte(str.substring(i, i+3));
404  }
405  return new String(b);
406  }
407 
408 }
static void nextProgress(String caption, String step, double inc)
Directory getDir(String dir)
Collection< String > doQueryFolders()
Definition: Directory.java:358
void writeXML(Element root)
Definition: Directory.java:257
Directory addDirectory(String name)
Definition: Directory.java:193
int compareTo(Directory o)
Definition: Directory.java:351
void setFiles(WorkFileSet files)
Definition: Directory.java:59
WorkFile addFile(String name)
Definition: Directory.java:201
Collection< ServerFile > doQueryFiles()
Definition: Directory.java:375
void setDirs(DirectorySet dirs)
Definition: Directory.java:51
void setParent(Directory parent)
Definition: Directory.java:75
void setName(String name)
Definition: Directory.java:67
void readXML(Element root)
Definition: Directory.java:239
void setServerPath(String serverPath)
Definition: Directory.java:103
WorkFile getFile(String file)
void setName(String name)
Definition: WorkFile.java:71
void setFile(File file)
Definition: WorkFile.java:63
void readXML(Element root)
Definition: WorkFile.java:151
void setDownModification(Date downModification)
Definition: WorkFile.java:55
void setServerLocked(boolean serverLocked)
Definition: WorkFile.java:87
void setParent(Directory parent)
Definition: WorkFile.java:79
void setServerLocker(String serverLocker)
Definition: WorkFile.java:95
void setServerOwner(String serverOwner)
Definition: WorkFile.java:103
void setServerSize(long serverSize)
Definition: WorkFile.java:119