IDEMPIERE-4673 PackOut fails to export attachments with File storage provider (#554)

* IDEMPIERE-4673 PackOut fails to export attachments with File storage provider

* Implement similar solution for Image and Archive

* Simplify the nested ifs

Change approach for MAttachment as is not working for Data Single
This commit is contained in:
Carlos Ruiz 2021-01-28 13:10:02 +01:00 committed by GitHub
parent e32f971587
commit 4493373b96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 108 additions and 1 deletions

View File

@ -18,13 +18,21 @@ package org.compiere.model;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Properties; import java.util.Properties;
import java.util.logging.Level; import java.util.logging.Level;
import org.adempiere.exceptions.AdempiereException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Target;
import org.apache.tools.ant.taskdefs.Zip;
import org.compiere.tools.FileUtil;
import org.compiere.util.CLogger; import org.compiere.util.CLogger;
import org.compiere.util.DB; import org.compiere.util.DB;
import org.compiere.util.Env; import org.compiere.util.Env;
@ -39,7 +47,7 @@ public class MArchive extends X_AD_Archive {
/** /**
* *
*/ */
private static final long serialVersionUID = -2384941426301490384L; private static final long serialVersionUID = 6934821005476123632L;
/** /**
* Get Archives * Get Archives
@ -296,4 +304,62 @@ public class MArchive extends X_AD_Archive {
provider = p; provider = p;
} }
/**
* Save the Archive as zip file, used by Pack Out when storage provider is not DB
* @return File - the temporary file
*/
public File saveAsZip() {
String name = MTable.get(Env.getCtx(), getAD_Table_ID()).getTableName() + "_" + getRecord_ID();
File tempfolder = null;
try {
Path tempPath = Files.createTempDirectory(name);
tempfolder = tempPath.toFile();
} catch (IOException e1) {
throw new AdempiereException("Unable to create temp folder", e1);
}
File destZipFile = null;
try {
destZipFile = File.createTempFile("IdempiereArchive", ".zip");
} catch (Throwable e) {
throw new AdempiereException("Unable to create temp file", e);
}
destZipFile.delete();
File destArchiveFile = null;
try {
destArchiveFile = File.createTempFile("IdempiereArchive", ".pdf", tempfolder);
} catch (Throwable e) {
throw new AdempiereException("Unable to create temp file", e);
}
destArchiveFile.delete();
Path path = destArchiveFile.toPath();
try {
Files.write(path, getBinaryData());
} catch (IOException e1) {
throw new AdempiereException(e1);
}
Zip zipper = new Zip();
zipper.setDestFile(destZipFile);
zipper.setBasedir(tempfolder);
zipper.setUpdate(true);
zipper.setCompress(true);
zipper.setCaseSensitive(false);
zipper.setFilesonly(true);
zipper.setTaskName("zip");
zipper.setTaskType("zip");
zipper.setProject(new Project());
zipper.setOwningTarget(new Target());
zipper.execute();
try {
FileUtil.deleteDirectory(tempfolder);
} catch (IOException e) {}
return destZipFile;
}
} // MArchive } // MArchive

View File

@ -1,12 +1,20 @@
package org.adempiere.pipo2; package org.adempiere.pipo2;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.nio.file.Files;
import java.util.List; import java.util.List;
import javax.xml.transform.sax.TransformerHandler; import javax.xml.transform.sax.TransformerHandler;
import org.adempiere.exceptions.AdempiereException; import org.adempiere.exceptions.AdempiereException;
import org.compiere.model.I_AD_Org; import org.compiere.model.I_AD_Org;
import org.compiere.model.MArchive;
import org.compiere.model.MAttachment;
import org.compiere.model.MClientInfo;
import org.compiere.model.MImage;
import org.compiere.model.MStorageProvider;
import org.compiere.model.MTable; import org.compiere.model.MTable;
import org.compiere.model.MTree; import org.compiere.model.MTree;
import org.compiere.model.PO; import org.compiere.model.PO;
@ -294,6 +302,39 @@ public class PoExporter {
return; return;
} }
if ("BinaryData".equals(columnName)) {
MClientInfo ci = MClientInfo.get(po.getAD_Client_ID());
if (po.get_Table_ID() == MAttachment.Table_ID && ci.getAD_StorageProvider_ID() > 0) {
MStorageProvider sp = new MStorageProvider(po.getCtx(), ci.getAD_StorageProvider_ID(), po.get_TrxName());
if (! MStorageProvider.METHOD_Database.equals(sp.getMethod())) {
MAttachment att = new MAttachment(po.getCtx(), po.get_ID(), po.get_TrxName());
File tmpfile = att.saveAsZip();
try {
value = Files.readAllBytes(tmpfile.toPath());
} catch (IOException e) {
throw new AdempiereException(e);
}
}
} else if (po.get_Table_ID() == MImage.Table_ID && ci.getStorageImage_ID() > 0) {
MStorageProvider sp = new MStorageProvider(po.getCtx(), ci.getStorageImage_ID(), po.get_TrxName());
if (! MStorageProvider.METHOD_Database.equals(sp.getMethod())) {
MImage image = new MImage(po.getCtx(), po.get_ID(), po.get_TrxName());
value = image.getBinaryData();
}
} else if (po.get_Table_ID() == MArchive.Table_ID && ci.getStorageArchive_ID() > 0) {
MStorageProvider sp = new MStorageProvider(po.getCtx(), ci.getStorageArchive_ID(), po.get_TrxName());
if (! MStorageProvider.METHOD_Database.equals(sp.getMethod())) {
MArchive archive = new MArchive(po.getCtx(), po.get_ID(), po.get_TrxName());
File tmpfile = archive.saveAsZip();
try {
value = Files.readAllBytes(tmpfile.toPath());
} catch (IOException e) {
throw new AdempiereException(e);
}
}
}
}
PackOut packOut = ctx.packOut; PackOut packOut = ctx.packOut;
byte[] data = null; byte[] data = null;
String dataType = null; String dataType = null;