import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.dom4j.Element;
import com.sinosoft.lis.pubfun.PubFun;
import com.utility.pdm.util.bean.BeanProperties;
import com.utility.pdm.util.bean.PdmInfoBean;
import com.utility.pdm.util.tools.AutoJavaBean;
import com.utility.pdm.util.tools.PdmTools;
@SuppressWarnings("rawtypes")
public class PdmUtil {
public static PdmInfoBean mPdmInfoBean = new PdmInfoBean();
public static int mTableSizt =0;//解析的表的个数
public static String packageName="com.ll.tablebean";//生成javabean的包名
private String mColumnComment="";//字段注释如果没有就设置为 字段名称
private String mMandatory ="";//是否为空 1不可为空 ;0可以空
private String ispk="";//是否为主键 1是 ;0否
private String isfk="";//是否为外键 1是;0否
AutoJavaBean mAutoJavaBean = new AutoJavaBean();
/**
* 获取表 这里返回一个list
* 也可以使用bean
* @param path
* @return
*/
@SuppressWarnings({ "unchecked" })
public List getTable(String path) {
List mTableList = new ArrayList();
int count = 0;
//========================获取数据模型信息==============================================================
Element root = PdmTools.ReadPdm(path);
Element tModelInfo = root.element("RootObject").element("Children").element("Model");
mPdmInfoBean.setModelName(tModelInfo.elementText("Name"));
mPdmInfoBean.setUserName(tModelInfo.elementText("Creator"));
mPdmInfoBean.setModifyName(tModelInfo.elementText("Modifier"));
Element tDBMSlInfo = root.element("RootObject").element("Children").element("Model").element("DBMS").element("Shortcut");
mPdmInfoBean.setDbName(tDBMSlInfo.elementText("Name"));
Element thyPsicalDiagramslInfo = root.element("RootObject").element("Children").element("Model").element("PhysicalDiagrams").element("PhysicalDiagram");
mPdmInfoBean.setDiagramName(thyPsicalDiagramslInfo.elementText("Name"));
//==============================END================================================================
Element tables = root.element("RootObject").element("Children").element("Model").element("Tables");
for (Iterator it = tables.elementIterator("Table"); it.hasNext();) {
count++;
mTableSizt++;
Element eltable = (Element) it.next();
Map tableMap= new HashMap();//存放表数据
tableMap.put("name", eltable.elementText("Name"));
tableMap.put("code", eltable.elementText("Code"));
tableMap.put("id", count);
mTableList.add(tableMap);
}
mPdmInfoBean.setmTableSizt(mTableSizt);
return mTableList;
}
/**
* 获取所有表中的所有字段
* @param eltable
*/
@SuppressWarnings({ "unchecked" })
public List getColumns(String path) {
List mColumnsList = new ArrayList();
Element root = PdmTools.ReadPdm(path);
Element tables = root.element("RootObject").element("Children").element("Model").element("Tables");
int count =0;
for (Iterator it = tables.elementIterator("Table"); it.hasNext();) {
Element eltable = (Element) it.next();//获取一个表
List pkList = new ArrayList();//用来存放主键,放置有多个主键所有用一个集合
count++;
//==========================================解析字段前先解析出主键=====================================
Element pkElement= eltable.element("Keys").element("Key").element("Key.Columns");
Iterator itPK = pkElement.elementIterator("Column");
while (itPK.hasNext()) {
Element elcolumn = (Element) itPK.next();
pkList.add(elcolumn.attributeValue("Ref"));
// System.out.println(elcolumn.attributeValue("Ref"));
}
//=============================================================================================
Iterator its = eltable.element("Columns").elementIterator("Column");
while (its.hasNext()) {
Element elcolumn = (Element) its.next();
Map columnMap= new HashMap();//存放表数据
String columnID = elcolumn.attributeValue("Id");//获取字段编号 用于判断字段是否主键、外键
//主键一定不为空,所有只有不为空的时候采取判断是否为主键 这样可以提高效率
if (elcolumn.elementText("Mandatory") != null ) {
ispk = isPK(columnID,pkList);
}else{
ispk ="0";//等于空就一定不是主键。
}
String length ="0";//处理字段长度
if (elcolumn.elementText("Length") == null ) {
length ="0";
}else{
length=elcolumn.elementText("Length");
}
String comment =elcolumn.elementText("Comment") != null ?elcolumn.elementText("Comment"):"";
comment= PubFun.replaceBlank(comment);
columnMap.put("table", eltable.elementText("Code"));
columnMap.put("name", elcolumn.elementText("Name"));
columnMap.put("code", elcolumn.elementText("Code"));
columnMap.put("comment", comment);
columnMap.put("notnull", elcolumn.elementText("Mandatory") != null ?elcolumn.elementText("Mandatory"): "0");
columnMap.put("ispk", ispk);
columnMap.put("datatype", getCoumnType(elcolumn.elementText("DataType")));
columnMap.put("length", length);
columnMap.put("id", count);
mColumnsList.add(columnMap);
}
}
return mColumnsList;
}
/**
* 获取表的所有字段属性
* @param path
* @return
*/
@SuppressWarnings({ "unused", "unchecked" })
public List getTableColumns(String path){
List mColumnsList = new ArrayList();
List mTableColumnsLit = new ArrayList();
int countT =0;//记录表的个数
//========================获取数据模型信息==============================================================
Element root = PdmTools.ReadPdm(path);
Element tModelInfo = root.element("RootObject").element("Children").element("Model");
mPdmInfoBean.setModelName(tModelInfo.elementText("Name"));
mPdmInfoBean.setUserName(tModelInfo.elementText("Creator"));
mPdmInfoBean.setModifyName(tModelInfo.elementText("Modifier"));
Element tDBMSlInfo = root.element("RootObject").element("Children").element("Model").element("DBMS").element("Shortcut");
mPdmInfoBean.setDbName(tDBMSlInfo.elementText("Name"));
Element thyPsicalDiagramslInfo = root.element("RootObject").element("Children").element("Model").element("PhysicalDiagrams").element("PhysicalDiagram");
mPdmInfoBean.setDiagramName(thyPsicalDiagramslInfo.elementText("Name"));
//==============================END================================================================
Element tables = root.element("RootObject").element("Children").element("Model").element("Tables");
for (Iterator it = tables.elementIterator("Table"); it.hasNext();) {
Element eltable = (Element) it.next();
List pkList = new ArrayList();//用来存放主键,放置有多个主键所有用一个集合
countT++;
int countC =0;//记录字段的个数
Map tableMap= new HashMap();//存放表数据
tableMap.put("id", countT);
tableMap.put("name", eltable.elementText("Name"));
tableMap.put("code", eltable.elementText("Code"));
//==========================================解析字段前先解析出主键=====================================
Element pkElement= eltable.element("Keys").element("Key").element("Key.Columns");
Iterator itPK = pkElement.elementIterator("Column");
while (itPK.hasNext()) {
Element elcolumn = (Element) itPK.next();
pkList.add(elcolumn.attributeValue("Ref"));
// System.out.println(elcolumn.attributeValue("Ref"));
}
//=============================================================================================
Iterator its = eltable.element("Columns").elementIterator("Column");
while (its.hasNext()) {
Element elcolumn = (Element) its.next();
countC ++;
Map columnMap= new HashMap();//存放表数据
String columnID = elcolumn.attributeValue("Id");//获取字段编号 用于判断字段是否主键、外键
//主键一定不为空,所有只有不为空的时候采取判断是否为主键 这样可以提高效率
if (elcolumn.elementText("Mandatory") != null ) {
ispk = isPK(columnID,pkList);
}else{
ispk ="0";//等于空就一定不是主键。
}
String length ="0";//处理字段长度
if (elcolumn.elementText("Length") == null ) {
length ="0";
}else{
length=elcolumn.elementText("Length");
}
String comment =elcolumn.elementText("Comment") != null ?elcolumn.elementText("Comment"):"";
comment= PubFun.replaceBlank(comment);
// columnMap.put("table", eltable.elementText("Code"));
columnMap.put("name", elcolumn.elementText("Name"));
columnMap.put("code", elcolumn.elementText("Code"));
columnMap.put("comment", comment);
columnMap.put("notnull", elcolumn.elementText("Mandatory") != null ?elcolumn.elementText("Mandatory"): "0");
columnMap.put("datatype", getCoumnType(elcolumn.elementText("DataType")));
columnMap.put("ispk", ispk);
columnMap.put("length", length);
mColumnsList.add(columnMap);
}
tableMap.put("size", countC);
tableMap.put("attribute", mColumnsList);
//===============================================================================================
mTableSizt = countT;
mPdmInfoBean.setmTableSizt(mTableSizt);
mTableColumnsLit.add(tableMap);
}
return mTableColumnsLit;
}
/**
* 解析信息的同时生成javabean
* @param pdmPath
* @param packageName
*/
public void makePDMToBean(String packageName,String path){
int countT =0;//记录表的个数
//========================获取数据模型信息==============================================================
Element root = PdmTools.ReadPdm(path);
Element tModelInfo = root.element("RootObject").element("Children").element("Model");
mPdmInfoBean.setModelName(tModelInfo.elementText("Name"));
mPdmInfoBean.setUserName(tModelInfo.elementText("Creator"));
mPdmInfoBean.setModifyName(tModelInfo.elementText("Modifier"));
Element tDBMSlInfo = root.element("RootObject").element("Children").element("Model").element("DBMS").element("Shortcut");
mPdmInfoBean.setDbName(tDBMSlInfo.elementText("Name"));
Element thyPsicalDiagramslInfo = root.element("RootObject").element("Children").element("Model").element("PhysicalDiagrams").element("PhysicalDiagram");
mPdmInfoBean.setDiagramName(thyPsicalDiagramslInfo.elementText("Name"));
//==============================END================================================================
Element tables = root.element("RootObject").element("Children").element("Model").element("Tables");
for (Iterator it = tables.elementIterator("Table"); it.hasNext();) {
Element eltable = (Element) it.next();
List pkList = new ArrayList();//用来存放主键,放置有多个主键所有用一个集合
countT++;
int countC =0;//记录字段的个数
/*Map tableMap= new HashMap();//存放表数据
tableMap.put("id", countT);
tableMap.put("name", eltable.elementText("Name"));
tableMap.put("code", eltable.elementText("Code"));*/
//==========================================解析字段前先解析出主键=====================================
Element pkElement= eltable.element("Keys").element("Key").element("Key.Columns");
Iterator itPK = pkElement.elementIterator("Column");
while (itPK.hasNext()) {
Element elcolumn = (Element) itPK.next();
pkList.add(elcolumn.attributeValue("Ref"));
// System.out.println(elcolumn.attributeValue("Ref"));
}
//=============================================================================================
Iterator its = eltable.element("Columns").elementIterator("Column");
ArrayList<BeanProperties> arraylist = new ArrayList();//bean属性
while (its.hasNext()) {
Element elcolumn = (Element) its.next();
countC ++;
BeanProperties tBeanProperties = new BeanProperties();
String columnID = elcolumn.attributeValue("Id");//获取字段编号 用于判断字段是否主键、外键
//主键一定不为空,所有只有不为空的时候采取判断是否为主键 这样可以提高效率
if (elcolumn.elementText("Mandatory") != null ) {
ispk = isPK(columnID,pkList);
}else{
ispk ="0";//等于空就一定不是主键。
}
String length ="0";//处理字段长度
if (elcolumn.elementText("Length") == null ) {
length ="0";
}else{
length=elcolumn.elementText("Length");
}
String comment =elcolumn.elementText("Comment") != null ?elcolumn.elementText("Comment"):"";
comment= PubFun.replaceBlank(comment);
/*Map columnMap= new HashMap();//存放表数据
columnMap.put("table", eltable.elementText("Code"));
columnMap.put("name", elcolumn.elementText("Name"));
columnMap.put("code", elcolumn.elementText("Code"));
columnMap.put("comment", elcolumn.elementText("Comment") != null ?elcolumn.elementText("Comment"): elcolumn.elementText("Comment")+elcolumn.elementText("Name"));
columnMap.put("notnull", elcolumn.elementText("Mandatory") != null ?elcolumn.elementText("Mandatory"): "0");
columnMap.put("datatype", getCoumnType(elcolumn.elementText("DataType")));
columnMap.put("ispk", ispk);
columnMap.put("length", length); */
tBeanProperties.setComment((elcolumn.elementText("Name") != null ?elcolumn.elementText("Name"):"")+" "+comment);
tBeanProperties.setName(elcolumn.elementText("Code"));
tBeanProperties.setType(getCoumnType(elcolumn.elementText("DataType")));
arraylist.add(tBeanProperties);
}
String className =eltable.elementText("Code")+"Bean";//
String tableName =eltable.elementText("Name");
mTableSizt = countT;
mPdmInfoBean.setmTableSizt(mTableSizt);
//===================================开始生成javabean============================================================
mAutoJavaBean.createBean(className, arraylist, packageName,tableName);
//=====================================读取一张生成一张==========================================
}
}
/**
* 获取字段相应的JAVA数据类型
* @param dbtype 数据库数据类型
* @returnjavaType
*/
private String getCoumnType(String dbtype){
String javaType ="String";//没有考虑到的全部用String
String mDBtype ="MYSQL";//以后要动态获取
if (mDBtype.toUpperCase().equals("MYSQL")) {//使用MYSQL数据类型
if (dbtype.toLowerCase().startsWith("int")) {
javaType ="int";
}else if (dbtype.toLowerCase().startsWith("double")) {
javaType ="double";
}
else if (dbtype.toLowerCase().startsWith("bool")) {//bool;boolean
javaType ="boolean";
}
else if (dbtype.toLowerCase().startsWith("date")) {
javaType ="Date";
}
}
else if (mDBtype.toUpperCase().equals("SQLSERVER")) {//使用SQLSERVER数据类型
}
else if (mDBtype.toUpperCase().equals("ORACLE")) {//使用ORACLE数据类型
}else {
System.out.println("占时不支持该数据库");
}
return javaType;
}
/**
* 判断是否为主键
* @param columnID
* @param pkList
* @return
*/
private String isPK(String columnID, List pkList) {
String pk ="0";
for (int i = 0; i < pkList.size(); i++) {
if (columnID.equals(pkList.get(i))) {
pk="1";
break;
}
}
return pk;
}
public static void main(String[] args) {
}
}