com.centit.support.database.metadata.HibernateMapInfo Maven / Gradle / Ivy
package com.centit.support.database.metadata;
import com.centit.support.algorithm.ReflectionOpt;
import com.centit.support.database.utils.FieldType;
import com.centit.support.xml.IgnoreDTDEntityResolver;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* 这个变量的属性没有和TableInfo一致起来,设计的时候有欠缺
*
* @author codefan
*/
public class HibernateMapInfo {
protected static final Logger logger = LoggerFactory.getLogger(ReflectionOpt.class);
private String className;
private String tableName;
private String tableLabelName;//中文名
private String tableComment;
private List keyProperties;
private List properties;
private boolean isMainTable;
private boolean complexId;
private String idType;
private String idName;
private List one2manys;
private List references;
public HibernateMapInfo() {
isMainTable = true;
complexId = false;
}
public static String trimToSimpleClassName(String className) {
String sClassSimpleName = className;
int p = className.lastIndexOf('.');
if (p > 0)
sClassSimpleName = className.substring(p + 1);
return sClassSimpleName;
}
public boolean isReferenceColumn(int refPos, String sCol) {
if (references == null || references.size() == 0)
return false;
int n = references.size();
if (refPos < 0 || n <= 0 || refPos >= n)
return false;
return references.get(refPos).containColumn(sCol);
}
public boolean isComplexId() {
return complexId;
}
public void setComplexId(boolean hasID) {
this.complexId = hasID;
}
public String getIdType() {
return idType;
}
public void setIdType(String idType) {
this.idType = idType;
}
public String getIdName() {
return idName;
}
public void setIdName(String idName) {
this.idName = idName;
}
public boolean isMainTable() {
return isMainTable;
}
public void setMainTable(boolean isMT) {
this.isMainTable = isMT;
}
public String getTableLabelName() {
return tableLabelName;
}
public void setTableLabelName(String sTableDesc) {
this.tableLabelName = sTableDesc;
}
public String getTableComment() {
return tableComment;
}
public void setTableComment(String sTableComment) {
this.tableComment = sTableComment;
}
public String getClassSimpleName() {
return trimToSimpleClassName(className);
}
private SimpleTableField loadField(Element fieldNode) {
SimpleTableField field = new SimpleTableField();
field.setPropertyName(fieldNode.attribute("name").getValue());
String sType = "";
Attribute atType = fieldNode.attribute("type");
if (atType != null)
sType = atType.getValue();
else {
atType = fieldNode.attribute("class");
if (atType != null)
sType = atType.getValue();
}
if (StringUtils.isNotBlank(sType)) {
try {
field.setFieldType(FieldType.mapToFieldType(Class.forName(sType)));
} catch (ClassNotFoundException e) {
logger.error(e.getMessage());
}
}
Element columnNode = fieldNode.element("column");
if (columnNode != null) {
Attribute attr;
field.setColumnName(columnNode.attribute("name").getValue());
attr = columnNode.attribute("length");
if (attr != null)
field.setMaxLength(Integer.valueOf(attr.getValue()));
attr = columnNode.attribute("not-null");
if (attr != null)
field.setMandatory(attr.getValue());
attr = columnNode.attribute("precision");
if (attr != null)
field.setPrecision(Integer.valueOf(attr.getValue()));
attr = columnNode.attribute("scale");
if (attr != null)
field.setScale(Integer.valueOf(attr.getValue()));
}
return field;
}
public void loadHibernateMetadata(String sPath, String sHbmFile) {
keyProperties = new ArrayList();
properties = new ArrayList();
one2manys = null;
references = null;
try (FileInputStream is = new FileInputStream(new File(sPath + sHbmFile))) {
//InputStream is = getClass().getResourceAsStream(sPath + sHbmFile);
SAXReader builder = new SAXReader(false);
builder.setValidation(false);
builder.setEntityResolver(new IgnoreDTDEntityResolver());
Attribute attr;
Document doc = builder.read(is);
Element classNode = doc.getRootElement().element("class");
className = classNode.attribute("name").getValue();
attr = classNode.attribute("table");
if (attr != null)
tableName = attr.getValue();
Element idNode = classNode.element("id");
if (idNode != null) {// 单个主键属性
SimpleTableField tf = loadField(idNode);
complexId = false;
idType = FieldType.trimType(tf.getJavaType().getName());
idName = tf.getPropertyName();
keyProperties.add(tf);
} else {//复合主键值
idNode = classNode.element("composite-id");//key-property
complexId = true;
idType = idNode.attributeValue("class");
idName = idNode.attributeValue("name");
idType = FieldType.trimType(idType);
List keyNodes = idNode.elements("key-property");
for (Element key : keyNodes) {
keyProperties.add(loadField(key));
}
/*
* if(isMainTable){
keyNodes = (List) idNode.elements("key-many-to-one");
for(Element key : keyNodes){
keyProperties.add(loadField(key));
}
}
keyNodes = (List) idNode.elements("key-one-to-one");
for(Element key : keyNodes){
keyProperties.add(loadField(key));
}
*/
}
List propNodes = classNode.elements("property");
for (Element prop : propNodes) {
properties.add(loadField(prop));
}
if (isMainTable) {
List setElements = classNode.elements("set");
//List one2manyNodes = (List)setElement.elements("//one-to-many");
if (setElements != null && setElements.size() > 0) {
one2manys = new ArrayList<>();
references = new ArrayList<>();
for (Element setElement : setElements) {
Element one2manyNode = setElement.element("one-to-many");
String sSubClassName = one2manyNode.attributeValue("class");
int p = sSubClassName.lastIndexOf('.');
if (p > 0) {
sSubClassName = sSubClassName.substring(p + 1);
}
HibernateMapInfo one2many = new HibernateMapInfo();
one2many.setMainTable(false);
one2many.loadHibernateMetadata(sPath, sSubClassName + ".hbm.xml");
one2manys.add(one2many);
SimpleTableReference ref = new SimpleTableReference();
ref.setParentTableName(tableName);
ref.setReferenceCode(setElement.attributeValue("name"));
Element keyElt = setElement.element("key");
List colElements = keyElt.elements("column");
for (Element colElt : colElements) {
ref.addReferenceColumn(colElt.attributeValue("name"),
colElt.attributeValue("column"));
}
//
references.add(ref);
}
}
}
} catch (DocumentException e) {
logger.error(e.getMessage(), e);// e.printStackTrace();
} catch (IOException e) {
logger.error(e.getMessage(), e);//e.printStackTrace();
}
}
public String getClassName() {
return className;
}
public void setClassName(String sClassName) {
this.className = sClassName;
}
public String getTableName() {
return tableName;
}
public void setTableName(String sTableName) {
this.tableName = sTableName;
}
public List getKeyProperties() {
if (keyProperties == null)
keyProperties = new ArrayList();
return keyProperties;
}
public void setKeyProperties(List keyProperties) {
this.keyProperties = keyProperties;
}
public List getProperties() {
if (properties == null)
properties = new ArrayList();
return properties;
}
public void setProperties(List properties) {
this.properties = properties;
}
public SimpleTableField getProperty(int indx) {
int n = getProperties().size();
if (n < 1 || indx < 0 || indx >= n)
return new SimpleTableField();
return properties.get(indx);
}
public SimpleTableField getKeyProperty(int indx) {
int n = getKeyProperties().size();
if (n < 1 || indx < 0 || indx >= n)
return new SimpleTableField();
return keyProperties.get(indx);
}
public List getOne2manys() {
if (one2manys == null)
one2manys = new ArrayList();
return one2manys;
}
public void setOne2manys(List one2manys) {
this.one2manys = one2manys;
}
public List getReferences() {
if (references == null)
references = new ArrayList();
return references;
}
public void setReferences(List references) {
this.references = references;
}
}