package com.justep.client;
import java.sql.Blob;
import java.sql.Clob;
import java.util.Iterator;
import javax.sql.rowset.serial.SerialBlob;
import javax.sql.rowset.serial.SerialClob;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import com.justep.common.SystemUtils;
import com.justep.system.data.Row;
import com.justep.system.data.Table;
import com.justep.system.transform.Row2Table;
import com.justep.system.transform.SimpleTransform;
import com.justep.system.transform.Table2Row;
import com.justep.system.transform.TransformConfig;
/**
* 序列化格式:
*
*
*
*
*
*
*
*
*
*/
public class TableWrapper{
private static final String TABLE_WRAPPER = "tableWrapper";
private static final String TABLE = "table";
private static final String EXT = "ext";
private static final String ITEM = "item";
private static final String NAME = "name";
private static final String INDEX = "index";
private static final String TYPE = "type";
private Table table = null;
public TableWrapper(){
}
public void setTable(Table table){
this.table = table;
}
public Table getTable(){
return this.table;
}
public void reader(Element xml, TransformConfig tfcfg) {
try{
Element tableE = xml.element(TABLE);
Element rows = (Element)tableE.elements().get(0);
this.table = new Row2Table().transform(rows);
Element ext = xml.element(EXT);
if (!ext.elements().isEmpty()){
int index = 0;
Iterator it = table.iterator();
while (it.hasNext()){
Row r = it.next();
for (String name : table.getColumnNames()){
if (SimpleTransform.BLOB.equals(table.getMetaData()
.getColumnMetaData(name).getType())){
Element item = (Element)ext.selectSingleNode("./*[@" + NAME +"='" + name + "' and @" + INDEX + "='" + index + "']");
if (item != null){
String type = item.attributeValue(TYPE);
String value = item.getTextTrim();
if (value==null)
value = "";
else{
value = new String(org.apache.commons.codec.binary.Base64.decodeBase64(value.getBytes()));
}
if ("clob".equals(type)){
r.setValue(name, new SerialClob(value.toCharArray()));
}else if ("blob".equals(type)){
r.setValue(name, new SerialBlob(value.getBytes()));
}else{
throw new RuntimeException("TableWrapper只支持clob或blob扩展");
}
}
}
}
index++;
}
}
}catch (Exception e){
throw new RuntimeException(e.getMessage()+"", e);
}
}
public Element writer(TransformConfig tfcfg) {
if (table == null){
throw new RuntimeException("table不允许为空");
}
Element tw = DocumentHelper.createElement(TABLE_WRAPPER);
Element tableE = tw.addElement(TABLE);
Element te = new Table2Row().transform(table, null);
tableE.add(te);
Element ext = tw.addElement(EXT);
try{
int index = 0;
Iterator it = table.iterator();
while (it.hasNext()){
Row r = it.next();
for (String name : table.getColumnNames()){
Object value = r.getValue(name);
if (SystemUtils.isNotNull(value) && SimpleTransform.BLOB.equals(table.getMetaData()
.getColumnMetaData(name).getType())){
Element item = ext.addElement(ITEM);
item.addAttribute(INDEX, index+"");
item.addAttribute(NAME, name);
if (value instanceof Clob){
item.addAttribute(TYPE, "clob");
Clob clob = (Clob)value;
String v = clob.getSubString(1, (int)clob.length());
v = new String(org.apache.commons.codec.binary.Base64.encodeBase64(v.getBytes()));
item.setText(v);
}else if (value instanceof Blob){
item.addAttribute(TYPE, "blob");
Blob blob = (Blob)value;
String v = new String(blob.getBytes(1, (int) blob.length()));
v = new String(org.apache.commons.codec.binary.Base64.encodeBase64(v.getBytes()));
item.setText(v);
}else{
throw new RuntimeException("TableWrapper只支持clob或blob扩展");
}
}
}
index++;
}
}catch (Exception e){
throw new RuntimeException(e.getMessage()+"", e);
}
return tw;
}
}