details:   https://code.openbravo.com/erp/devel/pi/rev/bed326b6ddf6
changeset: 20726:bed326b6ddf6
user:      Shankar Balachandran <shankar.balachandran <at> openbravo.com>
date:      Thu Jul 11 11:00:28 2013 +0530
summary:   Fixes Issue 0020623, Fixes Issue 24289: 32K limit for attachments

The attachment model has been changed as explained in the wiki[1].
A new ant task has been added to migrate attachments from old model to new 
model.

[1] http://wiki.openbravo.com/wiki/Attachments

diffstat:

 build.xml                                                                      
                         |    8 +-
 
modules/org.openbravo.client.application/src/org/openbravo/client/application/window/AttachmentsAH.java
 |   10 +-
 src-db/database/model/tables/C_FILE.xml                                        
                         |    4 +
 src-db/database/sourcedata/AD_COLUMN.xml                                       
                         |   35 ++
 src-db/database/sourcedata/AD_ELEMENT.xml                                      
                         |   14 +
 src-db/database/sourcedata/AD_REF_LIST.xml                                     
                         |   12 +
 src/org/openbravo/erpCommon/businessUtility/TabAttachments.java                
                         |  141 ++++++++-
 src/org/openbravo/erpCommon/businessUtility/TabAttachments_data.xsql           
                         |    5 +-
 src/org/openbravo/erpCommon/utility/MigrateAttachments.java                    
                         |  151 ++++++++++
 src/org/openbravo/erpCommon/utility/reporting/ReportManager.java               
                         |   11 +-
 10 files changed, 371 insertions(+), 20 deletions(-)

diffs (truncated from 617 to 300 lines):

diff -r b0cfc1e49543 -r bed326b6ddf6 build.xml
--- a/build.xml Fri Jul 12 15:03:06 2013 +0200
+++ b/build.xml Thu Jul 11 11:00:28 2013 +0530
@@ -12,7 +12,7 @@
 * under the License.
 * The Original Code is Openbravo ERP.
 * The Initial Developer of the Original Code is Openbravo SLU
-* All portions are Copyright (C) 2001-2012 Openbravo SLU
+* All portions are Copyright (C) 2001-2013 Openbravo SLU
 * All Rights Reserved.
 * Contributor(s):  ______________________________________.
 ************************************************************************
@@ -371,6 +371,12 @@
     <ant dir="${base.src}" target="compile.complete" inheritAll="true" 
inheritRefs="true" />
   </target>
 
+  <target name="migrate.attachments" depends="init">
+    <java classname="org.openbravo.erpCommon.utility.MigrateAttachments" 
fork="true" failonerror="yes" jvm="${env.JAVA_HOME}/bin/java">
+      <classpath refid="project.class.path" />
+    </java>
+  </target>
+
   <target name="smartbuild" depends="init">
     <property name="smart.mode" value="true" />
     <property name="onlyIfModified" value="true" />
diff -r b0cfc1e49543 -r bed326b6ddf6 
modules/org.openbravo.client.application/src/org/openbravo/client/application/window/AttachmentsAH.java
--- 
a/modules/org.openbravo.client.application/src/org/openbravo/client/application/window/AttachmentsAH.java
   Fri Jul 12 15:03:06 2013 +0200
+++ 
b/modules/org.openbravo.client.application/src/org/openbravo/client/application/window/AttachmentsAH.java
   Thu Jul 11 11:00:28 2013 +0530
@@ -10,8 +10,8 @@
  * License for the specific  language  governing  rights  and  limitations
  * under the License. 
  * The Original Code is Openbravo ERP. 
- * The Initial Developer of the Original Code is Openbravo SLU 
- * All portions are Copyright (C) 2011 Openbravo SLU 
+ * The Initial Developer of the Original Code is Openbravo SLU
+ * All portions are Copyright (C) 2011-2013 Openbravo SLU
  * All Rights Reserved. 
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -37,6 +37,7 @@
 import org.openbravo.dal.service.OBCriteria;
 import org.openbravo.dal.service.OBDal;
 import org.openbravo.dal.service.OBDao;
+import org.openbravo.erpCommon.businessUtility.TabAttachments;
 import org.openbravo.model.ad.ui.Tab;
 import org.openbravo.model.ad.utility.Attachment;
 import org.openbravo.utils.FileUtility;
@@ -79,8 +80,9 @@
   private void deleteFile(Attachment attachment) {
     String attachmentFolder = 
OBPropertiesProvider.getInstance().getOpenbravoProperties()
         .getProperty("attach.path");
-    String fileDirPath = attachmentFolder + "/" + 
DalUtil.getId(attachment.getTable()) + "-"
-        + attachment.getRecord();
+    String fileDir = 
TabAttachments.getAttachmentDirectory(attachment.getTable().getId(),
+        attachment.getRecord(), attachment.getName());
+    String fileDirPath = attachmentFolder + "/" + fileDir;
     FileUtility f = new FileUtility();
     final File file = new File(fileDirPath, attachment.getName());
     if (file.exists()) {
diff -r b0cfc1e49543 -r bed326b6ddf6 src-db/database/model/tables/C_FILE.xml
--- a/src-db/database/model/tables/C_FILE.xml   Fri Jul 12 15:03:06 2013 +0200
+++ b/src-db/database/model/tables/C_FILE.xml   Thu Jul 11 11:00:28 2013 +0530
@@ -57,6 +57,10 @@
         <default/>
         <onCreateDefault/>
       </column>
+      <column name="PATH" primaryKey="false" required="false" type="VARCHAR" 
size="2000" autoIncrement="false">
+        <default/>
+        <onCreateDefault/>
+      </column>
       <foreign-key foreignTable="AD_CLIENT" name="C_FILE_AD_CLIENT">
         <reference local="AD_CLIENT_ID" foreign="AD_CLIENT_ID"/>
       </foreign-key>
diff -r b0cfc1e49543 -r bed326b6ddf6 src-db/database/sourcedata/AD_COLUMN.xml
--- a/src-db/database/sourcedata/AD_COLUMN.xml  Fri Jul 12 15:03:06 2013 +0200
+++ b/src-db/database/sourcedata/AD_COLUMN.xml  Thu Jul 11 11:00:28 2013 +0530
@@ -309439,6 +309439,41 @@
 <!--D1BA6C3D01714981AE40690180B364AC-->  
<ISUSEDSEQUENCE><![CDATA[N]]></ISUSEDSEQUENCE>
 <!--D1BA6C3D01714981AE40690180B364AC--></AD_COLUMN>
 
+<!--D243A6CF3B804A92A59633006740DE1D--><AD_COLUMN>
+<!--D243A6CF3B804A92A59633006740DE1D-->  
<AD_COLUMN_ID><![CDATA[D243A6CF3B804A92A59633006740DE1D]]></AD_COLUMN_ID>
+<!--D243A6CF3B804A92A59633006740DE1D-->  
<AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
+<!--D243A6CF3B804A92A59633006740DE1D-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
+<!--D243A6CF3B804A92A59633006740DE1D-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
+<!--D243A6CF3B804A92A59633006740DE1D-->  <NAME><![CDATA[Path]]></NAME>
+<!--D243A6CF3B804A92A59633006740DE1D-->  <DESCRIPTION><![CDATA[Location of the 
file relative to the attachments folder]]></DESCRIPTION>
+<!--D243A6CF3B804A92A59633006740DE1D-->  <HELP><![CDATA[Location of the file 
relative to the attachments folder]]></HELP>
+<!--D243A6CF3B804A92A59633006740DE1D-->  
<COLUMNNAME><![CDATA[Path]]></COLUMNNAME>
+<!--D243A6CF3B804A92A59633006740DE1D-->  
<AD_TABLE_ID><![CDATA[800028]]></AD_TABLE_ID>
+<!--D243A6CF3B804A92A59633006740DE1D-->  
<AD_REFERENCE_ID><![CDATA[10]]></AD_REFERENCE_ID>
+<!--D243A6CF3B804A92A59633006740DE1D-->  
<FIELDLENGTH><![CDATA[2000]]></FIELDLENGTH>
+<!--D243A6CF3B804A92A59633006740DE1D-->  <ISKEY><![CDATA[N]]></ISKEY>
+<!--D243A6CF3B804A92A59633006740DE1D-->  <ISPARENT><![CDATA[N]]></ISPARENT>
+<!--D243A6CF3B804A92A59633006740DE1D-->  
<ISMANDATORY><![CDATA[N]]></ISMANDATORY>
+<!--D243A6CF3B804A92A59633006740DE1D-->  
<ISUPDATEABLE><![CDATA[Y]]></ISUPDATEABLE>
+<!--D243A6CF3B804A92A59633006740DE1D-->  
<ISIDENTIFIER><![CDATA[N]]></ISIDENTIFIER>
+<!--D243A6CF3B804A92A59633006740DE1D-->  <SEQNO><![CDATA[170]]></SEQNO>
+<!--D243A6CF3B804A92A59633006740DE1D-->  
<ISTRANSLATED><![CDATA[N]]></ISTRANSLATED>
+<!--D243A6CF3B804A92A59633006740DE1D-->  
<ISENCRYPTED><![CDATA[N]]></ISENCRYPTED>
+<!--D243A6CF3B804A92A59633006740DE1D-->  
<ISSELECTIONCOLUMN><![CDATA[N]]></ISSELECTIONCOLUMN>
+<!--D243A6CF3B804A92A59633006740DE1D-->  
<AD_ELEMENT_ID><![CDATA[026C0C35D730459B865E7242955C87F2]]></AD_ELEMENT_ID>
+<!--D243A6CF3B804A92A59633006740DE1D-->  
<ISSESSIONATTR><![CDATA[N]]></ISSESSIONATTR>
+<!--D243A6CF3B804A92A59633006740DE1D-->  
<ISSECONDARYKEY><![CDATA[N]]></ISSECONDARYKEY>
+<!--D243A6CF3B804A92A59633006740DE1D-->  
<ISDESENCRYPTABLE><![CDATA[N]]></ISDESENCRYPTABLE>
+<!--D243A6CF3B804A92A59633006740DE1D-->  
<DEVELOPMENTSTATUS><![CDATA[RE]]></DEVELOPMENTSTATUS>
+<!--D243A6CF3B804A92A59633006740DE1D-->  
<AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
+<!--D243A6CF3B804A92A59633006740DE1D-->  <POSITION><![CDATA[15]]></POSITION>
+<!--D243A6CF3B804A92A59633006740DE1D-->  
<ISTRANSIENT><![CDATA[N]]></ISTRANSIENT>
+<!--D243A6CF3B804A92A59633006740DE1D-->  <ISAUTOSAVE><![CDATA[Y]]></ISAUTOSAVE>
+<!--D243A6CF3B804A92A59633006740DE1D-->  
<VALIDATEONNEW><![CDATA[Y]]></VALIDATEONNEW>
+<!--D243A6CF3B804A92A59633006740DE1D-->  
<IMAGESIZEVALUESACTION><![CDATA[N]]></IMAGESIZEVALUESACTION>
+<!--D243A6CF3B804A92A59633006740DE1D-->  
<ISUSEDSEQUENCE><![CDATA[N]]></ISUSEDSEQUENCE>
+<!--D243A6CF3B804A92A59633006740DE1D--></AD_COLUMN>
+
 <!--D270E0FBA1074C439DC12CF8939EB830--><AD_COLUMN>
 <!--D270E0FBA1074C439DC12CF8939EB830-->  
<AD_COLUMN_ID><![CDATA[D270E0FBA1074C439DC12CF8939EB830]]></AD_COLUMN_ID>
 <!--D270E0FBA1074C439DC12CF8939EB830-->  
<AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
diff -r b0cfc1e49543 -r bed326b6ddf6 src-db/database/sourcedata/AD_ELEMENT.xml
--- a/src-db/database/sourcedata/AD_ELEMENT.xml Fri Jul 12 15:03:06 2013 +0200
+++ b/src-db/database/sourcedata/AD_ELEMENT.xml Thu Jul 11 11:00:28 2013 +0530
@@ -20629,6 +20629,20 @@
 <!--01A78AEEF3C411DDB34B001D09C4A2FE-->  <ISGLOSSARY><![CDATA[N]]></ISGLOSSARY>
 <!--01A78AEEF3C411DDB34B001D09C4A2FE--></AD_ELEMENT>
 
+<!--026C0C35D730459B865E7242955C87F2--><AD_ELEMENT>
+<!--026C0C35D730459B865E7242955C87F2-->  
<AD_ELEMENT_ID><![CDATA[026C0C35D730459B865E7242955C87F2]]></AD_ELEMENT_ID>
+<!--026C0C35D730459B865E7242955C87F2-->  
<AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
+<!--026C0C35D730459B865E7242955C87F2-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
+<!--026C0C35D730459B865E7242955C87F2-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
+<!--026C0C35D730459B865E7242955C87F2-->  
<COLUMNNAME><![CDATA[Path]]></COLUMNNAME>
+<!--026C0C35D730459B865E7242955C87F2-->  <NAME><![CDATA[Path]]></NAME>
+<!--026C0C35D730459B865E7242955C87F2-->  
<PRINTNAME><![CDATA[Path]]></PRINTNAME>
+<!--026C0C35D730459B865E7242955C87F2-->  <DESCRIPTION><![CDATA[Location of the 
file relative to the attachments folder]]></DESCRIPTION>
+<!--026C0C35D730459B865E7242955C87F2-->  <HELP><![CDATA[Location of the file 
relative to the attachments folder]]></HELP>
+<!--026C0C35D730459B865E7242955C87F2-->  
<AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
+<!--026C0C35D730459B865E7242955C87F2-->  <ISGLOSSARY><![CDATA[N]]></ISGLOSSARY>
+<!--026C0C35D730459B865E7242955C87F2--></AD_ELEMENT>
+
 <!--035BE38188FC44538E3DDDA769800CFB--><AD_ELEMENT>
 <!--035BE38188FC44538E3DDDA769800CFB-->  
<AD_ELEMENT_ID><![CDATA[035BE38188FC44538E3DDDA769800CFB]]></AD_ELEMENT_ID>
 <!--035BE38188FC44538E3DDDA769800CFB-->  
<AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
diff -r b0cfc1e49543 -r bed326b6ddf6 src-db/database/sourcedata/AD_REF_LIST.xml
--- a/src-db/database/sourcedata/AD_REF_LIST.xml        Fri Jul 12 15:03:06 
2013 +0200
+++ b/src-db/database/sourcedata/AD_REF_LIST.xml        Thu Jul 11 11:00:28 
2013 +0530
@@ -8807,6 +8807,18 @@
 <!--3D4C02EB56AA4380AF128782BDA72C3D-->  <SEQNO><![CDATA[10]]></SEQNO>
 <!--3D4C02EB56AA4380AF128782BDA72C3D--></AD_REF_LIST>
 
+<!--3E3B66241E984CD19E9CF195DB72638B--><AD_REF_LIST>
+<!--3E3B66241E984CD19E9CF195DB72638B-->  
<AD_REF_LIST_ID><![CDATA[3E3B66241E984CD19E9CF195DB72638B]]></AD_REF_LIST_ID>
+<!--3E3B66241E984CD19E9CF195DB72638B-->  
<AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
+<!--3E3B66241E984CD19E9CF195DB72638B-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
+<!--3E3B66241E984CD19E9CF195DB72638B-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
+<!--3E3B66241E984CD19E9CF195DB72638B-->  
<VALUE><![CDATA[SaveAttachmentsOldWay]]></VALUE>
+<!--3E3B66241E984CD19E9CF195DB72638B-->  <NAME><![CDATA[Save Attachments in 
old way]]></NAME>
+<!--3E3B66241E984CD19E9CF195DB72638B-->  <DESCRIPTION><![CDATA[Saves the 
attachments using the earlier folder structure]]></DESCRIPTION>
+<!--3E3B66241E984CD19E9CF195DB72638B-->  
<AD_REFERENCE_ID><![CDATA[A26BA480E2014707B47257024C3CBFF7]]></AD_REFERENCE_ID>
+<!--3E3B66241E984CD19E9CF195DB72638B-->  
<AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
+<!--3E3B66241E984CD19E9CF195DB72638B--></AD_REF_LIST>
+
 <!--3E6F1F271AE44931BE140ECECE5C86A2--><AD_REF_LIST>
 <!--3E6F1F271AE44931BE140ECECE5C86A2-->  
<AD_REF_LIST_ID><![CDATA[3E6F1F271AE44931BE140ECECE5C86A2]]></AD_REF_LIST_ID>
 <!--3E6F1F271AE44931BE140ECECE5C86A2-->  
<AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
diff -r b0cfc1e49543 -r bed326b6ddf6 
src/org/openbravo/erpCommon/businessUtility/TabAttachments.java
--- a/src/org/openbravo/erpCommon/businessUtility/TabAttachments.java   Fri Jul 
12 15:03:06 2013 +0200
+++ b/src/org/openbravo/erpCommon/businessUtility/TabAttachments.java   Thu Jul 
11 11:00:28 2013 +0530
@@ -11,7 +11,7 @@
  * under the License.
  * The Original Code is Openbravo ERP.
  * The Initial Developer of the Original Code is Openbravo SLU
- * All portions are Copyright (C) 2001-2011 Openbravo SLU
+ * All portions are Copyright (C) 2001-2013 Openbravo SLU
  * All Rights Reserved.
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -34,6 +34,7 @@
 import javax.servlet.http.HttpServletResponse;
 
 import org.apache.commons.fileupload.FileItem;
+import org.apache.log4j.Logger;
 import org.codehaus.jettison.json.JSONObject;
 import org.hibernate.criterion.Restrictions;
 import org.openbravo.base.secureApp.HttpSecureAppServlet;
@@ -45,8 +46,10 @@
 import org.openbravo.dal.service.OBDal;
 import org.openbravo.dal.service.OBDao;
 import org.openbravo.erpCommon.utility.OBError;
+import org.openbravo.erpCommon.utility.PropertyException;
 import org.openbravo.erpCommon.utility.SequenceIdData;
 import org.openbravo.erpCommon.utility.Utility;
+import org.openbravo.model.ad.datamodel.Table;
 import org.openbravo.model.ad.ui.Tab;
 import org.openbravo.model.ad.utility.Attachment;
 import org.openbravo.utils.FileUtility;
@@ -54,6 +57,7 @@
 
 public class TabAttachments extends HttpSecureAppServlet {
   private static final long serialVersionUID = 1L;
+  private static Logger log = Logger.getLogger(TabAttachments.class);
 
   @Override
   public void init(ServletConfig config) {
@@ -282,7 +286,7 @@
   private OBError insert(VariablesSecureApp vars, String strFileReference, 
String tableId,
       String key, String strDataType, String strText) throws IOException, 
ServletException {
 
-    String cFileId = strFileReference;
+    String cFileId = strFileReference, fileDir = null, path = null;
     OBError myMessage = null;
     myMessage = new OBError();
     myMessage.setTitle("");
@@ -290,6 +294,7 @@
     if (log4j.isDebugEnabled())
       log4j.debug("Deleting records");
     Connection conn = null;
+    fileDir = getAttachmentDirectoryForNewAttachments(tableId, key);
     try {
       conn = this.getTransactionConnection();
       final String inpName = "inpname";
@@ -316,17 +321,18 @@
         }
       }
       if (!fileExists) {
+        path = getPath(fileDir);
         // We only insert a new record if there is no record for this file
         TabAttachmentsData.insert(conn, this, cFileId, vars.getClient(), 
vars.getOrg(),
-            vars.getUser(), tableId, key, strDataType, strText, strName);
+            vars.getUser(), tableId, key, strDataType, strText, strName, path);
       } else {
         // We update the existing record
         TabAttachmentsData.update(this, vars.getUser(), strDataType, strText, 
cFileId);
       }
       try {
         // FIXME: Get the directory separator from Java runtime
-        final File uploadedDir = new File(globalParameters.strFTPDirectory + 
"/" + tableId + "-"
-            + key);
+
+        final File uploadedDir = new File(globalParameters.strFTPDirectory + 
"/" + fileDir);
         if (!uploadedDir.exists())
           uploadedDir.mkdirs();
         final File uploadedFile = new File(uploadedDir, strName);
@@ -543,16 +549,17 @@
 
   private void printPageFile(HttpServletResponse response, VariablesSecureApp 
vars,
       String strFileReference) throws IOException, ServletException {
+    String fileDir = null;
     final TabAttachmentsData[] data = TabAttachmentsData.selectEdit(this, 
strFileReference);
     if (data == null || data.length == 0)
       throw new ServletException("Missing file");
     FileUtility f = new FileUtility();
+    fileDir = getAttachmentDirectory(data[0].adTableId, data[0].adRecordId, 
data[0].name);
     // FIXME: Get the directory separator from Java runtime
-    final File file = new File(globalParameters.strFTPDirectory + "/" + 
data[0].adTableId + "-"
-        + data[0].adRecordId, data[0].name);
+    final File file = new File(globalParameters.strFTPDirectory + "/" + 
fileDir, data[0].name);
     if (file.exists())
-      f = new FileUtility(globalParameters.strFTPDirectory + "/" + 
data[0].adTableId + "-"
-          + data[0].adRecordId, data[0].name, false, true);
+      f = new FileUtility(globalParameters.strFTPDirectory + "/" + fileDir, 
data[0].name, false,
+          true);
     else
       f = new FileUtility(globalParameters.strFTPDirectory, strFileReference, 
false, true);
     if (data[0].datatypeContent.equals(""))
@@ -575,6 +582,122 @@
     out.close();
   }
 
+  /**
+   * Provides the directory in which the attachment has to be stored. For 
example for tableId "259",
+   * recordId "0F3A10E019754BACA5844387FB37B0D5", the file directory returned 
is
+   * "259/0F3/A10/E01/975/4BA/CA5/844/387/FB3/7B0/D5". In case 
'SaveAttachmentsOldWay' preference is
+   * enabled then the file directory returned is 
"259-0F3A10E019754BACA5844387FB37B0D5"
+   * 
+   * @param tableId
+   *          UUID of the table
+   * 
+   * @param recordId
+   *          UUID of the record
+   * 
+   * @return file directory to save the attachment
+   */
+  public static String getAttachmentDirectoryForNewAttachments(String tableID, 
String recordID) {
+    String fileDir = tableID + "-" + recordID;
+    String saveAttachmentsOldWay = null;
+    try {
+      saveAttachmentsOldWay = 
Preferences.getPreferenceValue("SaveAttachmentsOldWay", true,
+          OBContext.getOBContext().getCurrentClient(), OBContext.getOBContext()
+              .getCurrentOrganization(), OBContext.getOBContext().getUser(), 
OBContext
+              .getOBContext().getRole(), null);
+    } catch (PropertyException e) {
+      // if property not found, save attachments the new way
+      saveAttachmentsOldWay = "N";
+    }
+
+    if ("Y".equals(saveAttachmentsOldWay)) {
+      return fileDir;
+    } else {
+      fileDir = tableID + "/" + splitPath(recordID);
+    }

------------------------------------------------------------------------------
See everything from the browser to the database with AppDynamics
Get end-to-end visibility with application monitoring from AppDynamics
Isolate bottlenecks and diagnose root cause in seconds.
Start your free trial of AppDynamics Pro today!
http://pubads.g.doubleclick.net/gampad/clk?id=48808831&iu=/4140/ostg.clktrk
_______________________________________________
Openbravo-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openbravo-commits

Reply via email to