org.artifact.builder.design.DesignTable Maven / Gradle / Ivy
package org.artifact.builder.design;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.artifact.builder.enums.BuilderMethodEnum;
import org.w3c.dom.Element;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.XmlUtil;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@ToString
public class DesignTable {
/** 表名称 */
private String tableName;
/** 类名称 */
private String className;
/** 备注 */
private String remark;
/** 字段集 */
private List columns = new ArrayList<>();
/** 主键 */
private DesignColumn pk;
/** 方法 */
private Set methods = new HashSet<>();
/** 索引 */
private List indexs = new ArrayList<>();
/** 关系 */
private List relations = new ArrayList<>();
public DesignTable(Element table,DesignConfig designConfig) {
this.tableName = table.getAttribute("name");
// 1.去除前缀
className = StrUtil.removePrefix(this.tableName,designConfig.getTablePrefix());
// 2.转驼峰
className = StrUtil.toCamelCase(className);
// 3.首字母大写
className = StrUtil.upperFirst(className);
this.remark = table.getAttribute("remark");
BuilderMethodEnum[] methodEnum = designConfig.getMethods();
for (BuilderMethodEnum builderMethodEnum : methodEnum) {
methods.add(builderMethodEnum.name().toLowerCase());
}
String[] methodArray = StrUtil.split(table.getAttribute("methods"), ",");
for (String method : methodArray) {
methods.add(method);
}
// 字段
List columns = XmlUtil.getElements(XmlUtil.getElement(table, "columns"), "column");
for (Element element : columns) {
DesignColumn column = new DesignColumn(element,designConfig);
if (this.pk==null && column.isPk()) {
this.pk = column;
}
this.columns.add(column);
}
this.columns.add(new DesignColumn("create_date","createDate", "java.util.Date",false, "创建时间", false,"","datetime",""));
this.columns.add(new DesignColumn("modified_date","modifiedDate", "java.util.Date",false, "修改时间", false,"","datetime",""));
// 常规索引
if (XmlUtil.getElement(table, "indexs")!=null) {
List indexs = XmlUtil.getElements(XmlUtil.getElement(table, "indexs"), "index");
for (Element element : indexs) {
DesignIndex index = new DesignIndex(this,element);
this.indexs.add(index);
}
}
}
public DesignColumn getByName(String name) {
for (DesignColumn column : columns) {
if (column.getColumnName().equals(name)) {
return column;
}
}
return null;
}
}