18 package org.turro.elephant.documents;
21 import java.io.FileInputStream;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.UnsupportedEncodingException;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Collection;
28 import java.util.HashMap;
29 import java.util.HashSet;
30 import java.util.Iterator;
31 import java.util.List;
33 import java.util.Properties;
35 import java.util.TreeSet;
36 import java.util.logging.Level;
37 import java.util.logging.Logger;
38 import org.apache.commons.fileupload.FileItem;
39 import org.apache.commons.fileupload.FileItemFactory;
40 import org.apache.commons.fileupload.FileUploadException;
41 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
42 import org.apache.commons.fileupload.servlet.ServletFileUpload;
43 import org.jdom.Document;
44 import org.jdom.Element;
45 import org.jdom.JDOMException;
46 import org.jdom.input.SAXBuilder;
47 import org.turro.elephant.context.ElephantContext;
48 import org.turro.elephant.context.IConstructor;
49 import org.turro.elephant.impl.context.ContextFactory;
50 import org.turro.elephant.impl.util.Localizer;
51 import org.turro.elephant.impl.util.ProcessMessages;
52 import org.turro.elephant.security.IUser;
53 import org.turro.i18n.I_;
54 import org.turro.mail.impl.MailPool;
55 import org.turro.mail.impl.MailUser;
65 private String docsRoot;
66 private String selectedUser, selectedInsertion, selectedFolder, selectedId, selectedRoot;
74 this.constructor = constructor;
89 ArrayList v =
new ArrayList();
90 Element config = getConfiguration();
94 Iterator it = config.getChildren(
"root").iterator();
96 while (it.hasNext()) {
98 el = (Element) it.next();
99 if (el.getAttribute(
"role") ==
null || constructor.
isInRole(el.getAttributeValue(
"role"))) {
100 docsRoot = el.getTextTrim();
111 ArrayList v =
new ArrayList();
112 Element config = getConfiguration();
114 if (config !=
null) {
116 Iterator it = config.getChildren(
"root").iterator();
118 while (it.hasNext()) {
120 el = (Element) it.next();
121 if (el.getAttribute(
"role") ==
null || constructor.
isInRole(el.getAttributeValue(
"role"))) {
122 if (
"true".equals(el.getAttributeValue(
"appendUser"))) {
123 docsRoot = el.getTextTrim();
135 if (selectedUser ==
null && constructor.
getUser() !=
null) {
138 ArrayList v =
new ArrayList();
139 Element config = getConfiguration();
141 if (config !=
null) {
143 Iterator it = config.getChildren(
"root").iterator();
145 while (it.hasNext()) {
147 el = (Element) it.next();
148 if (el.getAttribute(
"role") ==
null || constructor.
isInRole(el.getAttributeValue(
"role"))) {
149 docsRoot = el.getTextTrim();
150 if (
"true".equals(el.getAttributeValue(
"appendUser"))) {
151 if (selectedUser !=
null) {
152 userRoot =
new DocumentsRoot(
this, docsRoot,
"/" + selectedUser, el);
160 if (userRoot !=
null) {
162 for (
int i = 0; i < 30; i++) {
177 this.selectedId = selectedId;
181 this.selectedRoot = selectedRoot;
186 if(selectedId.equals(dr.getAttribute(
"id"))) {
195 if (selectedInsertion ==
null) {
198 ArrayList v =
new ArrayList();
199 Element config = getConfiguration();
201 if (config !=
null) {
203 Iterator it = config.getChildren(
"root").iterator();
205 while (it.hasNext()) {
207 el = (Element) it.next();
208 if (el.getAttribute(
"role") ==
null || constructor.
isInRole(el.getAttributeValue(
"role"))) {
209 docsRoot = el.getTextTrim();
210 if (
"true".equals(el.getAttributeValue(
"appendUser"))) {
211 userRoot =
new DocumentsRoot(
this, docsRoot,
"/" + selectedInsertion, el);
225 ArrayList v =
new ArrayList();
227 fillFolders(v, dr.getFileDocumentRoot(),
"true".equals(dr.getAttribute(
"onlyLeafInsertion")));
235 ArrayList v =
new ArrayList();
244 ArrayList v =
new ArrayList();
246 fillFolders(v, dr.getFileDocumentRoot(),
false);
251 private void fillFolders(ArrayList v,
FileDocument root,
boolean onlyLeafInsert) {
254 if (fd.isDirectory()) {
255 fillFolders(v, fd, onlyLeafInsert);
259 if (leaf || !onlyLeafInsert) {
265 ArrayList v =
new ArrayList();
267 v.addAll(dr.getNotValidated());
281 Properties tmp =
new Properties();
282 if (
new File(file).exists()) {
285 fis =
new FileInputStream(file);
288 }
catch (Exception ex) {
296 FileOutputStream fos;
298 fos =
new FileOutputStream(file);
299 props.store(fos,
null);
301 }
catch (Exception ex) {
311 return constructor.
isInRole(
"documents:view-as");
315 return constructor.
isInRole(
"documents:new");
319 return constructor.
isInRole(
"documents:delete");
323 return constructor.
isInRole(
"documents:new-folder");
327 return constructor.
isInRole(
"documents:del-folder");
331 return constructor.
isInRole(
"documents:edit-allowed");
335 return constructor.
isInRole(
"documents:new-insertpoint");
339 return constructor.
isInRole(
"documents:validate");
343 return constructor.
isInRole(
"contact:list");
347 Set tmp =
new TreeSet();
349 if (usr.isInRole(
"documents:list")) {
357 if (selectedUser ==
null && constructor.
getUser() !=
null) {
361 return selectedUser !=
null && selectedUser.equals(user.
getId());
369 Set tmp =
new TreeSet();
370 Element config = getConfiguration();
372 if (config !=
null) {
374 Iterator it = config.getChildren(
"root").iterator();
376 while (it.hasNext()) {
378 el = (Element) it.next();
379 if (el.getAttribute(
"role") ==
null || constructor.
isInRole(el.getAttributeValue(
"role"))) {
380 docsRoot = el.getTextTrim();
391 return selectedInsertion !=
null && selectedInsertion.equals(fd.getName());
395 return selectedFolder !=
null && selectedFolder.equals(fd.getAbsolutePath());
403 return mode == 1 ?
"checked" :
"";
407 return mode == 2 ?
"checked" :
"";
411 return mode == 3 ?
"checked" :
"";
415 return mode == 4 ?
"checked" :
"";
419 return mode == 5 ?
"checked" :
"";
424 if(path.startsWith(dr.getPath())) {
431 private void processParameters() {
432 if (constructor.
isInRole(
"documents:new") && ServletFileUpload.isMultipartContent(constructor.
getRequest())) {
434 FileItemFactory factory =
new DiskFileItemFactory();
435 ServletFileUpload upload =
new ServletFileUpload(factory);
436 upload.setHeaderEncoding(ElephantContext.getEncoding());
437 List items = upload.parseRequest(constructor.
getRequest());
438 Iterator iter = items.iterator();
439 FileItem file =
null;
440 Map fields =
new HashMap();
441 while (iter.hasNext()) {
442 FileItem item = (FileItem) iter.next();
443 if (!item.isFormField()) {
446 fields.put(item.getFieldName(), item.getString(ElephantContext.getEncoding()));
451 String path = (String) fields.get(
"path");
452 selectedFolder = path;
453 String onlyName = file.getName();
454 int p = onlyName.lastIndexOf(
'/');
456 p = onlyName.lastIndexOf(
'\\');
458 new File(path).mkdirs();
459 DocumentsRoot dr = getFromPath(path);
460 boolean mustValidate = dr !=
null &&
"true".equals(dr.getAttribute(
"validate"));
461 String realName = path +
"/" + (mustValidate ?
"." :
"") + onlyName.substring(p + 1);
462 File uploadedFile =
new File(realName);
463 file.write(uploadedFile);
464 sendRequiresValidationEmail(dr, uploadedFile);
465 }
catch (Exception ex) {
466 Logger.getLogger(
DocumentsBean.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(
null), ex);
469 }
catch (FileUploadException ex) {
470 Logger.getLogger(
DocumentsBean.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(
null), ex);
471 }
catch (UnsupportedEncodingException ex) {
472 Logger.getLogger(
DocumentsBean.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(
null), ex);
474 }
else if (constructor.
isInRole(
"documents:delete") && constructor.
getParameter(
"delfile") !=
null) {
477 org.amic.util.file.FileUtil.deleteFile(file);
479 }
else if (constructor.
isInRole(
"documents:new-folder") && constructor.
getParameter(
"newfolder") !=
null) {
482 file =
new File(file.getPath() +
"/" + constructor.
getParameter(
"folder").replaceAll(
"[^\\/a-zA-Z0-9\\-\\_\\.]",
"_"));
485 }
else if (constructor.
isInRole(
"documents:edit-allowed") && constructor.
getParameter(
"saveallowed") !=
null) {
486 IUser user = ContextFactory.getUser(constructor);
487 user.impersonate(selectedUser);
490 if (
"true".equals(dr.getAttribute(
"appendUser"))) {
491 Properties props = dr.getPropertiesFromUser(user);
492 for (
int i = 0; i < 30; i++) {
493 props.remove(
"allowed." + i);
496 for (FileDocument fd : dr.getInsertionFolders()) {
498 props.put(
"allowed." + i++, fd.getName());
501 dr.savePropertiesToUser(props, user);
506 }
else if (constructor.
isInRole(
"documents:new-insertpoint") && constructor.
getParameter(
"addpoint") !=
null) {
508 if (
"true".equals(dr.getAttribute(
"appendUser")) && dr.getPath().equals(constructor.
getParameter(
"selectedRoot"))) {
512 }
else if (constructor.
isInRole(
"documents:validate") && constructor.
getParameter(
"validatefile") !=
null) {
515 file.renameTo(
new File(
516 file.getAbsolutePath().substring(0, file.getAbsolutePath().lastIndexOf(
"/")) +
518 file.getName().substring(1)));
522 selectedUser = constructor.
getParameter(
"selectedUser");
524 if (constructor.
getParameter(
"selectedInsertion") !=
null) {
525 selectedInsertion = constructor.
getParameter(
"selectedInsertion");
529 mode = Integer.valueOf(m);
534 private Element getConfiguration() {
535 Element config =
null;
537 if (confFile.exists()) {
538 SAXBuilder builder =
new SAXBuilder();
541 doc = builder.build(confFile);
542 config = doc.getRootElement();
543 }
catch (IOException ex) {
544 Logger.getLogger(
DocumentsBean.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(
null), ex);
545 }
catch (JDOMException ex) {
546 Logger.getLogger(
DocumentsBean.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(
null), ex);
552 private void sendRequiresValidationEmail(DocumentsRoot dr, File uploadedFile) {
553 MailPool mp = (MailPool) ContextFactory.getImplementation(constructor,
"IMailPool_Documents");
554 mp.setEncoding(ElephantContext.getEncoding());
555 mp.addCssFile(ElephantContext.getRealPath(
"/_internal/css/mail.css"));
556 String subject = I_.get(
"{file} requires validation").replaceAll(
"\\{file\\}", uploadedFile.getName());
557 Set<MailUser> emails =
new HashSet<MailUser>();
558 emails.add(
new MailUser(constructor, dr.getAttribute(
"owner"),
"#user", subject,
""));
559 for(MailUser mc : emails) {
560 mp.addToPool(
null, mc.getEmail(),
null, subject, mc.getMessage(
""),
"text/html");
static String getRootWebPath()
static String logMsg(String msg)
void setSelectedId(String selectedId)
FileDocument[] getFolders()
boolean isSelected(IUser user)
Collection getInsertionFoldersWrapped()
boolean getCanEditPermissions()
DocumentsRoot[] getRootsForInsertion()
Properties getPropertiesFile(String file)
FileDocument[] getNotValidated()
boolean isFolderSelected(FileDocument fd)
boolean getCanNewInsertPoint()
void savePropertiesFile(Properties props, String file)
void setConstructor(IConstructor constructor)
Set< DocumentsRoot > getInsertionPointsWrapped()
static final String DOCS_CONFIGURATION
DocumentsRoot getDocumentRootById()
boolean getCanDelFolder()
String getAddingInsertionPoint()
boolean getCanNewFolder()
ProcessMessages getMessages()
FileDocument[] getInsertionFolders()
String getManagingPermissions()
boolean isInsertionSelected(FileDocument fd)
DocumentsRoot[] getDocumentRoots()
DocumentsRoot[] getRealRoots()
void setAttribute(String key, String value)
IConstructor getConstructor()
void setSelectedRoot(String selectedRoot)
String getManagingFiles()
DocumentsRoot[] getRealRootsWithUser()
String readConfig(String property)
void setFileDocumentSelected(String selected)
Collection getInsertionPointsWrapped()
FileDocument[] getListFolders()
static ProcessMessages getInstance()
void addMessage(String message)
HttpServletRequest getRequest()
String getParameter(String param)
void setSessionAttribute(String key, Object value)
boolean isInRole(String role)