IDEMPIERE-1416 Pack-in issue when an attachment has been changed

This commit is contained in:
Carlos Ruiz 2014-02-05 08:26:41 -05:00
parent 6a4151faab
commit fef5e02b27
2 changed files with 71 additions and 14 deletions

View File

@ -21,14 +21,18 @@ import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
@ -225,6 +229,27 @@ public class PackIn {
return data;
}
public File[] readFilesFromBlob(String fileName) throws IOException {
ZipFile zf = new ZipFile(m_packageDirectory+File.separator+"blobs"+File.separator+fileName);
Enumeration<?> e = zf.entries();
ArrayList<File> files = new ArrayList<File>();
while (e.hasMoreElements()) {
ZipEntry ze = (ZipEntry) e.nextElement();
File file = new File(ze.getName());
FileOutputStream fout = new FileOutputStream(file);
InputStream in = zf.getInputStream(ze);
for (int c = in.read(); c != -1; c = in.read()) {
fout.write(c);
}
in.close();
fout.close();
files.add(file);
}
File[] retValue = new File[files.size()];
files.toArray(retValue);
return retValue;
}
/**
* @return package name
*/

View File

@ -1,5 +1,6 @@
package org.adempiere.pipo2;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Timestamp;
@ -7,6 +8,8 @@ import java.util.ArrayList;
import java.util.List;
import org.adempiere.exceptions.AdempiereException;
import org.compiere.model.MAttachment;
import org.compiere.model.MAttachmentEntry;
import org.compiere.model.MColumn;
import org.compiere.model.MTable;
import org.compiere.model.PO;
@ -285,25 +288,54 @@ public class PoFiller{
private void setBlob(String qName) {
Element pe = element.properties.get(qName);
String value = pe != null ? pe.contents.toString() : null;
Object data = null;
if (value != null && value.trim().length() > 0) {
String[] component = value.split("[|]");
if (component.length == 2) {
String fileName = component[0];
String dataType = component[1];
if (po instanceof MAttachment && "BinaryData".equals(qName)) {
PackIn packIn = ctx.packIn;
try {
byte[] bytes = packIn.readBlob(fileName);
if ("byte[]".equals(dataType)) {
data = bytes;
} else {
data = new String(bytes, "UTF-8");
String fileName = null;
String[] component = value.split("[|]");
if (component.length == 2) {
fileName = component[0];
File[] files;
try {
files = packIn.readFilesFromBlob(fileName);
} catch (IOException e) {
throw new AdempiereException(e.getLocalizedMessage(), e);
}
MAttachment attach = ((MAttachment)po);
for (File file : files) {
boolean found = false;
for (MAttachmentEntry entry : attach.getEntries()) {
if (entry.getName().equals(file.getName())) {
found = true;
attach.updateEntry(entry.getIndex(), file);
}
}
if (! found)
attach.addEntry(file);
}
} catch (IOException e) {
throw new AdempiereException(e.getLocalizedMessage(), e);
}
} else {
Object data = null;
byte[] bytes = null;
String fileName = null;
String[] component = value.split("[|]");
if (component.length == 2) {
fileName = component[0];
String dataType = component[1];
PackIn packIn = ctx.packIn;
try {
bytes = packIn.readBlob(fileName);
if ("byte[]".equals(dataType)) {
data = bytes;
} else {
data = new String(bytes, "UTF-8");
}
} catch (IOException e) {
throw new AdempiereException(e.getLocalizedMessage(), e);
}
}
po.set_ValueNoCheck(qName, data);
}
}
po.set_ValueNoCheck(qName, data);
}
}