🎨 优化策略定义逻辑.

This commit is contained in:
lijiahangmax
2024-12-29 13:11:09 +08:00
parent 6606d2ca76
commit 6b14c2ef9c
11 changed files with 57 additions and 32 deletions

View File

@@ -34,7 +34,7 @@ import org.dromara.visor.framework.common.handler.data.strategy.GenericsDataStra
* @since 2023/12/21 0:07 * @since 2023/12/21 0:07
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public interface GenericsDataDefinition { public interface GenericsStrategyDefinition {
/** /**
* 获取数据处理策略 * 获取数据处理策略
@@ -54,12 +54,32 @@ public interface GenericsDataDefinition {
return (S) SpringHolder.getBean(this.getStrategyClass()); return (S) SpringHolder.getBean(this.getStrategyClass());
} }
/**
* 获取默认值
*
* @param <M> model
* @return model
*/
default <M extends GenericsDataModel> M getDefault() {
return (M) this.getStrategy().getDefault();
}
/**
* 执行完整验证链
*
* @param beforeModel beforeModel
* @param afterModel afterModel
*/
default void doValid(GenericsDataModel beforeModel, GenericsDataModel afterModel) {
this.getStrategy().doValid(beforeModel, afterModel);
}
/** /**
* 反序列化对象 * 反序列化对象
* *
* @param serialModel serialModel * @param serialModel serialModel
* @param <M> Model * @param <M> model
* @return object * @return model
*/ */
default <M extends GenericsDataModel> M parse(String serialModel) { default <M extends GenericsDataModel> M parse(String serialModel) {
return (M) this.getStrategy().parse(serialModel); return (M) this.getStrategy().parse(serialModel);
@@ -69,14 +89,11 @@ public interface GenericsDataDefinition {
* 转为视图对象 * 转为视图对象
* *
* @param serialModel serialModel * @param serialModel serialModel
* @param <M> Model * @param <M> model
* @return viewModel * @return viewModel
*/ */
default <M extends GenericsDataModel> M toView(String serialModel) { default <M extends GenericsDataModel> M toView(String serialModel) {
GenericsDataStrategy<GenericsDataModel> strategy = this.getStrategy(); return (M) this.getStrategy().toView(serialModel);
GenericsDataModel model = strategy.parse(serialModel);
strategy.toView(model);
return (M) model;
} }
} }

View File

@@ -84,4 +84,13 @@ public abstract class AbstractGenericsDataStrategy<M extends GenericsDataModel>
public void toView(M model) { public void toView(M model) {
} }
@Override
public M toView(String serialModel) {
// 解析
M parse = this.parse(serialModel);
// 转为视图对象
this.toView(parse);
return parse;
}
} }

View File

@@ -65,4 +65,12 @@ public interface GenericsDataStrategy<M extends GenericsDataModel> {
*/ */
void toView(M model); void toView(M model);
/**
* 转为视图配置
*
* @param serialModel serialModel
* @return model
*/
M toView(String serialModel);
} }

View File

@@ -24,7 +24,7 @@ package org.dromara.visor.module.asset.enums;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import org.dromara.visor.framework.common.handler.data.GenericsDataDefinition; import org.dromara.visor.framework.common.handler.data.GenericsStrategyDefinition;
import org.dromara.visor.framework.common.handler.data.model.GenericsDataModel; import org.dromara.visor.framework.common.handler.data.model.GenericsDataModel;
import org.dromara.visor.framework.common.handler.data.strategy.GenericsDataStrategy; import org.dromara.visor.framework.common.handler.data.strategy.GenericsDataStrategy;
import org.dromara.visor.module.asset.handler.host.extra.strategy.HostLabelExtraStrategy; import org.dromara.visor.module.asset.handler.host.extra.strategy.HostLabelExtraStrategy;
@@ -39,7 +39,7 @@ import org.dromara.visor.module.asset.handler.host.extra.strategy.HostSshExtraSt
*/ */
@Getter @Getter
@AllArgsConstructor @AllArgsConstructor
public enum HostExtraItemEnum implements GenericsDataDefinition { public enum HostExtraItemEnum implements GenericsStrategyDefinition {
/** /**
* SSH 额外配置 * SSH 额外配置

View File

@@ -24,7 +24,7 @@ package org.dromara.visor.module.asset.enums;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import org.dromara.visor.framework.common.handler.data.GenericsDataDefinition; import org.dromara.visor.framework.common.handler.data.GenericsStrategyDefinition;
import org.dromara.visor.framework.common.handler.data.model.GenericsDataModel; import org.dromara.visor.framework.common.handler.data.model.GenericsDataModel;
import org.dromara.visor.framework.common.handler.data.strategy.GenericsDataStrategy; import org.dromara.visor.framework.common.handler.data.strategy.GenericsDataStrategy;
import org.dromara.visor.module.asset.handler.host.config.strategy.HostSshConfigStrategy; import org.dromara.visor.module.asset.handler.host.config.strategy.HostSshConfigStrategy;
@@ -38,7 +38,7 @@ import org.dromara.visor.module.asset.handler.host.config.strategy.HostSshConfig
*/ */
@Getter @Getter
@AllArgsConstructor @AllArgsConstructor
public enum HostTypeEnum implements GenericsDataDefinition { public enum HostTypeEnum implements GenericsStrategyDefinition {
/** /**
* SSH * SSH

View File

@@ -25,7 +25,6 @@ package org.dromara.visor.module.asset.service.impl;
import cn.orionsec.kit.lang.function.Functions; import cn.orionsec.kit.lang.function.Functions;
import cn.orionsec.kit.lang.utils.collect.Maps; import cn.orionsec.kit.lang.utils.collect.Maps;
import org.dromara.visor.framework.common.handler.data.model.GenericsDataModel; import org.dromara.visor.framework.common.handler.data.model.GenericsDataModel;
import org.dromara.visor.framework.common.handler.data.strategy.GenericsDataStrategy;
import org.dromara.visor.framework.common.utils.Valid; import org.dromara.visor.framework.common.utils.Valid;
import org.dromara.visor.framework.security.core.utils.SecurityUtils; import org.dromara.visor.framework.security.core.utils.SecurityUtils;
import org.dromara.visor.module.asset.entity.request.host.HostExtraQueryRequest; import org.dromara.visor.module.asset.entity.request.host.HostExtraQueryRequest;
@@ -120,7 +119,6 @@ public class HostExtraServiceImpl implements HostExtraService {
Long hostId = request.getHostId(); Long hostId = request.getHostId();
Long userId = SecurityUtils.getLoginUserId(); Long userId = SecurityUtils.getLoginUserId();
HostExtraItemEnum item = Valid.valid(HostExtraItemEnum::of, request.getItem()); HostExtraItemEnum item = Valid.valid(HostExtraItemEnum::of, request.getItem());
GenericsDataStrategy<GenericsDataModel> strategy = item.getStrategy();
// 查询原始配置 // 查询原始配置
DataExtraQueryDTO query = DataExtraQueryDTO.builder() DataExtraQueryDTO query = DataExtraQueryDTO.builder()
.userId(userId) .userId(userId)
@@ -136,7 +134,7 @@ public class HostExtraServiceImpl implements HostExtraService {
GenericsDataModel newExtra = item.parse(request.getExtra()); GenericsDataModel newExtra = item.parse(request.getExtra());
GenericsDataModel beforeExtra = item.parse(beforeExtraItem.getValue()); GenericsDataModel beforeExtra = item.parse(beforeExtraItem.getValue());
// 更新验证 // 更新验证
strategy.doValid(beforeExtra, newExtra); item.doValid(beforeExtra, newExtra);
// 更新配置 // 更新配置
return dataExtraApi.updateExtraValue(beforeExtraItem.getId(), newExtra.serial()); return dataExtraApi.updateExtraValue(beforeExtraItem.getId(), newExtra.serial());
} }
@@ -167,9 +165,8 @@ public class HostExtraServiceImpl implements HostExtraService {
* @return defaultValue * @return defaultValue
*/ */
private String checkInitItem(HostExtraItemEnum item, Long userId, Long hostId) { private String checkInitItem(HostExtraItemEnum item, Long userId, Long hostId) {
GenericsDataStrategy<GenericsDataModel> strategy = item.getStrategy();
// 初始化默认数据 // 初始化默认数据
String extraValue = strategy.getDefault().serial(); String extraValue = item.getDefault().serial();
// 插入默认值 // 插入默认值
DataExtraSetDTO set = DataExtraSetDTO.builder() DataExtraSetDTO set = DataExtraSetDTO.builder()
.userId(userId) .userId(userId)

View File

@@ -116,7 +116,7 @@ public class HostServiceImpl implements HostService {
this.checkHostNamePresent(record); this.checkHostNamePresent(record);
this.checkHostCodePresent(record); this.checkHostCodePresent(record);
// 设置主机配置 // 设置主机配置
record.setConfig(type.getStrategy().getDefault().serial()); record.setConfig(type.getDefault().serial());
// 插入主机 // 插入主机
int effect = hostDAO.insert(record); int effect = hostDAO.insert(record);
log.info("HostService-createHost effect: {}", effect); log.info("HostService-createHost effect: {}", effect);
@@ -193,7 +193,7 @@ public class HostServiceImpl implements HostService {
OperatorLogs.add(OperatorLogs.ID, id); OperatorLogs.add(OperatorLogs.ID, id);
OperatorLogs.add(OperatorLogs.NAME, host.getName()); OperatorLogs.add(OperatorLogs.NAME, host.getName());
// 更新前校验 // 更新前校验
type.getStrategy().doValid(beforeConfig, newConfig); type.doValid(beforeConfig, newConfig);
// 修改配置 // 修改配置
HostDO updateHost = HostDO.builder() HostDO updateHost = HostDO.builder()
.id(id) .id(id)

View File

@@ -23,7 +23,7 @@
package org.dromara.visor.module.infra.enums; package org.dromara.visor.module.infra.enums;
import lombok.Getter; import lombok.Getter;
import org.dromara.visor.framework.common.handler.data.GenericsDataDefinition; import org.dromara.visor.framework.common.handler.data.GenericsStrategyDefinition;
import org.dromara.visor.framework.common.handler.data.model.GenericsDataModel; import org.dromara.visor.framework.common.handler.data.model.GenericsDataModel;
import org.dromara.visor.framework.common.handler.data.strategy.GenericsDataStrategy; import org.dromara.visor.framework.common.handler.data.strategy.GenericsDataStrategy;
import org.dromara.visor.module.infra.handler.preference.strategy.SystemPreferenceStrategy; import org.dromara.visor.module.infra.handler.preference.strategy.SystemPreferenceStrategy;
@@ -37,7 +37,7 @@ import org.dromara.visor.module.infra.handler.preference.strategy.TerminalPrefer
* @since 2023/10/8 11:31 * @since 2023/10/8 11:31
*/ */
@Getter @Getter
public enum PreferenceTypeEnum implements GenericsDataDefinition { public enum PreferenceTypeEnum implements GenericsStrategyDefinition {
/** /**
* 系统偏好 * 系统偏好

View File

@@ -24,7 +24,7 @@ package org.dromara.visor.module.infra.enums;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import org.dromara.visor.framework.common.handler.data.GenericsDataDefinition; import org.dromara.visor.framework.common.handler.data.GenericsStrategyDefinition;
import org.dromara.visor.framework.common.handler.data.model.GenericsDataModel; import org.dromara.visor.framework.common.handler.data.model.GenericsDataModel;
import org.dromara.visor.framework.common.handler.data.strategy.GenericsDataStrategy; import org.dromara.visor.framework.common.handler.data.strategy.GenericsDataStrategy;
import org.dromara.visor.module.infra.handler.setting.strategy.SftpSystemSettingStrategy; import org.dromara.visor.module.infra.handler.setting.strategy.SftpSystemSettingStrategy;
@@ -38,7 +38,7 @@ import org.dromara.visor.module.infra.handler.setting.strategy.SftpSystemSetting
*/ */
@Getter @Getter
@AllArgsConstructor @AllArgsConstructor
public enum SystemSettingTypeEnum implements GenericsDataDefinition { public enum SystemSettingTypeEnum implements GenericsStrategyDefinition {
/** /**
* SFTP 配置 * SFTP 配置

View File

@@ -164,9 +164,7 @@ public class PreferenceServiceImpl implements PreferenceService {
public Map<String, Object> getDefaultPreferenceByType(String type, List<String> items) { public Map<String, Object> getDefaultPreferenceByType(String type, List<String> items) {
PreferenceTypeEnum preferenceType = Valid.valid(PreferenceTypeEnum::of, type); PreferenceTypeEnum preferenceType = Valid.valid(PreferenceTypeEnum::of, type);
// 获取默认值 // 获取默认值
Map<String, Object> defaultModel = preferenceType.getStrategy() Map<String, Object> defaultModel = preferenceType.getDefault().toMap();
.getDefault()
.toMap();
Map<String, Object> result = Maps.newMap(); Map<String, Object> result = Maps.newMap();
if (Lists.isEmpty(items)) { if (Lists.isEmpty(items)) {
defaultModel.forEach((k, v) -> result.put(k, defaultModel.get(k))); defaultModel.forEach((k, v) -> result.put(k, defaultModel.get(k)));
@@ -240,9 +238,7 @@ public class PreferenceServiceImpl implements PreferenceService {
// 初始化 // 初始化
if (Maps.isEmpty(config)) { if (Maps.isEmpty(config)) {
// 获取默认值 // 获取默认值
Map<String, Object> defaultConfig = type.getStrategy() Map<String, Object> defaultConfig = type.getDefault().toMap();
.getDefault()
.toMap();
config = Maps.map(defaultConfig, Function.identity(), Refs::json); config = Maps.map(defaultConfig, Function.identity(), Refs::json);
// 插入默认值 // 插入默认值
List<PreferenceDO> entities = config List<PreferenceDO> entities = config

View File

@@ -148,9 +148,7 @@ public class SystemSettingServiceImpl implements SystemSettingService {
// 初始化 // 初始化
if (Maps.isEmpty(settings)) { if (Maps.isEmpty(settings)) {
// 获取默认值 // 获取默认值
Map<String, Object> defaultConfig = settingType.getStrategy() Map<String, Object> defaultConfig = settingType.getDefault().toMap();
.getDefault()
.toMap();
settings = Maps.map(defaultConfig, Function.identity(), Refs::json); settings = Maps.map(defaultConfig, Function.identity(), Refs::json);
// 插入默认值 // 插入默认值
List<SystemSettingDO> entities = settings List<SystemSettingDO> entities = settings