Search in sources :

Example 1 with TableMeta

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

the class CodeGenHelpler method excludeTables.

/**
 * 排除指定的表,有些表不需要生成的
 *
 * @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);
    }
}
Also used : ArrayList(java.util.ArrayList) TableMeta(com.jfinal.plugin.activerecord.generator.TableMeta)

Example 2 with TableMeta

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

the class JbootServiceImplGenerator method generate.

public void generate(List<TableMeta> tableMetas) {
    System.out.println("Generate Service Impl ...");
    System.out.println("Service Impl Output Dir: " + outputDir);
    Engine engine = Engine.create("forServiceImpl");
    engine.setSourceFactory(new ClassPathSourceFactory());
    engine.addSharedMethod(new StrKit());
    engine.addSharedObject("getterTypeMap", getterTypeMap);
    engine.addSharedObject("javaKeyword", JavaKeyword.me);
    for (TableMeta tableMeta : tableMetas) {
        genBaseModelContent(tableMeta);
    }
    writeToFile(tableMetas);
}
Also used : ClassPathSourceFactory(com.jfinal.template.source.ClassPathSourceFactory) TableMeta(com.jfinal.plugin.activerecord.generator.TableMeta) StrKit(com.jfinal.kit.StrKit) Engine(com.jfinal.template.Engine)

Example 3 with TableMeta

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

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

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

the class JbootServiceInterfaceGenerator method generate.

@Override
public void generate(List<TableMeta> tableMetas) {
    System.out.println("Generate base model ...");
    System.out.println("Base Model Output Dir: " + baseModelOutputDir);
    Engine engine = Engine.create("forService");
    engine.setSourceFactory(new ClassPathSourceFactory());
    engine.addSharedMethod(new StrKit());
    engine.addSharedObject("getterTypeMap", getterTypeMap);
    engine.addSharedObject("javaKeyword", javaKeyword);
    for (TableMeta tableMeta : tableMetas) {
        genBaseModelContent(tableMeta);
    }
    writeToFile(tableMetas);
}
Also used : ClassPathSourceFactory(com.jfinal.template.source.ClassPathSourceFactory) TableMeta(com.jfinal.plugin.activerecord.generator.TableMeta) StrKit(com.jfinal.kit.StrKit) Engine(com.jfinal.template.Engine)

Aggregations

TableMeta (com.jfinal.plugin.activerecord.generator.TableMeta)6 MetaBuilder (com.jfinal.plugin.activerecord.generator.MetaBuilder)3 JSONObject (com.alibaba.fastjson.JSONObject)2 StrKit (com.jfinal.kit.StrKit)2 ColumnMeta (com.jfinal.plugin.activerecord.generator.ColumnMeta)2 Engine (com.jfinal.template.Engine)2 ClassPathSourceFactory (com.jfinal.template.source.ClassPathSourceFactory)2 File (java.io.File)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 JbootIllegalConfigException (io.jboot.exception.JbootIllegalConfigException)1 ArrayList (java.util.ArrayList)1