fixes issue 35313: documents were not being attached after send them by e-mail
authorCarlos Aristu <carlos.aristu@openbravo.com>
Tue, 21 Feb 2017 13:39:12 +0100
changeset 31536 24eba52754e3
parent 31535 5f9aab62bd8a
child 31537 d4ce87bb216b
fixes issue 35313: 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	Tue Feb 14 15:53:39 2017 +0530
+++ b/src/org/openbravo/erpCommon/utility/reporting/ReportManager.java	Tue Feb 21 13:39:12 2017 +0100
@@ -26,11 +26,11 @@
 import java.util.Locale;
 import java.util.Map;
 
+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;
@@ -173,21 +173,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);
 
@@ -196,7 +197,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	Tue Feb 14 15:53:39 2017 +0530
+++ b/src/org/openbravo/erpCommon/utility/reporting/printing/PrintController.java	Tue Feb 21 13:39:12 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())