com.aistor.generate.Generate.java Source code

Java tutorial

Introduction

Here is the source code for com.aistor.generate.Generate.java

Source

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

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Map;

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.aistor.common.utils.DateUtils;
import com.aistor.common.utils.FileUtils;
import com.aistor.common.utils.FreeMarkers;

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

/**
 * ??
 * @author Zaric
 * @version 2013-03-15
 */
public class Generate {

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

    public static void main(String[] args) throws Exception {

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

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

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

        String moduleName = "factory"; // ???sys
        String subModuleName = ""; // ????? 
        String className = "product"; // ??user
        String classAuthor = "Zaric"; // Zaric
        String functionName = "?"; // ??

        // ???
        Boolean isEnable = false;

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

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

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

        // ?
        String separator = File.separator;
        String classPath = new DefaultResourceLoader().getResource("").getFile().getPath();
        String templatePath = classPath.replace(
                separator + "webapp" + separator + "WEB-INF" + separator + "classes",
                separator + "java" + separator + "com" + separator + "aistor" + separator + "modules");
        String javaPath = classPath.replace(separator + "webapp" + separator + "WEB-INF" + separator + "classes",
                separator + "java" + separator + (StringUtils.lowerCase(packageName)).replace(".", separator));
        String viewPath = classPath.replace(separator + "classes", separator + "views");

        // ???
        Configuration cfg = new Configuration();
        cfg.setDirectoryForTemplateLoading(
                new File(templatePath.replace("modules", "generate" + separator + "template")));

        // ???
        Map<String, String> 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("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"));

        // ? Entity
        Template template = cfg.getTemplate("entity.ftl");
        String content = FreeMarkers.renderTemplate(template, model);
        String filePath = javaPath + separator + model.get("moduleName") + separator + "entity" + separator
                + StringUtils.lowerCase(subModuleName) + separator + model.get("ClassName") + ".java";
        writeFile(content, filePath);
        logger.info(filePath);

        // ? Dao
        template = cfg.getTemplate("dao.ftl");
        content = FreeMarkers.renderTemplate(template, model);
        filePath = javaPath + separator + model.get("moduleName") + separator + "dao" + separator
                + StringUtils.lowerCase(subModuleName) + separator + model.get("ClassName") + "Dao.java";
        writeFile(content, filePath);
        logger.info(filePath);

        // ? Service
        template = cfg.getTemplate("service.ftl");
        content = FreeMarkers.renderTemplate(template, model);
        filePath = javaPath + separator + model.get("moduleName") + separator + "service" + separator
                + StringUtils.lowerCase(subModuleName) + separator + model.get("ClassName") + "Service.java";
        writeFile(content, filePath);
        logger.info(filePath);

        // ? Controller
        template = cfg.getTemplate("controller.ftl");
        content = FreeMarkers.renderTemplate(template, model);
        filePath = javaPath + separator + model.get("moduleName") + separator + "web" + separator
                + StringUtils.lowerCase(subModuleName) + separator + model.get("ClassName") + "Controller.java";
        writeFile(content, filePath);
        logger.info(filePath);

        // ? ViewForm
        template = cfg.getTemplate("viewForm.ftl");
        content = FreeMarkers.renderTemplate(template, model);
        filePath = viewPath + separator + StringUtils.substringAfterLast(model.get("packageName"), ".") + separator
                + model.get("moduleName") + separator + StringUtils.lowerCase(subModuleName) + separator
                + model.get("className") + "Form.jsp";
        writeFile(content, filePath);
        logger.info(filePath);

        // ? ViewList
        template = cfg.getTemplate("viewList.ftl");
        content = FreeMarkers.renderTemplate(template, model);
        filePath = viewPath + separator + StringUtils.substringAfterLast(model.get("packageName"), ".") + separator
                + model.get("moduleName") + separator + StringUtils.lowerCase(subModuleName) + separator
                + model.get("className") + "List.jsp";
        writeFile(content, filePath);
        logger.info(filePath);

        logger.info("????");
    }

    /**
     * 
     * @param content
     * @param filePath
     */
    public static void writeFile(String content, String filePath) {
        try {
            if (FileUtils.createFile(filePath)) {
                FileWriter fileWriter = new FileWriter(filePath, true);
                BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
                bufferedWriter.write(content);
                bufferedWriter.close();
                fileWriter.close();
            } else {
                logger.info("??");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}