From 6b14c2ef9c63865a9591e2008107763d88d0cd72 Mon Sep 17 00:00:00 2001 From: lijiahangmax Date: Sun, 29 Dec 2024 13:11:09 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E4=BC=98=E5=8C=96=E7=AD=96=E7=95=A5?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E9=80=BB=E8=BE=91.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...n.java => GenericsStrategyDefinition.java} | 33 ++++++++++++++----- .../AbstractGenericsDataStrategy.java | 9 +++++ .../data/strategy/GenericsDataStrategy.java | 8 +++++ .../module/asset/enums/HostExtraItemEnum.java | 4 +-- .../module/asset/enums/HostTypeEnum.java | 4 +-- .../service/impl/HostExtraServiceImpl.java | 7 ++-- .../asset/service/impl/HostServiceImpl.java | 4 +-- .../infra/enums/PreferenceTypeEnum.java | 4 +-- .../infra/enums/SystemSettingTypeEnum.java | 4 +-- .../service/impl/PreferenceServiceImpl.java | 8 ++--- .../impl/SystemSettingServiceImpl.java | 4 +-- 11 files changed, 57 insertions(+), 32 deletions(-) rename orion-visor-framework/orion-visor-framework-common/src/main/java/org/dromara/visor/framework/common/handler/data/{GenericsDataDefinition.java => GenericsStrategyDefinition.java} (75%) diff --git a/orion-visor-framework/orion-visor-framework-common/src/main/java/org/dromara/visor/framework/common/handler/data/GenericsDataDefinition.java b/orion-visor-framework/orion-visor-framework-common/src/main/java/org/dromara/visor/framework/common/handler/data/GenericsStrategyDefinition.java similarity index 75% rename from orion-visor-framework/orion-visor-framework-common/src/main/java/org/dromara/visor/framework/common/handler/data/GenericsDataDefinition.java rename to orion-visor-framework/orion-visor-framework-common/src/main/java/org/dromara/visor/framework/common/handler/data/GenericsStrategyDefinition.java index ffb45287..0a628c9d 100644 --- a/orion-visor-framework/orion-visor-framework-common/src/main/java/org/dromara/visor/framework/common/handler/data/GenericsDataDefinition.java +++ b/orion-visor-framework/orion-visor-framework-common/src/main/java/org/dromara/visor/framework/common/handler/data/GenericsStrategyDefinition.java @@ -34,7 +34,7 @@ import org.dromara.visor.framework.common.handler.data.strategy.GenericsDataStra * @since 2023/12/21 0:07 */ @SuppressWarnings("unchecked") -public interface GenericsDataDefinition { +public interface GenericsStrategyDefinition { /** * 获取数据处理策略 @@ -54,12 +54,32 @@ public interface GenericsDataDefinition { return (S) SpringHolder.getBean(this.getStrategyClass()); } + /** + * 获取默认值 + * + * @param model + * @return model + */ + default 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 Model - * @return object + * @param model + * @return model */ default M parse(String serialModel) { return (M) this.getStrategy().parse(serialModel); @@ -69,14 +89,11 @@ public interface GenericsDataDefinition { * 转为视图对象 * * @param serialModel serialModel - * @param Model + * @param model * @return viewModel */ default M toView(String serialModel) { - GenericsDataStrategy strategy = this.getStrategy(); - GenericsDataModel model = strategy.parse(serialModel); - strategy.toView(model); - return (M) model; + return (M) this.getStrategy().toView(serialModel); } } diff --git a/orion-visor-framework/orion-visor-framework-common/src/main/java/org/dromara/visor/framework/common/handler/data/strategy/AbstractGenericsDataStrategy.java b/orion-visor-framework/orion-visor-framework-common/src/main/java/org/dromara/visor/framework/common/handler/data/strategy/AbstractGenericsDataStrategy.java index 32386baa..25d32968 100644 --- a/orion-visor-framework/orion-visor-framework-common/src/main/java/org/dromara/visor/framework/common/handler/data/strategy/AbstractGenericsDataStrategy.java +++ b/orion-visor-framework/orion-visor-framework-common/src/main/java/org/dromara/visor/framework/common/handler/data/strategy/AbstractGenericsDataStrategy.java @@ -84,4 +84,13 @@ public abstract class AbstractGenericsDataStrategy public void toView(M model) { } + @Override + public M toView(String serialModel) { + // 解析 + M parse = this.parse(serialModel); + // 转为视图对象 + this.toView(parse); + return parse; + } + } diff --git a/orion-visor-framework/orion-visor-framework-common/src/main/java/org/dromara/visor/framework/common/handler/data/strategy/GenericsDataStrategy.java b/orion-visor-framework/orion-visor-framework-common/src/main/java/org/dromara/visor/framework/common/handler/data/strategy/GenericsDataStrategy.java index 97546c6e..74043f37 100644 --- a/orion-visor-framework/orion-visor-framework-common/src/main/java/org/dromara/visor/framework/common/handler/data/strategy/GenericsDataStrategy.java +++ b/orion-visor-framework/orion-visor-framework-common/src/main/java/org/dromara/visor/framework/common/handler/data/strategy/GenericsDataStrategy.java @@ -65,4 +65,12 @@ public interface GenericsDataStrategy { */ void toView(M model); + /** + * 转为视图配置 + * + * @param serialModel serialModel + * @return model + */ + M toView(String serialModel); + } diff --git a/orion-visor-module-asset/orion-visor-module-asset-service/src/main/java/org/dromara/visor/module/asset/enums/HostExtraItemEnum.java b/orion-visor-module-asset/orion-visor-module-asset-service/src/main/java/org/dromara/visor/module/asset/enums/HostExtraItemEnum.java index a2cef157..a3fd5fb5 100644 --- a/orion-visor-module-asset/orion-visor-module-asset-service/src/main/java/org/dromara/visor/module/asset/enums/HostExtraItemEnum.java +++ b/orion-visor-module-asset/orion-visor-module-asset-service/src/main/java/org/dromara/visor/module/asset/enums/HostExtraItemEnum.java @@ -24,7 +24,7 @@ package org.dromara.visor.module.asset.enums; import lombok.AllArgsConstructor; 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.strategy.GenericsDataStrategy; 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 @AllArgsConstructor -public enum HostExtraItemEnum implements GenericsDataDefinition { +public enum HostExtraItemEnum implements GenericsStrategyDefinition { /** * SSH 额外配置 diff --git a/orion-visor-module-asset/orion-visor-module-asset-service/src/main/java/org/dromara/visor/module/asset/enums/HostTypeEnum.java b/orion-visor-module-asset/orion-visor-module-asset-service/src/main/java/org/dromara/visor/module/asset/enums/HostTypeEnum.java index 863bbb23..157b79c5 100644 --- a/orion-visor-module-asset/orion-visor-module-asset-service/src/main/java/org/dromara/visor/module/asset/enums/HostTypeEnum.java +++ b/orion-visor-module-asset/orion-visor-module-asset-service/src/main/java/org/dromara/visor/module/asset/enums/HostTypeEnum.java @@ -24,7 +24,7 @@ package org.dromara.visor.module.asset.enums; import lombok.AllArgsConstructor; 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.strategy.GenericsDataStrategy; 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 @AllArgsConstructor -public enum HostTypeEnum implements GenericsDataDefinition { +public enum HostTypeEnum implements GenericsStrategyDefinition { /** * SSH diff --git a/orion-visor-module-asset/orion-visor-module-asset-service/src/main/java/org/dromara/visor/module/asset/service/impl/HostExtraServiceImpl.java b/orion-visor-module-asset/orion-visor-module-asset-service/src/main/java/org/dromara/visor/module/asset/service/impl/HostExtraServiceImpl.java index 042acf7f..612bcb04 100644 --- a/orion-visor-module-asset/orion-visor-module-asset-service/src/main/java/org/dromara/visor/module/asset/service/impl/HostExtraServiceImpl.java +++ b/orion-visor-module-asset/orion-visor-module-asset-service/src/main/java/org/dromara/visor/module/asset/service/impl/HostExtraServiceImpl.java @@ -25,7 +25,6 @@ package org.dromara.visor.module.asset.service.impl; import cn.orionsec.kit.lang.function.Functions; 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.strategy.GenericsDataStrategy; import org.dromara.visor.framework.common.utils.Valid; import org.dromara.visor.framework.security.core.utils.SecurityUtils; import org.dromara.visor.module.asset.entity.request.host.HostExtraQueryRequest; @@ -120,7 +119,6 @@ public class HostExtraServiceImpl implements HostExtraService { Long hostId = request.getHostId(); Long userId = SecurityUtils.getLoginUserId(); HostExtraItemEnum item = Valid.valid(HostExtraItemEnum::of, request.getItem()); - GenericsDataStrategy strategy = item.getStrategy(); // 查询原始配置 DataExtraQueryDTO query = DataExtraQueryDTO.builder() .userId(userId) @@ -136,7 +134,7 @@ public class HostExtraServiceImpl implements HostExtraService { GenericsDataModel newExtra = item.parse(request.getExtra()); GenericsDataModel beforeExtra = item.parse(beforeExtraItem.getValue()); // 更新验证 - strategy.doValid(beforeExtra, newExtra); + item.doValid(beforeExtra, newExtra); // 更新配置 return dataExtraApi.updateExtraValue(beforeExtraItem.getId(), newExtra.serial()); } @@ -167,9 +165,8 @@ public class HostExtraServiceImpl implements HostExtraService { * @return defaultValue */ private String checkInitItem(HostExtraItemEnum item, Long userId, Long hostId) { - GenericsDataStrategy strategy = item.getStrategy(); // 初始化默认数据 - String extraValue = strategy.getDefault().serial(); + String extraValue = item.getDefault().serial(); // 插入默认值 DataExtraSetDTO set = DataExtraSetDTO.builder() .userId(userId) diff --git a/orion-visor-module-asset/orion-visor-module-asset-service/src/main/java/org/dromara/visor/module/asset/service/impl/HostServiceImpl.java b/orion-visor-module-asset/orion-visor-module-asset-service/src/main/java/org/dromara/visor/module/asset/service/impl/HostServiceImpl.java index 0b1e13c0..f4270189 100644 --- a/orion-visor-module-asset/orion-visor-module-asset-service/src/main/java/org/dromara/visor/module/asset/service/impl/HostServiceImpl.java +++ b/orion-visor-module-asset/orion-visor-module-asset-service/src/main/java/org/dromara/visor/module/asset/service/impl/HostServiceImpl.java @@ -116,7 +116,7 @@ public class HostServiceImpl implements HostService { this.checkHostNamePresent(record); this.checkHostCodePresent(record); // 设置主机配置 - record.setConfig(type.getStrategy().getDefault().serial()); + record.setConfig(type.getDefault().serial()); // 插入主机 int effect = hostDAO.insert(record); log.info("HostService-createHost effect: {}", effect); @@ -193,7 +193,7 @@ public class HostServiceImpl implements HostService { OperatorLogs.add(OperatorLogs.ID, id); OperatorLogs.add(OperatorLogs.NAME, host.getName()); // 更新前校验 - type.getStrategy().doValid(beforeConfig, newConfig); + type.doValid(beforeConfig, newConfig); // 修改配置 HostDO updateHost = HostDO.builder() .id(id) diff --git a/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/enums/PreferenceTypeEnum.java b/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/enums/PreferenceTypeEnum.java index afe2a911..ec7dc044 100644 --- a/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/enums/PreferenceTypeEnum.java +++ b/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/enums/PreferenceTypeEnum.java @@ -23,7 +23,7 @@ package org.dromara.visor.module.infra.enums; 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.strategy.GenericsDataStrategy; 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 */ @Getter -public enum PreferenceTypeEnum implements GenericsDataDefinition { +public enum PreferenceTypeEnum implements GenericsStrategyDefinition { /** * 系统偏好 diff --git a/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/enums/SystemSettingTypeEnum.java b/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/enums/SystemSettingTypeEnum.java index 1cb822aa..17ad4cdf 100644 --- a/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/enums/SystemSettingTypeEnum.java +++ b/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/enums/SystemSettingTypeEnum.java @@ -24,7 +24,7 @@ package org.dromara.visor.module.infra.enums; import lombok.AllArgsConstructor; 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.strategy.GenericsDataStrategy; 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 @AllArgsConstructor -public enum SystemSettingTypeEnum implements GenericsDataDefinition { +public enum SystemSettingTypeEnum implements GenericsStrategyDefinition { /** * SFTP 配置 diff --git a/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/service/impl/PreferenceServiceImpl.java b/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/service/impl/PreferenceServiceImpl.java index dc922bbb..e2086258 100644 --- a/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/service/impl/PreferenceServiceImpl.java +++ b/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/service/impl/PreferenceServiceImpl.java @@ -164,9 +164,7 @@ public class PreferenceServiceImpl implements PreferenceService { public Map getDefaultPreferenceByType(String type, List items) { PreferenceTypeEnum preferenceType = Valid.valid(PreferenceTypeEnum::of, type); // 获取默认值 - Map defaultModel = preferenceType.getStrategy() - .getDefault() - .toMap(); + Map defaultModel = preferenceType.getDefault().toMap(); Map result = Maps.newMap(); if (Lists.isEmpty(items)) { defaultModel.forEach((k, v) -> result.put(k, defaultModel.get(k))); @@ -240,9 +238,7 @@ public class PreferenceServiceImpl implements PreferenceService { // 初始化 if (Maps.isEmpty(config)) { // 获取默认值 - Map defaultConfig = type.getStrategy() - .getDefault() - .toMap(); + Map defaultConfig = type.getDefault().toMap(); config = Maps.map(defaultConfig, Function.identity(), Refs::json); // 插入默认值 List entities = config diff --git a/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/service/impl/SystemSettingServiceImpl.java b/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/service/impl/SystemSettingServiceImpl.java index c3a9d6ef..b298085c 100644 --- a/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/service/impl/SystemSettingServiceImpl.java +++ b/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/service/impl/SystemSettingServiceImpl.java @@ -148,9 +148,7 @@ public class SystemSettingServiceImpl implements SystemSettingService { // 初始化 if (Maps.isEmpty(settings)) { // 获取默认值 - Map defaultConfig = settingType.getStrategy() - .getDefault() - .toMap(); + Map defaultConfig = settingType.getDefault().toMap(); settings = Maps.map(defaultConfig, Function.identity(), Refs::json); // 插入默认值 List entities = settings