package com.ots.project.tool.gen.util;
|
import com.alibaba.fastjson.JSONObject;
|
import com.ots.common.constant.GenConstants;
|
import com.ots.common.utils.DateUtils;
|
import com.ots.common.utils.StringUtils;
|
import com.ots.project.tool.gen.domain.GenTable;
|
import com.ots.project.tool.gen.domain.GenTableColumn;
|
import org.apache.velocity.VelocityContext;
|
import java.util.ArrayList;
|
import java.util.HashSet;
|
import java.util.List;
|
public class VelocityUtils {
|
|
private static final String PROJECT_PATH = "main/java";
|
|
private static final String MYBATIS_PATH = "main/resources/mybatis";
|
|
private static final String TEMPLATES_PATH = "main/resources/templates";
|
|
public static VelocityContext prepareContext(GenTable genTable) {
|
String moduleName = genTable.getModuleName();
|
String businessName = genTable.getBusinessName();
|
String packageName = genTable.getPackageName();
|
String tplCategory = genTable.getTplCategory();
|
VelocityContext velocityContext = new VelocityContext();
|
velocityContext.put("tplCategory", genTable.getTplCategory());
|
velocityContext.put("tableName", genTable.getTableName());
|
velocityContext.put("functionName", genTable.getFunctionName());
|
velocityContext.put("ClassName", genTable.getClassName());
|
velocityContext.put("className", StringUtils.uncapitalize(genTable.getClassName()));
|
velocityContext.put("moduleName", genTable.getModuleName());
|
velocityContext.put("businessName", genTable.getBusinessName());
|
velocityContext.put("basePackage", getPackagePrefix(packageName));
|
velocityContext.put("packageName", packageName);
|
velocityContext.put("author", genTable.getFunctionAuthor());
|
velocityContext.put("datetime", DateUtils.getDate());
|
velocityContext.put("pkColumn", genTable.getPkColumn());
|
velocityContext.put("importList", getImportList(genTable.getColumns()));
|
velocityContext.put("permissionPrefix", getPermissionPrefix(moduleName, businessName));
|
velocityContext.put("columns", genTable.getColumns());
|
velocityContext.put("table", genTable);
|
if (GenConstants.TPL_TREE.equals(tplCategory)) {
|
setTreeVelocityContext(velocityContext, genTable);
|
}
|
return velocityContext;
|
}
|
public static void setTreeVelocityContext(VelocityContext context, GenTable genTable) {
|
String options = genTable.getOptions();
|
JSONObject paramsObj = JSONObject.parseObject(options);
|
String treeCode = getTreecode(paramsObj);
|
String treeParentCode = getTreeParentCode(paramsObj);
|
String treeName = getTreeName(paramsObj);
|
context.put("treeCode", treeCode);
|
context.put("treeParentCode", treeParentCode);
|
context.put("treeName", treeName);
|
context.put("expandColumn", getExpandColumn(genTable));
|
if (paramsObj.containsKey(GenConstants.TREE_PARENT_CODE)) {
|
context.put("tree_parent_code", paramsObj.getString(GenConstants.TREE_PARENT_CODE));
|
}
|
}
|
|
public static List<String> getTemplateList(String tplCategory) {
|
List<String> templates = new ArrayList<String>();
|
templates.add("vm/java/domain.java.vm");
|
templates.add("vm/java/mapper.java.vm");
|
templates.add("vm/java/service.java.vm");
|
templates.add("vm/java/serviceImpl.java.vm");
|
templates.add("vm/java/controller.java.vm");
|
templates.add("vm/xml/mapper.xml.vm");
|
if (GenConstants.TPL_CRUD.equals(tplCategory)) {
|
templates.add("vm/html/list.html.vm");
|
} else if (GenConstants.TPL_TREE.equals(tplCategory)) {
|
templates.add("vm/html/tree.html.vm");
|
templates.add("vm/html/list-tree.html.vm");
|
}
|
templates.add("vm/html/add.html.vm");
|
templates.add("vm/html/edit.html.vm");
|
templates.add("vm/sql/sql.vm");
|
return templates;
|
}
|
|
public static String getFileName(String template, GenTable genTable) {
|
|
String fileName = "";
|
|
String packageName = genTable.getPackageName();
|
|
String moduleName = genTable.getModuleName();
|
|
String className = genTable.getClassName();
|
|
String businessName = genTable.getBusinessName();
|
String javaPath = PROJECT_PATH + "/" + StringUtils.replace(packageName, ".", "/");
|
String mybatisPath = MYBATIS_PATH + "/" + moduleName;
|
String htmlPath = TEMPLATES_PATH + "/" + moduleName + "/" + businessName;
|
if (template.contains("domain.java.vm")) {
|
fileName = StringUtils.format("{}/{}/domain/{}.java", javaPath, moduleName, className);
|
} else if (template.contains("mapper.java.vm")) {
|
fileName = StringUtils.format("{}/{}/mapper/{}Mapper.java", javaPath, moduleName, className);
|
} else if (template.contains("service.java.vm")) {
|
fileName = StringUtils.format("{}/{}/service/I{}Service.java", javaPath, moduleName, className);
|
} else if (template.contains("serviceImpl.java.vm")) {
|
fileName = StringUtils.format("{}/{}/service/impl/{}ServiceImpl.java", javaPath, moduleName, className);
|
} else if (template.contains("controller.java.vm")) {
|
fileName = StringUtils.format("{}/{}/controller/{}Controller.java", javaPath, moduleName, className);
|
} else if (template.contains("mapper.xml.vm")) {
|
fileName = StringUtils.format("{}/{}Mapper.xml", mybatisPath, className);
|
} else if (template.contains("list.html.vm")) {
|
fileName = StringUtils.format("{}/{}.html", htmlPath, businessName);
|
} else if (template.contains("list-tree.html.vm")) {
|
fileName = StringUtils.format("{}/{}.html", htmlPath, businessName);
|
} else if (template.contains("tree.html.vm")) {
|
fileName = StringUtils.format("{}/tree.html", htmlPath);
|
} else if (template.contains("add.html.vm")) {
|
fileName = StringUtils.format("{}/add.html", htmlPath);
|
} else if (template.contains("edit.html.vm")) {
|
fileName = StringUtils.format("{}/edit.html", htmlPath);
|
} else if (template.contains("sql.vm")) {
|
fileName = businessName + "Menu.sql";
|
}
|
return fileName;
|
}
|
|
public static String getPackagePrefix(String packageName) {
|
int lastIndex = packageName.lastIndexOf(".");
|
String basePackage = StringUtils.substring(packageName, 0, lastIndex);
|
return basePackage;
|
}
|
|
public static HashSet<String> getImportList(List<GenTableColumn> columns) {
|
HashSet<String> importList = new HashSet<String>();
|
for (GenTableColumn column : columns) {
|
if (!column.isSuperColumn() && GenConstants.TYPE_DATE.equals(column.getJavaType())) {
|
importList.add("java.util.Date");
|
} else if (!column.isSuperColumn() && GenConstants.TYPE_BIGDECIMAL.equals(column.getJavaType())) {
|
importList.add("java.math.BigDecimal");
|
}
|
}
|
return importList;
|
}
|
|
public static String getPermissionPrefix(String moduleName, String businessName) {
|
return StringUtils.format("{}:{}", moduleName, businessName);
|
}
|
|
public static String getTreecode(JSONObject paramsObj) {
|
if (paramsObj.containsKey(GenConstants.TREE_CODE)) {
|
return StringUtils.toCamelCase(paramsObj.getString(GenConstants.TREE_CODE));
|
}
|
return "";
|
}
|
|
public static String getTreeParentCode(JSONObject paramsObj) {
|
if (paramsObj.containsKey(GenConstants.TREE_PARENT_CODE)) {
|
return StringUtils.toCamelCase(paramsObj.getString(GenConstants.TREE_PARENT_CODE));
|
}
|
return "";
|
}
|
|
public static String getTreeName(JSONObject paramsObj) {
|
if (paramsObj.containsKey(GenConstants.TREE_NAME)) {
|
return StringUtils.toCamelCase(paramsObj.getString(GenConstants.TREE_NAME));
|
}
|
return "";
|
}
|
|
public static int getExpandColumn(GenTable genTable) {
|
String options = genTable.getOptions();
|
JSONObject paramsObj = JSONObject.parseObject(options);
|
String treeName = paramsObj.getString(GenConstants.TREE_NAME);
|
int num = 0;
|
for (GenTableColumn column : genTable.getColumns()) {
|
if (column.isList()) {
|
num++;
|
String columnName = column.getColumnName();
|
if (columnName.equals(treeName)) {
|
break;
|
}
|
}
|
}
|
return num;
|
}
|
}
|