com.kg.component.generator.config.StrategyConfig Maven / Gradle / Ivy
/*
* Copyright (c) 2011-2021, baomidou ([email protected]).
*
* 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
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* 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 com.kg.component.generator.config;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.kg.component.generator.config.builder.*;
import com.kg.component.generator.config.po.LikeTable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* 策略配置项
*
* @author YangHu, tangguo, hubin
* @since 2016/8/30
*/
public class StrategyConfig {
private StrategyConfig() {
}
/**
* 是否大写命名(默认 false)
*/
private boolean isCapitalMode;
/**
* 是否跳过视图(默认 false)
*/
private boolean skipView;
/**
* 过滤表前缀
* example: addTablePrefix("t_")
* result: t_simple -> Simple
*/
private final Set tablePrefix = new HashSet<>();
/**
* 过滤表后缀
* example: addTableSuffix("_0")
* result: t_simple_0 -> Simple
*/
private final Set tableSuffix = new HashSet<>();
/**
* 过滤字段前缀
* example: addFieldPrefix("is_")
* result: is_deleted -> deleted
*/
private final Set fieldPrefix = new HashSet<>();
/**
* 过滤字段后缀
* example: addFieldSuffix("_flag")
* result: deleted_flag -> deleted
*/
private final Set fieldSuffix = new HashSet<>();
/**
* 需要包含的表名,允许正则表达式(与exclude二选一配置)
* 当{@link #enableSqlFilter}为true时,正则表达式无效.
*/
private final Set include = new HashSet<>();
/**
* 需要排除的表名,允许正则表达式
* 当{@link #enableSqlFilter}为true时,正则表达式无效.
*/
private final Set exclude = new HashSet<>();
/**
* 启用sql过滤,语法不能支持使用sql过滤表的话,可以考虑关闭此开关.
*
* @since 3.3.1
*/
private boolean enableSqlFilter = true;
/**
* 启用 schema 默认 false
*/
private boolean enableSchema;
/**
* 包含表名
*
* @since 3.3.0
*/
private LikeTable likeTable;
/**
* 不包含表名
*
* @since 3.3.0
*/
private LikeTable notLikeTable;
private final Entity.Builder entityBuilder = new Entity.Builder(this);
private final DTO.Builder dtoBuilder = new DTO.Builder(this);
private final Excels.Builder excelsBuilder = new Excels.Builder(this);
private final Controller.Builder controllerBuilder = new Controller.Builder(this);
private final IndexVue.Builder indexVueBuilder = new IndexVue.Builder(this);
private final PermissionSQL.Builder permissionSQLBuilder = new PermissionSQL.Builder(this);
private final Mapper.Builder mapperBuilder = new Mapper.Builder(this);
private final Service.Builder serviceBuilder = new Service.Builder(this);
private Entity entity;
private DTO dto;
private Excels excels;
private Controller controller;
private Mapper mapper;
private Service service;
private IndexVue indexVue;
private PermissionSQL permissionSQL;
/**
* 实体配置构建者
*
* @return 实体配置构建者
* @since 3.5.0
*/
@NotNull
public Entity.Builder entityBuilder() {
return entityBuilder;
}
/**
* 实体配置
*
* @return 实体配置
* @since 3.5.0
*/
@NotNull
public Entity entity() {
if (entity == null) {
this.entity = entityBuilder.get();
}
return entity;
}
/**
* DTO配置构建者
*
* @return DTO配置构建者
* @since 3.5.0
*/
@NotNull
public DTO.Builder dtoBuilder() {
return dtoBuilder;
}
/**
* DTO配置
*
* @return DTO配置
* @since 3.5.0
*/
@NotNull
public DTO dto() {
if (dto == null) {
this.dto = dtoBuilder.get();
}
return dto;
}
/**
* Excels配置构建者
*
* @return Excels配置构建者
* @since 3.5.0
*/
@NotNull
public Excels.Builder excelsBuilder() {
return excelsBuilder;
}
/**
* Excels配置
*
* @return Excels配置
* @since 3.5.0
*/
@NotNull
public Excels excels() {
if (excels == null) {
this.excels = excelsBuilder.get();
}
return excels;
}
/**
* 控制器配置构建者
*
* @return 控制器配置构建者
* @since 3.5.0
*/
@NotNull
public Controller.Builder controllerBuilder() {
return controllerBuilder;
}
/**
* 控制器配置
*
* @return 控制器配置
* @since 3.5.0
*/
@NotNull
public Controller controller() {
if (controller == null) {
this.controller = controllerBuilder.get();
}
return controller;
}
/**
* IndexVue配置构建者
*
* @return IndexVue配置构建者
* @since 3.5.0
*/
@NotNull
public IndexVue.Builder indexVueBuilder() {
return indexVueBuilder;
}
/**
* IndexVue配置
*
* @return IndexVue配置
* @since 3.5.0
*/
@NotNull
public IndexVue indexVue() {
if (indexVue == null) {
this.indexVue = indexVueBuilder.get();
}
return indexVue;
}
/**
* PermissionSQL配置构建者
*
* @return PermissionSQL配置构建者
* @since 3.5.0
*/
@NotNull
public PermissionSQL.Builder permissionSqlBuilder() {
return permissionSQLBuilder;
}
/**
* PermissionSQL配置
*
* @return PermissionSQL配置
* @since 3.5.0
*/
@NotNull
public PermissionSQL permissionSQL() {
if (permissionSQL == null) {
this.permissionSQL = permissionSQLBuilder.get();
}
return permissionSQL;
}
/**
* Mapper配置构建者
*
* @return Mapper配置构建者
* @since 3.5.0
*/
@NotNull
public Mapper.Builder mapperBuilder() {
return mapperBuilder;
}
/**
* Mapper配置
*
* @return Mapper配置
* @since 3.5.0
*/
@NotNull
public Mapper mapper() {
if (mapper == null) {
this.mapper = mapperBuilder.get();
}
return mapper;
}
/**
* Service配置构建者
*
* @return Service配置构建者
* @since 3.5.0
*/
@NotNull
public Service.Builder serviceBuilder() {
return serviceBuilder;
}
/**
* Service配置
*
* @return Service配置
* @since 3.5.0
*/
@NotNull
public Service service() {
if (service == null) {
this.service = serviceBuilder.get();
}
return service;
}
/**
* 大写命名、字段符合大写字母数字下划线命名
*
* @param word 待判断字符串
*/
public boolean isCapitalModeNaming(@NotNull String word) {
return isCapitalMode && StringUtils.isCapitalMode(word);
}
/**
* 表名称匹配过滤表前缀
*
* @param tableName 表名称
* @since 3.3.2
*/
public boolean startsWithTablePrefix(@NotNull String tableName) {
return this.tablePrefix.stream().anyMatch(tableName::startsWith);
}
/**
* 验证配置项
*
* @since 3.5.0
*/
public void validate() {
boolean isInclude = this.getInclude().size() > 0;
boolean isExclude = this.getExclude().size() > 0;
if (isInclude && isExclude) {
throw new IllegalArgumentException(" 标签中 与 只能配置一项!");
}
if (this.getNotLikeTable() != null && this.getLikeTable() != null) {
throw new IllegalArgumentException(" 标签中 与 只能配置一项!");
}
}
/**
* 包含表名匹配
*
* @param tableName 表名
* @return 是否匹配
* @since 3.5.0
*/
public boolean matchIncludeTable(@NotNull String tableName) {
return matchTable(tableName, this.getInclude());
}
/**
* 排除表名匹配
*
* @param tableName 表名
* @return 是否匹配
* @since 3.5.0
*/
public boolean matchExcludeTable(@NotNull String tableName) {
return matchTable(tableName, this.getExclude());
}
/**
* 表名匹配
*
* @param tableName 表名
* @param matchTables 匹配集合
* @return 是否匹配
* @since 3.5.0
*/
private boolean matchTable(@NotNull String tableName, @NotNull Set matchTables) {
return matchTables.stream().anyMatch(t -> tableNameMatches(t, tableName));
}
/**
* 表名匹配
*
* @param matchTableName 匹配表名
* @param dbTableName 数据库表名
* @return 是否匹配
*/
private boolean tableNameMatches(@NotNull String matchTableName, @NotNull String dbTableName) {
return matchTableName.equalsIgnoreCase(dbTableName) || StringUtils.matches(matchTableName, dbTableName);
}
public boolean isCapitalMode() {
return isCapitalMode;
}
public boolean isSkipView() {
return skipView;
}
@NotNull
public Set getTablePrefix() {
return tablePrefix;
}
@NotNull
public Set getTableSuffix() {
return tableSuffix;
}
@NotNull
public Set getFieldPrefix() {
return fieldPrefix;
}
@NotNull
public Set getFieldSuffix() {
return fieldSuffix;
}
@NotNull
public Set getInclude() {
return include;
}
@NotNull
public Set getExclude() {
return exclude;
}
public boolean isEnableSqlFilter() {
return enableSqlFilter;
}
public boolean isEnableSchema() {
return enableSchema;
}
@Nullable
public LikeTable getLikeTable() {
return likeTable;
}
@Nullable
public LikeTable getNotLikeTable() {
return notLikeTable;
}
/**
* 策略配置构建者
*
* @author nieqiurong 2020/10/11.
* @since 3.5.0
*/
public static class Builder extends BaseBuilder {
private final StrategyConfig strategyConfig;
public Builder() {
super(new StrategyConfig());
strategyConfig = super.build();
}
/**
* 开启大写命名
*
* @return this
* @since 3.5.0
*/
public Builder enableCapitalMode() {
this.strategyConfig.isCapitalMode = true;
return this;
}
/**
* 开启跳过视图
*
* @return this
* @since 3.5.0
*/
public Builder enableSkipView() {
this.strategyConfig.skipView = true;
return this;
}
/**
* 禁用sql过滤
*
* @return this
* @since 3.5.0
*/
public Builder disableSqlFilter() {
this.strategyConfig.enableSqlFilter = false;
return this;
}
/**
* 启用 schema
*
* @return this
* @since 3.5.1
*/
public Builder enableSchema() {
this.strategyConfig.enableSchema = true;
return this;
}
/**
* 增加过滤表前缀
*
* @param tablePrefix 过滤表前缀
* @return this
* @since 3.5.0
*/
public Builder addTablePrefix(@NotNull String... tablePrefix) {
return addTablePrefix(Arrays.asList(tablePrefix));
}
public Builder addTablePrefix(@NotNull List tablePrefixList) {
this.strategyConfig.tablePrefix.addAll(tablePrefixList);
return this;
}
/**
* 增加过滤表后缀
*
* @param tableSuffix 过滤表后缀
* @return this
* @since 3.5.1
*/
public Builder addTableSuffix(String... tableSuffix) {
return addTableSuffix(Arrays.asList(tableSuffix));
}
public Builder addTableSuffix(@NotNull List tableSuffixList) {
this.strategyConfig.tableSuffix.addAll(tableSuffixList);
return this;
}
/**
* 增加过滤字段前缀
*
* @param fieldPrefix 过滤字段前缀
* @return this
* @since 3.5.0
*/
public Builder addFieldPrefix(@NotNull String... fieldPrefix) {
return addFieldPrefix(Arrays.asList(fieldPrefix));
}
public Builder addFieldPrefix(@NotNull List fieldPrefix) {
this.strategyConfig.fieldPrefix.addAll(fieldPrefix);
return this;
}
/**
* 增加过滤字段后缀
*
* @param fieldSuffix 过滤字段后缀
* @return this
* @since 3.5.1
*/
public Builder addFieldSuffix(@NotNull String... fieldSuffix) {
return addFieldSuffix(Arrays.asList(fieldSuffix));
}
public Builder addFieldSuffix(@NotNull List fieldSuffixList) {
this.strategyConfig.fieldSuffix.addAll(fieldSuffixList);
return this;
}
/**
* 增加包含的表名
*
* @param include 包含表
* @return this
* @since 3.5.0
*/
public Builder addInclude(@NotNull String... include) {
this.strategyConfig.include.addAll(Arrays.asList(include));
return this;
}
public Builder addInclude(@NotNull List includes) {
this.strategyConfig.include.addAll(includes);
return this;
}
public Builder addInclude(@NotNull String include) {
this.strategyConfig.include.addAll(Arrays.asList(include.split(",")));
return this;
}
/**
* 增加排除表
*
* @param exclude 排除表
* @return this
* @since 3.5.0
*/
public Builder addExclude(@NotNull String... exclude) {
return addExclude(Arrays.asList(exclude));
}
public Builder addExclude(@NotNull List excludeList) {
this.strategyConfig.exclude.addAll(excludeList);
return this;
}
/**
* 包含表名
*
* @return this
*/
public Builder likeTable(@NotNull LikeTable likeTable) {
this.strategyConfig.likeTable = likeTable;
return this;
}
/**
* 不包含表名
*
* @return this
*/
public Builder notLikeTable(@NotNull LikeTable notLikeTable) {
this.strategyConfig.notLikeTable = notLikeTable;
return this;
}
@Override
@NotNull
public StrategyConfig build() {
this.strategyConfig.validate();
return strategyConfig;
}
}
}