com.kg.component.generator.config.builder.Controller 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.builder;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.kg.component.generator.ITemplate;
import com.kg.component.generator.config.ConstVal;
import com.kg.component.generator.config.StrategyConfig;
import com.kg.component.generator.config.po.TableInfo;
import com.kg.component.generator.function.ConverterFileName;
import com.kg.component.generator.util.ClassUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.Map;
/**
* 控制器属性配置
*
* @author nieqiurong 2020/10/11.
* @since 3.5.0
*/
public class Controller implements ITemplate {
private final static Logger LOGGER = LoggerFactory.getLogger(Controller.class);
private Controller() {
}
/**
* 生成 @RestController
控制器(默认 false)
*
* @Controller
-> @RestController
*
*/
private boolean restStyle;
/**
* 驼峰转连字符(默认 false)
*
* @RequestMapping("/managerUserActionHistory")
-> @RequestMapping("/manager-user-action-history")
*
*/
private boolean hyphenStyle;
/**
* 自定义继承的Controller类全称,带包名
*/
private String superClass;
/**
* 转换输出控制器文件名称
*
* @since 3.5.0
*/
private ConverterFileName converterFileName = (entityName -> entityName + ConstVal.CONTROLLER);
/**
* 是否覆盖已有文件(默认 false)
*
* @since 3.5.2
*/
private boolean fileOverride;
public boolean isRestStyle() {
return restStyle;
}
public boolean isHyphenStyle() {
return hyphenStyle;
}
@Nullable
public String getSuperClass() {
return superClass;
}
@NotNull
public ConverterFileName getConverterFileName() {
return converterFileName;
}
public boolean isFileOverride() {
return fileOverride;
}
@Override
@NotNull
public Map renderData(@NotNull ConfigBuilder config, @NotNull TableInfo tableInfo) {
Map data = new HashMap<>(5);
String mapping = (StringUtils.isNotBlank(config.getPackageConfig().getModuleName()) ? config.getPackageConfig().getModuleName() + "/" : "")
+ (this.hyphenStyle ? StringUtils.camelToHyphen(tableInfo.getEntityPath()) : tableInfo.getEntityPath());
data.put("controllerMapping", mapping);
String authorize = (StringUtils.isNotBlank(config.getPackageConfig().getModuleName()) ? config.getPackageConfig().getModuleName() + ":" : "")
+ (this.hyphenStyle ? StringUtils.camelToHyphen(tableInfo.getEntityPath()) : tableInfo.getEntityPath()) + ":";
data.put("controllerAuthorizePre", authorize);
data.put("restControllerStyle", this.restStyle);
data.put("superControllerClassPackage", StringUtils.isBlank(superClass) ? null : superClass);
data.put("superControllerClass", ClassUtils.getSimpleName(this.superClass));
return data;
}
public static class Builder extends BaseBuilder {
private final Controller controller = new Controller();
public Builder(@NotNull StrategyConfig strategyConfig) {
super(strategyConfig);
}
/**
* 父类控制器
*
* @param clazz 父类控制器
* @return this
*/
public Builder superClass(@NotNull Class> clazz) {
return superClass(clazz.getName());
}
/**
* 父类控制器
*
* @param superClass 父类控制器类名
* @return this
*/
public Builder superClass(@NotNull String superClass) {
this.controller.superClass = superClass;
return this;
}
/**
* 开启驼峰转连字符
*
* @return this
* @since 3.5.0
*/
public Builder enableHyphenStyle() {
this.controller.hyphenStyle = true;
return this;
}
/**
* 开启生成@RestController控制器
*
* @return this
* @since 3.5.0
*/
public Builder enableRestStyle() {
this.controller.restStyle = true;
return this;
}
/**
* 转换输出文件名称
*
* @param converter 转换处理
* @return this
* @since 3.5.0
*/
public Builder convertFileName(@NotNull ConverterFileName converter) {
this.controller.converterFileName = converter;
return this;
}
/**
* 格式化文件名称
*
* @param format 格式
* @return this
* @since 3.5.0
*/
public Builder formatFileName(@NotNull String format) {
return convertFileName((entityName) -> String.format(format, entityName));
}
/**
* 覆盖已有文件
*
* @since 3.5.3
*/
public Builder enableFileOverride() {
this.controller.fileOverride = true;
return this;
}
@NotNull
public Controller get() {
return this.controller;
}
}
}