org.apdplat.platform.generator.ModelGenerator.java Source code

Java tutorial

Introduction

Here is the source code for org.apdplat.platform.generator.ModelGenerator.java

Source

/**
 * 
 * APDPlat - Application Product Development Platform
 * Copyright (c) 2013, ??, yang-shangchuan@qq.com
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

package org.apdplat.platform.generator;

import org.apdplat.module.system.service.PropertyHolder;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

/**
 *?
 * @author ??
 */
public class ModelGenerator extends Generator {
    private final static ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();

    private static Configuration freemarkerConfiguration = null;

    static {
        factory.setTemplateLoaderPath(PropertyHolder.getProperty("model.generator.freemarker.template"));
        try {
            freemarkerConfiguration = factory.createConfiguration();
        } catch (IOException | TemplateException e) {
            LOG.error("??", e);
        }
    }

    /**
     * ????EXCEL
     * ????????
     * @param module ???
     * @return ???
     */
    private static List<InputStream> getModelExcels(String module) {
        List<InputStream> ins = new ArrayList<>();
        try {
            String pattern = "classpath*:generator/" + module + "/*.xls";
            LOG.info("?" + pattern);
            Resource[] rs = resourcePatternResolver.getResources(pattern);
            LOG.info("???" + rs.length);

            for (Resource r : rs) {
                try {
                    InputStream in = r.getInputStream();
                    LOG.info("" + r.getFile().getAbsolutePath());
                    ins.add(in);
                } catch (Exception e) {
                    LOG.error("?MODEL", e);
                }
            }
        } catch (Exception e) {
            LOG.error("?MODEL", e);
        }
        return ins;
    }

    /**
     * ??
     * 
     * ??EXCEL???JAVA??
     * 
     * @param moduleProjectName ???????EXCEL???
     * @return JAVA??
     */
    public static List<ModelInfo> generate(String moduleProjectName) {
        List<ModelInfo> all = new ArrayList<>();
        List<InputStream> ins = getModelExcels(moduleProjectName);
        for (InputStream in : ins) {
            List<ModelInfo> modelInfos = readModelInfos(in);
            generate(modelInfos, moduleProjectName);
            all.addAll(modelInfos);
        }
        return all;
    }

    /**
     * JAVA?????
     * @param modelInfos JAVA??
     * @param moduleProjectName ???
     */
    private static void generate(List<ModelInfo> modelInfos, String moduleProjectName) {
        for (ModelInfo modelInfo : modelInfos) {
            generate(modelInfo, moduleProjectName);
            LOG.info("-----------------------------------------------------------------------------");
            LOG.info("" + modelInfo.getModelPackage());
            LOG.info("??" + modelInfo.getModelChinese());
            LOG.info("??" + modelInfo.getModelEnglish());

            for (Attr attr : modelInfo.getAttrs()) {
                LOG.info("        " + attr.toString());
            }
            LOG.info("-----------------------------------------------------------------------------");
        }
    }

    /**
     * JAVA?????
     * @param modelInfo JAVA??
     * @param moduleProjectName ???
     */
    private static void generate(ModelInfo modelInfo, String moduleProjectName) {
        String workspaceModuleBasePath = ModelGenerator.class.getResource("/").getFile().replace("target/classes/",
                "") + "../" + moduleProjectName + "/src/main/java/";

        String templateName = "model.ftl";

        LOG.info("?Model");
        LOG.info("workspaceModuleBasePath" + workspaceModuleBasePath);

        Map<String, Object> context = new HashMap<>();
        context.put("modelInfo", modelInfo);

        boolean result = false;
        try {
            Template template = freemarkerConfiguration.getTemplate(templateName, ENCODING);
            String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, context);
            String modelPath = modelInfo.getModelPackage().replace(".", "/");
            String modelName = modelInfo.getModelEnglish();
            result = saveFile(workspaceModuleBasePath, modelPath, modelName, content);
        } catch (IOException | TemplateException e) {
            LOG.error("?MODEL", e);
        }
        if (result) {
            LOG.info("Model??");
        } else {
            LOG.info("?Model");
        }
    }

    /**
     * ??
     * @param workspaceModuleBasePath ?
     * @param modelPath 
     * @param modelName ??
     * @param content 
     * @return ??
     */
    private static boolean saveFile(String workspaceModuleBasePath, String modelPath, String modelName,
            String content) {
        if (workspaceModuleBasePath == null) {
            return false;
        }
        File file = new File(workspaceModuleBasePath);
        file = new File(file, modelPath);
        if (!file.exists()) {
            file.mkdirs();
        }
        file = new File(file, modelName + ".java");
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                LOG.error("?MODEL", e);
            }
        } else {
            LOG.info("?? " + file.getAbsolutePath() + " ?");
            return false;
        }
        saveFile(file, content);
        return true;
    }

    /**
     * ??JAVA
     */
    final public static class ModelInfo {
        //??
        private String modelPackage;
        //????
        private String modelEnglish;
        //?
        private String modelChinese;
        //?Date
        private boolean hasDateTime;
        //?DicItem
        private boolean hasDicItem;
        private List<Attr> attrs = new ArrayList<>();

        public boolean isHasDateTime() {
            return hasDateTime;
        }

        public void setHasDateTime(boolean hasDateTime) {
            this.hasDateTime = hasDateTime;
        }

        public boolean isHasDicItem() {
            return hasDicItem;
        }

        public void setHasDicItem(boolean hasDicItem) {
            this.hasDicItem = hasDicItem;
        }

        public List<Attr> getAttrs() {
            return attrs;
        }

        public void addAttr(Attr attr) {
            this.attrs.add(attr);
        }

        public void removeAttr(Attr attr) {
            this.attrs.remove(attr);
        }

        public String getModelChinese() {
            return modelChinese;
        }

        public void setModelChinese(String modelChinese) {
            this.modelChinese = modelChinese;
        }

        public String getModelEnglish() {
            return modelEnglish;
        }

        public void setModelEnglish(String modelEnglish) {
            this.modelEnglish = modelEnglish;
        }

        public String getModelPackage() {
            return modelPackage;
        }

        public void setModelPackage(String modelPackage) {
            this.modelPackage = modelPackage;
        }

        public boolean isHasOneToMany() {
            for (Attr attr : attrs) {
                if (MapType.validType("OneToMany").equals(attr.map)) {
                    return true;
                }
            }
            return false;
        }

        public boolean isHasMap() {
            for (Attr attr : attrs) {
                if (!MapType.validType("None").equals(attr.map)) {
                    return true;
                }
                //DicItemDicItem
                if (attr.type.equals(AttrType.validType("DicItem"))) {
                    attr.map = "DicItem";
                    return true;
                }
            }
            return false;
        }
    }

    /**
     * ?
     */
    final public static class Attr {

        private String name;
        private String type;
        private int length;
        private String des;
        private boolean searchable = false;
        private String map = MapType.validType("None");
        private String attrRef = "None";
        private boolean renderIgnore = false;
        private String dic = DicType.validType("None");
        private String dicName;

        public String getDic() {
            return dic;
        }

        public void setDic(String dic) {
            this.dic = dic;
        }

        public String getDicName() {
            return dicName;
        }

        public void setDicName(String dicName) {
            this.dicName = dicName;
        }

        public boolean isRenderIgnore() {
            return renderIgnore;
        }

        public void setRenderIgnore(boolean renderIgnore) {
            this.renderIgnore = renderIgnore;
        }

        public boolean isSearchable() {
            return searchable;
        }

        public void setSearchable(boolean searchable) {
            this.searchable = searchable;
        }

        public String getDes() {
            return des;
        }

        public void setDes(String des) {
            this.des = des;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getMap() {
            return map;
        }

        public void setMap(String map) {
            this.map = map;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public int getLength() {
            return length;
        }

        public void setLength(int length) {
            this.length = length;
        }

        public String getAttrRef() {
            return attrRef;
        }

        public void setAttrRef(String attrRef) {
            this.attrRef = attrRef;
        }

        @Override
        public String toString() {
            return "Attr{" + "name=" + name + ", type=" + type + ", length=" + length + ", des=" + des
                    + ", searchable=" + searchable + ", map=" + map + ", attrRef=" + attrRef + ", renderIgnore="
                    + renderIgnore + ", dic=" + dic + ", dicName=" + dicName + '}';
        }
    }

    final public static class MapType {

        private static final Map<String, String> types = new HashMap<>();

        static {
            types.put("None", "None");
            types.put("ManyToOne", "ManyToOne");
            types.put("ManyToMany", "ManyToMany");
            types.put("OneToOne", "OneToOne");
            types.put("OneToMany", "OneToMany");
        }

        public static String validType(String typeName) {
            String type = types.get(typeName);
            if (type == null) {
                StringBuilder str = new StringBuilder();
                str.append("? ");
                for (String key : types.keySet()) {
                    str.append(key).append(",");
                }
                str = str.deleteCharAt(str.length() - 1);
                str.append(" ");
                throw new RuntimeException(
                        "?" + type + "??" + str.toString());
            }
            return type;
        }
    }

    final public static class DicType {

        private static final Map<String, String> types = new HashMap<>();

        static {
            types.put("None", "None");
            types.put("SimpleDic", "SimpleDic");
            types.put("TreeDic", "TreeDic");
        }

        public static String validType(String typeName) {
            String type = types.get(typeName);
            if (type == null) {
                StringBuilder str = new StringBuilder();
                str.append("? ");
                for (String key : types.keySet()) {
                    str.append(key).append(",");
                }
                str = str.deleteCharAt(str.length() - 1);
                str.append(" ");
                throw new RuntimeException(
                        "?" + type + "??" + str.toString());
            }
            return type;
        }
    }

    final public static class AttrType {

        private static final Map<String, String> types = new HashMap<>();

        static {
            types.put("String", "String");
            types.put("Integer", "Integer");
            types.put("Float", "Float");
            types.put("DicItem", "DicItem");
            types.put("Date", "Date");
            types.put("Time", "Time");
        }

        public static String validType(String typeName) {
            String type = types.get(typeName);
            if (type == null) {
                types.put(typeName, typeName);
                return typeName;
            }
            return type;
        }
    }

    /**
     * ???EXCEL
     * ?EXCELJAVA
     * @param inputStream ??EXCEL
     * @return JAVA
     */
    private static List<ModelInfo> readModelInfos(InputStream inputStream) {
        List<ModelInfo> models = new ArrayList<>();
        try {
            HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
            for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
                HSSFSheet sheet = workbook.getSheetAt(i);
                try {
                    HSSFRow row = sheet.getRow(2);
                    if (row == null) {
                        LOG.info("???" + sheet.getSheetName());
                        continue;
                    }
                    HSSFCell cell = row.getCell(1);
                    //??
                    String modelPackage = cell.getStringCellValue();
                    row = sheet.getRow(3);
                    cell = row.getCell(1);
                    //??
                    String modelEnglish = cell.getStringCellValue();
                    row = sheet.getRow(4);
                    cell = row.getCell(1);
                    //??
                    String modelChinese = cell.getStringCellValue();

                    ModelInfo modelInfo = new ModelInfo();
                    modelInfo.setModelPackage(modelPackage);
                    modelInfo.setModelEnglish(modelEnglish);
                    modelInfo.setModelChinese(modelChinese);

                    int rows = sheet.getPhysicalNumberOfRows();
                    //8??
                    for (int rowNumber = 7; rowNumber < rows; rowNumber++) {
                        HSSFRow oneRow = sheet.getRow(rowNumber);

                        if (oneRow == null) {
                            continue;
                        }
                        Attr attr = new Attr();
                        //??
                        HSSFCell oneCell = oneRow.getCell(0);
                        if (oneCell != null) {
                            String cellValue = oneCell.getStringCellValue();
                            if (cellValue != null && !"".equals(cellValue.trim())
                                    && !"null".equals(cellValue.trim().toLowerCase())) {
                                attr.setDes(cellValue);
                            } else {
                                continue;
                            }
                        }
                        //??
                        oneCell = oneRow.getCell(1);
                        if (oneCell != null) {
                            String cellValue = oneCell.getStringCellValue();
                            if (cellValue != null && !"".equals(cellValue.trim())
                                    && !"null".equals(cellValue.trim().toLowerCase())) {
                                attr.setName(cellValue);
                            } else {
                                continue;
                            }
                        }
                        //
                        oneCell = oneRow.getCell(2);
                        if (oneCell != null) {
                            String cellValue = oneCell.getStringCellValue();
                            if (cellValue != null && !"".equals(cellValue.trim())
                                    && !"null".equals(cellValue.trim().toLowerCase())) {
                                attr.setType(AttrType.validType(cellValue));
                            } else {
                                attr.setType(AttrType.validType("String"));
                            }
                        }
                        //?string
                        oneCell = oneRow.getCell(3);
                        if (oneCell != null) {
                            if (oneCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
                                String cellValue = oneCell.getStringCellValue();
                                if (cellValue != null && !"".equals(cellValue.trim())
                                        && !"null".equals(cellValue.trim().toLowerCase())) {
                                    try {
                                        int length = Integer.parseInt(cellValue);
                                        attr.setLength(length);
                                    } catch (Exception e) {
                                        LOG.error("?" + cellValue);
                                    }
                                }
                            }
                            if (oneCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
                                double length = oneCell.getNumericCellValue();
                                attr.setLength((int) length);
                            }
                        }
                        //??
                        oneCell = oneRow.getCell(4);
                        if (oneCell != null) {
                            boolean cellValue = oneCell.getBooleanCellValue();
                            attr.setSearchable(cellValue);
                        }
                        //??
                        oneCell = oneRow.getCell(5);
                        if (oneCell != null) {
                            boolean cellValue = oneCell.getBooleanCellValue();
                            attr.setRenderIgnore(cellValue);
                        }
                        //??
                        oneCell = oneRow.getCell(6);
                        if (oneCell != null) {
                            String cellValue = oneCell.getStringCellValue();
                            if (cellValue != null && !"".equals(cellValue.trim())
                                    && !"null".equals(cellValue.trim().toLowerCase())) {
                                attr.setDic(DicType.validType(cellValue));
                            }
                        }
                        //???
                        oneCell = oneRow.getCell(7);
                        if (oneCell != null) {
                            String cellValue = oneCell.getStringCellValue();
                            if (cellValue != null && !"".equals(cellValue.trim())
                                    && !"null".equals(cellValue.trim().toLowerCase())) {
                                attr.setDicName(cellValue);
                            }
                        }
                        //
                        oneCell = oneRow.getCell(8);
                        if (oneCell != null) {
                            String cellValue = oneCell.getStringCellValue();
                            if (cellValue != null && !"".equals(cellValue.trim())
                                    && !"null".equals(cellValue.trim().toLowerCase())) {
                                attr.setMap(MapType.validType(cellValue));
                            }
                        }
                        //
                        oneCell = oneRow.getCell(9);
                        if (oneCell != null) {
                            String cellValue = oneCell.getStringCellValue();
                            if (cellValue != null && !"".equals(cellValue.trim())
                                    && !"null".equals(cellValue.trim().toLowerCase())) {
                                attr.setAttrRef(cellValue);
                            }
                        }
                        if ("Date".equals(attr.getType()) || "Time".equals(attr.getType())) {
                            modelInfo.setHasDateTime(true);
                        }
                        if ("DicItem".equals(attr.getType())) {
                            modelInfo.setHasDicItem(true);
                            //DicItemdicSimpleDic
                            if (!"SimpleDic".equals(attr.getDic()) && !"TreeDic".equals(attr.getDic())) {
                                attr.setDic("SimpleDic");
                            }
                            if (attr.getDicName() == null || "".equals(attr.getDicName())) {
                                attr.setDicName(attr.getName());
                            }
                        }
                        modelInfo.addAttr(attr);
                    }
                    models.add(modelInfo);
                } catch (Exception e) {
                    LOG.error("?:" + sheet.getSheetName() + " ", e);
                }
            }
        } catch (IOException e) {
            LOG.error("?MODEL", e);
        }
        return models;
    }
}