📄 impoperator.java
字号:
package cn.myapps.core.expimp.imp.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.sql.DataSource;
import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.ddlutils.DatabaseOperationException;
import org.apache.ddlutils.Platform;
import org.apache.ddlutils.PlatformFactory;
import org.apache.ddlutils.io.DataReader;
import org.apache.ddlutils.io.DataSink;
import org.apache.ddlutils.io.DataSinkException;
import org.apache.ddlutils.model.Database;
import org.apache.ddlutils.model.Table;
import org.apache.ddlutils.platform.oracle.Oracle8Platform;
import org.apache.log4j.Logger;
import cn.myapps.constans.Environment;
import cn.myapps.core.expimp.imp.ejb.ImpActivity;
import cn.myapps.core.expimp.imp.ejb.ImpBillDefi;
import cn.myapps.core.expimp.imp.ejb.ImpColumn;
import cn.myapps.core.expimp.imp.ejb.ImpColumnMapping;
import cn.myapps.core.expimp.imp.ejb.ImpComponent;
import cn.myapps.core.expimp.imp.ejb.ImpDocument;
import cn.myapps.core.expimp.imp.ejb.ImpElement;
import cn.myapps.core.expimp.imp.ejb.ImpExcelMappingConfig;
import cn.myapps.core.expimp.imp.ejb.ImpForm;
import cn.myapps.core.expimp.imp.ejb.ImpImageRepository;
import cn.myapps.core.expimp.imp.ejb.ImpItem;
import cn.myapps.core.expimp.imp.ejb.ImpMappingConfig;
import cn.myapps.core.expimp.imp.ejb.ImpPage;
import cn.myapps.core.expimp.imp.ejb.ImpQuery;
import cn.myapps.core.expimp.imp.ejb.ImpReportConfig;
import cn.myapps.core.expimp.imp.ejb.ImpRepository;
import cn.myapps.core.expimp.imp.ejb.ImpResource;
import cn.myapps.core.expimp.imp.ejb.ImpStyleRepository;
import cn.myapps.core.expimp.imp.ejb.ImpTableColumn;
import cn.myapps.core.expimp.imp.ejb.ImpView;
public class ImpOperator {
private Platform platform;
private String schema;
private Database model = null;
private static Logger log = Logger.getLogger(ImpOperator.class);
public ImpOperator(DataSource ds) {
BasicDataSource dbcpDS = ((BasicDataSource) ds);
platform = PlatformFactory.createNewPlatformInstance(ds);
if (platform instanceof Oracle8Platform) {
schema = dbcpDS.getUsername().toUpperCase();
}
}
public Collection parse(File file) throws Exception {
Database model = this.getModel();
if (file != null) {
FileInputStream fs = new FileInputStream(file);
final ArrayList readObjects = new ArrayList();
DataReader reader = new DataReader();
reader.setModel(model);
reader.setCaseSensitive(false);
reader.setSink(new DataSink() {
public void start() throws DataSinkException {
}
public void addBean(DynaBean bean) throws DataSinkException {
readObjects.add(bean);
}
public void end() throws DataSinkException {
}
});
reader.parse(fs);
fs.close();
return readObjects;
}
return null;
}
/**
* 根据名称获取表的所有记录
*
* @param table
* @param readObjects
* @return
*/
private Map getDynaBeansOfTable(String tableName, Collection readObjects) {
Table table = this.getModel().findTable(tableName);
return getDynaBeansOfTable(table, readObjects);
}
private Map getDynaBeansOfTable(Table table, Collection readObjects) {
Map rtn = new LinkedHashMap();
String tableName = table.getName();
Collection beans = getDynaBeans(tableName, readObjects);
rtn.put(table, beans);
return rtn;
}
private Collection getDynaBeans(String tableName, Collection readObjects) {
Collection rtn = new ArrayList();
for (Iterator iter = readObjects.iterator(); iter.hasNext();) {
DynaBean bean = (DynaBean) iter.next();
String matchTableName = bean.getDynaClass().getName();
if (tableName.equalsIgnoreCase(matchTableName)) {
rtn.add(bean);
}
}
return rtn;
}
/**
* insert data to database
*
* @param impobj
* 要insert的数据
* @throws Exception
*/
public Collection writeDataToDB(Collection datas) {
Collection messages = new ArrayList();
if (datas != null && datas.size() > 0) {
Database model = this.getModel();
Connection conn = platform.borrowConnection();
if (conn != null) {
if (model != null) {
log.info("Data size:" + datas.size());
log
.info("***************** IMPORT START *******************");
for (Iterator iter = datas.iterator(); iter.hasNext();) {
DynaBean bean = (DynaBean) iter.next();
String tName = bean.getDynaClass().getName();
String elName = tName.substring(2, tName.length());
try {
platform.insert(conn, model, bean);
log.info("INSERT INTO:"
+ bean.getDynaClass().getName() + " NAME:"
+ getElementName(bean, tName));
messages.add(elName + ":"
+ getElementName(bean, tName)
+ " Successed");
} catch (DatabaseOperationException doe0) {
// resource数据则覆盖
if (tName.equals(ImpResource.NAME)) {
platform.update(conn, model, bean);
} else {
String errorMessage = elName + ":"
+ getElementName(bean, tName)
+ " have existed";
log.error(errorMessage + " "
+ doe0.getMessage());
messages.add(elName + ":"
+ getElementName(bean, tName)
+ " Failed");
}
}
}
commit(conn);
log
.info("***************** IMPORT COMPLETE *******************");
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
return messages;
}
public String getElementName(DynaBean bean, String tName) {
String rtn = "";
if (tName.equals(ImpBillDefi.NAME)) {
rtn = (String) bean.get(ImpBillDefi.IMPFLOW_NAME);
} else if (tName.equals(ImpItem.NAME)) {
rtn = (String) bean.get(ImpItem.IMPOBJECT_NAME) + " Value "
+ (String) bean.get(ImpItem.COLUMN_VARCHARVALUE);
} else if (tName.equals(ImpDocument.NAME)) {
rtn = "Doc of " + bean.get(ImpDocument.COLUMN_FORMNAME);
} else {
rtn = (String) bean.get(ImpActivity.IMPOBJECT_NAME);
}
return rtn;
}
/**
* 根据url把要import的图片写入到应用服务器相应目录
*
* @param files
* @param urls
* @throws Exception
*/
public void writeFilesToDir(File[] files, Collection urls, Environment evt)
throws Exception {
if (urls != null && urls.size() > 0) {
for (Iterator iter = urls.iterator(); iter.hasNext();) {
String url = (String) iter.next();
if (url != null && url.trim().length() > 0) {
String fileName = url.substring(url.lastIndexOf("/") + 1,
url.length());
String filePath = evt.getWebcontextRealPath(url);
for (int i = 0; i < files.length; i++) {
if (fileName.equals(files[i].getName())) {
FileInputStream in = new FileInputStream(files[i]);
FileOutputStream out = new FileOutputStream(
filePath);
int nNumber;
byte[] buffer = new byte[512];
while ((nNumber = in.read(buffer)) != -1) {
out.write(buffer, 0, nNumber);
}
in.close();
out.close();
break;
}
}
}
}
}
}
private Database getModel() {
if (platform == null) {
return null;
}
if (model == null) {
if (schema != null && schema.trim().length() > 0) {
model = platform.readModelFromDatabase(" ", null, schema, null);
} else {
model = platform.readModelFromDatabase(" ");
}
}
return model;
}
private void commit(Connection conn) {
try {
conn.commit();
} catch (SQLException e) {
System.out.println("commit fail");
}
}
private void rollback(Connection conn) {
try {
conn.rollback();
} catch (SQLException e) {
System.out.println("rollback fail");
}
}
public static void main(String[] args) throws Exception {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -