com.hesine.manager.generate.Generate.java Source code

Java tutorial

Introduction

Here is the source code for com.hesine.manager.generate.Generate.java

Source

/**
 * Copyright &copy; 2012-2013 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.hesine.manager.generate;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hesine.manager.generate.db.DBOperator;
import com.hesine.manager.generate.file.FileGenUtils;
import com.hesine.manager.generate.property.SystemConfig;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.DefaultResourceLoader;

import com.google.common.collect.Maps;
import com.hesine.manager.utils.DateUtils;
import com.hesine.manager.utils.FileUtils;
import com.hesine.manager.utils.FreeMarkers;

import freemarker.template.Configuration;
import freemarker.template.Template;

/**
 * ??
 * @author ThinkGem
 * @version 2013-06-21
 */
public class Generate {

    private static Logger logger = LoggerFactory.getLogger(Generate.class);

    public static void execute(File curProjectPath) {
        // ==========  ?? ====================

        // ??????
        // ?{packageName}/{moduleName}/{dao,entity,service,web}/{subModuleName}/{className}

        // packageName ????applicationContext.xmlsrping-mvc.xml?base-package?packagesToScan?4?
        String packageName = "com.hesine.manager";

        //      String moduleName = "function";         // ???sys
        String moduleName = ""; // ???sys
        String tableName = "tb_product"; // ???sys
        String subModuleName = ""; // ?????
        String className = "product"; // ??product
        String classAuthor = "Jason"; // ThinkGem
        String functionName = "?"; // ??
        List<Map<String, String>> fileds = new ArrayList<Map<String, String>>();
        // map
        Map<String, String> dataName = new HashMap<String, String>();
        dataName.put("dataType", "String");
        dataName.put("name", "name");
        dataName.put("methodName", "Name");
        dataName.put("comment", "??");
        dataName.put("dbName", "name");

        Map<String, String> dataDesc = new HashMap<String, String>();
        dataDesc.put("dataType", "String");
        dataDesc.put("name", "description");
        dataDesc.put("methodName", "Description");
        dataDesc.put("comment", "??");
        dataDesc.put("dbName", "description");

        fileds.add(dataName);
        fileds.add(dataDesc);

        // ???
        Boolean isEnable = true;

        // ==========  ?? ====================

        if (!isEnable) {
            logger.error("????isEnable = true");
            return;
        }

        //        StringUtils.isBlank(moduleName) ||
        if (StringUtils.isBlank(packageName) || StringUtils.isBlank(className)
                || StringUtils.isBlank(functionName)) {
            logger.error("??????????????");
            return;
        }

        // ?
        String separator = File.separator;

        // ?
        File projectPath = curProjectPath;
        while (!new File(projectPath.getPath() + separator + "src" + separator + "main").exists()) {
            projectPath = projectPath.getParentFile();
        }
        logger.info("Project Path: {}", projectPath);

        // ?
        String tplPath = StringUtils.replace(projectPath + "/src/main/java/com/hesine/manager/generate/template",
                "/", separator);
        logger.info("Template Path: {}", tplPath);

        // Java
        String javaPath = StringUtils.replaceEach(
                projectPath + "/src/main/java/" + StringUtils.lowerCase(packageName), new String[] { "/", "." },
                new String[] { separator, separator });
        logger.info("Java Path: {}", javaPath);

        // Xml
        String xmlPath = StringUtils.replace(projectPath + "/src/main/resources/mybatis", "/", separator);
        logger.info("Xml Path: {}", xmlPath);

        // 
        String viewPath = StringUtils.replace(projectPath + "/src/main/webapp/WEB-INF/views", "/", separator);
        logger.info("View Path: {}", viewPath);

        // ???
        Map<String, Object> model = Maps.newHashMap();
        model.put("packageName", StringUtils.lowerCase(packageName));
        model.put("moduleName", StringUtils.lowerCase(moduleName));
        model.put("subModuleName",
                StringUtils.isNotBlank(subModuleName) ? "." + StringUtils.lowerCase(subModuleName) : "");
        model.put("className", StringUtils.uncapitalize(className));
        model.put("ClassName", StringUtils.capitalize(className));
        model.put("classAuthor", StringUtils.isNotBlank(classAuthor) ? classAuthor : "Generate Tools");
        model.put("classVersion", DateUtils.getDate());
        model.put("functionName", functionName);
        //      model.put("tableName", model.get("moduleName")+(StringUtils.isNotBlank(subModuleName)
        //            ?"_"+StringUtils.lowerCase(subModuleName):"")+"_"+model.get("className"));
        model.put("tableName", tableName);

        model.put("urlPrefix",
                model.get("moduleName")
                        + (StringUtils.isNotBlank(subModuleName) ? "/" + StringUtils.lowerCase(subModuleName) : "")
                        + "/" + model.get("className"));
        model.put("viewPrefix", //StringUtils.substringAfterLast(model.get("packageName"),".")+"/"+
                model.get("urlPrefix"));
        model.put("permissionPrefix",
                model.get("moduleName")
                        + (StringUtils.isNotBlank(subModuleName) ? ":" + StringUtils.lowerCase(subModuleName) : "")
                        + ":" + model.get("className"));
        model.put("fileds", fileds);

        //        // ? Entity
        //        FileGenUtils.generateEntity(tplPath, javaPath, model);
        //
        //        // ? sqlMap
        //        FileGenUtils.generateSqlMap(tplPath, xmlPath, model);
        //
        //        // ? Model
        //        FileGenUtils.generateModel(tplPath, javaPath, model);
        //
        //        // ? Dao
        //        FileGenUtils.generateDao(tplPath, javaPath, model);
        //
        //        // ? Service
        //        FileGenUtils.generateService(tplPath, javaPath, model);
        //
        //        // ? ServiceImpl
        //        FileGenUtils.generateServiceImpl(tplPath, javaPath, model);
        //
        //        // ? Controller
        //        FileGenUtils.generateController(tplPath, javaPath, model);

        // ? add.ftl
        FileGenUtils.generateAddFtl(tplPath, viewPath, model);

        // ? edit.ftl
        FileGenUtils.generateEditFtl(tplPath, viewPath, model);

        // ? list.ftl
        FileGenUtils.generateListFtl(tplPath, viewPath, model);

        logger.info("Generate Success.");
    }

    public static void execute(File curProjectPath, String packageName, String moduleName,
            Map<String, String> table, List<Map<String, String>> fileds) {
        // ==========  ?? ====================

        // ??????
        // ?{packageName}/{moduleName}/{dao,entity,service,web}/{subModuleName}/{className}

        // packageName ????applicationContext.xmlsrping-mvc.xml?base-package?packagesToScan?4?
        //        String packageName = "com.hesine.manager";

        //      String moduleName = "function";         // ???sys
        //        String moduleName = "";         // ???sys
        //        String tableName = "tb_product";         // ???sys
        //        String subModuleName = "";            // ?????
        //        String className = "product";         // ??product
        //        String classAuthor = "Jason";      // ThinkGem
        //        String functionName = "?";         // ??
        String tableName = table.get("tableName"); // ???sys
        String subModuleName = ""; // ?????
        String className = table.get("className"); // ??product
        String classAuthor = "Jason"; // ThinkGem
        String functionName = table.get("comment"); // ??

        // ???
        Boolean isEnable = true;

        // ==========  ?? ====================

        if (!isEnable) {
            logger.error("????isEnable = true");
            return;
        }

        //        StringUtils.isBlank(moduleName) ||
        if (StringUtils.isBlank(packageName) || StringUtils.isBlank(className)
                || StringUtils.isBlank(functionName)) {
            logger.error("??????????????");
            return;
        }

        // ?
        String separator = File.separator;

        // ?
        File projectPath = curProjectPath;
        //        while(!new File(projectPath.getPath()+separator+"src"+separator+"main").exists()){
        //            projectPath = projectPath.getParentFile();
        //        }
        logger.info("Project Path: {}", projectPath);

        // ?
        String tmpProjectPath = "/Users/zhanghongbing/Workspaces/Projects/hesine-projects/github-framework/hesine-demo/hesine-portal";
        String tplPath = StringUtils.replace(tmpProjectPath + "/src/main/java/com/hesine/manager/generate/template",
                "/", separator);
        //        String tplPath = StringUtils.replace(projectPath+"/src/main/java/com/hesine/manager/generate/template", "/", separator);
        logger.info("Template Path: {}", tplPath);

        // Java
        String javaPath = StringUtils.replaceEach(
                projectPath + "/src/main/java/" + StringUtils.lowerCase(packageName), new String[] { "/", "." },
                new String[] { separator, separator });
        logger.info("Java Path: {}", javaPath);

        // Xml
        String xmlPath = StringUtils.replace(projectPath + "/src/main/resources/mybatis", "/", separator);
        logger.info("Xml Path: {}", xmlPath);

        // 
        String viewPath = StringUtils.replace(projectPath + "/src/main/webapp/WEB-INF/views", "/", separator);
        logger.info("View Path: {}", viewPath);

        // ???
        Map<String, Object> model = Maps.newHashMap();
        model.put("packageName", StringUtils.lowerCase(packageName));
        model.put("moduleName", StringUtils.lowerCase(moduleName));
        model.put("subModuleName",
                StringUtils.isNotBlank(subModuleName) ? "." + StringUtils.lowerCase(subModuleName) : "");
        model.put("className", StringUtils.uncapitalize(className));
        model.put("ClassName", StringUtils.capitalize(className));
        model.put("classAuthor", StringUtils.isNotBlank(classAuthor) ? classAuthor : "Generate Tools");
        model.put("classVersion", DateUtils.getDate());
        model.put("functionName", functionName);
        //      model.put("tableName", model.get("moduleName")+(StringUtils.isNotBlank(subModuleName)
        //            ?"_"+StringUtils.lowerCase(subModuleName):"")+"_"+model.get("className"));
        model.put("tableName", tableName);

        model.put("urlPrefix",
                model.get("moduleName")
                        + (StringUtils.isNotBlank(subModuleName) ? "/" + StringUtils.lowerCase(subModuleName) : "")
                        + "/" + model.get("className"));
        model.put("viewPrefix", //StringUtils.substringAfterLast(model.get("packageName"),".")+"/"+
                model.get("urlPrefix"));
        model.put("permissionPrefix",
                model.get("moduleName")
                        + (StringUtils.isNotBlank(subModuleName) ? ":" + StringUtils.lowerCase(subModuleName) : "")
                        + ":" + model.get("className"));
        model.put("fileds", fileds);

        // ? Entity
        FileGenUtils.generateEntity(tplPath, javaPath, model);

        // ? sqlMap
        FileGenUtils.generateSqlMap(tplPath, xmlPath, model);

        // ? Model
        FileGenUtils.generateModel(tplPath, javaPath, model);

        // ? Dao
        FileGenUtils.generateDao(tplPath, javaPath, model);

        // ? Service
        FileGenUtils.generateService(tplPath, javaPath, model);

        // ? ServiceImpl
        FileGenUtils.generateServiceImpl(tplPath, javaPath, model);

        // ? Controller
        FileGenUtils.generateController(tplPath, javaPath, model);
        //
        //        // ? add.ftl
        //        FileGenUtils.generateAddFtl(tplPath, viewPath, model);
        //
        //        // ? edit.ftl
        //        FileGenUtils.generateEditFtl(tplPath, viewPath, model);

        //        // ? list.ftl
        //        FileGenUtils.generateListFtl(tplPath, viewPath, model);

        logger.info("Generate Success.");
    }

    public static void main(String[] args) throws Exception {
        File projectPath = new DefaultResourceLoader().getResource("").getFile();
        //        Generate.execute(projectPath);

        // 
        String packageName = "com.hesine.manager";
        String moduleName = "";

        // ?
        List<Map<String, String>> list = DBOperator.getTables("tb_");
        for (Map<String, String> a : list) {
            System.out.println(a.toString());
            if (a.get("tableName").equals("tb_userinfo")) {
                List<Map<String, String>> listTC = DBOperator.getTableColumns(a.get("tableName"));
                // ?
                for (Map<String, String> b : listTC) {
                    System.out.println(b.toString());
                    for (String key : b.keySet()) {
                        System.out.println(key + " : " + b.get(key));
                    }

                }
                Generate.execute(projectPath, packageName, moduleName, a, listTC);
                break;
            }
        }

    }

}