package org.wyg.gen;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wyg.template.PathInfo;
import org.wyg.template.Table;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class GenService {
private static Logger logger = LoggerFactory.getLogger(GenService.class);
private Configuration cfg = null;
public GenService() {
if (cfg == null) {
try {
String classPath = this.getClass().getResource("/").getPath();
cfg = new Configuration();
cfg.setDirectoryForTemplateLoading(new File(classPath));
cfg.setObjectWrapper(new DefaultObjectWrapper());
cfg.setSetting(Configuration.CACHE_STORAGE_KEY,
"strong:20, soft:250");
cfg.setEncoding(Locale.CHINESE, "UTF-8");
logger.info("freemarker初始化完成");
} catch (IOException e) {
logger.error("", e);
} catch (TemplateException e) {
logger.error("", e);
}
}
}
// 生成Mapper
public void genMapper(List<Table> list, PathInfo pathInfo)
throws IOException, UnsupportedEncodingException,
FileNotFoundException, TemplateException {
File file = new File(pathInfo.getSrcPath() + pathInfo.getMapperPkg());
for (Table table : list) {
logger.info("生成表{}的mapper", table);
Map<String, Object> root = new HashMap<String, Object>();
root.put("table", table);
root.put("mapperPkg", pathInfo.getMapperPkg().replaceAll("/", "."));
Template temp = cfg.getTemplate("mapper.ftl");
String fileName = file.getAbsolutePath() + File.separator
+ table.getModel() + ".xml";
logger.info("文件绝对路径:{}", fileName);
OutputStreamWriter out = new OutputStreamWriter(
new FileOutputStream(fileName), "UTF-8");
temp.process(root, out);
out.flush();
out.close();
logger.info("文件:{},写结束", fileName);
}
}
// 生成Model源码
public void genModel(List<Table> list, PathInfo pathInfo)
throws IOException, UnsupportedEncodingException,
FileNotFoundException, TemplateException {
File file = new File(pathInfo.getSrcPath() + pathInfo.getModelPkg());
for (Table table : list) {
logger.info("生成表{}的model", table);
Map<String, Object> root = new HashMap<String, Object>();
root.put("table", table);
root.put("modelPkg", pathInfo.getModelPkg().replaceAll("/", "."));
Template temp = cfg.getTemplate("model.ftl");
String fileName = file.getAbsolutePath() + File.separator
+ table.getModel() + ".java";
logger.info("文件绝对路径:{}", fileName);
OutputStreamWriter out = new OutputStreamWriter(
new FileOutputStream(fileName), "UTF-8");
temp.process(root, out);
out.flush();
out.close();
logger.info("文件:{},写结束", fileName);
}
}
// 生成Service源码
public void genService(List<Table> list, PathInfo pathInfo)
throws IOException, UnsupportedEncodingException,
FileNotFoundException, TemplateException {
File file = new File(pathInfo.getSrcPath() + pathInfo.getServicePkg());
if ((!file.exists() && file.mkdirs()) || file.exists()) {
for (Table table : list) {
Map<String, Object> root = new HashMap<String, Object>();
root.put("table", table);
root.put("servicePkg",
pathInfo.getServicePkg().replaceAll("/", "."));
root.put("modelPkg", pathInfo.getModelPkg()
.replaceAll("/", "."));
root.put("mapperPkg",
pathInfo.getMapperPkg().replaceAll("/", "."));
Template temp = cfg.getTemplate("service.ftl");
String fileName = file.getAbsolutePath() + File.separator
+ table.getModel() + "Service" + ".java";
logger.info("文件绝对路径:{}", fileName);
OutputStreamWriter out = new OutputStreamWriter(
new FileOutputStream(fileName), "UTF-8");
temp.process(root, out);
out.flush();
out.close();
logger.info("文件:{},写结束", fileName);
}
}
}
// 生成Controller源码
public void genController(List<Table> list, PathInfo pathInfo)
throws IOException, UnsupportedEncodingException,
FileNotFoundException, TemplateException {
File file = new File(pathInfo.getSrcPath()
+ pathInfo.getControllerPkg());
if ((!file.exists() && file.mkdirs()) || file.exists()) {
for (Table table : list) {
Map<String, Object> root = new HashMap<String, Object>();
root.put("table", table);
root.put("servicePkg",
pathInfo.getServicePkg().replaceAll("/", "."));
root.put("modelPkg", pathInfo.getModelPkg()
.replaceAll("/", "."));
root.put("mapperPkg",
pathInfo.getMapperPkg().replaceAll("/", "."));
root.put("controllerPkg", pathInfo.getControllerPkg()
.replaceAll("/", "."));
Template temp = cfg.getTemplate("controller.ftl");
String fileName = file.getAbsolutePath() + File.separator
+ table.getModel() + "Controller" + ".java";
logger.info("文件绝对路径:{}", fileName);
OutputStreamWriter out = new OutputStreamWriter(
new FileOutputStream(fileName), "UTF-8");
temp.process(root, out);
out.flush();
out.close();
logger.info("文件:{},写结束", fileName);
}
}
}
// form 生成
public void genForm(List<Table> list, PathInfo pathInfo)
throws IOException, UnsupportedEncodingException,
FileNotFoundException, TemplateException {
for (Table table : list) {
File file = new File(pathInfo.getJspSrcPath() + "/"
+ pathInfo.getJspPath() + "/" + table.getName());
if ((!file.exists() && file.mkdirs()) || file.exists()) {
Map<String, Object> root = new HashMap<String, Object>();
root.put("table", table);
root.put("servicePkg",
pathInfo.getServicePkg().replaceAll("/", "."));
root.put("modelPkg", pathInfo.getModelPkg()
.replaceAll("/", "."));
root.put("mapperPkg",
pathInfo.getMapperPkg().replaceAll("/", "."));
root.put("controllerPkg", pathInfo.getControllerPkg()
.replaceAll("/", "."));
Template temp = cfg.getTemplate("form.ftl");
String fileName = file.getAbsolutePath() + File.separator
+ table.getName() + "-form" + ".jsp";
logger.info("文件绝对路径:{}", fileName);
OutputStreamWriter out = new OutputStreamWriter(
new FileOutputStream(fileName), "UTF-8");
temp.process(root, out);
logger.info("生成内容输出到控制台:");
temp.process(root, new OutputStreamWriter(System.out));
out.flush();
out.close();
logger.info("文件:{},写结束", fileName);
}
}
}
// 信息展示 detail 生成
public void genDetail(List<Table> list, PathInfo pathInfo)
throws IOException, UnsupportedEncodingException,
FileNotFoundException, TemplateException {
for (Table table : list) {
File file = new File(pathInfo.getJspSrcPath() + "/"
+ pathInfo.getJspPath() + "/" + table.getName());
if ((!file.exists() && file.mkdirs()) || file.exists()) {
Map<String, Object> root = new HashMap<String, Object>();
root.put("table", table);
root.put("servicePkg",
pathInfo.getServicePkg().replaceAll("/", "."));
root.put("modelPkg", pathInfo.getModelPkg()
.replaceAll("/", "."));
root.put("mapperPkg",
pathInfo.getMapperPkg().replaceAll("/", "."));
root.put("controllerPkg", pathInfo.getControllerPkg()
.replaceAll("/", "."));
Template temp = cfg.getTemplate("detail.ftl");
String fileName = file.getAbsolutePath() + File.separator
+ table.getName() + "-detail" + ".jsp";
logger.info("文件绝对路径:{}", fileName);
OutputStreamWriter out = new OutputStreamWriter(
new FileOutputStream(fileName), "UTF-8");
temp.process(root, out);
logge