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

com.lq.doc.SpringBootDoc Maven / Gradle / Ivy

package com.lq.doc;

import com.lq.comment.GlobArgs;
import com.lq.comment.jdbc.JdbcConfig;
import com.lq.comment.jdbc.JdbcUtil;
import com.lq.comment.util.JsonOperate;
import com.lq.comment.util.StringUtil;
import com.lq.doc.entity.ApiRequestEntity;
import com.lq.doc.present.LoadController;
import com.lq.doc.present.LocalHtmlDocGenerator;

import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

public class SpringBootDoc {

    private final JdbcUtil jdbcUtil;
    private final GlobArgs globArgs;
    private String projectName;

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

    public static SpringBootDoc getInstance(Class aClass, JdbcConfig jdbcConfig) throws SQLException {
        return new SpringBootDoc(aClass, jdbcConfig);
    }

    private SpringBootDoc(Class aClass, JdbcConfig jdbcConfig) throws SQLException {
        globArgs = new GlobArgs();
        String path = new File(aClass.getResource("").getPath()).getPath();
        globArgs.projectPath = path.substring(0, path.indexOf(File.separator + "target" + File.separator));
        String filePath = globArgs.projectPath + File.separator + "src" + File.separator + "main" + File.separator + "java" +
                File.separator + new File(aClass.getName()).getPath().replace(".", File.separator) + ".java";
        globArgs.rootPackagePath = filePath.replace(aClass.getSimpleName() + ".java", "");
        globArgs.packageName = aClass.getPackage().getName();
        jdbcUtil = new JdbcUtil(jdbcConfig);
        projectName = globArgs.projectPath.substring(globArgs.projectPath.lastIndexOf("\\") + 1);
        if (projectName.contains("-")) {
            projectName = projectName.replaceAll("-", "_");
        }
        projectName = StringUtil.humpToUnderline(projectName);
        createProjectApiDocTable();
    }

    private void createProjectApiDocTable() throws SQLException {
        String createTable = "CREATE TABLE if not EXISTS `" + projectName + "` (\n" +
                "  `api_tag` varchar(128) COLLATE utf8_unicode_ci NOT NULL,\n" +
                "  `api_desc` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,\n" +
                "  `request_info` longtext COLLATE utf8_unicode_ci,\n" +
                "  PRIMARY KEY (`api_tag`)\n" +
                ") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;\n" +
                "\n";
        jdbcUtil.ddl(createTable);
    }


    public void loadController(Class controller, Class... controllers) throws Exception {
        parameterConstruct(ctl -> {
            try {
                loadController(ctl, null, false);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, controller, controllers);
    }

    public void loadController(Class controller) throws Exception {
        loadController(controller, null, false);
    }


    public void loadController(Class controller, List loadMethodNames) throws Exception {
        loadController(controller, loadMethodNames, false);
    }

    public void loadController(Class controller, List loadMethodNames, boolean forceUpdate) throws Exception {
        String controllerPackageName = controller.getPackage().getName();
        String[] split = controllerPackageName.replace(globArgs.packageName, "").split("\\.");
        String path = "";
        for (String s : split) {
            path += s + File.separator;
        }
        String controllerSimpleName = controller.getSimpleName();
        path = globArgs.rootPackagePath + path.substring(1, path.length() - 1) + File.separator + controllerSimpleName + ".java";
        List apiRequestEntities = LoadController.getInstance(globArgs).parseController(new File(path));
        if (apiRequestEntities != null) {
            String sqlPrefix = forceUpdate ? "replace" : "insert ignore";
            String insertStartSql = sqlPrefix + " into " + projectName + "(api_tag,api_desc,request_info) values(";
            for (ApiRequestEntity apiRequestEntity : apiRequestEntities) {
                if (loadMethodNames != null && loadMethodNames.size() > 0) {
                    if (loadMethodNames.contains(apiRequestEntity.getMethodName())) {
                        String apiTag = controllerSimpleName + "_" + apiRequestEntity.getMethodName();
                        String json = JsonOperate.objectToJson(apiRequestEntity);
                        String insertSql = insertStartSql + "'" + apiTag + "','','" + json + "')";
                        jdbcUtil.update(insertSql);
                    }
                } else {
                    String apiTag = controllerSimpleName + "_" + apiRequestEntity.getMethodName();
                    String json = JsonOperate.objectToJson(apiRequestEntity);
                    String insertSql = insertStartSql + "'" + apiTag + "','','" + json + "')";
                    jdbcUtil.update(insertSql);
                }
            }
        }
    }

    public void generateLocalHtmlDco(Class controller, Class... controllers) {
        parameterConstruct(ctl -> {
            try {
                generateLocalHtmlDco(ctl);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, controller, controllers);
    }

    public void generateLocalHtmlDco(Class controller) throws Exception {
        List apiRequestEntities = LoadController.getInstance(globArgs).realParseController(controller);
        StringBuilder sb = new StringBuilder();
        for (ApiRequestEntity apiRequestEntity : apiRequestEntities) {
            String tableHtml = LocalHtmlDocGenerator.getInstance(globArgs).apiRequestEntityToTableHtml(apiRequestEntity);
            sb.append(tableHtml);
        }
        LocalHtmlDocGenerator.getInstance(globArgs).createTableHtml(sb.toString(), controller.getSimpleName());
    }

    private void parameterConstruct(Consumer> consumer, Class controller, Class... controllers) {
        List> arrayList = new ArrayList<>(Arrays.asList(controllers));
        arrayList.add(controller);
        for (Class ctrl : arrayList) {
            consumer.accept(ctrl);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy