/**
 * Copyright (c) 2015-2021, Michael Yang 杨福海 (fuhai999@gmail.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.jboot.codegen;

import com.jfinal.plugin.activerecord.dialect.*;
import com.jfinal.plugin.activerecord.generator.MetaBuilder;
import com.jfinal.plugin.activerecord.generator.TableMeta;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import io.jboot.Jboot;
import io.jboot.db.datasource.DataSourceConfig;
import io.jboot.exception.JbootIllegalConfigException;
import io.jboot.utils.StrUtil;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 代码生成工具类
 */
public class CodeGenHelpler {


    /**
     * 获取数据源
     *
     * @return
     */
    public static DataSource getDatasource() {
        DataSourceConfig datasourceConfig = Jboot.config(DataSourceConfig.class, "jboot.datasource");
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl(datasourceConfig.getUrl());
        config.setUsername(datasourceConfig.getUser());
        config.setPassword(datasourceConfig.getPassword());
        config.setDriverClassName(datasourceConfig.getDriverClassName());

        return new HikariDataSource(config);
    }


    public static MetaBuilder createMetaBuilder() {
        MetaBuilder metaBuilder = new MetaBuilder(getDatasource());
        metaBuilder.setGenerateRemarks(true);
        DataSourceConfig datasourceConfig = Jboot.config(DataSourceConfig.class, "jboot.datasource");
        switch (datasourceConfig.getType()) {
            case DataSourceConfig.TYPE_MYSQL:
                metaBuilder.setDialect(new MysqlDialect());
                break;
            case DataSourceConfig.TYPE_ORACLE:
                metaBuilder.setDialect(new OracleDialect());
                break;
            case DataSourceConfig.TYPE_SQLSERVER:
                metaBuilder.setDialect(new SqlServerDialect());
                break;
            case DataSourceConfig.TYPE_SQLITE:
                metaBuilder.setDialect(new Sqlite3Dialect());
                break;
            case DataSourceConfig.TYPE_ANSISQL:
                metaBuilder.setDialect(new AnsiSqlDialect());
                break;
            case DataSourceConfig.TYPE_POSTGRESQL:
                metaBuilder.setDialect(new PostgreSqlDialect());
                break;
            default:
                throw new JbootIllegalConfigException("only support datasource type : mysql、orcale、sqlserver、sqlite、ansisql and postgresql, please check your jboot.properties. ");
        }

        return metaBuilder;

    }


    /**
     * 排除指定的表，有些表不需要生成的
     *
     * @param list
     * @param excludeTables
     */
    public static void excludeTables(List<TableMeta> list, String excludeTables) {
        if (StrUtil.isNotBlank(excludeTables)) {
            List<TableMeta> newTableMetaList = new ArrayList<>();
            Set<String> excludeTableSet = StrUtil.splitToSet(excludeTables.toLowerCase(), ",");
            for (TableMeta tableMeta : list) {
                if (excludeTableSet.contains(tableMeta.name.toLowerCase())) {
                    System.out.println("exclude table : " + tableMeta.name);
                    continue;
                }
                newTableMetaList.add(tableMeta);
            }
            list.clear();
            list.addAll(newTableMetaList);
        }
    }


}



@@ -1836,123 +1836,234 @@
     
-MetaBuilder metaBuilder = new MetaBuilder(getDatasource());%0A        metaBuilder.setGenerateRemarks(true);%0A        D
+return createMetaBuilder(getDatasource(), Jboot.config(DataSourceConfig.class, %22jboot.datasource%22).getType());%0A    %7D%0A%0A%0A    public static MetaBuilder createMetaBuilder(DataSource dataSource) %7B%0A        return createMetaBuilder(d
 ataS
@@ -2063,36 +2063,31 @@
 r(dataSource
-Config datas
+, DataS
 ourceConfig 
@@ -2089,66 +2089,176 @@
 nfig
- = Jboot.config(DataSourceConfig.class, %22jboot.
+.TYPE_MYSQL);%0A    %7D%0A%0A%0A    public static MetaBuilder createMetaBuilder(DataSource dataSource, String type) %7B%0A        MetaBuilder metaBuilder = new MetaBuilder(
 data
-s
+S
 ource
-%22
 );%0A 
@@ -2268,42 +2268,66 @@
     
-switch (datasourceConfig.getT
+metaBuilder.setGenerateRemarks(true);%0A        switch (t
 ype
-()
 ) %7B%0A
@@ -3367,17 +3367,16 @@
 uilder;%0A
-%0A
     %7D%0A%0A%0A

optimize CodeGenHelpler