优化标准数据模型.

This commit is contained in:
lijiahangmax
2024-06-12 00:21:25 +08:00
parent 3a5b84eec4
commit bfb80afee2
20 changed files with 205 additions and 224 deletions

View File

@@ -1,9 +1,8 @@
package com.orion.visor.framework.common.handler.data; package com.orion.visor.framework.common.handler.data;
import com.alibaba.fastjson.JSON;
import com.orion.spring.SpringHolder; import com.orion.spring.SpringHolder;
import com.orion.visor.framework.common.handler.data.model.GenericsDataModel; import com.orion.visor.framework.common.handler.data.model.GenericsDataModel;
import com.orion.visor.framework.common.handler.data.strategy.MapDataStrategy; import com.orion.visor.framework.common.handler.data.strategy.GenericsDataStrategy;
/** /**
* 标准数据定义 * 标准数据定义
@@ -12,45 +11,50 @@ import com.orion.visor.framework.common.handler.data.strategy.MapDataStrategy;
* @version 1.0.0 * @version 1.0.0
* @since 2023/12/21 0:07 * @since 2023/12/21 0:07
*/ */
@SuppressWarnings("unchecked")
public interface GenericsDataDefinition { public interface GenericsDataDefinition {
/**
* 获取数据模型类型
*
* @return class
*/
Class<? extends GenericsDataModel> getModel();
/** /**
* 获取数据处理策略 * 获取数据处理策略
* *
* @return class * @return class
*/ */
Class<? extends MapDataStrategy<? extends GenericsDataModel>> getStrategy(); Class<? extends GenericsDataStrategy<? extends GenericsDataModel>> getStrategyClass();
/** /**
* 获取数据模型策略处理器 * 获取数据模型策略处理器
* *
* @param <Model> Model * @param <M> Model
* @param <Strategy> Strategy * @param <S> Strategy
* @return StrategyBean * @return Strategy Bean
*/ */
@SuppressWarnings("unchecked") default <M extends GenericsDataModel, S extends GenericsDataStrategy<M>> S getStrategy() {
default <Model extends GenericsDataModel, Strategy extends MapDataStrategy<Model>> Strategy getStrategyBean() { return (S) SpringHolder.getBean(this.getStrategyClass());
return (Strategy) SpringHolder.getBean(this.getStrategy());
} }
/** /**
* 反序列化对象 * 反序列化对象
* *
* @param json json * @param serialModel serialModel
* @param <Model> Model * @param <M> Model
* @return object * @return object
*/ */
@SuppressWarnings("unchecked") default <M extends GenericsDataModel> M parse(String serialModel) {
default <Model extends GenericsDataModel> Model parse(String json) { return (M) this.getStrategy().parse(serialModel);
return (Model) JSON.parseObject(json, this.getModel());
} }
/**
* 转为视图对象
*
* @param serialModel serialModel
* @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;
}
} }

View File

@@ -2,6 +2,8 @@ package com.orion.visor.framework.common.handler.data.model;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import java.util.Map;
/** /**
* 标准数据模型 * 标准数据模型
* *
@@ -20,4 +22,14 @@ public interface GenericsDataModel {
return JSON.toJSONString(this); return JSON.toJSONString(this);
} }
/**
* 转为 map
*
* @return map
*/
default Map<String, Object> toMap() {
return JSON.parseObject(this.serial());
}
} }

View File

@@ -0,0 +1,65 @@
package com.orion.visor.framework.common.handler.data.strategy;
import com.alibaba.fastjson.JSON;
import com.orion.visor.framework.common.handler.data.model.GenericsDataModel;
/**
* 标准数据处理策略 基类
*
* @author Jiahang Li
* @version 1.0.0
* @since 2024/6/11 21:44
*/
public abstract class AbstractGenericsDataStrategy<M extends GenericsDataModel> implements GenericsDataStrategy<M> {
protected final Class<M> modelClass;
public AbstractGenericsDataStrategy(Class<M> modelClass) {
this.modelClass = modelClass;
}
/**
* 更新填充
*
* @param beforeModel 修改前的配置
* @param afterModel 修改后的配置
*/
protected void updateFill(M beforeModel, M afterModel) {
}
/**
* 预校验参数
*
* @param model model
*/
protected void preValid(M model) {
}
/**
* 校验参数
*
* @param model model
*/
protected void valid(M model) {
}
@Override
public void doValid(M beforeModel, M afterModel) {
// 预校验参数
this.preValid(afterModel);
// 更新填充
this.updateFill(beforeModel, afterModel);
// 校验参数
this.valid(afterModel);
}
@Override
public M parse(String serialModel) {
return JSON.parseObject(serialModel, modelClass);
}
@Override
public void toView(M model) {
}
}

View File

@@ -9,60 +9,38 @@ import com.orion.visor.framework.common.handler.data.model.GenericsDataModel;
* @version 1.0.0 * @version 1.0.0
* @since 2023/12/20 22:09 * @since 2023/12/20 22:09
*/ */
public interface GenericsDataStrategy<Model extends GenericsDataModel, View> { public interface GenericsDataStrategy<M extends GenericsDataModel> {
/** /**
* 获取默认值 * 获取默认值
* *
* @return 默认值 * @return 默认值
*/ */
Model getDefault(); M getDefault();
/**
* 更新填充
*
* @param beforeModel 修改前的配置
* @param afterModel 修改后的配置
*/
void updateFill(Model beforeModel, Model afterModel);
/**
* 预校验参数
*
* @param model model
*/
void preValid(Model model);
/**
* 校验参数
*
* @param model model
*/
void valid(Model model);
/** /**
* 执行完整验证链 * 执行完整验证链
* <p> * <p>
* preValid > updateFill > preValid * preValid > updateFill > valid
* *
* @param beforeModel beforeModel * @param beforeModel beforeModel
* @param afterModel afterModel * @param afterModel afterModel
*/ */
default void doValidChain(Model beforeModel, Model afterModel) { void doValid(M beforeModel, M afterModel);
// 预校验参数
this.preValid(afterModel); /**
// 更新填充 * 解析数据
this.updateFill(beforeModel, afterModel); *
// 校验参数 * @param serialModel serialModel
this.valid(afterModel); * @return model
} */
M parse(String serialModel);
/** /**
* 转为视图配置 * 转为视图配置
* *
* @param model model * @param model model
* @return 视图配置
*/ */
View toView(String model); void toView(M model);
} }

View File

@@ -1,22 +0,0 @@
package com.orion.visor.framework.common.handler.data.strategy;
import com.alibaba.fastjson.JSONObject;
import com.orion.visor.framework.common.handler.data.model.GenericsDataModel;
import java.util.Map;
/**
* map 数据处理策略
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023/12/20 22:11
*/
public interface MapDataStrategy<Model extends GenericsDataModel> extends GenericsDataStrategy<Model, Map<String, Object>> {
@Override
default Map<String, Object> toView(String model) {
return JSONObject.parseObject(model);
}
}

View File

@@ -3,8 +3,7 @@ package com.orion.visor.module.asset.enums;
import com.orion.visor.framework.common.enums.EnableStatus; import com.orion.visor.framework.common.enums.EnableStatus;
import com.orion.visor.framework.common.handler.data.GenericsDataDefinition; import com.orion.visor.framework.common.handler.data.GenericsDataDefinition;
import com.orion.visor.framework.common.handler.data.model.GenericsDataModel; import com.orion.visor.framework.common.handler.data.model.GenericsDataModel;
import com.orion.visor.framework.common.handler.data.strategy.MapDataStrategy; import com.orion.visor.framework.common.handler.data.strategy.GenericsDataStrategy;
import com.orion.visor.module.asset.handler.host.config.model.HostSshConfigModel;
import com.orion.visor.module.asset.handler.host.config.strategy.HostSshConfigStrategy; import com.orion.visor.module.asset.handler.host.config.strategy.HostSshConfigStrategy;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
@@ -24,7 +23,6 @@ public enum HostConfigTypeEnum implements GenericsDataDefinition {
* SSH 配置 * SSH 配置
*/ */
SSH("ssh", SSH("ssh",
HostSshConfigModel.class,
HostSshConfigStrategy.class, HostSshConfigStrategy.class,
EnableStatus.ENABLED.getValue()), EnableStatus.ENABLED.getValue()),
@@ -32,9 +30,7 @@ public enum HostConfigTypeEnum implements GenericsDataDefinition {
private final String type; private final String type;
private final Class<? extends GenericsDataModel> model; private final Class<? extends GenericsDataStrategy<? extends GenericsDataModel>> strategyClass;
private final Class<? extends MapDataStrategy<? extends GenericsDataModel>> strategy;
private final Integer defaultStatus; private final Integer defaultStatus;

View File

@@ -2,9 +2,7 @@ package com.orion.visor.module.asset.enums;
import com.orion.visor.framework.common.handler.data.GenericsDataDefinition; import com.orion.visor.framework.common.handler.data.GenericsDataDefinition;
import com.orion.visor.framework.common.handler.data.model.GenericsDataModel; import com.orion.visor.framework.common.handler.data.model.GenericsDataModel;
import com.orion.visor.framework.common.handler.data.strategy.MapDataStrategy; import com.orion.visor.framework.common.handler.data.strategy.GenericsDataStrategy;
import com.orion.visor.module.asset.handler.host.extra.model.HostLabelExtraModel;
import com.orion.visor.module.asset.handler.host.extra.model.HostSshExtraModel;
import com.orion.visor.module.asset.handler.host.extra.strategy.HostLabelExtraStrategy; import com.orion.visor.module.asset.handler.host.extra.strategy.HostLabelExtraStrategy;
import com.orion.visor.module.asset.handler.host.extra.strategy.HostSshExtraStrategy; import com.orion.visor.module.asset.handler.host.extra.strategy.HostSshExtraStrategy;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
@@ -24,20 +22,18 @@ public enum HostExtraItemEnum implements GenericsDataDefinition {
/** /**
* SSH 额外配置 * SSH 额外配置
*/ */
SSH("ssh", HostSshExtraModel.class, HostSshExtraStrategy.class), SSH("ssh", HostSshExtraStrategy.class),
/** /**
* 标签额外配置 * 标签额外配置
*/ */
LABEL("label", HostLabelExtraModel.class, HostLabelExtraStrategy.class), LABEL("label", HostLabelExtraStrategy.class),
; ;
private final String item; private final String item;
private final Class<? extends GenericsDataModel> model; private final Class<? extends GenericsDataStrategy<? extends GenericsDataModel>> strategyClass;
private final Class<? extends MapDataStrategy<? extends GenericsDataModel>> strategy;
public static HostExtraItemEnum of(String type) { public static HostExtraItemEnum of(String type) {
if (type == null) { if (type == null) {

View File

@@ -1,13 +1,12 @@
package com.orion.visor.module.asset.handler.host.config.strategy; package com.orion.visor.module.asset.handler.host.config.strategy;
import com.alibaba.fastjson.JSON;
import com.orion.lang.utils.Booleans; import com.orion.lang.utils.Booleans;
import com.orion.lang.utils.Charsets; import com.orion.lang.utils.Charsets;
import com.orion.lang.utils.Exceptions; import com.orion.lang.utils.Exceptions;
import com.orion.lang.utils.Strings; import com.orion.lang.utils.Strings;
import com.orion.visor.framework.common.constant.Const; import com.orion.visor.framework.common.constant.Const;
import com.orion.visor.framework.common.constant.ErrorMessage; import com.orion.visor.framework.common.constant.ErrorMessage;
import com.orion.visor.framework.common.handler.data.strategy.MapDataStrategy; import com.orion.visor.framework.common.handler.data.strategy.AbstractGenericsDataStrategy;
import com.orion.visor.framework.common.security.PasswordModifier; import com.orion.visor.framework.common.security.PasswordModifier;
import com.orion.visor.framework.common.utils.Valid; import com.orion.visor.framework.common.utils.Valid;
import com.orion.visor.module.asset.dao.HostIdentityDAO; import com.orion.visor.module.asset.dao.HostIdentityDAO;
@@ -18,7 +17,6 @@ import com.orion.visor.module.asset.handler.host.config.model.HostSshConfigModel
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.Map;
/** /**
* 主机 SSH 配置策略 * 主机 SSH 配置策略
@@ -28,7 +26,7 @@ import java.util.Map;
* @since 2023/9/19 14:26 * @since 2023/9/19 14:26
*/ */
@Component @Component
public class HostSshConfigStrategy implements MapDataStrategy<HostSshConfigModel> { public class HostSshConfigStrategy extends AbstractGenericsDataStrategy<HostSshConfigModel> {
@Resource @Resource
private HostKeyDAO hostKeyDAO; private HostKeyDAO hostKeyDAO;
@@ -40,6 +38,10 @@ public class HostSshConfigStrategy implements MapDataStrategy<HostSshConfigModel
private static final String USERNAME = "root"; private static final String USERNAME = "root";
public HostSshConfigStrategy() {
super(HostSshConfigModel.class);
}
@Override @Override
public HostSshConfigModel getDefault() { public HostSshConfigModel getDefault() {
return HostSshConfigModel.builder() return HostSshConfigModel.builder()
@@ -55,7 +57,7 @@ public class HostSshConfigStrategy implements MapDataStrategy<HostSshConfigModel
} }
@Override @Override
public void preValid(HostSshConfigModel model) { protected void preValid(HostSshConfigModel model) {
// 验证认证类型 // 验证认证类型
Valid.valid(HostSshAuthTypeEnum::of, model.getAuthType()); Valid.valid(HostSshAuthTypeEnum::of, model.getAuthType());
// 验证系统版本 // 验证系统版本
@@ -77,13 +79,13 @@ public class HostSshConfigStrategy implements MapDataStrategy<HostSshConfigModel
} }
@Override @Override
public void valid(HostSshConfigModel model) { protected void valid(HostSshConfigModel model) {
// 验证填充后的参数 // 验证填充后的参数
Valid.valid(model); Valid.valid(model);
} }
@Override @Override
public void updateFill(HostSshConfigModel beforeModel, HostSshConfigModel afterModel) { protected void updateFill(HostSshConfigModel beforeModel, HostSshConfigModel afterModel) {
// 加密密码 // 加密密码
this.checkEncryptPassword(beforeModel, afterModel); this.checkEncryptPassword(beforeModel, afterModel);
afterModel.setHasPassword(null); afterModel.setHasPassword(null);
@@ -91,14 +93,12 @@ public class HostSshConfigStrategy implements MapDataStrategy<HostSshConfigModel
} }
@Override @Override
public Map<String, Object> toView(String config) { public void toView(HostSshConfigModel model) {
if (config == null) { if (model == null) {
return null; return;
} }
HostSshConfigModel model = JSON.parseObject(config, HostSshConfigModel.class);
model.setHasPassword(Strings.isNotBlank(model.getPassword())); model.setHasPassword(Strings.isNotBlank(model.getPassword()));
model.setPassword(null); model.setPassword(null);
return JSON.parseObject(JSON.toJSONString(model));
} }
/** /**

View File

@@ -1,7 +1,7 @@
package com.orion.visor.module.asset.handler.host.extra.strategy; package com.orion.visor.module.asset.handler.host.extra.strategy;
import com.orion.visor.framework.common.constant.Const; import com.orion.visor.framework.common.constant.Const;
import com.orion.visor.framework.common.handler.data.strategy.MapDataStrategy; import com.orion.visor.framework.common.handler.data.strategy.AbstractGenericsDataStrategy;
import com.orion.visor.module.asset.handler.host.extra.model.HostLabelExtraModel; import com.orion.visor.module.asset.handler.host.extra.model.HostLabelExtraModel;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@@ -13,7 +13,11 @@ import org.springframework.stereotype.Component;
* @since 2024/2/29 23:16 * @since 2024/2/29 23:16
*/ */
@Component @Component
public class HostLabelExtraStrategy implements MapDataStrategy<HostLabelExtraModel> { public class HostLabelExtraStrategy extends AbstractGenericsDataStrategy<HostLabelExtraModel> {
public HostLabelExtraStrategy() {
super(HostLabelExtraModel.class);
}
@Override @Override
public HostLabelExtraModel getDefault() { public HostLabelExtraModel getDefault() {
@@ -36,12 +40,4 @@ public class HostLabelExtraStrategy implements MapDataStrategy<HostLabelExtraMod
} }
} }
@Override
public void preValid(HostLabelExtraModel model) {
}
@Override
public void valid(HostLabelExtraModel model) {
}
} }

View File

@@ -1,7 +1,7 @@
package com.orion.visor.module.asset.handler.host.extra.strategy; package com.orion.visor.module.asset.handler.host.extra.strategy;
import com.orion.visor.framework.common.constant.ErrorMessage; import com.orion.visor.framework.common.constant.ErrorMessage;
import com.orion.visor.framework.common.handler.data.strategy.MapDataStrategy; import com.orion.visor.framework.common.handler.data.strategy.AbstractGenericsDataStrategy;
import com.orion.visor.framework.common.utils.Valid; import com.orion.visor.framework.common.utils.Valid;
import com.orion.visor.framework.security.core.utils.SecurityUtils; import com.orion.visor.framework.security.core.utils.SecurityUtils;
import com.orion.visor.module.asset.dao.HostIdentityDAO; import com.orion.visor.module.asset.dao.HostIdentityDAO;
@@ -22,7 +22,7 @@ import javax.annotation.Resource;
* @since 2023/12/20 22:17 * @since 2023/12/20 22:17
*/ */
@Component @Component
public class HostSshExtraStrategy implements MapDataStrategy<HostSshExtraModel> { public class HostSshExtraStrategy extends AbstractGenericsDataStrategy<HostSshExtraModel> {
@Resource @Resource
private HostKeyDAO hostKeyDAO; private HostKeyDAO hostKeyDAO;
@@ -33,6 +33,10 @@ public class HostSshExtraStrategy implements MapDataStrategy<HostSshExtraModel>
@Resource @Resource
private DataPermissionApi dataPermissionApi; private DataPermissionApi dataPermissionApi;
public HostSshExtraStrategy() {
super(HostSshExtraModel.class);
}
@Override @Override
public HostSshExtraModel getDefault() { public HostSshExtraModel getDefault() {
return HostSshExtraModel.builder() return HostSshExtraModel.builder()
@@ -40,10 +44,6 @@ public class HostSshExtraStrategy implements MapDataStrategy<HostSshExtraModel>
.build(); .build();
} }
@Override
public void updateFill(HostSshExtraModel beforeModel, HostSshExtraModel afterModel) {
}
@Override @Override
public void preValid(HostSshExtraModel model) { public void preValid(HostSshExtraModel model) {
HostExtraSshAuthTypeEnum authType = Valid.valid(HostExtraSshAuthTypeEnum::of, model.getAuthType()); HostExtraSshAuthTypeEnum authType = Valid.valid(HostExtraSshAuthTypeEnum::of, model.getAuthType());
@@ -79,8 +79,4 @@ public class HostSshExtraStrategy implements MapDataStrategy<HostSshExtraModel>
} }
} }
@Override
public void valid(HostSshExtraModel model) {
}
} }

View File

@@ -8,7 +8,7 @@ import com.orion.visor.framework.common.constant.ErrorMessage;
import com.orion.visor.framework.common.enums.BooleanBit; import com.orion.visor.framework.common.enums.BooleanBit;
import com.orion.visor.framework.common.enums.EnableStatus; import com.orion.visor.framework.common.enums.EnableStatus;
import com.orion.visor.framework.common.handler.data.model.GenericsDataModel; import com.orion.visor.framework.common.handler.data.model.GenericsDataModel;
import com.orion.visor.framework.common.handler.data.strategy.MapDataStrategy; import com.orion.visor.framework.common.handler.data.strategy.GenericsDataStrategy;
import com.orion.visor.framework.common.utils.Valid; import com.orion.visor.framework.common.utils.Valid;
import com.orion.visor.module.asset.convert.HostConfigConvert; import com.orion.visor.module.asset.convert.HostConfigConvert;
import com.orion.visor.module.asset.dao.HostConfigDAO; import com.orion.visor.module.asset.dao.HostConfigDAO;
@@ -53,7 +53,7 @@ public class HostConfigServiceImpl implements HostConfigService {
// 转换 // 转换
HostConfigVO vo = HostConfigConvert.MAPPER.to(config); HostConfigVO vo = HostConfigConvert.MAPPER.to(config);
// 获取配置 // 获取配置
Map<String, Object> configMap = configType.getStrategyBean().toView(config.getConfig()); Map<String, Object> configMap = configType.toView(config.getConfig()).toMap();
vo.setConfig(configMap); vo.setConfig(configMap);
return vo; return vo;
} }
@@ -121,10 +121,10 @@ public class HostConfigServiceImpl implements HostConfigService {
OperatorLogs.add(OperatorLogs.TYPE, type.getType()); OperatorLogs.add(OperatorLogs.TYPE, type.getType());
// 检查版本 // 检查版本
Valid.eq(record.getVersion(), request.getVersion(), ErrorMessage.DATA_MODIFIED); Valid.eq(record.getVersion(), request.getVersion(), ErrorMessage.DATA_MODIFIED);
MapDataStrategy<GenericsDataModel> strategy = type.getStrategyBean(); GenericsDataStrategy<GenericsDataModel> strategy = type.getStrategy();
GenericsDataModel beforeConfig = type.parse(record.getConfig()); GenericsDataModel beforeConfig = type.parse(record.getConfig());
// 更新前校验 // 更新前校验
strategy.doValidChain(beforeConfig, newConfig); strategy.doValid(beforeConfig, newConfig);
// 修改配置 // 修改配置
HostConfigDO update = new HostConfigDO(); HostConfigDO update = new HostConfigDO();
update.setId(record.getId()); update.setId(record.getId());
@@ -225,7 +225,7 @@ public class HostConfigServiceImpl implements HostConfigService {
insert.setHostId(hostId); insert.setHostId(hostId);
insert.setType(type.getType()); insert.setType(type.getType());
insert.setStatus(type.getDefaultStatus()); insert.setStatus(type.getDefaultStatus());
insert.setConfig(type.getStrategyBean().getDefault().serial()); insert.setConfig(type.getStrategy().getDefault().serial());
insert.setVersion(Const.DEFAULT_VERSION); insert.setVersion(Const.DEFAULT_VERSION);
return insert; return insert;
} }
@@ -244,7 +244,7 @@ public class HostConfigServiceImpl implements HostConfigService {
} }
// 转为视图 // 转为视图
HostConfigVO vo = HostConfigConvert.MAPPER.to(row); HostConfigVO vo = HostConfigConvert.MAPPER.to(row);
Map<String, Object> config = type.getStrategyBean().toView(row.getConfig()); Map<String, Object> config = type.toView(row.getConfig()).toMap();
vo.setConfig(config); vo.setConfig(config);
return vo; return vo;
} }

View File

@@ -3,7 +3,7 @@ package com.orion.visor.module.asset.service.impl;
import com.orion.lang.function.Functions; import com.orion.lang.function.Functions;
import com.orion.lang.utils.collect.Maps; import com.orion.lang.utils.collect.Maps;
import com.orion.visor.framework.common.handler.data.model.GenericsDataModel; import com.orion.visor.framework.common.handler.data.model.GenericsDataModel;
import com.orion.visor.framework.common.handler.data.strategy.MapDataStrategy; import com.orion.visor.framework.common.handler.data.strategy.GenericsDataStrategy;
import com.orion.visor.framework.common.utils.Valid; import com.orion.visor.framework.common.utils.Valid;
import com.orion.visor.framework.security.core.utils.SecurityUtils; import com.orion.visor.framework.security.core.utils.SecurityUtils;
import com.orion.visor.module.asset.entity.request.host.HostExtraQueryRequest; import com.orion.visor.module.asset.entity.request.host.HostExtraQueryRequest;
@@ -98,7 +98,7 @@ 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());
MapDataStrategy<GenericsDataModel> strategy = item.getStrategyBean(); GenericsDataStrategy<GenericsDataModel> strategy = item.getStrategy();
// 查询原始配置 // 查询原始配置
DataExtraQueryDTO query = DataExtraQueryDTO.builder() DataExtraQueryDTO query = DataExtraQueryDTO.builder()
.userId(userId) .userId(userId)
@@ -114,7 +114,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.doValidChain(beforeExtra, newExtra); strategy.doValid(beforeExtra, newExtra);
// 更新配置 // 更新配置
return dataExtraApi.updateExtraValue(beforeExtraItem.getId(), newExtra.serial()); return dataExtraApi.updateExtraValue(beforeExtraItem.getId(), newExtra.serial());
} }
@@ -129,12 +129,11 @@ public class HostExtraServiceImpl implements HostExtraService {
* @return viewMap * @return viewMap
*/ */
private Map<String, Object> checkItemAndToView(HostExtraItemEnum item, String extraValue, Long userId, Long hostId) { private Map<String, Object> checkItemAndToView(HostExtraItemEnum item, String extraValue, Long userId, Long hostId) {
MapDataStrategy<GenericsDataModel> strategy = item.getStrategyBean();
if (extraValue == null) { if (extraValue == null) {
// 初始化默认数据 // 初始化默认数据
extraValue = this.checkInitItem(item, userId, hostId); extraValue = this.checkInitItem(item, userId, hostId);
} }
return strategy.toView(extraValue); return item.toView(extraValue).toMap();
} }
/** /**
@@ -146,7 +145,7 @@ 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) {
MapDataStrategy<GenericsDataModel> strategy = item.getStrategyBean(); GenericsDataStrategy<GenericsDataModel> strategy = item.getStrategy();
// 初始化默认数据 // 初始化默认数据
String extraValue = strategy.getDefault().serial(); String extraValue = strategy.getDefault().serial();
// 插入默认值 // 插入默认值

View File

@@ -1,30 +0,0 @@
package com.orion.visor.module.infra.handler.preference.model;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.orion.lang.utils.Refs;
import com.orion.lang.utils.collect.Maps;
import java.util.Map;
import java.util.function.Function;
/**
* 偏好
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023/10/8 13:54
*/
public interface PreferenceModel {
/**
* 转为 map
*
* @return map
*/
default Map<String, String> toMap() {
JSONObject map = JSON.parseObject(JSON.toJSONString(this));
return Maps.map(map, Function.identity(), Refs::json);
}
}

View File

@@ -1,21 +0,0 @@
package com.orion.visor.module.infra.handler.preference.strategy;
import com.orion.visor.module.infra.handler.preference.model.PreferenceModel;
/**
* 偏好处理策略
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023/10/8 13:49
*/
public interface IPreferenceStrategy<Model extends PreferenceModel> {
/**
* 获取默认值
*
* @return 默认值
*/
Model getDefault();
}

View File

@@ -1,8 +1,10 @@
package com.orion.visor.module.infra.enums; package com.orion.visor.module.infra.enums;
import com.orion.spring.SpringHolder; import com.orion.visor.framework.common.handler.data.GenericsDataDefinition;
import com.orion.visor.module.infra.handler.preference.model.PreferenceModel; import com.orion.visor.framework.common.handler.data.model.GenericsDataModel;
import com.orion.visor.module.infra.handler.preference.strategy.IPreferenceStrategy; import com.orion.visor.framework.common.handler.data.strategy.GenericsDataStrategy;
import com.orion.visor.module.infra.handler.preference.strategy.SystemPreferenceStrategy;
import com.orion.visor.module.infra.handler.preference.strategy.TerminalPreferenceStrategy;
import lombok.Getter; import lombok.Getter;
/** /**
@@ -13,32 +15,28 @@ import lombok.Getter;
* @since 2023/10/8 11:31 * @since 2023/10/8 11:31
*/ */
@Getter @Getter
public enum PreferenceTypeEnum { public enum PreferenceTypeEnum implements GenericsDataDefinition {
/** /**
* 系统偏好 * 系统偏好
*/ */
SYSTEM("systemPreferenceStrategy"), SYSTEM(SystemPreferenceStrategy.class),
/** /**
* 终端偏好 * 终端偏好
*/ */
TERMINAL("terminalPreferenceStrategy"), TERMINAL(TerminalPreferenceStrategy.class),
; ;
PreferenceTypeEnum(String beanName) { PreferenceTypeEnum(Class<? extends GenericsDataStrategy<? extends GenericsDataModel>> strategyClass) {
this.type = this.name(); this.type = this.name();
this.beanName = beanName; this.strategyClass = strategyClass;
} }
private final String type; private final String type;
/** private final Class<? extends GenericsDataStrategy<? extends GenericsDataModel>> strategyClass;
* 策越 bean 名称
* 可能跨模块所以不用 class
*/
private final String beanName;
public static PreferenceTypeEnum of(String type) { public static PreferenceTypeEnum of(String type) {
if (type == null) { if (type == null) {
@@ -52,15 +50,4 @@ public enum PreferenceTypeEnum {
return null; return null;
} }
/**
* 获取策略
*
* @param <M> model
* @param <T> type
* @return IPreferenceStrategy
*/
public <M extends PreferenceModel, T extends IPreferenceStrategy<M>> T getStrategy() {
return SpringHolder.getBean(beanName);
}
} }

View File

@@ -1,5 +1,6 @@
package com.orion.visor.module.infra.handler.preference.model; package com.orion.visor.module.infra.handler.preference.model;
import com.orion.visor.framework.common.handler.data.model.GenericsDataModel;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Builder; import lombok.Builder;
import lombok.Data; import lombok.Data;
@@ -16,7 +17,7 @@ import lombok.NoArgsConstructor;
@Builder @Builder
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
public class SystemPreferenceModel implements PreferenceModel { public class SystemPreferenceModel implements GenericsDataModel {
/** /**
* 是否使用侧边菜单 * 是否使用侧边菜单

View File

@@ -2,6 +2,7 @@ package com.orion.visor.module.infra.handler.preference.model;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.orion.lang.able.IJsonObject; import com.orion.lang.able.IJsonObject;
import com.orion.visor.framework.common.handler.data.model.GenericsDataModel;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Builder; import lombok.Builder;
import lombok.Data; import lombok.Data;
@@ -20,7 +21,7 @@ import java.util.List;
@Builder @Builder
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
public class TerminalPreferenceModel implements PreferenceModel { public class TerminalPreferenceModel implements GenericsDataModel {
/** /**
* 新建连接类型 * 新建连接类型

View File

@@ -1,5 +1,7 @@
package com.orion.visor.module.infra.handler.preference.strategy; package com.orion.visor.module.infra.handler.preference.strategy;
import com.orion.lang.utils.Exceptions;
import com.orion.visor.framework.common.handler.data.strategy.AbstractGenericsDataStrategy;
import com.orion.visor.module.infra.handler.preference.model.SystemPreferenceModel; import com.orion.visor.module.infra.handler.preference.model.SystemPreferenceModel;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@@ -10,8 +12,12 @@ import org.springframework.stereotype.Component;
* @version 1.0.0 * @version 1.0.0
* @since 2023/10/8 13:48 * @since 2023/10/8 13:48
*/ */
@Component("systemPreferenceStrategy") @Component
public class SystemPreferenceStrategy implements IPreferenceStrategy<SystemPreferenceModel> { public class SystemPreferenceStrategy extends AbstractGenericsDataStrategy<SystemPreferenceModel> {
public SystemPreferenceStrategy() {
super(SystemPreferenceModel.class);
}
@Override @Override
public SystemPreferenceModel getDefault() { public SystemPreferenceModel getDefault() {
@@ -28,4 +34,9 @@ public class SystemPreferenceStrategy implements IPreferenceStrategy<SystemPrefe
.build(); .build();
} }
@Override
public SystemPreferenceModel parse(String serialModel) {
throw Exceptions.unsupported();
}
} }

View File

@@ -1,8 +1,10 @@
package com.orion.visor.module.infra.handler.preference.strategy; package com.orion.visor.module.infra.handler.preference.strategy;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.orion.lang.utils.Exceptions;
import com.orion.lang.utils.collect.Lists; import com.orion.lang.utils.collect.Lists;
import com.orion.net.host.ssh.TerminalType; import com.orion.net.host.ssh.TerminalType;
import com.orion.visor.framework.common.handler.data.strategy.AbstractGenericsDataStrategy;
import com.orion.visor.module.infra.handler.preference.model.TerminalPreferenceModel; import com.orion.visor.module.infra.handler.preference.model.TerminalPreferenceModel;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@@ -13,8 +15,12 @@ import org.springframework.stereotype.Component;
* @version 1.0.0 * @version 1.0.0
* @since 2023/12/8 14:46 * @since 2023/12/8 14:46
*/ */
@Component("terminalPreferenceStrategy") @Component
public class TerminalPreferenceStrategy implements IPreferenceStrategy<TerminalPreferenceModel> { public class TerminalPreferenceStrategy extends AbstractGenericsDataStrategy<TerminalPreferenceModel> {
public TerminalPreferenceStrategy() {
super(TerminalPreferenceModel.class);
}
@Override @Override
public TerminalPreferenceModel getDefault() { public TerminalPreferenceModel getDefault() {
@@ -24,7 +30,7 @@ public class TerminalPreferenceStrategy implements IPreferenceStrategy<TerminalP
.fontFamily("_") .fontFamily("_")
.fontSize(13) .fontSize(13)
.lineHeight(1.12) .lineHeight(1.12)
.letterSpacing(1) .letterSpacing(0)
.fontWeight("normal") .fontWeight("normal")
.fontWeightBold("bold") .fontWeightBold("bold")
.cursorStyle("bar") .cursorStyle("bar")
@@ -100,7 +106,7 @@ public class TerminalPreferenceStrategy implements IPreferenceStrategy<TerminalP
.selectAll(false) .selectAll(false)
.search(true) .search(true)
.copy(true) .copy(true)
.paste(false) .paste(true)
.interrupt(false) .interrupt(false)
.enter(false) .enter(false)
.fontSizePlus(false) .fontSizePlus(false)
@@ -118,7 +124,7 @@ public class TerminalPreferenceStrategy implements IPreferenceStrategy<TerminalP
.theme(new JSONObject()) .theme(new JSONObject())
.displaySetting(JSONObject.parseObject(defaultDisplaySetting)) .displaySetting(JSONObject.parseObject(defaultDisplaySetting))
.actionBarSetting(JSONObject.parseObject(actionBarSetting)) .actionBarSetting(JSONObject.parseObject(actionBarSetting))
.rightMenuSetting(Lists.of("selectAll", "copy", "fontSizePlus", "fontSizeSubtract", "search", "clear")) .rightMenuSetting(Lists.of("selectAll", "copy", "paste", "fontSizePlus", "fontSizeSubtract", "search", "clear"))
.interactSetting(JSONObject.parseObject(defaultInteractSetting)) .interactSetting(JSONObject.parseObject(defaultInteractSetting))
.pluginsSetting(JSONObject.parseObject(defaultPluginsSetting)) .pluginsSetting(JSONObject.parseObject(defaultPluginsSetting))
.sessionSetting(JSONObject.parseObject(defaultSessionSetting)) .sessionSetting(JSONObject.parseObject(defaultSessionSetting))
@@ -126,4 +132,9 @@ public class TerminalPreferenceStrategy implements IPreferenceStrategy<TerminalP
.build(); .build();
} }
@Override
public TerminalPreferenceModel parse(String serialModel) {
throw Exceptions.unsupported();
}
} }

View File

@@ -142,14 +142,14 @@ 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, String> defaultModel = preferenceType.getStrategy() Map<String, Object> defaultModel = preferenceType.getStrategy()
.getDefault() .getDefault()
.toMap(); .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, Refs.unref(defaultModel.get(k)))); defaultModel.forEach((k, v) -> result.put(k, defaultModel.get(k)));
} else { } else {
items.forEach(s -> result.put(s, Refs.unref(defaultModel.get(s)))); items.forEach(s -> result.put(s, defaultModel.get(s)));
} }
return result; return result;
} }
@@ -218,9 +218,10 @@ public class PreferenceServiceImpl implements PreferenceService {
// 初始化 // 初始化
if (Maps.isEmpty(config)) { if (Maps.isEmpty(config)) {
// 获取默认值 // 获取默认值
config = type.getStrategy() Map<String, Object> defaultConfig = type.getStrategy()
.getDefault() .getDefault()
.toMap(); .toMap();
config = Maps.map(defaultConfig, Function.identity(), Refs::json);
// 插入默认值 // 插入默认值
List<PreferenceDO> entities = config List<PreferenceDO> entities = config
.entrySet() .entrySet()