🎨 优化策略定义逻辑.

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
*/
@SuppressWarnings("unchecked")
public interface GenericsDataDefinition {
public interface GenericsStrategyDefinition {
/**
* 获取数据处理策略
@@ -54,12 +54,32 @@ public interface GenericsDataDefinition {
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 <M> Model
* @return object
* @param <M> model
* @return model
*/
default <M extends GenericsDataModel> M parse(String serialModel) {
return (M) this.getStrategy().parse(serialModel);
@@ -69,14 +89,11 @@ public interface GenericsDataDefinition {
* 转为视图对象
*
* @param serialModel serialModel
* @param <M> Model
* @param <M> model
* @return viewModel
*/
default <M extends GenericsDataModel> M toView(String serialModel) {
GenericsDataStrategy<GenericsDataModel> strategy = this.getStrategy();
GenericsDataModel model = strategy.parse(serialModel);
strategy.toView(model);
return (M) model;
return (M) this.getStrategy().toView(serialModel);
}
}

View File

@@ -84,4 +84,13 @@ public abstract class AbstractGenericsDataStrategy<M extends GenericsDataModel>
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);
/**
* 转为视图配置
*
* @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.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 额外配置

View File

@@ -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

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.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<GenericsDataModel> 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<GenericsDataModel> strategy = item.getStrategy();
// 初始化默认数据
String extraValue = strategy.getDefault().serial();
String extraValue = item.getDefault().serial();
// 插入默认值
DataExtraSetDTO set = DataExtraSetDTO.builder()
.userId(userId)

View File

@@ -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)

View File

@@ -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 {
/**
* 系统偏好

View File

@@ -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 配置

View File

@@ -164,9 +164,7 @@ public class PreferenceServiceImpl implements PreferenceService {
public Map<String, Object> getDefaultPreferenceByType(String type, List<String> items) {
PreferenceTypeEnum preferenceType = Valid.valid(PreferenceTypeEnum::of, type);
// 获取默认值
Map<String, Object> defaultModel = preferenceType.getStrategy()
.getDefault()
.toMap();
Map<String, Object> defaultModel = preferenceType.getDefault().toMap();
Map<String, Object> 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<String, Object> defaultConfig = type.getStrategy()
.getDefault()
.toMap();
Map<String, Object> defaultConfig = type.getDefault().toMap();
config = Maps.map(defaultConfig, Function.identity(), Refs::json);
// 插入默认值
List<PreferenceDO> entities = config

View File

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