📄 tableconfig.java
字号:
package com.testDataBuilder.config;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import com.testDataBuilder.core.role.ComplexObj;
import com.testDataBuilder.core.role.Role;
import com.testDataBuilder.core.role.RoleFactory;
import com.testDataBuilder.dbMetaInfo.Database;
import com.testDataBuilder.dbMetaInfo.ForeignKey;
import com.testDataBuilder.dbMetaInfo.Table;
import com.testDataBuilder.exception.BaseException;
import com.testDataBuilder.exception.TagNotFoundException;
import com.testDataBuilder.util.Global;
import com.testDataBuilder.util.XmlFileUtil;
public class TableConfig {
static Logger logger = Logger.getLogger(TableConfig.class);
public static final String TAG_ROLES = "roles";
public static final String TAG_TABLE = "table";
public static final String TAG_NAME = "name";
public static final String TAG_CLOSE_ID_AUTO_INSERT = "closeIdAutoInsert";
public static final String TAG_COUNT = "count";
//public static final String TAG_INSERT_TYPE = "insertType";
public static final String TAG_ON_ERROR = "onError";
public static final String TAG_INIT = "init";
public static final String TAG_DESTROY = "destroy";
public static final String TAG_COLUMN = "column";
public static final String TAG_REF = "ref";
public static final String TAG_VALUE = "value";
public static final String TAG_CODE = "code";
/**
* <table name="tableName" closeIdAutoInsert="false" count="500"
insertType="idInsert" onError="" >
<init description="数据表的初始化">sel语句</init>
<destroy description="当数据传输完成后需要对目的表的清理工作.">sql语句</destroy>
<column name="columnName" ref="birthday">
<code>
</code>
</column>
<column name="cID" ref="class.id" isPK="false" value="3"/>
<column name="cName" ref="class.name"/>
<column name="sex" ref="sex"/>
</table>
*/
public static final String CONIFG_DEF_VALUE = "<?xml version='1.0' encoding='utf-8'?>" + Global.LINE_SEP
+ "<table name='tableName' closeIdAutoInsert='false' count='100' onError='ignore'>" + Global.LINE_SEP
+ "<dataTypes>"+ Global.LINE_SEP
+ "</dataTypes>"+ Global.LINE_SEP
+ "</table>";
public String getConfigDefValue(){
return CONIFG_DEF_VALUE.replaceAll("tableName", this.getTableName());
}
public TableConfig configure(File file) throws BaseException{
if (!file.exists()) {
throw new BaseException("file[" + file.getName()
+ "] is not exists!");
}
XmlFileUtil xmlFile = new XmlFileUtil(file);
try {
Element ele = xmlFile.getDoc(false, null).getRootElement();
return configure(ele);
} catch (Exception e) {
throw new BaseException(e);
}
}
/**
* <p><code>configure</code></p>
* @param tableEle
* @return
* @throws TagNotFoundException
* @author LiuXiaojie 2007-6-17
*/
public TableConfig configure(Element tableEle) throws BaseException{
String strName = tableEle.attributeValue(TAG_NAME);
if(StringUtils.isEmpty(strName)){
throw new TagNotFoundException(TAG_NAME);
}
this.setTableName(strName);
String onError = tableEle.attributeValue(TAG_ON_ERROR);
if(StringUtils.isNotEmpty(onError)){
this.setOnError(onError);
}
String strCount = tableEle.attributeValue(TAG_COUNT);
if(StringUtils.isNotEmpty(strCount)){
try{
this.setCount(Long.valueOf(strCount).longValue());
}catch(Exception ex){
throw new BaseException(ex);
}
}
String closeIdAutoInsert = tableEle.attributeValue(TAG_CLOSE_ID_AUTO_INSERT);
if(closeIdAutoInsert != null
&& closeIdAutoInsert.equalsIgnoreCase("true")){
this.setCloseIdAutoInsert(true);
}
String init = tableEle.elementTextTrim(TAG_INIT);
if(StringUtils.isNotEmpty(init)){
this.setInit(init);
}
String destroy = tableEle.elementTextTrim(TAG_DESTROY);
if(StringUtils.isNotEmpty(destroy)){
this.setDestroy(destroy);
}
List columns = tableEle.elements(TAG_COLUMN);
if(columns != null){
for(int i=0;i < columns.size();i++){
Element columnEle = (Element) columns.get(i);
ColumnConfig columnConfig = ColumnConfig.configure(columnEle);
this.addColumnConfig(columnConfig);
}
}
this.roleFactory = new RoleFactory(strName);
roleFactory.setTableConfig(this);
Element ele = tableEle.element(TAG_ROLES);
if(ele != null){
this.roleFactory.configure(ele);
}
return this;
}
@Override
public String toString() {
return this.tableName;
}
public Element toElement(){
Element tableConfigEle = DocumentHelper.createElement(TAG_TABLE);
tableConfigEle.addAttribute(TAG_NAME, this.getTableName());
tableConfigEle.addAttribute(TAG_CLOSE_ID_AUTO_INSERT, this.isCloseIdAutoInsert() + "");
tableConfigEle.addAttribute(TAG_COUNT, this.getCount() + "");
tableConfigEle.addAttribute(TAG_ON_ERROR, this.getOnError());
if(StringUtils.isNotEmpty(this.getInit())){
Element initEle = tableConfigEle.element(TAG_INIT);
if(initEle == null){
initEle = tableConfigEle.addElement(TAG_INIT);
}
initEle.setText(this.getInit());
}
if(StringUtils.isNotEmpty(this.getDestroy())){
Element destroyEle = tableConfigEle.element(TAG_DESTROY);
if(destroyEle == null){
destroyEle = tableConfigEle.addElement(TAG_DESTROY);
}
destroyEle.setText(this.getDestroy());
}
List<ColumnConfig> columnConfigs = this.getColumnConfigs();
if(columnConfigs != null && columnConfigs.size() > 0){
for(ColumnConfig columnConfig : columnConfigs){
tableConfigEle.add(columnConfig.toElement());
}
}
RoleFactory roleFactory = this.getRoleFactory();
if(roleFactory != null){
Element roleFactoryEle = roleFactory.toElement();
tableConfigEle.add(roleFactoryEle);
}else{
tableConfigEle.addElement(TAG_ROLES);
}
return tableConfigEle;
}
public void saveConfig() throws IOException{
String xml = this.toElement().asXML();
xml = XmlFileUtil.formatXml(xml, "UTF-8");
FileUtils.writeStringToFile(this.getConfigFile(),
xml, "utf-8");
}
public void deleteAndBankupFile(){
File configFile = getConfigFile();
if(configFile.exists()){
if(!configFile.renameTo(new File(getBankupConfigFileName()))){
configFile.delete();
}
}
}
public void updateAllRefs(String oldGenerateName, String newGenerateName){
if(this.getColumnConfigs() != null){
for(ColumnConfig columnConfig : this.getColumnConfigs()){
if(columnConfig.getRef().equalsIgnoreCase(oldGenerateName)){
columnConfig.setRef(newGenerateName);
}else if(columnConfig.getRef().startsWith(oldGenerateName + ".")){
String tempRef = columnConfig.getRef().replace(oldGenerateName + ".", newGenerateName + ".");
columnConfig.setRef(tempRef);
}
}
}
}
public void deleteBankupFile(){
File bankupFile = new File(getBankupConfigFileName());
if(bankupFile.exists()){
bankupFile.delete();
}
}
public File getConfigFile(){
return WorkspaceDataCache.getInstance().getTableConfigFile(tableName);
}
public String getBankupConfigFileName(){
return this.getConfigFile().getAbsolutePath() + ".bank";
}
/**
* 表名.
*/
private String tableName;
/**
* 关闭自动增长列.
*/
private boolean closeIdAutoInsert = false;
/**
* 插入的数据行数.
*/
private long count = 100;
/**
* 初始化sql语句.
*/
private String init = null;
/**
* 数据插完后的清理.sql语句.
*/
private String destroy = null;
private RoleFactory roleFactory;
private Boolean isGenerate = Boolean.TRUE;
/**
* 退出.
*/
public static final String ON_ERROR_EXIT = "exit";
/**
* 忽略.
*/
public static final String ON_ERROR_IGNORE = "ignore";
/**
* 询问.
*/
public static final String ON_ERROR_QUERY = "query";
private String onError = ON_ERROR_IGNORE;
/**
* 先删掉再添加.
*/
// public static final String INSERT_TYPE_DELETE = "delete";
//
// public static final String INSERT_TYPE_ID_INSERT = "idInsert";
//
// private String insertType = INSERT_TYPE_ID_INSERT;
/**
* 列.
*/
private List<ColumnConfig> columnConfigs = new LinkedList<ColumnConfig>();
public void addColumnConfig(ColumnConfig columnConfig){
columnConfig.setTableConfig(this);
this.getColumnConfigs().add(columnConfig);
}
public boolean isCloseIdAutoInsert() {
return closeIdAutoInsert;
}
public void setCloseIdAutoInsert(boolean closeIdAutoInsert) {
this.closeIdAutoInsert = closeIdAutoInsert;
}
public List<ColumnConfig> getColumnConfigs() {
return columnConfigs;
}
public static List<ColumnConfig> cloneColumnConfigs(List<ColumnConfig> columnConfigs){
if(columnConfigs != null){
List<ColumnConfig> tempColumnConfigs = new LinkedList<ColumnConfig>();
for(ColumnConfig columnConfig : columnConfigs){
tempColumnConfigs.add(columnConfig.clone());
}
return tempColumnConfigs;
}
return null;
}
/**
* 设置columnConfig.
* <p><code>setColumnConfigs</code></p>
* @param columnConfigs
* @author LiuXiaojie 2007-12-11
*/
public void setColumnConfigs(List<ColumnConfig> columnConfigs) {
this.columnConfigs = columnConfigs;
}
public long getCount() {
return count;
}
public void setCount(long count) {
this.count = count;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -