Search in sources :

Example 1 with ColumnMeta

use of com.jfinal.plugin.activerecord.generator.ColumnMeta 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 ColumnMeta

use of com.jfinal.plugin.activerecord.generator.ColumnMeta 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 ColumnMeta

use of com.jfinal.plugin.activerecord.generator.ColumnMeta in project mica by lets-mica.

the class DataDictionaryGenerator method generateTable.

@Override
protected void generateTable(TableMeta tableMeta, StringBuilder ret) {
    ret.append("Table: ").append(tableMeta.name);
    if (StrKit.notBlank(tableMeta.remarks)) {
        ret.append("(").append(clearBlank(tableMeta.remarks)).append(")");
    }
    ret.append("\n");
    String separateLine = genSeparateLine(tableMeta);
    ret.append(separateLine);
    genTableHead(tableMeta, ret);
    ret.append(separateLine);
    for (ColumnMeta columnMeta : tableMeta.columnMetas) {
        genColumn(tableMeta, columnMeta, ret);
    }
    ret.append(separateLine);
    ret.append("\n");
}
Also used : ColumnMeta(com.jfinal.plugin.activerecord.generator.ColumnMeta)

Aggregations

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