All Downloads are FREE. Search and download functionalities are using the official Maven repository.

shz.generator.DefaultGenerator Maven / Gradle / Ivy

package shz.generator;

import shz.core.*;
import shz.core.cl.ClassLoaderHelp;
import shz.generator.module.*;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.text.Collator;
import java.util.*;

public abstract class DefaultGenerator extends Generator {
    protected final Properties properties;

    protected DefaultGenerator(Properties properties) {
        this.properties = properties;
    }

    @Override
    protected final Connection conn() {
        Properties prop = new Properties();
        prop.putAll(properties);

        prop.put("useInformationSchema", "true");
        prop.put("remarksReporting", "true");

        String url = prop.getProperty("url");
        try {
            return DriverManager.getConnection(url, prop);
        } catch (SQLException e) {
            ClassLoaderHelp.load(prop.getProperty("driverClassName"));
        }

        try {
            return DriverManager.getConnection(url, prop);
        } catch (SQLException e) {
            throw PRException.of(e);
        }
    }

    /**
     * 拼接java文件内容
     */
    private void append(Tgp tgp, GenInfo genInfo, List data, Class cls) {
        AppendData appendData = BeanHelp.of(cls);
        appendData.generator = this;

        data.add("package " + genInfo.packageName + ";\n");

        Set imports = new HashSet<>();
        List annotations = appendData.annotations(tgp, imports);
        String classStr = appendData.cls(tgp, imports);
        List content = appendData.content(tgp, imports);

        if (!imports.isEmpty()) {
            List sort = sort(imports);
            sort.set(sort.size() - 1, sort.get(sort.size() - 1) + "\n");
            data.addAll(sort);
        }

        String comment = appendData.comment(tgp.table);
        if (NullHelp.nonBlank(comment)) data.add(comment);

        data.addAll(annotations);
        data.add(classStr + " {");
        data.addAll(content);
        data.add("}");
    }

    /**
     * 对imports行进行排序
     */
    private List sort(Collection collection) {
        List list = ToList.explicitCollect(collection.stream(), collection.size());
        list.sort(Collator.getInstance(Locale.ENGLISH));
        return list;
    }

    @Override
    protected final void appendEntityData(Tgp tgp, List data) {
        append(tgp, tgp.entityGenInfo, data, appendEntityCls());
    }

    protected Class appendEntityCls() {
        return AppendEntityData.class;
    }

    @Override
    protected final void appendVoData(Tgp tgp, List data) {
        append(tgp, tgp.voGenInfo, data, appendVoCls());
    }

    protected Class appendVoCls() {
        return AppendVoData.class;
    }

    @Override
    protected final void appendApiData(Tgp tgp, List data) {
        append(tgp, tgp.apiGenInfo, data, appendApiCls());
    }

    protected Class appendApiCls() {
        return AppendApiData.class;
    }

    @Override
    protected final void appendServiceData(Tgp tgp, List data) {
        append(tgp, tgp.serviceGenInfo, data, appendServiceCls());
    }

    protected Class appendServiceCls() {
        return AppendServiceData.class;
    }

    @Override
    protected final void appendServiceImplData(Tgp tgp, List data) {
        append(tgp, tgp.serviceImplGenInfo, data, appendServiceImplCls());
    }

    protected Class appendServiceImplCls() {
        return AppendServiceImplData.class;
    }

    @Override
    protected final void appendRepositoryData(Tgp tgp, List data) {
        append(tgp, tgp.repositoryGenInfo, data, appendRepositoryCls());
    }

    protected Class appendRepositoryCls() {
        return AppendRepositoryData.class;
    }

    @Override
    protected final void appendControllerData(Tgp tgp, List data) {
        append(tgp, tgp.controllerGenInfo, data, appendControllerCls());
    }

    protected Class appendControllerCls() {
        return AppendControllerData.class;
    }

    @Override
    protected final void appendApiClientData(Tgp tgp, List data) {
        append(tgp, tgp.apiClientGenInfo, data, appendApiClientCls());
    }

    protected Class appendApiClientCls() {
        return AppendApiClientData.class;
    }

    @Override
    protected final void appendApiClientFallbackData(Tgp tgp, List data) {
        append(tgp, tgp.apiClientFallbackGenInfo, data, appendApiClientFallbackCls());
    }

    protected Class appendApiClientFallbackCls() {
        return AppendApiClientFallbackData.class;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy