fixes issue 35318: documents were not being attached after send them by e-mail
authorCarlos Aristu <carlos.aristu@openbravo.com>
Tue, 21 Feb 2017 14:16:33 +0100
changeset 31444 d074c8f621ff
parent 31443 4c82e13385e5
child 31445 d2f5d812ddda
fixes issue 35318: documents were not being attached after send them by e-mail

The CoreAttachImplementation class is deleting the original report file after completing the upload process. When attaching a document using the e-mail pop-up this is done before attaching the document into the e-mail. This explains why the document was not being received on the e-mail.

To avoid this problem, now a copy of the original document is sent to the AttachImplementationManager, this way the original file will never be deleted by an upload process handler.

Together with this, now when attaching a document after printing (not by e-mail) we have to delete the original file because in other case it will be kept on disk.
src/org/openbravo/erpCommon/utility/reporting/ReportManager.java
src/org/openbravo/erpCommon/utility/reporting/printing/PrintController.java
--- a/src/org/openbravo/erpCommon/utility/reporting/ReportManager.java	Thu Feb 16 11:42:21 2017 -0500
+++ b/src/org/openbravo/erpCommon/utility/reporting/ReportManager.java	Tue Feb 21 14:16:33 2017 +0100
@@ -29,11 +29,11 @@
 import net.sf.jasperreports.engine.JRException;
 import net.sf.jasperreports.engine.JasperPrint;
 
+import org.apache.commons.io.FileUtils;
 import org.apache.log4j.Logger;
 import org.openbravo.base.secureApp.VariablesSecureApp;
 import org.openbravo.base.weld.WeldUtils;
 import org.openbravo.client.application.attachment.AttachImplementationManager;
-import org.openbravo.client.application.attachment.CoreAttachImplementation;
 import org.openbravo.client.application.report.ReportingUtils;
 import org.openbravo.client.application.report.ReportingUtils.ExportType;
 import org.openbravo.database.ConnectionProvider;
@@ -166,21 +166,22 @@
       throw new ReportingException(Utility.messageBD(connectionProvider, "AttachmentExists",
           vars.getLanguage()));
 
-    final String destination = CoreAttachImplementation.getAttachmentDirectoryForNewAttachments(
-        tableId, report.getDocumentId());
-
-    // First move the file to the correct destination
-    final File destinationFolder = new File(_strAttachmentPath + "/" + destination);
-    if (!destinationFolder.exists()) {
-      destinationFolder.mkdirs();
-    }
-    report.setTargetDirectory(destinationFolder);
-
     final JasperPrint jasperPrint = processReport(report, vars);
     saveReport(report, jasperPrint);
 
     final File sourceFile = new File(report.getTargetLocation());
 
+    // We create a copy of the original file which will be the one used by the upload process. This
+    // is because the default attach implementation handler is deleting the file after completing
+    // the upload process and this way we prevent the deletion of the original file.
+    final File temporaryAttachFolder = new File(ReportingUtils.getTempFolder());
+    if (!temporaryAttachFolder.exists()) {
+      temporaryAttachFolder.mkdirs();
+    }
+    FileUtils.copyFileToDirectory(sourceFile, temporaryAttachFolder, true);
+    final File attachFile = new File(temporaryAttachFolder.getAbsolutePath() + File.separator
+        + sourceFile.getName());
+
     AttachImplementationManager aim = WeldUtils
         .getInstanceFromStaticBeanManager(AttachImplementationManager.class);
 
@@ -189,7 +190,7 @@
     requestParams.put("E22E8E3B737D4A47A691A073951BBF16", textForAttachment);
 
     aim.upload(requestParams, vars.getSessionValue("inpTabId"), report.getDocumentId(),
-        vars.getOrg(), sourceFile);
+        vars.getOrg(), attachFile);
 
     report.setAttached(true);
 
--- a/src/org/openbravo/erpCommon/utility/reporting/printing/PrintController.java	Thu Feb 16 11:42:21 2017 -0500
+++ b/src/org/openbravo/erpCommon/utility/reporting/printing/PrintController.java	Tue Feb 21 14:16:33 2017 +0100
@@ -621,10 +621,17 @@
             + tableId);
       // Save the report as a attachment because it is being
       // transferred to the user
+      File attachedFile = null;
       try {
-        reportManager.createAttachmentForReport(this, report, tableId, vars);
+        attachedFile = reportManager.createAttachmentForReport(this, report, tableId, vars);
       } catch (final ReportingException exception) {
         throw new ServletException(exception);
+      } finally {
+        // Delete the original file generated for the attachment because the upload process has
+        // already copied it on the proper location
+        if (attachedFile != null && attachedFile.exists()) {
+          attachedFile.delete();
+        }
       }
     } else {
       if (log4j.isDebugEnabled())