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

com.lq.cli.SpringBootCli Maven / Gradle / Ivy

package com.lq.cli;


import com.lq.cli.comment.CliConfig;
import com.lq.cli.comment.CheckUtil;
import com.lq.cli.comment.TaskArgs;
import com.lq.cli.init.CreateApplicationXmlTask;
import com.lq.cli.init.CreateConfigTask;
import com.lq.cli.init.CreatePomXmlTask;
import com.lq.comment.jdbc.JdbcConfig;
import com.lq.comment.jdbc.JdbcUtil;
import com.lq.comment.jdbc.TableInfo;
import com.lq.cli.mybatis.*;
import com.lq.cli.other.CreateRedisConfigTask;

import java.io.File;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;

public class SpringBootCli {

    private static SpringBootCli instance = null;
    private final TaskArgs taskArgs;
    private final ExecutorService executorService;

    private SpringBootCli(Class aClass, JdbcConfig jdbcConfig, CliConfig cliConfig) {
        taskArgs = new TaskArgs();
        taskArgs.jdbcConfig = jdbcConfig;
        taskArgs.cliConfig = cliConfig == null ? new CliConfig.Builder().build() : cliConfig;
        String path = new File(aClass.getResource("").getPath()).getPath();
        taskArgs.projectPath = path.substring(0, path.indexOf(File.separator + "target" + File.separator));
        String filePath = taskArgs.projectPath + File.separator + "src" + File.separator + "main" + File.separator + "java" +
                File.separator + new File(aClass.getName()).getPath().replace(".", File.separator) + ".java";
        taskArgs.rootPackagePath = filePath.replace(aClass.getSimpleName() + ".java", "");
        taskArgs.packageName = aClass.getPackage().getName();
        int threadNumber = Math.min(Runtime.getRuntime().availableProcessors() + 1, 4);
        executorService = Executors.newFixedThreadPool(threadNumber);
    }

    public static SpringBootCli getInstance(Class clazz, String jdbcUserName, String jdbcPwd, String dbName) {
        return getInstance(clazz, new JdbcConfig.Builder(jdbcUserName, jdbcPwd, dbName).build(), new CliConfig.Builder().build());
    }

    public static SpringBootCli getInstance(Class clazz, JdbcConfig jdbcConfig) {
        return getInstance(clazz, jdbcConfig, new CliConfig.Builder().build());
    }


    public static SpringBootCli getInstance(Class clazz, JdbcConfig jdbcConfig, CliConfig cliConfig) {
        if (instance == null) {
            synchronized (SpringBootCli.class) {
                if (instance == null) {
                    instance = new SpringBootCli(clazz, jdbcConfig, cliConfig);
                }
            }
        }
        return instance;
    }


    public void initSpringBoot(String... filterMybatisTableNames) throws Exception {
        try {
            JdbcUtil.checkMysqlConnectorJar();
            executorService.submit(new CreatePomXmlTask(taskArgs));
            executorService.submit(new CreateConfigTask(taskArgs));
            executorService.submit(new CreateApplicationXmlTask(taskArgs));
            Future> listFuture = executorService.submit(new CreateJavaBeanTask(taskArgs));
            List tableInfos = listFuture.get();
            if (tableInfos != null) {
                if (filterMybatisTableNames != null && filterMybatisTableNames.length > 0) {
                    List tableNames = Arrays.asList(filterMybatisTableNames);
                    tableInfos = tableInfos.stream()
                            .filter(tableInfo -> !tableNames.contains(tableInfo.getTableName()))
                            .collect(Collectors.toList());
                }
                if (taskArgs.cliConfig.isUseRedis()) {
                    executorService.submit(new CreateRedisConfigTask(taskArgs, tableInfos));
                }
                executorService.submit(new CreateMapperTask(taskArgs, tableInfos));
                executorService.submit(new CreateServiceTask(taskArgs, tableInfos));
                executorService.submit(new CreateMapperXmlTask(taskArgs, tableInfos));
                if (taskArgs.cliConfig.isGenerateController()) {
                    executorService.submit(new CreateControllerTask(taskArgs, tableInfos));
                }
            }
        } finally {
            executorService.shutdown();
        }
    }

    public void generateMybatisByTableName(boolean useRedis,String tableName, String... tableNames) throws Exception {
        taskArgs.cliConfig.setUseRedis(useRedis);
        if (taskArgs.cliConfig.isUseRedis()) {
            new CheckUtil(taskArgs).checkRedis();
        }
        generateFilterByTableName(tableInfo -> {
            List infos = Collections.singletonList(tableInfo);
            executorService.submit(new CreateMapperTask(taskArgs, infos));
            executorService.submit(new CreateServiceTask(taskArgs, infos));
            executorService.submit(new CreateMapperXmlTask(taskArgs, infos));
            if (taskArgs.cliConfig.isUseRedis()) {
                executorService.submit(new CreateRedisConfigTask(taskArgs, infos));
            }
        }, tableName, tableNames);
    }

    public void generateControllerByTableName(String tableName, String... tableNames) throws Exception {
        generateFilterByTableName(tableInfo ->
                executorService.submit(new CreateControllerTask(taskArgs, Collections.singletonList(tableInfo))), tableName, tableNames);
    }


    private void generateFilterByTableName(Consumer consumer, String tableName, String... tableNames) throws ExecutionException, InterruptedException {
        try {
            JdbcUtil.checkMysqlConnectorJar();
            Future> listFuture = executorService.submit(new CreateJavaBeanTask(taskArgs));
            List tableInfos = listFuture.get();
            if (tableInfos != null) {
                List tableNameList = new ArrayList<>(Arrays.asList(tableNames));
                tableNameList.add(tableName);
                tableInfos.stream().filter(tableInfo -> tableNameList.contains(tableInfo.getTableName())).forEach(consumer);
            }
        } finally {
            executorService.shutdown();
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy