逐步替换旧的表配置服务

This commit is contained in:
makejava 2021-08-16 18:10:52 +08:00
parent 8deb87db46
commit 8f080783ff
8 changed files with 249 additions and 103 deletions

View File

@ -1,8 +1,10 @@
package com.sjhy.plugin.dto; package com.sjhy.plugin.dto;
import com.intellij.database.model.DasColumn; import com.intellij.database.model.DasColumn;
import com.intellij.psi.PsiField;
import com.sjhy.plugin.entity.TypeMapper; import com.sjhy.plugin.entity.TypeMapper;
import com.sjhy.plugin.tool.CurrGroupUtils; import com.sjhy.plugin.tool.CurrGroupUtils;
import com.sjhy.plugin.tool.DocCommentUtils;
import com.sjhy.plugin.tool.NameUtils; import com.sjhy.plugin.tool.NameUtils;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
@ -21,6 +23,15 @@ import java.util.regex.Pattern;
@Data @Data
@NoArgsConstructor @NoArgsConstructor
public class ColumnInfoDTO { public class ColumnInfoDTO {
public ColumnInfoDTO(PsiField field) {
this.name = field.getName();
this.comment = DocCommentUtils.getComment(field.getDocComment());
this.type = field.getType().getCanonicalText();
this.custom = false;
this.ext = new HashMap<>();
}
public ColumnInfoDTO(DasColumn column) { public ColumnInfoDTO(DasColumn column) {
this.name = NameUtils.getInstance().getJavaName(column.getName()); this.name = NameUtils.getInstance().getJavaName(column.getName());
this.comment = column.getComment(); this.comment = column.getComment();

View File

@ -3,12 +3,12 @@ package com.sjhy.plugin.dto;
import com.intellij.database.model.DasColumn; import com.intellij.database.model.DasColumn;
import com.intellij.database.psi.DbTable; import com.intellij.database.psi.DbTable;
import com.intellij.database.util.DasUtil; import com.intellij.database.util.DasUtil;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiField;
import com.intellij.util.containers.JBIterable; import com.intellij.util.containers.JBIterable;
import com.sjhy.plugin.entity.ColumnInfo; import com.sjhy.plugin.entity.ColumnInfo;
import com.sjhy.plugin.entity.TableInfo; import com.sjhy.plugin.entity.TableInfo;
import com.sjhy.plugin.tool.CollectionUtil; import com.sjhy.plugin.tool.*;
import com.sjhy.plugin.tool.NameUtils;
import com.sjhy.plugin.tool.StringUtils;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
@ -28,11 +28,31 @@ import java.util.stream.Collectors;
@Data @Data
@NoArgsConstructor @NoArgsConstructor
public class TableInfoDTO { public class TableInfoDTO {
public TableInfoDTO(TableInfoDTO dto, DbTable dbTable) { public TableInfoDTO(TableInfoDTO dto, DbTable dbTable) {
this(dbTable); this(dbTable);
merge(dto, this); merge(dto, this);
} }
public TableInfoDTO(TableInfoDTO dto, PsiClass psiClass) {
this(psiClass);
merge(dto, this);
}
private TableInfoDTO(PsiClass psiClass) {
this.name = psiClass.getName();
this.preName = "";
this.comment = DocCommentUtils.getComment(psiClass.getDocComment());
this.templateGroupName = "";
this.savePackageName = "";
this.savePath = "";
this.saveModelName = "";
this.fullColumn = new ArrayList<>();
for (PsiField field : psiClass.getAllFields()) {
this.fullColumn.add(new ColumnInfoDTO(field));
}
}
private TableInfoDTO(DbTable dbTable) { private TableInfoDTO(DbTable dbTable) {
this.name = NameUtils.getInstance().getClassName(dbTable.getName()); this.name = NameUtils.getInstance().getClassName(dbTable.getName());
this.preName = ""; this.preName = "";
@ -129,6 +149,39 @@ public class TableInfoDTO {
*/ */
private String saveModelName; private String saveModelName;
public TableInfo toTableInfo(PsiClass psiClass) {
TableInfo tableInfo = new TableInfo();
tableInfo.setPsiClassObj(psiClass);
tableInfo.setName(this.getName());
tableInfo.setPreName(this.getPreName());
tableInfo.setTemplateGroupName(this.getTemplateGroupName());
tableInfo.setSavePackageName(this.getSavePackageName());
tableInfo.setSavePath(this.getSavePath());
tableInfo.setComment(this.getComment());
tableInfo.setSaveModelName(this.getSaveModelName());
tableInfo.setFullColumn(new ArrayList<>());
tableInfo.setPkColumn(new ArrayList<>());
tableInfo.setOtherColumn(new ArrayList<>());
for (PsiField field : psiClass.getAllFields()) {
if (PsiClassGenerateUtils.isSkipField(field)) {
continue;
}
ColumnInfo columnInfo = new ColumnInfo();
columnInfo.setName(field.getName());
columnInfo.setShortType(field.getType().getPresentableText());
columnInfo.setType(field.getType().getCanonicalText());
columnInfo.setComment(DocCommentUtils.getComment(field.getDocComment()));
columnInfo.setCustom(false);
tableInfo.getFullColumn().add(columnInfo);
if (PsiClassGenerateUtils.isPkField(field)) {
tableInfo.getPkColumn().add(columnInfo);
} else {
tableInfo.getOtherColumn().add(columnInfo);
}
}
return tableInfo;
}
public TableInfo toTableInfo(DbTable dbTable) { public TableInfo toTableInfo(DbTable dbTable) {
TableInfo tableInfo = new TableInfo(); TableInfo tableInfo = new TableInfo();
tableInfo.setObj(dbTable); tableInfo.setObj(dbTable);
@ -169,7 +222,7 @@ public class TableInfoDTO {
return tableInfo; return tableInfo;
} }
public static TableInfoDTO parse(TableInfo tableInfo) { public static TableInfoDTO valueOf(TableInfo tableInfo) {
TableInfoDTO dto = new TableInfoDTO(); TableInfoDTO dto = new TableInfoDTO();
dto.setName(tableInfo.getName()); dto.setName(tableInfo.getName());
dto.setTemplateGroupName(tableInfo.getTemplateGroupName()); dto.setTemplateGroupName(tableInfo.getTemplateGroupName());

View File

@ -3,6 +3,7 @@ package com.sjhy.plugin.dto;
import com.intellij.database.model.DasNamespace; import com.intellij.database.model.DasNamespace;
import com.intellij.database.psi.DbElement; import com.intellij.database.psi.DbElement;
import com.intellij.database.psi.DbTable; import com.intellij.database.psi.DbTable;
import com.intellij.psi.PsiClass;
import com.sjhy.plugin.entity.TableInfo; import com.sjhy.plugin.entity.TableInfo;
import lombok.Data; import lombok.Data;
@ -47,16 +48,32 @@ public class TableInfoSettingsDTO {
return builder.toString(); return builder.toString();
} }
private String generateKey(PsiClass psiClass) {
return psiClass.getQualifiedName();
}
/** /**
* 读取表信息 * 表信息
* *
* @param dbTable 原始表对象 * @param psiClass psi类
* @return 储存的表信息 * @return {@link TableInfo}
*/ */
@SuppressWarnings("Duplicates")
public TableInfo readTableInfo(PsiClass psiClass) {
String key = generateKey(psiClass);
TableInfoDTO dto = this.tableInfoMap.get(key);
dto = new TableInfoDTO(dto, psiClass);
this.tableInfoMap.put(key, dto);
return dto.toTableInfo(psiClass);
}
/**
* 读表信息
*
* @param dbTable 数据库表
* @return {@link TableInfo}
*/
@SuppressWarnings("Duplicates")
public TableInfo readTableInfo(DbTable dbTable) { public TableInfo readTableInfo(DbTable dbTable) {
if (dbTable == null) {
return null;
}
String key = generateKey(dbTable); String key = generateKey(dbTable);
TableInfoDTO dto = this.tableInfoMap.get(key); TableInfoDTO dto = this.tableInfoMap.get(key);
dto = new TableInfoDTO(dto, dbTable); dto = new TableInfoDTO(dto, dbTable);
@ -74,11 +91,16 @@ public class TableInfoSettingsDTO {
return; return;
} }
DbTable dbTable = tableInfo.getObj(); DbTable dbTable = tableInfo.getObj();
if (dbTable == null) { PsiClass psiClass = tableInfo.getPsiClassObj();
String key;
if (dbTable != null) {
key = generateKey(dbTable);
} else if (psiClass != null) {
key = generateKey(psiClass);
} else {
return; return;
} }
String key = generateKey(dbTable); this.tableInfoMap.put(key, TableInfoDTO.valueOf(tableInfo));
this.tableInfoMap.put(key, TableInfoDTO.parse(tableInfo));
} }
/** /**

View File

@ -3,6 +3,7 @@ package com.sjhy.plugin.service;
import com.intellij.database.psi.DbTable; import com.intellij.database.psi.DbTable;
import com.intellij.openapi.components.PersistentStateComponent; import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.ServiceManager; import com.intellij.openapi.components.ServiceManager;
import com.intellij.psi.PsiClass;
import com.sjhy.plugin.dto.TableInfoSettingsDTO; import com.sjhy.plugin.dto.TableInfoSettingsDTO;
import com.sjhy.plugin.entity.TableInfo; import com.sjhy.plugin.entity.TableInfo;
import com.sjhy.plugin.service.impl.TableInfoSettingsServiceImpl; import com.sjhy.plugin.service.impl.TableInfoSettingsServiceImpl;
@ -31,6 +32,14 @@ public interface TableInfoSettingsService extends PersistentStateComponent<Table
*/ */
TableInfo getTableInfo(DbTable dbTable); TableInfo getTableInfo(DbTable dbTable);
/**
* 获取表信息
*
* @param psiClass psi类
* @return {@link TableInfo}
*/
TableInfo getTableInfo(PsiClass psiClass);
/** /**
* 保存表信息 * 保存表信息
* *

View File

@ -57,18 +57,19 @@ public class CodeGenerateServiceImpl implements CodeGenerateService {
/** /**
* 生成代码并自动保存到对应位置使用统一配置 * 生成代码并自动保存到对应位置使用统一配置
* @param templates 模板 *
* @param templates 模板
* @param unifiedConfig 是否使用统一配置 * @param unifiedConfig 是否使用统一配置
* @param title 是否显示提示 * @param title 是否显示提示
* @param entityMode * @param entityMode 实体类生成模式
*/ */
@Override @Override
public void generateByUnifiedConfig(Collection<Template> templates, boolean unifiedConfig, boolean title, public void generateByUnifiedConfig(Collection<Template> templates, boolean unifiedConfig, boolean title,
boolean entityMode) { boolean entityMode) {
// 获取选中表信息 // 获取选中表信息
TableInfo selectedTableInfo; TableInfo selectedTableInfo;
List<TableInfo> tableInfoList; List<TableInfo> tableInfoList;
if(!entityMode) { if (!entityMode) {
selectedTableInfo = tableInfoService.getTableInfoAndConfig(cacheDataUtils.getSelectDbTable()); selectedTableInfo = tableInfoService.getTableInfoAndConfig(cacheDataUtils.getSelectDbTable());
tableInfoList = tableInfoService.getTableInfoAndConfig(cacheDataUtils.getDbTableList()); tableInfoList = tableInfoService.getTableInfoAndConfig(cacheDataUtils.getDbTableList());
} else { } else {
@ -77,7 +78,13 @@ public class CodeGenerateServiceImpl implements CodeGenerateService {
} }
// 校验选中表的保存路径是否正确 // 校验选中表的保存路径是否正确
if (StringUtils.isEmpty(selectedTableInfo.getSavePath())) { if (StringUtils.isEmpty(selectedTableInfo.getSavePath())) {
Messages.showInfoMessage(selectedTableInfo.getObj().getName() + "表配置信息不正确,请尝试重新配置", GlobalDict.TITLE_INFO); if (selectedTableInfo.getObj() != null) {
Messages.showInfoMessage(selectedTableInfo.getObj().getName() + "表配置信息不正确,请尝试重新配置", GlobalDict.TITLE_INFO);
} else if (selectedTableInfo.getPsiClassObj() != null) {
Messages.showInfoMessage(selectedTableInfo.getPsiClassObj().getName() + "类配置信息不正确,请尝试重新配置", GlobalDict.TITLE_INFO);
} else {
Messages.showInfoMessage("配置信息不正确,请尝试重新配置", GlobalDict.TITLE_INFO);
}
return; return;
} }
// 将未配置的表进行配置覆盖 // 将未配置的表进行配置覆盖
@ -87,6 +94,7 @@ public class CodeGenerateServiceImpl implements CodeGenerateService {
tableInfo.setSaveModelName(finalSelectedTableInfo.getSaveModelName()); tableInfo.setSaveModelName(finalSelectedTableInfo.getSaveModelName());
tableInfo.setSavePackageName(finalSelectedTableInfo.getSavePackageName()); tableInfo.setSavePackageName(finalSelectedTableInfo.getSavePackageName());
tableInfo.setSavePath(finalSelectedTableInfo.getSavePath()); tableInfo.setSavePath(finalSelectedTableInfo.getSavePath());
tableInfo.setPreName(finalSelectedTableInfo.getPreName());
tableInfoService.save(tableInfo); tableInfoService.save(tableInfo);
} }
}); });

View File

@ -3,6 +3,7 @@ package com.sjhy.plugin.service.impl;
import com.intellij.database.psi.DbTable; import com.intellij.database.psi.DbTable;
import com.intellij.openapi.components.State; import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage; import com.intellij.openapi.components.Storage;
import com.intellij.psi.PsiClass;
import com.sjhy.plugin.dto.TableInfoSettingsDTO; import com.sjhy.plugin.dto.TableInfoSettingsDTO;
import com.sjhy.plugin.entity.TableInfo; import com.sjhy.plugin.entity.TableInfo;
import com.sjhy.plugin.service.TableInfoSettingsService; import com.sjhy.plugin.service.TableInfoSettingsService;
@ -43,6 +44,17 @@ public class TableInfoSettingsServiceImpl implements TableInfoSettingsService {
return Objects.requireNonNull(getState()).readTableInfo(dbTable); return Objects.requireNonNull(getState()).readTableInfo(dbTable);
} }
/**
* 获取表信息
*
* @param psiClass psi类
* @return {@link TableInfo}
*/
@Override
public TableInfo getTableInfo(PsiClass psiClass) {
return Objects.requireNonNull(getState()).readTableInfo(psiClass);
}
/** /**
* 保存表信息 * 保存表信息
* *

View File

@ -0,0 +1,37 @@
package com.sjhy.plugin.tool;
import com.intellij.psi.PsiElement;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.psi.javadoc.PsiDocToken;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
/**
* 文档注释工具类
*
* @author makejava
* @version 1.0.0
* @date 2021/08/16 17:37
*/
public class DocCommentUtils {
/**
* 获取注释信息获取第一条文本类型注释内容不存在则返回null
*
* @param docComment 文档注释
* @return 注释内容
*/
public static String getComment(@Nullable PsiDocComment docComment) {
if (docComment == null) {
return null;
}
return Arrays.stream(docComment.getDescriptionElements())
.filter(o -> o instanceof PsiDocToken)
.map(PsiElement::getText)
.findFirst()
.map(String::trim)
.orElse(null);
}
}

View File

@ -16,7 +16,7 @@ import com.sjhy.plugin.entity.TableInfo;
import com.sjhy.plugin.entity.Template; import com.sjhy.plugin.entity.Template;
import com.sjhy.plugin.service.CodeGenerateService; import com.sjhy.plugin.service.CodeGenerateService;
import com.sjhy.plugin.service.SettingsStorageService; import com.sjhy.plugin.service.SettingsStorageService;
import com.sjhy.plugin.service.TableInfoService; import com.sjhy.plugin.service.TableInfoSettingsService;
import com.sjhy.plugin.tool.CacheDataUtils; import com.sjhy.plugin.tool.CacheDataUtils;
import com.sjhy.plugin.tool.ModuleUtils; import com.sjhy.plugin.tool.ModuleUtils;
import com.sjhy.plugin.tool.ProjectUtils; import com.sjhy.plugin.tool.ProjectUtils;
@ -89,7 +89,7 @@ public class SelectSavePath extends DialogWrapper {
/** /**
* 表信息服务 * 表信息服务
*/ */
private TableInfoService tableInfoService; private TableInfoSettingsService tableInfoService;
/** /**
* 项目对象 * 项目对象
*/ */
@ -125,8 +125,6 @@ public class SelectSavePath extends DialogWrapper {
return this.contentPane; return this.contentPane;
} }
/** /**
* 构造方法 * 构造方法
*/ */
@ -134,7 +132,7 @@ public class SelectSavePath extends DialogWrapper {
super(project); super(project);
this.entityMode = entityMode; this.entityMode = entityMode;
this.project = project; this.project = project;
this.tableInfoService = TableInfoService.getInstance(project); this.tableInfoService = TableInfoSettingsService.getInstance();
this.codeGenerateService = CodeGenerateService.getInstance(project); this.codeGenerateService = CodeGenerateService.getInstance(project);
// 初始化module存在资源路径的排前面 // 初始化module存在资源路径的排前面
this.moduleList = new LinkedList<>(); this.moduleList = new LinkedList<>();
@ -151,84 +149,11 @@ public class SelectSavePath extends DialogWrapper {
this.initEvent(); this.initEvent();
init(); init();
setTitle(GlobalDict.TITLE_INFO); setTitle(GlobalDict.TITLE_INFO);
//初始化路径
refreshPath();
} }
private void initEvent() { private void initEvent() {
}
private void refreshData() {
}
@Override
protected void doOKAction() {
onOK();
super.doOKAction();
}
/**
* 确认按钮回调事件
*/
private void onOK() {
List<Template> selectTemplateList = templateSelectComponent.getAllSelectedTemplate();
// 如果选择的模板是空的
if (selectTemplateList.isEmpty()) {
Messages.showWarningDialog("Can't Select Template!", GlobalDict.TITLE_INFO);
return;
}
String savePath = pathField.getText();
if (StringUtils.isEmpty(savePath)) {
Messages.showWarningDialog("Can't Select Save Path!", GlobalDict.TITLE_INFO);
return;
}
// 针对Linux系统路径做处理
savePath = savePath.replace("\\", "/");
// 保存路径使用相对路径
String basePath = project.getBasePath();
if (!StringUtils.isEmpty(basePath) && savePath.startsWith(basePath)) {
if (savePath.length() > basePath.length()) {
if ("/".equals(savePath.substring(basePath.length(), basePath.length() + 1))) {
savePath = savePath.replace(basePath, ".");
}
} else {
savePath = savePath.replace(basePath, ".");
}
}
// 保存配置
TableInfo tableInfo;
if(!entityMode) {
tableInfo = tableInfoService.getTableInfoAndConfig(cacheDataUtils.getSelectDbTable());
} else {
tableInfo = tableInfoService.getTableInfoAndConfigByPsiClass(cacheDataUtils.getSelectPsiClass());
}
tableInfo.setSavePath(savePath);
tableInfo.setSavePackageName(packageField.getText());
tableInfo.setPreName(preField.getText());
tableInfo.setTemplateGroupName(templateSelectComponent.getselectedGroupName());
Module module = getSelectModule();
if (module != null) {
tableInfo.setSaveModelName(module.getName());
}
tableInfoService.save(tableInfo);
// 生成代码
codeGenerateService.generateByUnifiedConfig(selectTemplateList, unifiedConfig.isSelected(), !titleConfig.isSelected(), this.entityMode);
}
/**
* 初始化方法
*/
private void initPanel() {
// 初始化模板组
this.templateSelectComponent = new TemplateSelectComponent();
templatePanel.add(this.templateSelectComponent.getMainPanel(), BorderLayout.CENTER);
//初始化Module选择
for (Module module : this.moduleList) {
moduleComboBox.addItem(module.getName());
}
//监听module选择事件 //监听module选择事件
moduleComboBox.addActionListener(e -> { moduleComboBox.addActionListener(e -> {
// 刷新路径 // 刷新路径
@ -274,9 +199,6 @@ public class SelectSavePath extends DialogWrapper {
packageChooseButton.setEnabled(false); packageChooseButton.setEnabled(false);
} }
//初始化路径
refreshPath();
//选择路径 //选择路径
pathChooseButton.addActionListener(e -> { pathChooseButton.addActionListener(e -> {
//将当前选中的model设置为基础路径 //将当前选中的model设置为基础路径
@ -290,13 +212,15 @@ public class SelectSavePath extends DialogWrapper {
pathField.setText(virtualFile.getPath()); pathField.setText(virtualFile.getPath());
} }
}); });
}
private void refreshData() {
// 获取选中的表信息鼠标右键的那张表并提示未知类型 // 获取选中的表信息鼠标右键的那张表并提示未知类型
TableInfo tableInfo; TableInfo tableInfo;
if(entityMode) { if(entityMode) {
tableInfo = tableInfoService.getTableInfoAndConfigByPsiClass(cacheDataUtils.getSelectPsiClass()); tableInfo = tableInfoService.getTableInfo(cacheDataUtils.getSelectPsiClass());
} else { } else {
tableInfo = tableInfoService.getTableInfoAndConfig(cacheDataUtils.getSelectDbTable()); tableInfo = tableInfoService.getTableInfo(cacheDataUtils.getSelectDbTable());
} }
// 设置默认配置信息 // 设置默认配置信息
@ -328,6 +252,76 @@ public class SelectSavePath extends DialogWrapper {
} }
} }
@Override
protected void doOKAction() {
onOK();
super.doOKAction();
}
/**
* 确认按钮回调事件
*/
private void onOK() {
List<Template> selectTemplateList = templateSelectComponent.getAllSelectedTemplate();
// 如果选择的模板是空的
if (selectTemplateList.isEmpty()) {
Messages.showWarningDialog("Can't Select Template!", GlobalDict.TITLE_INFO);
return;
}
String savePath = pathField.getText();
if (StringUtils.isEmpty(savePath)) {
Messages.showWarningDialog("Can't Select Save Path!", GlobalDict.TITLE_INFO);
return;
}
// 针对Linux系统路径做处理
savePath = savePath.replace("\\", "/");
// 保存路径使用相对路径
String basePath = project.getBasePath();
if (!StringUtils.isEmpty(basePath) && savePath.startsWith(basePath)) {
if (savePath.length() > basePath.length()) {
if ("/".equals(savePath.substring(basePath.length(), basePath.length() + 1))) {
savePath = savePath.replace(basePath, ".");
}
} else {
savePath = savePath.replace(basePath, ".");
}
}
// 保存配置
TableInfo tableInfo;
if(!entityMode) {
tableInfo = tableInfoService.getTableInfo(cacheDataUtils.getSelectDbTable());
} else {
tableInfo = tableInfoService.getTableInfo(cacheDataUtils.getSelectPsiClass());
}
tableInfo.setSavePath(savePath);
tableInfo.setSavePackageName(packageField.getText());
tableInfo.setPreName(preField.getText());
tableInfo.setTemplateGroupName(templateSelectComponent.getselectedGroupName());
Module module = getSelectModule();
if (module != null) {
tableInfo.setSaveModelName(module.getName());
}
// 保存配置
tableInfoService.saveTableInfo(tableInfo);
// 生成代码
codeGenerateService.generateByUnifiedConfig(selectTemplateList, unifiedConfig.isSelected(), !titleConfig.isSelected(), this.entityMode);
}
/**
* 初始化方法
*/
private void initPanel() {
// 初始化模板组
this.templateSelectComponent = new TemplateSelectComponent();
templatePanel.add(this.templateSelectComponent.getMainPanel(), BorderLayout.CENTER);
//初始化Module选择
for (Module module : this.moduleList) {
moduleComboBox.addItem(module.getName());
}
}
/** /**
* 获取选中的Module * 获取选中的Module
* *