BrightSide Workbench Full Report + Source Code
DocumentsRoot.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.IOException;
22 import java.util.Collection;
23 import java.util.Properties;
24 import java.util.Vector;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27 import org.amic.util.file.FileUtil;
28 import org.amic.util.string.Cipher;
29 import org.jdom.Element;
30 import org.turro.elephant.context.ElephantContext;
31 import org.turro.elephant.security.IUser;
32 import org.turro.i18n.I_;
33 
38 public class DocumentsRoot {
39  private DocumentsBean docBean;
40  private String realRoot, docsRoot, privateKey, selected;
41  private Element configuration;
42 
44  public DocumentsRoot(DocumentsBean docBean, String realRoot, String relativeRoot, Element config) {
45  this.docBean = docBean;
46  this.realRoot = realRoot;
47  this.docsRoot = realRoot + relativeRoot;
48  this.configuration = config;
49  privateKey = Cipher.digest(docsRoot, 30);
50  docBean.setAttribute("@" + privateKey, docsRoot);
51  }
52 
53  public Properties getPropertiesFromUser(IUser user) {
54  File userFolder = new File(getPath() + "/" + user.getId());
55  if(!userFolder.exists()) {
56  userFolder.mkdirs();
57  Properties props = new Properties();
58  props.put("root.name", user.getName());
59  savePropertiesTo(user.getId(), props);
60  return props;
61  }
62  return getPropertiesFrom(user.getId());
63  }
64 
65  public void savePropertiesToUser(Properties props, IUser user) {
66  File userFolder = new File(getPath() + "/" + user.getId());
67  if(!userFolder.exists()) {
68  userFolder.mkdirs();
69  }
70  savePropertiesTo(user.getId(), props);
71  }
72 
73  public Properties getPropertiesFrom(String folder) {
75  getPath() + "/" + folder + "/.docs.properties");
76  }
77 
78  public void savePropertiesTo(String folder, Properties props) {
80  getPath() + "/" + folder + "/.docs.properties");
81  }
82 
84  return docBean;
85  }
86 
88  return new FileDocument(this, null, docsRoot);
89  }
90 
91  public void setFileDocumentSelected(String selected) {
92  this.selected = selected;
93  }
94 
96  return new FileDocument(this, null, docsRoot + selected);
97  }
98 
99  public String getAttribute(String attrib) {
100  return configuration != null ? configuration.getAttributeValue(attrib) : null;
101  }
102 
103  public boolean getCanNew() {
104  return docBean.getCanNew();
105  }
106 
107  public boolean getCanDelete() {
108  return docBean.getCanDelete();
109  }
110 
111  public boolean getCanNewFolder() {
112  return docBean.getCanNewFolder();
113  }
114 
115  public boolean getCanDelFolder() {
116  return docBean.getCanDelFolder();
117  }
118 
119  public String getDownloadURL() {
120  return docBean.getDownloadURL() + "?down_path=@" + privateKey;
121  }
122 
123  public String getPath() {
124  return realRoot;
125  }
126 
127  public String getRelativePath(String path) {
128  if(path.startsWith(docsRoot)) {
129  return path.substring(docsRoot.length());
130  }
131  return path;
132  }
133 
134  public Collection getInsertionPointsWrapped() {
135  Vector v = new Vector();
136  String pattern = getAttribute("insertionPattern");
137  for(FileDocument fd : getFileDocumentRoot().getListFolders()) {
138  if(pattern == null || fd.getName().matches(pattern)) {
139  v.add(new FileDocumentWrapper(fd, true));
140  }
141  }
142  return v;
143  }
144 
146  Vector v = new Vector();
147  String pattern = getAttribute("insertionPattern");
148  for(FileDocument fd : getFileDocumentRoot().getListFolders()) {
149  if(pattern == null || fd.getName().matches(pattern)) {
150  v.add(fd);
151  }
152  }
153  return (FileDocument[]) v.toArray(new FileDocument[0]);
154  }
155 
156  public Collection<FileDocument> getNotValidated() {
157  Vector v = new Vector();
158  fillNotValidated(v, getFileDocumentRoot());
159  return v;
160  }
161 
162  private void fillNotValidated(Vector v, FileDocument root) {
163  for (FileDocument fd : root.getNotValidated()) {
164  if(fd.isHidden()) {
165  v.add(fd);
166  }
167  else if (fd.isDirectory()) {
168  fillNotValidated(v, fd);
169  }
170  }
171  }
172 
173  public String readConfig(String property) {
174  return getFileDocumentRoot().readConfig(property);
175  }
176 
177  public String readRootConfig(String property) {
178  Properties props = docBean.getPropertiesFile(realRoot + "/.docs.properties");
179  if(props != null) {
180  return props.getProperty(property);
181  }
182  return null;
183  }
184 
185  public void addInsertionPoint(String ident, String name) {
186  if(ident.matches(getAttribute("insertionPattern"))) {
187  new File(getPath() + "/" + ident).mkdirs();
188  Properties props = new Properties();
189  props.put("root.name", name);
190  savePropertiesTo(ident, props);
191  copyTemplate(ident);
192  docBean.getMessages().addMessage(I_.get("Insertion point created"));
193  }
194  else {
195  docBean.getMessages().addError(I_.get("Identifier error, does not match pattern"));
196  }
197  }
198 
199  private void copyTemplate(String ident) {
200  try {
201  String template = getAttribute("template");
202  FileUtil.copy(getPath() + template, getPath() + "/" + ident);
203  } catch (IOException ex) {
204  Logger.getLogger(DocumentsRoot.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
205  }
206  }
207 
208 }
void savePropertiesFile(Properties props, String file)
void setAttribute(String key, String value)
void savePropertiesToUser(Properties props, IUser user)
void addInsertionPoint(String ident, String name)
Collection< FileDocument > getNotValidated()
DocumentsRoot(DocumentsBean docBean, String realRoot, String relativeRoot, Element config)
void savePropertiesTo(String folder, Properties props)
Properties getPropertiesFrom(String folder)
static String get(String msg)
Definition: I_.java:41