Search in sources :

Example 1 with MetaBuilder

use of com.jfinal.plugin.activerecord.generator.MetaBuilder in project jboot by yangfuhai.

the class ApiJsonGenerator method genMockJson.

/**
 * 生成 Mock Json 数据
 *
 * @param config
 */
public static void genMockJson(JsonGeneratorConfig config) {
    Map<String, Map<String, Object>> root = new LinkedHashMap<>();
    File file = new File(config.getJsonFilePathPathAbsolute());
    // 如果文件存在,则先读取其配置,然后再修改
    if (file.exists()) {
        String oldJson = FileUtil.readString(file);
        JSONObject rootJsonObject = JSONObject.parseObject(oldJson, Feature.OrderedField);
        if (rootJsonObject != null && !rootJsonObject.isEmpty()) {
            for (String classOrSimpleName : rootJsonObject.keySet()) {
                root.put(classOrSimpleName, rootJsonObject.getJSONObject(classOrSimpleName));
            }
        }
    }
    MetaBuilder builder = CodeGenHelpler.createMetaBuilder(config.getDatasource(), config.getType(), false);
    List<TableMeta> tableMetas = builder.build();
    if (config.tableMetaFilter != null) {
        tableMetas = tableMetas.stream().filter(config.tableMetaFilter).collect(Collectors.toList());
    }
    for (TableMeta tableMeta : tableMetas) {
        Map<String, Object> classMockData = new LinkedHashMap<>();
        for (ColumnMeta columnMeta : tableMeta.columnMetas) {
            Object mockData = createMockData(columnMeta);
            if (mockData != null && !"".equals(mockData)) {
                classMockData.put(columnMeta.attrName, mockData);
            }
        }
        if (!classMockData.isEmpty()) {
            root.put(StrKit.firstCharToLowerCase(tableMeta.modelName), classMockData);
        }
    }
    String jsonContent = JSONObject.toJSONString(root, true);
    FileUtil.writeString(file, jsonContent);
    System.out.println("Gen Remarks Json File ----->" + FileUtil.getCanonicalPath(file));
}
Also used : MetaBuilder(com.jfinal.plugin.activerecord.generator.MetaBuilder) JSONObject(com.alibaba.fastjson.JSONObject) ColumnMeta(com.jfinal.plugin.activerecord.generator.ColumnMeta) JSONObject(com.alibaba.fastjson.JSONObject) TableMeta(com.jfinal.plugin.activerecord.generator.TableMeta) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) File(java.io.File) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with MetaBuilder

use of com.jfinal.plugin.activerecord.generator.MetaBuilder in project jboot by yangfuhai.

the class ApiJsonGenerator method genRemarksJson.

/**
 * 生成 Model 的字段备注数据
 */
public static void genRemarksJson(JsonGeneratorConfig config) {
    Map<String, Map<String, String>> root = new LinkedHashMap<>();
    File file = new File(config.getJsonFilePathPathAbsolute());
    // 如果文件存在,则先读取其配置,然后再修改
    if (file.exists()) {
        String oldJson = FileUtil.readString(file);
        JSONObject rootJsonObject = JSONObject.parseObject(oldJson, Feature.OrderedField);
        if (rootJsonObject != null && !rootJsonObject.isEmpty()) {
            for (String classOrSimpleName : rootJsonObject.keySet()) {
                Map<String, String> remarks = new LinkedHashMap<>();
                JSONObject modelRemarks = rootJsonObject.getJSONObject(classOrSimpleName);
                modelRemarks.forEach((k, v) -> remarks.put(k, String.valueOf(v)));
                root.put(classOrSimpleName, remarks);
            }
        }
    }
    MetaBuilder builder = CodeGenHelpler.createMetaBuilder(config.getDatasource(), config.getType(), false);
    List<TableMeta> tableMetas = builder.build();
    if (config.tableMetaFilter != null) {
        tableMetas = tableMetas.stream().filter(config.tableMetaFilter).collect(Collectors.toList());
    }
    for (TableMeta tableMeta : tableMetas) {
        Map<String, String> modelRemarks = new LinkedHashMap<>();
        for (ColumnMeta columnMeta : tableMeta.columnMetas) {
            if (StrUtil.isNotBlank(columnMeta.remarks)) {
                modelRemarks.put(columnMeta.attrName, columnMeta.remarks);
            }
        }
        if (!modelRemarks.isEmpty()) {
            root.put(StrKit.firstCharToLowerCase(tableMeta.modelName), modelRemarks);
        }
    }
    String jsonContent = JSONObject.toJSONString(root, true);
    FileUtil.writeString(file, jsonContent);
    System.out.println("Gen Remarks Json File ----->" + FileUtil.getCanonicalPath(file));
}
Also used : MetaBuilder(com.jfinal.plugin.activerecord.generator.MetaBuilder) JSONObject(com.alibaba.fastjson.JSONObject) ColumnMeta(com.jfinal.plugin.activerecord.generator.ColumnMeta) TableMeta(com.jfinal.plugin.activerecord.generator.TableMeta) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) File(java.io.File) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with MetaBuilder

use of com.jfinal.plugin.activerecord.generator.MetaBuilder in project jboot by yangfuhai.

the class CodeGenHelpler method createMetaBuilder.

public static MetaBuilder createMetaBuilder(DataSource dataSource, String type, boolean removeNoPrimaryKeyTable) {
    MetaBuilder metaBuilder = removeNoPrimaryKeyTable ? new MetaBuilder(dataSource) : new MetaBuilder(dataSource) {

        @Override
        protected void removeNoPrimaryKeyTable(List<TableMeta> ret) {
        // do Nothing...
        }
    };
    metaBuilder.setGenerateRemarks(true);
    switch(type) {
        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;
}
Also used : MetaBuilder(com.jfinal.plugin.activerecord.generator.MetaBuilder) JbootIllegalConfigException(io.jboot.exception.JbootIllegalConfigException) TableMeta(com.jfinal.plugin.activerecord.generator.TableMeta)

Aggregations

MetaBuilder (com.jfinal.plugin.activerecord.generator.MetaBuilder)3 TableMeta (com.jfinal.plugin.activerecord.generator.TableMeta)3 JSONObject (com.alibaba.fastjson.JSONObject)2 ColumnMeta (com.jfinal.plugin.activerecord.generator.ColumnMeta)2 File (java.io.File)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 JbootIllegalConfigException (io.jboot.exception.JbootIllegalConfigException)1