From 1df6d43f26f7211873b130da5bbf5fd3b22b4c93 Mon Sep 17 00:00:00 2001 From: linbangquan <1437892690@qq.com> Date: Mon, 4 Dec 2023 09:27:58 +0800 Subject: [PATCH] =?UTF-8?q?[=E5=8A=9F=E8=83=BD]=20=E5=90=8E=E7=AB=AF-cmdb?= =?UTF-8?q?=E8=8A=82=E7=82=B9=E5=A2=9E=E5=8A=A0=E5=B1=9E=E6=80=A7=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E5=80=BC=E6=A0=A1=E9=AA=8C=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 关联 #[1038886606700544]后端-cmdb节点增加属性类型值校验逻辑 http://192.168.0.96:8090/demo/rdm.html#/task-detail/939050947543040/939050947543050/1038886606700544 --- .../handler/EnumValueHandler.java | 20 +++++ .../handler/FileValueHandler.java | 20 +++++ .../handler/SelectValueHandler.java | 24 ++++++ .../handler/TableValueHandler.java | 24 ++++++ .../stephandler/CmdbSyncProcessComponent.java | 79 +++++++++---------- .../service/cientity/CiEntityServiceImpl.java | 2 +- 6 files changed, 127 insertions(+), 42 deletions(-) diff --git a/src/main/java/neatlogic/module/cmdb/attrvaluehandler/handler/EnumValueHandler.java b/src/main/java/neatlogic/module/cmdb/attrvaluehandler/handler/EnumValueHandler.java index 71af278b..868b2a15 100644 --- a/src/main/java/neatlogic/module/cmdb/attrvaluehandler/handler/EnumValueHandler.java +++ b/src/main/java/neatlogic/module/cmdb/attrvaluehandler/handler/EnumValueHandler.java @@ -16,8 +16,13 @@ package neatlogic.module.cmdb.attrvaluehandler.handler; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; import neatlogic.framework.cmdb.attrvaluehandler.core.IAttrValueHandler; +import neatlogic.framework.cmdb.dto.ci.AttrVo; import neatlogic.framework.cmdb.enums.SearchExpression; +import neatlogic.framework.cmdb.exception.attr.AttrValueIrregularException; +import org.apache.commons.collections4.CollectionUtils; import org.springframework.stereotype.Service; @@ -89,4 +94,19 @@ public class EnumValueHandler implements IAttrValueHandler { return 4; } + @Override + public boolean valid(AttrVo attrVo, JSONArray valueList) { + if (CollectionUtils.isNotEmpty(valueList)) { + JSONObject config = attrVo.getConfig(); + JSONArray members = config.getJSONArray("members"); + for (int i = 0; i < valueList.size(); i++) { + String v = valueList.getString(i); + if (!members.contains(v)) { + throw new AttrValueIrregularException(attrVo, v); + } + } + } + return true; + } + } diff --git a/src/main/java/neatlogic/module/cmdb/attrvaluehandler/handler/FileValueHandler.java b/src/main/java/neatlogic/module/cmdb/attrvaluehandler/handler/FileValueHandler.java index 195c3d3e..2cab46de 100644 --- a/src/main/java/neatlogic/module/cmdb/attrvaluehandler/handler/FileValueHandler.java +++ b/src/main/java/neatlogic/module/cmdb/attrvaluehandler/handler/FileValueHandler.java @@ -19,6 +19,7 @@ package neatlogic.module.cmdb.attrvaluehandler.handler; import neatlogic.framework.cmdb.attrvaluehandler.core.IAttrValueHandler; import neatlogic.framework.cmdb.dto.ci.AttrVo; import neatlogic.framework.cmdb.enums.SearchExpression; +import neatlogic.framework.cmdb.exception.attr.AttrValueIrregularException; import neatlogic.framework.file.dao.mapper.FileMapper; import neatlogic.framework.file.dto.FileVo; import com.alibaba.fastjson.JSONArray; @@ -163,4 +164,23 @@ public class FileValueHandler implements IAttrValueHandler { } } } + + @Override + public boolean valid(AttrVo attrVo, JSONArray valueList) { + if (CollectionUtils.isNotEmpty(valueList)) { + for (int i = 0; i < valueList.size(); i++) { + String value = valueList.getString(i); + try { + Long fileId = Long.valueOf(value); + FileVo fileVo = fileMapper.getFileById(fileId); + if (fileVo == null) { + throw new AttrValueIrregularException(attrVo, value); + } + } catch (NumberFormatException ex) { + throw new AttrValueIrregularException(attrVo, value); + } + } + } + return true; + } } diff --git a/src/main/java/neatlogic/module/cmdb/attrvaluehandler/handler/SelectValueHandler.java b/src/main/java/neatlogic/module/cmdb/attrvaluehandler/handler/SelectValueHandler.java index ee167a7b..f453c4c6 100644 --- a/src/main/java/neatlogic/module/cmdb/attrvaluehandler/handler/SelectValueHandler.java +++ b/src/main/java/neatlogic/module/cmdb/attrvaluehandler/handler/SelectValueHandler.java @@ -21,6 +21,7 @@ import neatlogic.framework.cmdb.dto.ci.AttrVo; import neatlogic.framework.cmdb.dto.ci.CiVo; import neatlogic.framework.cmdb.dto.cientity.CiEntityVo; import neatlogic.framework.cmdb.enums.SearchExpression; +import neatlogic.framework.cmdb.exception.attr.AttrValueIrregularException; import neatlogic.module.cmdb.dao.mapper.ci.CiMapper; import neatlogic.module.cmdb.dao.mapper.cientity.CiEntityMapper; import com.alibaba.fastjson.JSONArray; @@ -30,6 +31,7 @@ import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.ArrayList; import java.util.List; +import java.util.Objects; @Service @@ -169,4 +171,26 @@ public class SelectValueHandler implements IAttrValueHandler { public int getSort() { return 5; } + + @Override + public boolean valid(AttrVo attrVo, JSONArray valueList) { + if (CollectionUtils.isNotEmpty(valueList)) { + for (int i = 0; i < valueList.size(); i++) { + String value = valueList.getString(i); + try { + Long attrId = Long.valueOf(value); + CiEntityVo ciEntity = ciEntityMapper.getCiEntityBaseInfoById(attrId); + if (ciEntity == null) { + throw new AttrValueIrregularException(attrVo, value); + } + if (!Objects.equals(ciEntity.getCiId(), attrVo.getTargetCiId())) { + throw new AttrValueIrregularException(attrVo, value); + } + } catch (NumberFormatException ex) { + throw new AttrValueIrregularException(attrVo, value); + } + } + } + return true; + } } diff --git a/src/main/java/neatlogic/module/cmdb/attrvaluehandler/handler/TableValueHandler.java b/src/main/java/neatlogic/module/cmdb/attrvaluehandler/handler/TableValueHandler.java index 25962c58..c5cfdb00 100644 --- a/src/main/java/neatlogic/module/cmdb/attrvaluehandler/handler/TableValueHandler.java +++ b/src/main/java/neatlogic/module/cmdb/attrvaluehandler/handler/TableValueHandler.java @@ -21,6 +21,7 @@ import neatlogic.framework.cmdb.dto.ci.AttrVo; import neatlogic.framework.cmdb.dto.ci.CiVo; import neatlogic.framework.cmdb.dto.cientity.CiEntityVo; import neatlogic.framework.cmdb.enums.SearchExpression; +import neatlogic.framework.cmdb.exception.attr.AttrValueIrregularException; import neatlogic.module.cmdb.dao.mapper.ci.CiMapper; import neatlogic.module.cmdb.dao.mapper.cientity.CiEntityMapper; import neatlogic.module.cmdb.service.cientity.CiEntityService; @@ -31,6 +32,7 @@ import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.ArrayList; import java.util.List; +import java.util.Objects; @Service @@ -160,4 +162,26 @@ public class TableValueHandler implements IAttrValueHandler { } } } + + @Override + public boolean valid(AttrVo attrVo, JSONArray valueList) { + if (CollectionUtils.isNotEmpty(valueList)) { + for (int i = 0; i < valueList.size(); i++) { + String value = valueList.getString(i); + try { + Long attrId = Long.valueOf(value); + CiEntityVo ciEntity = ciEntityMapper.getCiEntityBaseInfoById(attrId); + if (ciEntity == null) { + throw new AttrValueIrregularException(attrVo, value); + } + if (!Objects.equals(ciEntity.getCiId(), attrVo.getTargetCiId())) { + throw new AttrValueIrregularException(attrVo, value); + } + } catch (NumberFormatException ex) { + throw new AttrValueIrregularException(attrVo, value); + } + } + } + return true; + } } diff --git a/src/main/java/neatlogic/module/cmdb/process/stephandler/CmdbSyncProcessComponent.java b/src/main/java/neatlogic/module/cmdb/process/stephandler/CmdbSyncProcessComponent.java index 1d7c8d68..0cac8b87 100644 --- a/src/main/java/neatlogic/module/cmdb/process/stephandler/CmdbSyncProcessComponent.java +++ b/src/main/java/neatlogic/module/cmdb/process/stephandler/CmdbSyncProcessComponent.java @@ -27,15 +27,15 @@ import neatlogic.framework.cmdb.dto.ci.RelVo; import neatlogic.framework.cmdb.dto.cientity.AttrFilterVo; import neatlogic.framework.cmdb.dto.cientity.CiEntityVo; import neatlogic.framework.cmdb.dto.cientity.RelEntityVo; +import neatlogic.framework.cmdb.dto.globalattr.GlobalAttrItemVo; import neatlogic.framework.cmdb.dto.globalattr.GlobalAttrVo; import neatlogic.framework.cmdb.dto.transaction.CiEntityTransactionVo; -import neatlogic.framework.cmdb.enums.PropHandlerType; import neatlogic.framework.cmdb.enums.RelDirectionType; import neatlogic.framework.cmdb.enums.SearchExpression; import neatlogic.framework.cmdb.enums.TransactionActionType; -import neatlogic.framework.cmdb.exception.attr.AttrValueIrregularException; import neatlogic.framework.cmdb.exception.ci.CiNotFoundException; import neatlogic.framework.cmdb.exception.cientity.NewCiEntityNotFoundException; +import neatlogic.framework.cmdb.exception.globalattr.GlobalAttrValueIrregularException; import neatlogic.framework.cmdb.utils.RelUtil; import neatlogic.framework.common.constvalue.Expression; import neatlogic.framework.common.constvalue.InputFrom; @@ -213,6 +213,7 @@ public class CmdbSyncProcessComponent extends ProcessStepHandlerBase { CiEntityTransactionVo ciEntityTransactionVo = new CiEntityTransactionVo(); ciEntityTransactionVo.setCiEntityUuid(uuid); ciEntityTransactionVo.setCiId(configObject.getCiId()); + ciEntityTransactionVo.setAllowCommit(true); Long id = configObject.getId(); if (id != null) { ciEntityTransactionVo.setCiEntityId(id); @@ -242,15 +243,6 @@ public class CmdbSyncProcessComponent extends ProcessStepHandlerBase { } } } - for (int i = ciEntityTransactionList.size() - 1; i >= 0; i--) { - CiEntityTransactionVo t = ciEntityTransactionList.get(i); - boolean hasChange = ciEntityService.validateCiEntityTransaction(t); - if (hasChange) { - t.setAllowCommit(true); - } else { - ciEntityTransactionList.remove(i); - } - } if (CollectionUtils.isNotEmpty(ciEntityTransactionList)) { EscapeTransactionJob.State s = new EscapeTransactionJob(() -> { InputFromContext.init(InputFrom.ITSM); @@ -532,12 +524,12 @@ public class CmdbSyncProcessComponent extends ProcessStepHandlerBase { CiEntitySyncMappingVo descriptionMappingObj = mappingMap.get("description"); if (descriptionMappingObj != null) { JSONArray valueList = descriptionMappingObj.getValueList(); - if (CollectionUtils.isEmpty(valueList)) { - } - String description = valueList.getString(0); - if (StringUtils.isBlank(description)) { + if (CollectionUtils.isNotEmpty(valueList)) { + String description = valueList.getString(0); + if (StringUtils.isNotBlank(description)) { + ciEntityTransactionVo.setDescription(description); + } } - ciEntityTransactionVo.setDescription(description); } /** 属性 **/ @@ -587,25 +579,6 @@ public class CmdbSyncProcessComponent extends ProcessStepHandlerBase { } } } - if (PropHandlerType.SELECT.getValue().equals(attrVo.getType())) { - if (CollectionUtils.isNotEmpty(valueList)) { - for (int i = 0; i < valueList.size(); i++) { - String value = valueList.getString(i); - try { - Long attrId = Long.valueOf(value); - CiEntityVo ciEntity = ciEntityMapper.getCiEntityBaseInfoById(attrId); - if (ciEntity == null) { - throw new AttrValueIrregularException(attrVo, value); - } - if (!Objects.equals(ciEntity.getCiId(), attrVo.getTargetCiId())) { - throw new AttrValueIrregularException(attrVo, value); - } - } catch (NumberFormatException ex) { - throw new AttrValueIrregularException(attrVo, value); - } - } - } - } JSONObject attrEntity = new JSONObject(); attrEntity.put("type", attrVo.getType()); attrEntity.put("config", attrVo.getConfig()); @@ -717,12 +690,31 @@ public class CmdbSyncProcessComponent extends ProcessStepHandlerBase { continue; } JSONArray valueList = new JSONArray(); - String mappingMode = mappingObj.getMappingMode(); - if (Objects.equals(mappingMode, "constant")) { - // 映射模式为常量 - JSONArray valueArray = mappingObj.getValueList(); - if (CollectionUtils.isNotEmpty(valueArray)) { - valueList = valueArray; + // 映射模式为常量 + JSONArray valueArray = mappingObj.getValueList(); + if (CollectionUtils.isNotEmpty(valueArray)) { + for (int i = 0; i < valueArray.size(); i++) { + Object valueObj = valueArray.get(i); + if (valueObj instanceof JSONObject) { + valueList.add(valueObj); + } else { + boolean flag = false; + List itemList = globalAttrVo.getItemList(); + for (GlobalAttrItemVo item : itemList) { + if (Objects.equals(item.getValue(), valueObj)) { + JSONObject jsonObj = new JSONObject(); + jsonObj.put("id", item.getId()); + jsonObj.put("value", item.getValue()); + jsonObj.put("sort", item.getSort()); + jsonObj.put("attrId", globalAttrVo.getId()); + valueList.add(jsonObj); + flag = true; + } + } + if (!flag) { + throw new GlobalAttrValueIrregularException(globalAttrVo, valueObj.toString()); + } + } } } JSONObject globalAttrEntity = new JSONObject(); @@ -990,6 +982,11 @@ public class CmdbSyncProcessComponent extends ProcessStepHandlerBase { } else { newValueList.addAll(valueList); } + for (int i = newValueList.size() - 1; i >= 0; i--) { + if (StringUtils.isBlank(newValueList.getString(i))){ + newValueList.remove(i); + } + } } } diff --git a/src/main/java/neatlogic/module/cmdb/service/cientity/CiEntityServiceImpl.java b/src/main/java/neatlogic/module/cmdb/service/cientity/CiEntityServiceImpl.java index ce8e44a4..4efb7bd6 100644 --- a/src/main/java/neatlogic/module/cmdb/service/cientity/CiEntityServiceImpl.java +++ b/src/main/java/neatlogic/module/cmdb/service/cientity/CiEntityServiceImpl.java @@ -892,7 +892,7 @@ public class CiEntityServiceImpl implements CiEntityService, ICiEntityCrossoverS /* 校验值是否符合数据类型 */ - if (attrEntityTransactionVo != null && CollectionUtils.isNotEmpty(attrEntityTransactionVo.getValueList()) && !attrVo.isNeedTargetCi()) { + if (attrEntityTransactionVo != null && CollectionUtils.isNotEmpty(attrEntityTransactionVo.getValueList())) { IAttrValueHandler attrHandler = AttrValueHandlerFactory.getHandler(attrVo.getType()); if (attrHandler != null) { attrHandler.valid(attrVo, attrEntityTransactionVo.getValueList()); -- Gitee