package com.matrix.codeGeneration.ext;
|
|
import java.io.File;
|
import java.util.ArrayList;
|
import java.util.Date;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
import org.springframework.beans.BeanUtils;
|
|
import com.matrix.codeGeneration.model.CodeFile;
|
import com.matrix.codeGeneration.model.CodeModel;
|
import com.matrix.codeGeneration.model.CommonData;
|
import com.matrix.codeGeneration.model.Constant;
|
import com.matrix.codeGeneration.model.OutDataSource;
|
import com.matrix.codeGeneration.model.TableClassModel;
|
import com.matrix.codeGeneration.plugin.DateUtils;
|
|
/**
|
* matrix4代码文件模型
|
*/
|
public class Matrix4Models implements CodeModel {
|
|
/**
|
* 模型名称用来标识这个模型
|
*/
|
String modelName="";
|
|
/**
|
* 包名
|
*/
|
private String packageName="";
|
|
/**
|
* 名称后缀
|
*/
|
private String suffixName="";
|
|
/**
|
* 名称前缀
|
*/
|
private String prefixName="";
|
|
/**
|
* 文件扩展名称
|
*/
|
private String fileExtName="";
|
|
/**
|
* 模板名称
|
*/
|
private String templateName="";
|
|
/**
|
* 导入类列表
|
*/
|
private Map<String, String> importList = new HashMap<>();
|
|
/**
|
* 类的变量名称
|
*/
|
private String classVariableName="";
|
|
|
|
@Override
|
public List<CodeFile> buildCodeFile(OutDataSource dataSource) {
|
List<CodeFile> codeFileList = new ArrayList<>();
|
for (TableClassModel tableClassModel : dataSource.getTableClassModels()) {
|
Map<String, Object> fileData = new HashMap<>();
|
tableClassModel.setBeanClassName(getModelClassName(Constant.JAVA_BEAN,tableClassModel,dataSource));
|
tableClassModel.setFullClassName(getModelClassName(modelName,tableClassModel,dataSource));
|
tableClassModel.setQualifiedClassName(getModelQualifiedClassName(modelName,tableClassModel,dataSource));
|
System.out.println(tableClassModel.getClassName());
|
System.out.println(this.modelName);
|
System.out.println("--------------");
|
|
if (Constant.JAVA_BEAN.equals(this.modelName)) {
|
|
this.getImportList().put("Extend", "com.matrix.core.anotations.Extend");
|
this.getImportList().put("EntityDTO", "com.matrix.core.pojo.EntityDTO");
|
this.getImportList().put("EntityDTOExt", "com.matrix.core.publicBean.EntityDTOExt");
|
|
} else if (Constant.MYBATIS_DAO.equals(this.modelName)) {
|
this.setImportList(new HashMap<String, String>());
|
this.getImportList().put("bean",
|
getModelQualifiedClassName(Constant.JAVA_BEAN, tableClassModel, dataSource));
|
|
this.getImportList().put("List", "java.util.List");
|
this.getImportList().put("Param", "org.apache.ibatis.annotations.Param");
|
this.getImportList().put("PaginationVO", "com.matrix.core.pojo.PaginationVO");
|
this.getImportList().put("Map", "java.util.Map");
|
|
} else if (Constant.MYBATIS_DAO_IMPL.equals(this.modelName)) {
|
|
} else if (Constant.SERVICE.equals(this.modelName)) {
|
|
this.setImportList(new HashMap<String, String>());
|
this.getImportList().put("bean",
|
getModelQualifiedClassName(Constant.JAVA_BEAN, tableClassModel, dataSource));
|
this.getImportList().put("List", "java.util.List");
|
this.getImportList().put("BaseServices", "com.matrix.core.web.BaseServices");
|
this.getImportList().put("PaginationVO", "com.matrix.core.pojo.PaginationVO");
|
|
} else if (Constant.SERVICE_IMPL.equals(this.modelName)) {
|
this.setImportList(new HashMap<String, String>());
|
this.getImportList().put("bean",
|
getModelQualifiedClassName(Constant.JAVA_BEAN, tableClassModel, dataSource));
|
|
this.getImportList().put("services",
|
getModelQualifiedClassName(Constant.SERVICE, tableClassModel, dataSource));
|
this.getImportList().put("dao",
|
getModelQualifiedClassName(Constant.MYBATIS_DAO, tableClassModel, dataSource));
|
this.getImportList().put("springframework.Service", "org.springframework.stereotype.Service");
|
this.getImportList().put("Autowired", "org.springframework.beans.factory.annotation.Autowired");
|
this.getImportList().put("List", "java.util.List");
|
this.getImportList().put("Map", "java.util.Map");
|
this.getImportList().put("MatrixConstance", "com.matrix.core.constance.MatrixConstance");
|
this.getImportList().put("WebUtil", "com.matrix.core.tools.WebUtil");
|
this.getImportList().put("GlobleException", "com.matrix.core.exception.GlobleException");
|
this.getImportList().put("ModelUtils", "com.matrix.core.tools.ModelUtils");
|
this.getImportList().put("SystemErrorCode", "com.matrix.core.constance.SystemErrorCode");
|
this.getImportList().put("UUIDUtil", "com.matrix.core.tools.UUIDUtil");
|
this.getImportList().put("PaginationVO", "com.matrix.core.pojo.PaginationVO");
|
this.getImportList().put("SysUsers", "com.xincheng.common.bean.SysUsers");
|
|
|
fileData.put("daoClassName", getModelClassName(Constant.MYBATIS_DAO, tableClassModel, dataSource));
|
fileData.put("daoVariableName", getModelVariablesName(Constant.MYBATIS_DAO, tableClassModel, dataSource));
|
fileData.put("serviceInterface", getModelClassName(Constant.SERVICE, tableClassModel, dataSource));
|
|
|
} else if (Constant.ACTION.equals(this.modelName)) {
|
this.getImportList().put("bean",
|
getModelQualifiedClassName(Constant.JAVA_BEAN, tableClassModel, dataSource));
|
//this.getImportList().put("services",
|
// getModelQualifiedClassName(Constant.SERVICE, tableClassModel, dataSource));
|
this.getImportList().put("Controller", "org.springframework.stereotype.Controller");
|
this.getImportList().put("Autowired", "org.springframework.beans.factory.annotation.Autowired");
|
this.getImportList().put("AjaxResult", "com.matrix.core.pojo.AjaxResult");
|
this.getImportList().put("GlobleException", "com.matrix.core.exception.GlobleException");
|
this.getImportList().put("PaginationVO", "com.matrix.core.pojo.PaginationVO");
|
this.getImportList().put("WebUtil", "com.matrix.core.tools.WebUtil");
|
this.getImportList().put("SaveRequestToken", "com.matrix.core.anotations.SaveRequestToken");
|
this.getImportList().put("RemoveRequestToken", "com.matrix.core.anotations.RemoveRequestToken");
|
this.getImportList().put("BaseController", "com.matrix.core.web.BaseAction");
|
this.getImportList().put("RequestMapping", "org.springframework.web.bind.annotation.RequestMapping");
|
this.getImportList().put("ResponseBody", "org.springframework.web.bind.annotation.ResponseBody");
|
this.getImportList().put("AppConstance", "static com.xincheng.common.constance.AppConstance.*");
|
this.getImportList().put("SystemMessageCode", "com.matrix.core.constance.SystemMessageCode");
|
this.getImportList().put("SystemErrorCode", "com.matrix.core.constance.SystemErrorCode");
|
this.getImportList().put("Map", "java.util.Map");
|
this.getImportList().put("RedisUserLoginUtils", "com.matrix.core.redis.RedisUserLoginUtils");
|
this.getImportList().put("SysUsers", "com.xincheng.commonApi.bean.SysUsers");
|
}
|
fileData.put("importList", this.getImportList());
|
CodeFile codeFile = new CodeFile();
|
// 计算文件的名称
|
codeFile.setFileName(getFileName(tableClassModel, this));
|
codeFile.setSavePath(File.separator + getPackageName().replace(".", File.separator) + File.separator);
|
fileData.put("codeModel", this);
|
fileData.put("beanQualifiedClassName", getModelQualifiedClassName(Constant.JAVA_BEAN, tableClassModel, dataSource));
|
fileData.put("dataSource", dataSource);
|
TableClassModel thisTableClassModel= new TableClassModel();
|
BeanUtils.copyProperties(tableClassModel, thisTableClassModel);
|
fileData.put("tableClassModel", thisTableClassModel);
|
fileData.put("commonData", new CommonData());
|
fileData.put("time", DateUtils.dateFormatStr(new Date(), "yyyy-MM-dd HH:mm"));
|
fileData.put("ClassName", getModelClassName(this.getModelName(), tableClassModel, dataSource));
|
codeFile.setData(fileData);
|
codeFile.setTemplateName(this.templateName);
|
codeFileList.add(codeFile);
|
}
|
return codeFileList;
|
|
}
|
|
/**
|
* 计算文件名称
|
*
|
* @param tableClassModel
|
* @return
|
*/
|
public String getFileName(TableClassModel tableClassModel, Matrix4Models ahcsModels) {
|
StringBuffer name = new StringBuffer();
|
String prefix = ahcsModels.getPrefixName();
|
String suffix = ahcsModels.getSuffixName();
|
|
|
String extensionName = ahcsModels.getFileExtName();
|
if (prefix != null) {
|
name.append(prefix);
|
}
|
// 这里判断如果是html 则应该将文件名首字母小写
|
if(".html".equals(extensionName)) {
|
String className = tableClassModel.getClassName();
|
String initial = className.substring(0, 1).toLowerCase();
|
name.append(initial+className.substring(1));
|
}else {
|
name.append(tableClassModel.getClassName());
|
}
|
if (suffix != null) {
|
name.append(suffix);
|
}
|
name.append(extensionName);
|
return name.toString();
|
}
|
|
/**
|
* 获取一个模型的变量名
|
*
|
* @param modelName
|
* @param dataSource
|
* @return
|
*/
|
public String getModelVariablesName(String modelName, TableClassModel tableClassModel, OutDataSource dataSource) {
|
return dataSource.getNameConvert()
|
.classNameToVariableName(getModelClassName(modelName, tableClassModel, dataSource));
|
}
|
|
/**
|
* 获取一个模型的全类名
|
*
|
* @param modelName
|
* @param dataSource
|
* @return
|
*/
|
public String getModelQualifiedClassName(String modelName, TableClassModel tableClassModel,
|
OutDataSource dataSource) {
|
Matrix4Models ahcsMOdels = ((Matrix4Models) dataSource.getCodeModels().get(modelName));
|
String className = ahcsMOdels.getPackageName() + "." + ahcsMOdels.getPrefixName()
|
+ tableClassModel.getClassName() + ahcsMOdels.getSuffixName();
|
return className;
|
}
|
|
/**
|
* 获取一个模型的类名
|
*
|
* @param modelName
|
* @param dataSource
|
* @return
|
*/
|
public String getModelClassName(String modelName, TableClassModel tableClassModel, OutDataSource dataSource) {
|
Matrix4Models ahcsMOdels = ((Matrix4Models) dataSource.getCodeModels().get(modelName));
|
String className = ahcsMOdels.getPrefixName() + tableClassModel.getClassName() + ahcsMOdels.getSuffixName();
|
return className;
|
}
|
|
|
|
public String getModelName() {
|
return modelName;
|
}
|
|
public void setModelName(String modelName) {
|
this.modelName = modelName;
|
}
|
|
public String getPackageName() {
|
return packageName;
|
}
|
|
public void setPackageName(String packageName) {
|
this.packageName = packageName;
|
}
|
|
public String getSuffixName() {
|
return suffixName;
|
}
|
|
public void setSuffixName(String suffixName) {
|
this.suffixName = suffixName;
|
}
|
|
public String getPrefixName() {
|
return prefixName;
|
}
|
|
public void setPrefixName(String prefixName) {
|
this.prefixName = prefixName;
|
}
|
|
public String getFileExtName() {
|
return fileExtName;
|
}
|
|
public void setFileExtName(String fileExtName) {
|
this.fileExtName = fileExtName;
|
}
|
|
public String getTemplateName() {
|
return templateName;
|
}
|
|
public void setTemplateName(String templateName) {
|
this.templateName = templateName;
|
}
|
|
public Map<String, String> getImportList() {
|
return importList;
|
}
|
|
public void setImportList(Map<String, String> importList) {
|
this.importList = importList;
|
}
|
|
public String getClassVariableName() {
|
return classVariableName;
|
}
|
|
public void setClassVariableName(String classVariableName) {
|
this.classVariableName = classVariableName;
|
}
|
|
|
|
}
|