Feature Request [ 1670034 ] Store Attachments In File System Or In DB

https://sourceforge.net/tracker/index.php?func=detail&aid=1670034&group_id=176962&atid=879335
This commit is contained in:
kthiemann 2007-02-27 16:52:16 +00:00
parent 3224cb77d0
commit 6f41457450
2 changed files with 442 additions and 18 deletions

View File

@ -17,11 +17,30 @@
package org.compiere.model;
import java.io.*;
import java.nio.channels.FileChannel;
import java.sql.*;
import java.util.*;
import java.util.logging.*;
import java.util.zip.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.compiere.util.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
* Attachment Model.
@ -86,6 +105,8 @@ public class MAttachment extends X_AD_Attachment
public MAttachment(Properties ctx, int AD_Attachment_ID, String trxName)
{
super (ctx, AD_Attachment_ID, trxName);
initAttachmentStoreDetails(ctx, trxName);
} // MAttachment
/**
@ -100,6 +121,7 @@ public class MAttachment extends X_AD_Attachment
this (ctx, 0, trxName);
setAD_Table_ID (AD_Table_ID);
setRecord_ID (Record_ID);
initAttachmentStoreDetails(ctx, trxName);
} // MAttachment
/**
@ -111,6 +133,7 @@ public class MAttachment extends X_AD_Attachment
public MAttachment(Properties ctx, ResultSet rs, String trxName)
{
super(ctx, rs, trxName);
initAttachmentStoreDetails(ctx, trxName);
} // MAttachment
/** Indicator for no data */
@ -121,6 +144,40 @@ public class MAttachment extends X_AD_Attachment
/** List of Entry Data */
private ArrayList<MAttachmentEntry> m_items = null;
/** is this client using the file system for attachments */
private boolean isStoreAttachmentsOnFileSystem = false;
/** attachment (root) path - if file system is used */
private String m_attachmentPathRoot = "";
/** string replaces the attachment root in stored xml file
* to allow the changing of the attachment root. */
private final String ATTACHMENT_FOLDER_PLACEHOLDER = "%ATTACHMENT_FOLDER%";
/**
* Get the isStoreAttachmentsOnFileSystem and attachmentPath for the client.
* @param ctx
* @param trxName
*/
private void initAttachmentStoreDetails(Properties ctx, String trxName){
final MClient client = new MClient(ctx, this.getAD_Client_ID(), trxName);
isStoreAttachmentsOnFileSystem = client.isStoreAttachmentsOnFileSystem();
if(isStoreAttachmentsOnFileSystem){
if(File.separatorChar == '\\'){
m_attachmentPathRoot = client.getWindowsAttachmentPath();
} else {
m_attachmentPathRoot = client.getUnixAttachmentPath();
}
if("".equals(m_attachmentPathRoot)){
log.severe("no attachmentPath defined");
} else if (!m_attachmentPathRoot.endsWith(File.separator)){
log.warning("attachment path doesn't end with " + File.separator);
m_attachmentPathRoot = m_attachmentPathRoot + File.separator;
log.fine(m_attachmentPathRoot);
}
}
}
/**
* Set Client Org
* @param AD_Client_ID client
@ -277,13 +334,24 @@ public class MAttachment extends X_AD_Attachment
/**
* Delete Entry
* @param index index
*
* @param index
* index
* @return true if deleted
*/
public boolean deleteEntry (int index)
{
if (index >= 0 && index < m_items.size())
{
public boolean deleteEntry(int index) {
if (index >= 0 && index < m_items.size()) {
if(isStoreAttachmentsOnFileSystem){
//remove files
final MAttachmentEntry entry = m_items.get(index);
final File file = entry.getFile();
log.fine("delete: " + file.getAbsolutePath());
if(file !=null && file.exists()){
if(!file.delete()){
log.warning("unable to delete " + file.getAbsolutePath());
}
}
}
m_items.remove(index);
log.config("Index=" + index + " - NewSize=" + m_items.size());
return true;
@ -306,14 +374,21 @@ public class MAttachment extends X_AD_Attachment
/**
* Get Entry Name
* @param index index
*
* @param index
* index
* @return name or null
*/
public String getEntryName (int index)
{
public String getEntryName(int index) {
MAttachmentEntry item = getEntry(index);
if (item != null)
return item.getName();
if (item != null){
//strip path
String name = item.getName();
if(name!=null && isStoreAttachmentsOnFileSystem){
name = name.substring(name.lastIndexOf(File.separator)+1);
}
return name;
}
return null;
} // getEntryName
@ -375,11 +450,24 @@ public class MAttachment extends X_AD_Attachment
return null;
} // getEntryFile
/**
* Save Entry Data in Zip File format
* @return true if saved
*/
private boolean saveLOBData()
{
if(isStoreAttachmentsOnFileSystem){
return saveLOBDataToFileSystem();
}
return saveLOBDataToDB();
}
/**
* Save Entry Data in Zip File format into the database.
* @return true if saved
*/
private boolean saveLOBDataToDB()
{
if (m_items == null || m_items.size() == 0)
{
@ -423,11 +511,118 @@ public class MAttachment extends X_AD_Attachment
return false;
} // saveLOBData
/**
* Save Entry Data to the file system.
* @return true if saved
*/
private boolean saveLOBDataToFileSystem()
{
if("".equals(m_attachmentPathRoot)){
log.severe("no attachmentPath defined");
return false;
}
if (m_items == null || m_items.size() == 0) {
setBinaryData(null);
return true;
}
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
final DocumentBuilder builder = factory.newDocumentBuilder();
final Document document = builder.newDocument();
final Element root = document.createElement("attachments");
document.appendChild(root);
document.setXmlStandalone(true);
// create xml entries
for (int i = 0; i < m_items.size(); i++) {
log.fine(m_items.get(i).toString());
File entryFile = m_items.get(i).getFile();
final String path = entryFile.getAbsolutePath();
// if local file - copy to central attachment folder
log.fine(path + " - " + m_attachmentPathRoot);
if (!path.startsWith(m_attachmentPathRoot)) {
log.fine("move file: " + path);
FileChannel in = null;
FileChannel out = null;
try {
//create destination folder
final File destFolder = new File(m_attachmentPathRoot + File.separator + getAttachmentPathSnippet());
if(!destFolder.exists()){
if(!destFolder.mkdirs()){
log.warning("unable to create folder: " + destFolder.getPath());
}
}
final File destFile = new File(m_attachmentPathRoot + File.separator
+ getAttachmentPathSnippet() + File.separator + entryFile.getName());
in = new FileInputStream(entryFile).getChannel();
out = new FileOutputStream(destFile).getChannel();
in.transferTo(0, in.size(), out);
in.close();
out.close();
if(entryFile.exists()){
if(!entryFile.delete()){
entryFile.deleteOnExit();
}
}
entryFile = destFile;
} catch (IOException e) {
e.printStackTrace();
log.severe("unable to copy file " + entryFile.getAbsolutePath() + " to "
+ m_attachmentPathRoot + File.separator +
getAttachmentPathSnippet() + File.separator + entryFile.getName());
} finally {
if (in != null && in.isOpen()) {
in.close();
}
if (out != null && out.isOpen()) {
out.close();
}
}
}
final Element entry = document.createElement("entry");
//entry.setAttribute("name", m_items.get(i).getName());
entry.setAttribute("name", getEntryName(i));
String filePathToStore = entryFile.getAbsolutePath();
filePathToStore = filePathToStore.replaceFirst(m_attachmentPathRoot.replaceAll("\\\\","\\\\\\\\"), ATTACHMENT_FOLDER_PLACEHOLDER);
log.fine(filePathToStore);
entry.setAttribute("file", filePathToStore);
root.appendChild(entry);
}
final Source source = new DOMSource(document);
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
final Result result = new StreamResult(bos);
final Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.transform(source, result);
final byte[] xmlData = bos.toByteArray();
log.fine(bos.toString());
setBinaryData(xmlData);
return true;
} catch (Exception e) {
log.log(Level.SEVERE, "saveLOBData", e);
}
setBinaryData(null);
return false;
}
/**
* Load Data into local m_data
* @return true if success
*/
private boolean loadLOBData ()
{
if(isStoreAttachmentsOnFileSystem){
return loadLOBDataFromFileSystem();
}
return loadLOBDataFromDB();
}
/**
* Load Data from database
* @return true if success
*/
private boolean loadLOBDataFromDB ()
{
// Reset
m_items = new ArrayList<MAttachmentEntry>();
@ -482,6 +677,110 @@ public class MAttachment extends X_AD_Attachment
return true;
} // loadLOBData
/**
* Load Data from file system
* @return true if success
*/
private boolean loadLOBDataFromFileSystem(){
if("".equals(m_attachmentPathRoot)){
log.severe("no attachmentPath defined");
return false;
}
// Reset
m_items = new ArrayList<MAttachmentEntry>();
//
byte[] data = getBinaryData();
if (data == null)
return true;
log.fine("TextFileSize=" + data.length);
if (data.length == 0)
return true;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
final DocumentBuilder builder = factory.newDocumentBuilder();
final Document document = builder.parse(new ByteArrayInputStream(data));
final NodeList entries = document.getElementsByTagName("entry");
for (int i = 0; i < entries.getLength(); i++) {
final Node entryNode = entries.item(i);
final NamedNodeMap attributes = entryNode.getAttributes();
final Node fileNode = attributes.getNamedItem("file");
final Node nameNode = attributes.getNamedItem("name");
if(fileNode==null || nameNode==null){
log.severe("no filename for entry " + i);
m_items = null;
return false;
}
log.fine("name: " + nameNode.getNodeValue());
String filePath = fileNode.getNodeValue();
log.fine("filePath: " + filePath);
if(filePath!=null){
filePath = filePath.replaceFirst(ATTACHMENT_FOLDER_PLACEHOLDER, m_attachmentPathRoot.replaceAll("\\\\","\\\\\\\\"));
//just to be shure...
String replaceSeparator = File.separator;
if(!replaceSeparator.equals("/")){
replaceSeparator = "\\\\";
}
filePath = filePath.replaceAll("/", replaceSeparator);
filePath = filePath.replaceAll("\\\\", replaceSeparator);
}
log.fine("filePath: " + filePath);
final File file = new File(filePath);
if (file.exists()) {
// read files into byte[]
final byte[] dataEntry = new byte[(int) file.length()];
try {
final FileInputStream fileInputStream = new FileInputStream(file);
fileInputStream.read(dataEntry);
fileInputStream.close();
} catch (FileNotFoundException e) {
log.severe("File Not Found.");
e.printStackTrace();
} catch (IOException e1) {
log.severe("Error Reading The File.");
e1.printStackTrace();
}
final MAttachmentEntry entry = new MAttachmentEntry(filePath,
dataEntry, m_items.size() + 1);
m_items.add(entry);
} else {
log.severe("file not found: " + file.getAbsolutePath());
}
}
} catch (SAXException sxe) {
// Error generated during parsing)
Exception x = sxe;
if (sxe.getException() != null)
x = sxe.getException();
x.printStackTrace();
log.severe(x.getMessage());
} catch (ParserConfigurationException pce) {
// Parser with specified options can't be built
pce.printStackTrace();
log.severe(pce.getMessage());
} catch (IOException ioe) {
// I/O error
ioe.printStackTrace();
log.severe(ioe.getMessage());
}
return true;
}
/**
* Returns a path snippet, containing client, org, table and record id.
* @return String
*/
private String getAttachmentPathSnippet(){
return this.getAD_Client_ID() + File.separator +
this.getAD_Org_ID() + File.separator +
this.getAD_Table_ID() + File.separator + this.getRecord_ID();
}
/**
* Before Save
@ -495,6 +794,32 @@ public class MAttachment extends X_AD_Attachment
return saveLOBData(); // save in BinaryData
} // beforeSave
/**
* Executed before Delete operation.
* @return true if record can be deleted
*/
protected boolean beforeDelete ()
{
if(isStoreAttachmentsOnFileSystem){
//delete all attachment files and folder
for(int i=0; i<m_items.size(); i++) {
final MAttachmentEntry entry = m_items.get(i);
final File file = entry.getFile();
if(file !=null && file.exists()){
if(!file.delete()){
log.warning("unable to delete " + file.getAbsolutePath());
}
}
}
final File folder = new File(m_attachmentPathRoot + getAttachmentPathSnippet());
if(folder.exists()){
if(!folder.delete()){
log.warning("unable to delete " + folder.getAbsolutePath());
}
}
}
return true;
} // beforeDelete
/**************************************************************************
* Test

View File

@ -1,6 +1,6 @@
/******************************************************************************
* Product: Adempiere ERP & CRM Smart Business Solution *
* Copyright (C) 1999-2006 ComPiere, Inc. All Rights Reserved. *
* Copyright (C) 1999-2007 ComPiere, Inc. All Rights Reserved. *
* This program is free software;
you can redistribute it and/or modify it *
* under the terms version 2 of the GNU General Public License as published *
@ -48,6 +48,7 @@ setIsSmtpAuthorization (false); // N
setIsUseBetaFunctions (true); // Y
setMMPolicy (null); // F
setName (null);
setStoreAttachmentsOnFileSystem (false);
setValue (null);
}
*/
@ -114,6 +115,8 @@ public String getAD_Language()
{
return (String)get_Value("AD_Language");
}
/** Column name AD_Language */
public static final String COLUMNNAME_AD_Language = "AD_Language";
/** AutoArchive AD_Reference_ID=334 */
public static final int AUTOARCHIVE_AD_Reference_ID=334;
@ -145,6 +148,8 @@ public String getAutoArchive()
{
return (String)get_Value("AutoArchive");
}
/** Column name AutoArchive */
public static final String COLUMNNAME_AutoArchive = "AutoArchive";
/** Set Description.
@param Description Optional short description of the record */
public void setDescription (String Description)
@ -162,6 +167,8 @@ public String getDescription()
{
return (String)get_Value("Description");
}
/** Column name Description */
public static final String COLUMNNAME_Description = "Description";
/** Set Document Directory.
@param DocumentDir Directory for documents from the application server */
public void setDocumentDir (String DocumentDir)
@ -179,6 +186,8 @@ public String getDocumentDir()
{
return (String)get_Value("DocumentDir");
}
/** Column name DocumentDir */
public static final String COLUMNNAME_DocumentDir = "DocumentDir";
/** Set EMail Test.
@param EMailTest Test EMail */
public void setEMailTest (String EMailTest)
@ -196,6 +205,8 @@ public String getEMailTest()
{
return (String)get_Value("EMailTest");
}
/** Column name EMailTest */
public static final String COLUMNNAME_EMailTest = "EMailTest";
/** Set Cost Immediately.
@param IsCostImmediate Update Costs immediately for testing */
public void setIsCostImmediate (boolean IsCostImmediate)
@ -214,6 +225,8 @@ if (oo != null)
}
return false;
}
/** Column name IsCostImmediate */
public static final String COLUMNNAME_IsCostImmediate = "IsCostImmediate";
/** Set Multi Lingual Documents.
@param IsMultiLingualDocument Documents are Multi Lingual */
public void setIsMultiLingualDocument (boolean IsMultiLingualDocument)
@ -232,6 +245,8 @@ if (oo != null)
}
return false;
}
/** Column name IsMultiLingualDocument */
public static final String COLUMNNAME_IsMultiLingualDocument = "IsMultiLingualDocument";
/** Set Post Immediately.
@param IsPostImmediate Post the accounting immediately for testing */
public void setIsPostImmediate (boolean IsPostImmediate)
@ -250,6 +265,8 @@ if (oo != null)
}
return false;
}
/** Column name IsPostImmediate */
public static final String COLUMNNAME_IsPostImmediate = "IsPostImmediate";
/** Set Server EMail.
@param IsServerEMail Send EMail from Server */
public void setIsServerEMail (boolean IsServerEMail)
@ -268,6 +285,8 @@ if (oo != null)
}
return false;
}
/** Column name IsServerEMail */
public static final String COLUMNNAME_IsServerEMail = "IsServerEMail";
/** Set SMTP Authentication.
@param IsSmtpAuthorization Your mail server requires Authentication */
public void setIsSmtpAuthorization (boolean IsSmtpAuthorization)
@ -286,6 +305,8 @@ if (oo != null)
}
return false;
}
/** Column name IsSmtpAuthorization */
public static final String COLUMNNAME_IsSmtpAuthorization = "IsSmtpAuthorization";
/** Set Use Beta Functions.
@param IsUseBetaFunctions Enable the use of Beta Functionality */
public void setIsUseBetaFunctions (boolean IsUseBetaFunctions)
@ -304,6 +325,8 @@ if (oo != null)
}
return false;
}
/** Column name IsUseBetaFunctions */
public static final String COLUMNNAME_IsUseBetaFunctions = "IsUseBetaFunctions";
/** MMPolicy AD_Reference_ID=335 */
public static final int MMPOLICY_AD_Reference_ID=335;
@ -331,6 +354,8 @@ public String getMMPolicy()
{
return (String)get_Value("MMPolicy");
}
/** Column name MMPolicy */
public static final String COLUMNNAME_MMPolicy = "MMPolicy";
/** Set Model Validation Classes.
@param ModelValidationClasses List of data model validation classes separated by ;
*/
@ -350,6 +375,8 @@ public String getModelValidationClasses()
{
return (String)get_Value("ModelValidationClasses");
}
/** Column name ModelValidationClasses */
public static final String COLUMNNAME_ModelValidationClasses = "ModelValidationClasses";
/** Set Name.
@param Name Alphanumeric identifier of the entity */
public void setName (String Name)
@ -374,6 +401,8 @@ return (String)get_Value("Name");
{
return new KeyNamePair(get_ID(), getName());
}
/** Column name Name */
public static final String COLUMNNAME_Name = "Name";
/** Set Request EMail.
@param RequestEMail EMail address to send automated mails from or receive mails for automated processing (fully qualified) */
public void setRequestEMail (String RequestEMail)
@ -391,6 +420,8 @@ public String getRequestEMail()
{
return (String)get_Value("RequestEMail");
}
/** Column name RequestEMail */
public static final String COLUMNNAME_RequestEMail = "RequestEMail";
/** Set Request Folder.
@param RequestFolder EMail folder to process incoming emails;
if empty INBOX is used */
@ -410,6 +441,8 @@ public String getRequestFolder()
{
return (String)get_Value("RequestFolder");
}
/** Column name RequestFolder */
public static final String COLUMNNAME_RequestFolder = "RequestFolder";
/** Set Request User.
@param RequestUser User Name (ID) of the email owner */
public void setRequestUser (String RequestUser)
@ -427,6 +460,8 @@ public String getRequestUser()
{
return (String)get_Value("RequestUser");
}
/** Column name RequestUser */
public static final String COLUMNNAME_RequestUser = "RequestUser";
/** Set Request User Password.
@param RequestUserPW Password of the user name (ID) for mail processing */
public void setRequestUserPW (String RequestUserPW)
@ -444,6 +479,8 @@ public String getRequestUserPW()
{
return (String)get_Value("RequestUserPW");
}
/** Column name RequestUserPW */
public static final String COLUMNNAME_RequestUserPW = "RequestUserPW";
/** Set Mail Host.
@param SMTPHost Hostname of Mail Server for SMTP and IMAP */
public void setSMTPHost (String SMTPHost)
@ -461,6 +498,47 @@ public String getSMTPHost()
{
return (String)get_Value("SMTPHost");
}
/** Column name SMTPHost */
public static final String COLUMNNAME_SMTPHost = "SMTPHost";
/** Set Store Attachments On File System.
@param StoreAttachmentsOnFileSystem Store Attachments On File System */
public void setStoreAttachmentsOnFileSystem (boolean StoreAttachmentsOnFileSystem)
{
set_Value ("StoreAttachmentsOnFileSystem", Boolean.valueOf(StoreAttachmentsOnFileSystem));
}
/** Get Store Attachments On File System.
@return Store Attachments On File System */
public boolean isStoreAttachmentsOnFileSystem()
{
Object oo = get_Value("StoreAttachmentsOnFileSystem");
if (oo != null)
{
if (oo instanceof Boolean) return ((Boolean)oo).booleanValue();
return "Y".equals(oo);
}
return false;
}
/** Column name StoreAttachmentsOnFileSystem */
public static final String COLUMNNAME_StoreAttachmentsOnFileSystem = "StoreAttachmentsOnFileSystem";
/** Set Unix Attachment Path.
@param UnixAttachmentPath Unix Attachment Path - If you change this value make sure to copy the attachments to the new path! */
public void setUnixAttachmentPath (String UnixAttachmentPath)
{
if (UnixAttachmentPath != null && UnixAttachmentPath.length() > 255)
{
log.warning("Length > 255 - truncated");
UnixAttachmentPath = UnixAttachmentPath.substring(0,254);
}
set_Value ("UnixAttachmentPath", UnixAttachmentPath);
}
/** Get Unix Attachment Path.
@return Unix Attachment Path - If you change this value make sure to copy the attachments to the new path! */
public String getUnixAttachmentPath()
{
return (String)get_Value("UnixAttachmentPath");
}
/** Column name UnixAttachmentPath */
public static final String COLUMNNAME_UnixAttachmentPath = "UnixAttachmentPath";
/** Set Search Key.
@param Value Search key for the record in the format required - must be unique */
public void setValue (String Value)
@ -479,4 +557,25 @@ public String getValue()
{
return (String)get_Value("Value");
}
/** Column name Value */
public static final String COLUMNNAME_Value = "Value";
/** Set Windows Attachment Path.
@param WindowsAttachmentPath Windows Attachment Path - If you change this value make sure to copy the attachments to the new path! */
public void setWindowsAttachmentPath (String WindowsAttachmentPath)
{
if (WindowsAttachmentPath != null && WindowsAttachmentPath.length() > 255)
{
log.warning("Length > 255 - truncated");
WindowsAttachmentPath = WindowsAttachmentPath.substring(0,254);
}
set_Value ("WindowsAttachmentPath", WindowsAttachmentPath);
}
/** Get Windows Attachment Path.
@return Windows Attachment Path - If you change this value make sure to copy the attachments to the new path! */
public String getWindowsAttachmentPath()
{
return (String)get_Value("WindowsAttachmentPath");
}
/** Column name WindowsAttachmentPath */
public static final String COLUMNNAME_WindowsAttachmentPath = "WindowsAttachmentPath";
}