diff --git a/src/main/java/neatlogic/module/cmdb/api/ci/SaveCiApi.java b/src/main/java/neatlogic/module/cmdb/api/ci/SaveCiApi.java index aa0219bd06c4e0364f3163db84042523ab5e3c20..68cbbc57160f23ebde74f38a67946c347dc7f7e0 100644 --- a/src/main/java/neatlogic/module/cmdb/api/ci/SaveCiApi.java +++ b/src/main/java/neatlogic/module/cmdb/api/ci/SaveCiApi.java @@ -123,6 +123,11 @@ public class SaveCiApi extends PrivateApiComponentBase { if (!CiAuthChecker.chain().checkCiManagePrivilege(ciId).check()) { throw new CiAuthException(); } + if (oldCiVo.getRht() - oldCiVo.getLft() > 1) { + oldCiVo.setHasChildren(true); + } else { + oldCiVo.setHasChildren(false); + } oldCiVo.setHasData(ciSchemaMapper.checkTableHasData(ciVo.getCiTableName()) > 0); if ((oldCiVo.getHasChildren() || oldCiVo.getHasData()) && !Objects.equals(oldCiVo.getIsAbstract(), ciVo.getIsAbstract())) { throw new CiIsAbstractedException(CiIsAbstractedException.Type.UPDATEABSTRACT, ciVo.getLabel()); diff --git a/src/main/java/neatlogic/module/cmdb/api/ci/process/GetCiForProcessMappingApi.java b/src/main/java/neatlogic/module/cmdb/api/ci/process/GetCiForProcessMappingApi.java new file mode 100644 index 0000000000000000000000000000000000000000..5d1a7cc5de4ea9589e6a20d10b2a5f663ecda82b --- /dev/null +++ b/src/main/java/neatlogic/module/cmdb/api/ci/process/GetCiForProcessMappingApi.java @@ -0,0 +1,148 @@ +package neatlogic.module.cmdb.api.ci.process; + +import com.alibaba.fastjson.JSONObject; +import neatlogic.framework.auth.core.AuthAction; +import neatlogic.framework.cmdb.dto.ci.AttrVo; +import neatlogic.framework.cmdb.dto.ci.CiViewVo; +import neatlogic.framework.cmdb.dto.ci.CiVo; +import neatlogic.framework.cmdb.dto.ci.RelVo; +import neatlogic.framework.cmdb.dto.globalattr.GlobalAttrVo; +import neatlogic.framework.cmdb.exception.ci.CiNotFoundException; +import neatlogic.framework.cmdb.exception.ci.CiUniqueRuleAttrTypeIrregularException; +import neatlogic.framework.cmdb.utils.RelUtil; +import neatlogic.framework.common.constvalue.ApiParamType; +import neatlogic.framework.process.auth.PROCESS; +import neatlogic.framework.restful.annotation.*; +import neatlogic.framework.restful.constvalue.OperationTypeEnum; +import neatlogic.framework.restful.core.privateapi.PrivateApiComponentBase; +import neatlogic.module.cmdb.dao.mapper.ci.AttrMapper; +import neatlogic.module.cmdb.dao.mapper.ci.CiMapper; +import neatlogic.module.cmdb.dao.mapper.ci.CiViewMapper; +import neatlogic.module.cmdb.dao.mapper.ci.RelMapper; +import neatlogic.module.cmdb.dao.mapper.globalattr.GlobalAttrMapper; +import org.apache.commons.collections4.CollectionUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; + +@Service +@AuthAction(action = PROCESS.class) +@OperationType(type = OperationTypeEnum.SEARCH) +public class GetCiForProcessMappingApi extends PrivateApiComponentBase { + + @Autowired + private CiMapper ciMapper; + + @Resource + private AttrMapper attrMapper; + + @Resource + private RelMapper relMapper; + + @Resource + private GlobalAttrMapper globalAttrMapper; + + @Autowired + private CiViewMapper ciViewMapper; + + @Override + public String getName() { + return "nmcacp.getciforprocessmappingapi.getname"; + } + + + @Input({ + @Param(name = "id", type = ApiParamType.LONG, isRequired = true, desc = "term.cmdb.ciid"), + @Param(name = "rootId", type = ApiParamType.LONG, desc = "term.cmdb.rootciid") + }) + @Output({ + @Param(explode = CiVo.class) + }) + @Description(desc = "nmcacp.getciforprocessmappingapi.getname") + @Override + public Object myDoService(JSONObject paramObj) throws Exception { + Long ciId = paramObj.getLong("id"); + CiVo ciVo = ciMapper.getCiById(ciId); + if (ciVo == null) { + throw new CiNotFoundException(ciId); + } + Long rootId = paramObj.getLong("rootId"); + if (Objects.equals(rootId, ciId)) { + rootId = null; + } + List rootUpwardCiIdList = new ArrayList<>(); + if (rootId != null) { + CiVo rootCiVo = ciMapper.getCiById(rootId); + if (rootCiVo == null) { + throw new CiNotFoundException(rootId); + } + List upwardCiList = ciMapper.getUpwardCiListByLR(rootCiVo.getLft(), rootCiVo.getRht()); + rootUpwardCiIdList = upwardCiList.stream().map(CiVo::getId).collect(Collectors.toList()); + } + // 唯一规则属性列表 + List uniqueAttrIdList = ciMapper.getCiUniqueByCiId(ciId); + ciVo.setUniqueAttrIdList(uniqueAttrIdList); + // 属性 + List attrList = attrMapper.getAttrByCiId(ciId); + for (AttrVo attrVo : attrList) { + if (uniqueAttrIdList.contains(attrVo.getId()) && Objects.equals(attrVo.getType(), "expression")) { + attrVo.setCiLabel(ciVo.getLabel()); + attrVo.setCiName(ciVo.getName()); + throw new CiUniqueRuleAttrTypeIrregularException(attrVo); + } + } + for (int i = attrList.size() - 1; i >= 0; i--) { + AttrVo attrVo = attrList.get(i); + if (attrVo.getAllowEdit() != null && attrVo.getAllowEdit().equals(0)) { + attrList.remove(i); + continue; + } + if (CollectionUtils.isNotEmpty(rootUpwardCiIdList)) { + if (rootUpwardCiIdList.contains(attrVo.getCiId())) { + attrList.remove(i); + } + } + } + ciVo.setAttrList(attrList); + // 关系 + List relList = RelUtil.ClearRepeatRel(relMapper.getRelByCiId(ciId)); + for (int i = relList.size() - 1; i >= 0; i--) { + RelVo relVo = relList.get(i); + if (relVo.getAllowEdit() != null && relVo.getAllowEdit().equals(0)) { + relList.remove(i); + continue; + } + if (CollectionUtils.isNotEmpty(rootUpwardCiIdList)) { + if (Objects.equals(relVo.getDirection(), "from") && rootUpwardCiIdList.contains(relVo.getFromCiId())) { + relList.remove(i); + } else if (Objects.equals(relVo.getDirection(), "to") && rootUpwardCiIdList.contains(relVo.getToCiId())) { + relList.remove(i); + } + } + } + ciVo.setRelList(relList); + if (CollectionUtils.isEmpty(rootUpwardCiIdList)) { + // 全局属性 + GlobalAttrVo globalAttrVo = new GlobalAttrVo(); + globalAttrVo.setIsActive(1); + List globalAttrList = globalAttrMapper.searchGlobalAttr(globalAttrVo); + ciVo.setGlobalAttrList(globalAttrList); + } + // 字段显示设置列表 + CiViewVo ciViewVo = new CiViewVo(); + ciViewVo.setCiId(ciId); + List ciViewList = RelUtil.ClearCiViewRepeatRel(ciViewMapper.getCiViewByCiId(ciViewVo)); + ciVo.setViewList(ciViewList); + return ciVo; + } + + @Override + public String getToken() { + return "cmdb/ci/get/forprocessmapping"; + } +} diff --git a/src/main/java/neatlogic/module/cmdb/process/exception/AbstractCiTargetCiIdAttrNotFoundException.java b/src/main/java/neatlogic/module/cmdb/process/exception/AbstractCiTargetCiIdAttrNotFoundException.java new file mode 100644 index 0000000000000000000000000000000000000000..29d1ceae280ca626105b338e21e0c4ad2457ebed --- /dev/null +++ b/src/main/java/neatlogic/module/cmdb/process/exception/AbstractCiTargetCiIdAttrNotFoundException.java @@ -0,0 +1,11 @@ +package neatlogic.module.cmdb.process.exception; + +import neatlogic.framework.cmdb.dto.ci.CiVo; +import neatlogic.framework.exception.core.ApiRuntimeException; + +public class AbstractCiTargetCiIdAttrNotFoundException extends ApiRuntimeException { + + public AbstractCiTargetCiIdAttrNotFoundException(CiVo ciVo) { + super("nmcpe.abstractcitargetciidattrnotfoundexception.abstractcitargetciidattrnotfoundexception", ciVo.getLabel(), ciVo.getName()); + } +} 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 aa415c366f33da4279b20a6a88a3f03ee4e157cf..9f51ddd3742ca11c79392842cf042478c24e86a2 100644 --- a/src/main/java/neatlogic/module/cmdb/process/stephandler/CmdbSyncProcessComponent.java +++ b/src/main/java/neatlogic/module/cmdb/process/stephandler/CmdbSyncProcessComponent.java @@ -35,8 +35,7 @@ import neatlogic.framework.cmdb.dto.transaction.CiEntityTransactionVo; import neatlogic.framework.cmdb.enums.*; import neatlogic.framework.cmdb.exception.attr.AttrValueIrregularException; import neatlogic.framework.cmdb.exception.attrtype.AttrTypeNotFoundException; -import neatlogic.framework.cmdb.exception.ci.CiNotFoundException; -import neatlogic.framework.cmdb.exception.cientity.NewCiEntityNotFoundException; +import neatlogic.framework.cmdb.exception.ci.*; import neatlogic.framework.cmdb.exception.globalattr.GlobalAttrValueIrregularException; import neatlogic.framework.cmdb.utils.RelUtil; import neatlogic.framework.common.constvalue.Expression; @@ -63,10 +62,10 @@ import neatlogic.module.cmdb.dao.mapper.ci.AttrMapper; import neatlogic.module.cmdb.dao.mapper.ci.CiMapper; import neatlogic.module.cmdb.dao.mapper.ci.RelMapper; import neatlogic.module.cmdb.dao.mapper.cientity.CiEntityMapper; -import neatlogic.module.cmdb.dao.mapper.cientity.RelEntityMapper; import neatlogic.module.cmdb.dao.mapper.globalattr.GlobalAttrMapper; import neatlogic.module.cmdb.process.constvalue.CmdbAuditDetailType; import neatlogic.module.cmdb.process.dto.*; +import neatlogic.module.cmdb.process.exception.AbstractCiTargetCiIdAttrNotFoundException; import neatlogic.module.cmdb.service.cientity.CiEntityService; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.MapUtils; @@ -102,9 +101,6 @@ public class CmdbSyncProcessComponent extends ProcessStepHandlerBase { @Resource private CiEntityMapper ciEntityMapper; - @Resource - private RelEntityMapper relEntityMapper; - @Resource private CiEntityService ciEntityService; @@ -212,21 +208,24 @@ public class CmdbSyncProcessComponent extends ProcessStepHandlerBase { Map ciEntityTransactionMap = new HashMap<>(); Map dependencyConfigMap = new HashMap<>(); for (CiEntitySyncConfigVo configObject : configList) { + List uniqueAttrIdList = ciMapper.getCiUniqueByCiId(configObject.getCiId()); + List attrList = attrMapper.getAttrByCiId(configObject.getCiId()); + for (AttrVo attrVo : attrList) { + if (uniqueAttrIdList.contains(attrVo.getId()) && Objects.equals(attrVo.getType(), "expression")) { + attrVo.setCiLabel(configObject.getCiLabel()); + attrVo.setCiName(configObject.getCiName()); + throw new CiUniqueRuleAttrTypeIrregularException(attrVo); + } + } String uuid = configObject.getUuid(); CiEntityTransactionVo ciEntityTransactionVo = new CiEntityTransactionVo(); ciEntityTransactionVo.setCiEntityUuid(uuid); ciEntityTransactionVo.setCiId(configObject.getCiId()); +// ciEntityTransactionVo.setCiName(configObject.getCiName()); ciEntityTransactionVo.setAllowCommit(true); if (StringUtils.isNotBlank(configObject.getEditMode())) { ciEntityTransactionVo.setEditMode(configObject.getEditMode()); } -// Long id = configObject.getId(); -// if (id != null) { -// ciEntityTransactionVo.setCiEntityId(id); -// ciEntityTransactionVo.setAction(TransactionActionType.UPDATE.getValue()); -// } else { -// ciEntityTransactionVo.setAction(TransactionActionType.INSERT.getValue()); -// } Integer isStart = configObject.getIsStart(); if (Objects.equals(isStart, 1)) { startConfigList.add(configObject); @@ -250,9 +249,8 @@ public class CmdbSyncProcessComponent extends ProcessStepHandlerBase { } } if (CollectionUtils.isNotEmpty(ciEntityTransactionList)) { + // 遍历ciEntityTransactionList,根据唯一规则属性值删除重复配置信息 removeDuplicatesByUniqueAttrValue(ciEntityTransactionList); - /* 处理和校验下拉框类型和表格类型属性的值 */ -// validSelectAndTableAttrValueList(ciEntityTransactionList); EscapeTransactionJob.State s = new EscapeTransactionJob(() -> { InputFromContext.init(InputFrom.ITSM); Long transactionGroupId = ciEntityService.saveCiEntity(ciEntityTransactionList); @@ -369,112 +367,6 @@ public class CmdbSyncProcessComponent extends ProcessStepHandlerBase { } } - /** - * 遍历configList,根据唯一规则属性值删除重复配置信息 - * @param configList 配置信息列表 - * @return - */ - private List removeDuplicatesByUniqueAttrValue2(List configList) { - // 保存唯一规则属性值组成的字符串与uuid的映射关系 - Map uniqueAttrValueListJoinStr2UuidMap = new HashMap<>(); - // 保存将要删除uuid与等价替换uuid的映射关系 - Map toDeleteUuid2EquivalentUuidMap = new HashMap<>(); - // 反向遍历,如果唯一规则属性值相同,后面数据优先级高 - for (int i = configList.size() - 1; i >= 0; i--) { - CiEntitySyncConfigVo configObj = configList.get(i); - Long ciId = configObj.getCiId(); - CiVo ciVo = ciMapper.getCiById(ciId); - if (ciVo == null) { - String ciName = configObj.getCiName(); - throw new CiNotFoundException(ciName); - } - List uniqueAttrIdList = ciVo.getUniqueAttrIdList(); - if (CollectionUtils.isEmpty(uniqueAttrIdList)) { - continue; - } - Map key2ValueListMap = new HashMap<>(); - List mappingList = configObj.getMappingList(); - for (CiEntitySyncMappingVo mappingObj : mappingList) { - String key = mappingObj.getKey(); - JSONArray valueList = mappingObj.getValueList(); - key2ValueListMap.put(key, valueList); - } - List list = new ArrayList<>(); - list.add(ciId.toString()); - for (Long attrId : uniqueAttrIdList) { - JSONArray valueList = key2ValueListMap.get("attr_" + attrId); - if (valueList != null) { - list.add(valueList.toJSONString()); - } - } - String uniqueAttrValueListJoinStr = String.join(",", list); - String equivalentUuid = uniqueAttrValueListJoinStr2UuidMap.get(uniqueAttrValueListJoinStr); - if (StringUtils.isNotBlank(equivalentUuid)) { - toDeleteUuid2EquivalentUuidMap.put(configObj.getUuid(), equivalentUuid); - continue; - } - //校验唯一规则 - CiEntityVo ciEntityConditionVo = new CiEntityVo(); - ciEntityConditionVo.setCiId(ciId); - ciEntityConditionVo.setAttrIdList(new ArrayList() {{ - this.add(0L); - }}); - ciEntityConditionVo.setRelIdList(new ArrayList() {{ - this.add(0L); - }}); - for (Long attrId : ciVo.getUniqueAttrIdList()) { - AttrFilterVo filterVo = new AttrFilterVo(); - filterVo.setAttrId(attrId); - filterVo.setExpression(SearchExpression.EQ.getExpression()); - List valueList = new ArrayList<>(); - JSONArray valueArray = key2ValueListMap.get("attr_" + attrId); - if (CollectionUtils.isNotEmpty(valueArray)) { - valueList = valueArray.toJavaList(String.class); - } - filterVo.setValueList(valueList); - ciEntityConditionVo.addAttrFilter(filterVo); - } - if (CollectionUtils.isNotEmpty(ciEntityConditionVo.getAttrFilterList())) { - List checkList = ciEntityService.searchCiEntity(ciEntityConditionVo); - if (CollectionUtils.isNotEmpty(checkList)) { - configObj.setId(checkList.get(0).getId()); - } - } - uniqueAttrValueListJoinStr2UuidMap.put(uniqueAttrValueListJoinStr, configObj.getUuid()); - } - - List newConfigList = new ArrayList<>(); - for (CiEntitySyncConfigVo configObj : configList) { - if (toDeleteUuid2EquivalentUuidMap.containsKey(configObj.getUuid())) { - continue; - } - List mappingList = configObj.getMappingList(); - for (CiEntitySyncMappingVo mappingObj : mappingList) { - String mappingMode = mappingObj.getMappingMode(); - if (!Objects.equals(mappingMode, "new")) { - continue; - } - JSONArray valueList = mappingObj.getValueList(); - if (CollectionUtils.isEmpty(valueList)) { - continue; - } - for (int i = 0; i < valueList.size(); i++) { - JSONObject valueObj = valueList.getJSONObject(i); - if (MapUtils.isEmpty(valueObj)) { - continue; - } - String ciEntityUuid = valueObj.getString("ciEntityUuid"); - String equivalentUuid = toDeleteUuid2EquivalentUuidMap.get(ciEntityUuid); - if (StringUtils.isNotBlank(equivalentUuid)) { - valueObj.put("ciEntityUuid", equivalentUuid); - } - } - } - newConfigList.add(configObj); - } - return newConfigList; - } - /** * 遍历ciEntityTransactionList,根据唯一规则属性值删除重复配置信息 * @param ciEntityTransactionList 配置信息列表 @@ -581,11 +473,11 @@ public class CmdbSyncProcessComponent extends ProcessStepHandlerBase { } } // 遍历configList,将“批量操作”的配置信息根据表单数据转换成多条“单个操作”配置信息 - handleBatchDataSource(originalConfigList, startConfigObj, formAttributeDataMap, newConfigList, formConfig); + List ciEntitySyncConfigList = new ArrayList<>(); + ciEntitySyncConfigList.add(startConfigObj); + handleBatchDataSource(originalConfigList, ciEntitySyncConfigList, formAttributeDataMap, newConfigList, formConfig); } return newConfigList; -// // 遍历configList,根据唯一规则属性值删除重复配置信息 -// return removeDuplicatesByUniqueAttrValue(newConfigList); } private List createCiEntityTransactionVo( @@ -617,62 +509,10 @@ public class CmdbSyncProcessComponent extends ProcessStepHandlerBase { } /** 属性 **/ -// JSONObject attrEntityData = new JSONObject(); -// List attrList = attrMapper.getAttrByCiId(ciId); -// for (AttrVo attrVo : attrList) { -// if (Objects.equals(attrVo.getType(), "expression")) { -// continue; -// } -// String key = "attr_" + attrVo.getId(); -// CiEntitySyncMappingVo mappingObj = mappingMap.get(key); -// if (mappingObj == null) { -// JSONObject attrEntity = new JSONObject(); -// attrEntity.put("type", attrVo.getType()); -// attrEntity.put("config", attrVo.getConfig()); -// attrEntity.put("valueList", new JSONArray()); -// attrEntityData.put(key, attrEntity); -// continue; -// } -// // 映射模式为常量 -// JSONArray valueList = mappingObj.getValueList(); -// if (CollectionUtils.isNotEmpty(valueList)) { -// for (int i = valueList.size() - 1; i >= 0; i--) { -// if (valueList.get(i) instanceof JSONObject) { -// JSONObject valueObj = valueList.getJSONObject(i); -// String attrCiEntityUuid = valueObj.getString("uuid"); -// Long attrCiEntityId = valueObj.getLong("id"); -// if (attrCiEntityId == null && StringUtils.isNotBlank(attrCiEntityUuid)) { -// CiEntityTransactionVo tmpVo = ciEntityTransactionMap.get(attrCiEntityUuid); -// if (tmpVo != null) { -// //替换掉原来的ciEntityUuid为新的ciEntityId -// valueList.set(i, tmpVo.getCiEntityId()); -// } else { -// //使用uuid寻找配置项 -// CiEntityVo uuidCiEntityVo = ciEntityMapper.getCiEntityBaseInfoByUuid(attrCiEntityUuid); -// if (uuidCiEntityVo == null) { -// throw new NewCiEntityNotFoundException(attrCiEntityUuid); -// } else { -// valueList.set(i, uuidCiEntityVo.getId()); -// } -// } -// } else if (attrCiEntityId != null) { -// valueList.set(i, attrCiEntityId); -// } else { -// valueList.remove(i); -// } -// } -// } -// } -// JSONObject attrEntity = new JSONObject(); -// attrEntity.put("type", attrVo.getType()); -// attrEntity.put("config", attrVo.getConfig()); -// attrEntity.put("valueList", valueList); -// attrEntityData.put(key, attrEntity); -// } JSONObject attrEntityData = buildAttrEntityData(ciEntityTransactionMap, mainConfigObj, mappingMap); ciEntityTransactionVo.setAttrEntityData(attrEntityData); - - Long ciEntityId = getCiEntityId(ciEntityTransactionMap, mainConfigObj, mappingMap); + /** 根据唯一属性列表查询配置项id **/ + Long ciEntityId = getCiEntityIdByUniqueAttrIdList(ciEntityTransactionMap, mainConfigObj); if (ciEntityId != null) { ciEntityTransactionVo.setCiEntityId(ciEntityId); ciEntityTransactionVo.setAction(TransactionActionType.UPDATE.getValue()); @@ -680,142 +520,9 @@ public class CmdbSyncProcessComponent extends ProcessStepHandlerBase { ciEntityTransactionVo.setAction(TransactionActionType.INSERT.getValue()); } /** 关系 **/ -// JSONObject relEntityData = new JSONObject(); -// List relList = RelUtil.ClearRepeatRel(relMapper.getRelByCiId(ciId)); -// for (RelVo relVo : relList) { -// String key = "rel" + relVo.getDirection() + "_" + relVo.getId(); -// CiEntitySyncMappingVo mappingObj = mappingMap.get(key); -// if (mappingObj == null) { -// continue; -// } -// String mappingMode = mappingObj.getMappingMode(); -// if (Objects.equals(mappingMode, "new")) { -// JSONArray valueList = mappingObj.getValueList(); -// if (CollectionUtils.isNotEmpty(valueList)) { -// List ciEntityIdList = new ArrayList<>(); -// List alreadyExistRelList = new ArrayList<>(); -// for (int i = 0; i < valueList.size(); i++) { -// JSONObject valueObj = valueList.getJSONObject(i); -// if (MapUtils.isEmpty(valueObj)) { -// continue; -// } -// String ciEntityUuid = valueObj.getString("ciEntityUuid"); -// if (StringUtils.isBlank(ciEntityUuid)) { -// continue; -// } -// Long ciEntityId = valueObj.getLong("ciEntityId"); -// if (ciEntityId == null) { -// CiEntityTransactionVo tmpVo = ciEntityTransactionMap.get(ciEntityUuid); -// if (tmpVo != null) { -// ciEntityId = tmpVo.getCiEntityId(); -// valueObj.put("ciEntityId", ciEntityId); -// } else { -// CiEntityVo uuidCiEntityVo = ciEntityMapper.getCiEntityBaseInfoByUuid(ciEntityUuid); -// if (uuidCiEntityVo == null) { -// throw new NewCiEntityNotFoundException(valueObj.getString("ciEntityUuid")); -// } else { -// ciEntityId = uuidCiEntityVo.getId(); -// valueObj.put("ciEntityId", ciEntityId); -// } -// } -// } -// ciEntityIdList.add(ciEntityId); -// String type = valueObj.getString("type"); -// if (!Objects.equals(type, "new")) { -// continue; -// } -// CiEntitySyncConfigVo dependencyConfig = dependencyConfigMap.get(ciEntityUuid); -// if (dependencyConfig == null) { -// continue; -// } -// // 关系选择追加模式时,需要将该配置项原来就关联的关系数据补充到valueList中 -// if (Objects.equals(dependencyConfig.getAction(), "append") && ciEntityTransactionVo.getCiEntityId() != null) { -// List relEntityList; -// if (relVo.getDirection().equals(RelDirectionType.FROM.getValue())) { -// relEntityList = relEntityMapper.getRelEntityByFromCiEntityIdAndRelId(ciEntityTransactionVo.getCiEntityId(), relVo.getId(), null); -// for (RelEntityVo relEntityVo : relEntityList) { -// JSONObject jsonObj = new JSONObject(); -// jsonObj.put("ciEntityId", relEntityVo.getToCiEntityId()); -// jsonObj.put("ciEntityName", relEntityVo.getToCiEntityName()); -// jsonObj.put("ciId", relEntityVo.getToCiId()); -// alreadyExistRelList.add(jsonObj); -// } -// } else { -// relEntityList = relEntityMapper.getRelEntityByToCiEntityIdAndRelId(ciEntityTransactionVo.getCiEntityId(), relVo.getId(), null); -// for (RelEntityVo relEntityVo : relEntityList) { -// JSONObject jsonObj = new JSONObject(); -// jsonObj.put("ciEntityId", relEntityVo.getFromCiEntityId()); -// jsonObj.put("ciEntityName", relEntityVo.getFromCiEntityName()); -// jsonObj.put("ciId", relEntityVo.getFromCiId()); -// alreadyExistRelList.add(jsonObj); -// } -// } -// } -// List list = createCiEntityTransactionVo(ciEntityTransactionMap, dependencyConfig, dependencyConfigMap); -// ciEntityTransactionList.addAll(list); -// } -// // 关系选择追加模式时,需要将该配置项原来就关联的关系数据补充到valueList中 -// for (JSONObject jsonObj : alreadyExistRelList) { -// if (ciEntityIdList.contains(jsonObj.getLong("ciEntityId"))) { -// continue; -// } -// valueList.add(jsonObj); -// } -// JSONObject relEntity = new JSONObject(); -// relEntity.put("valueList", valueList); -// relEntityData.put(key, relEntity); -// } -// } -// } JSONObject relEntityData = buildRelEntityData(ciEntityTransactionMap, mainConfigObj, dependencyConfigMap, mappingMap, ciEntityTransactionList); ciEntityTransactionVo.setRelEntityData(relEntityData); /** 全局属性 **/ -// JSONObject globalAttrEntityData = new JSONObject(); -// GlobalAttrVo searchVo = new GlobalAttrVo(); -// searchVo.setIsActive(1); -// List globalAttrList = globalAttrMapper.searchGlobalAttr(searchVo); -// for (GlobalAttrVo globalAttrVo : globalAttrList) { -// String key = "global_" + globalAttrVo.getId(); -// CiEntitySyncMappingVo mappingObj = mappingMap.get(key); -// if (mappingObj == null) { -// JSONObject globalAttrEntity = new JSONObject(); -// globalAttrEntity.put("valueList", new JSONArray()); -// globalAttrEntityData.put(key, globalAttrEntity); -// continue; -// } -// JSONArray valueList = new JSONArray(); -// // 映射模式为常量 -// 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) -// || Objects.equals(item.getId().toString(), valueObj.toString())) { -// 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(); -// globalAttrEntity.put("valueList", valueList); -// globalAttrEntityData.put(key, globalAttrEntity); -// } JSONObject globalAttrEntityData = buildGlobalAttrEntityData(mainConfigObj, mappingMap); ciEntityTransactionVo.setGlobalAttrEntityData(globalAttrEntityData); @@ -836,11 +543,9 @@ public class CmdbSyncProcessComponent extends ProcessStepHandlerBase { Map mappingMap ) { JSONObject attrEntityData = new JSONObject(); -// Map attrMap = new HashMap<>(); Long ciId = mainConfigObj.getCiId(); List attrList = attrMapper.getAttrByCiId(ciId); for (AttrVo attrVo : attrList) { -// attrMap.put(attrVo.getId(), attrVo); if (Objects.equals(attrVo.getType(), "expression")) { continue; } @@ -875,10 +580,15 @@ public class CmdbSyncProcessComponent extends ProcessStepHandlerBase { return attrEntityData; } - private Long getCiEntityId( + /** + * 根据唯一属性列表查询配置项id + * @param ciEntityTransactionMap + * @param mainConfigObj + * @return + */ + private Long getCiEntityIdByUniqueAttrIdList( Map ciEntityTransactionMap, - CiEntitySyncConfigVo mainConfigObj, - Map mappingMap) { + CiEntitySyncConfigVo mainConfigObj) { Long ciId = mainConfigObj.getCiId(); CiVo ciVo = ciMapper.getCiById(ciId); List uniqueAttrIdList = ciVo.getUniqueAttrIdList(); @@ -896,18 +606,23 @@ public class CmdbSyncProcessComponent extends ProcessStepHandlerBase { this.add(0L); }}); for (Long attrId : ciVo.getUniqueAttrIdList()) { - AttrFilterVo filterVo = new AttrFilterVo(); - filterVo.setAttrId(attrId); - filterVo.setExpression(SearchExpression.EQ.getExpression()); List valueList = new ArrayList<>(); AttrEntityTransactionVo attrEntityTransaction = ciEntityTransactionVo.getAttrEntityTransactionByAttrId(attrId); -// CiEntitySyncMappingVo mappingObj = mappingMap.get("attr_" + attrId); if (attrEntityTransaction != null) { JSONArray valueArray = attrEntityTransaction.getValueList(); if (CollectionUtils.isNotEmpty(valueArray)) { valueList = valueArray.toJavaList(String.class); } } + if (CollectionUtils.isEmpty(valueList)) { + AttrVo attr = attrMapper.getAttrById(attrId); + attr.setCiName(ciVo.getName()); + attr.setCiLabel(ciVo.getLabel()); + throw new CiUniqueAttrNotFoundException(attr); + } + AttrFilterVo filterVo = new AttrFilterVo(); + filterVo.setAttrId(attrId); + filterVo.setExpression(SearchExpression.EQ.getExpression()); filterVo.setValueList(valueList); ciEntityConditionVo.addAttrFilter(filterVo); } @@ -951,8 +666,6 @@ public class CmdbSyncProcessComponent extends ProcessStepHandlerBase { if (Objects.equals(mappingMode, "new")) {// 表示关系 JSONArray valueList = mappingObj.getValueList(); if (CollectionUtils.isNotEmpty(valueList)) { -// List ciEntityIdList = new ArrayList<>(); -// List alreadyExistRelList = new ArrayList<>(); for (int i = 0; i < valueList.size(); i++) { JSONObject valueObj = valueList.getJSONObject(i); if (MapUtils.isEmpty(valueObj)) { @@ -971,7 +684,7 @@ public class CmdbSyncProcessComponent extends ProcessStepHandlerBase { List list = createCiEntityTransactionVo(ciEntityTransactionMap, dependencyConfig, dependencyConfigMap); ciEntityTransactionList.addAll(list); // 关系选择追加模式时,需要将该配置项原来就关联的关系数据补充到valueList中 - if (Objects.equals(dependencyConfig.getAction(), "append") && ciEntityTransactionVo.getCiEntityId() != null) {// + if (Objects.equals(dependencyConfig.getAction(), "append") && ciEntityTransactionVo.getCiEntityId() != null) { valueObj.put("action", RelActionType.INSERT.getValue()); } else { valueObj.put("action", RelActionType.REPLACE.getValue()); @@ -982,36 +695,10 @@ public class CmdbSyncProcessComponent extends ProcessStepHandlerBase { if (ciEntityId == null) { CiEntityTransactionVo tmpVo = ciEntityTransactionMap.get(ciEntityUuid); if (tmpVo != null) { - ciEntityId = tmpVo.getCiEntityId();// + ciEntityId = tmpVo.getCiEntityId(); valueObj.put("ciEntityId", ciEntityId); - } else { - // TODO 这部分不会执行 - CiEntityVo uuidCiEntityVo = ciEntityMapper.getCiEntityBaseInfoByUuid(ciEntityUuid); - if (uuidCiEntityVo == null) { - throw new NewCiEntityNotFoundException(valueObj.getString("ciEntityUuid")); - } else { - ciEntityId = uuidCiEntityVo.getId(); - valueObj.put("ciEntityId", ciEntityId); - } } } -// ciEntityIdList.add(ciEntityId); -// String type = valueObj.getString("type"); -// if (!Objects.equals(type, "new")) {// 新配置信息 -// continue; -// } -// CiEntitySyncConfigVo dependencyConfig = dependencyConfigMap.get(ciEntityUuid); -// if (dependencyConfig == null) { -// continue; -// } -// // 关系选择追加模式时,需要将该配置项原来就关联的关系数据补充到valueList中 -// if (Objects.equals(dependencyConfig.getAction(), "append") && ciEntityTransactionVo.getCiEntityId() != null) {// -// valueObj.put("action", RelActionType.INSERT.getValue()); -// } else { -// valueObj.put("action", RelActionType.REPLACE.getValue()); -// } -// List list = createCiEntityTransactionVo(ciEntityTransactionMap, dependencyConfig, dependencyConfigMap); -// ciEntityTransactionList.addAll(list); } JSONObject relEntity = new JSONObject(); relEntity.put("valueList", valueList); @@ -1083,123 +770,6 @@ public class CmdbSyncProcessComponent extends ProcessStepHandlerBase { return globalAttrEntityData; } - /** - * 处理和校验下拉框类型和表格类型属性的值 - * - * @param ciEntityTransactionList - */ - private void validSelectAndTableAttrValueList(List ciEntityTransactionList) { - for (CiEntityTransactionVo ciEntityTransaction : ciEntityTransactionList) { - Long ciId = ciEntityTransaction.getCiId(); - List attrList = attrMapper.getAttrByCiId(ciId); - for (AttrVo attrVo : attrList) { - if (Objects.equals(attrVo.getType(), PropHandlerType.SELECT.getValue()) || Objects.equals(attrVo.getType(), PropHandlerType.TABLE.getValue())) { - IAttrValueHandler attrHandler = AttrValueHandlerFactory.getHandler(attrVo.getType()); - if (attrHandler == null) { - throw new AttrTypeNotFoundException(attrVo.getType()); - } - AttrEntityTransactionVo attrEntityTransactionVo = ciEntityTransaction.getAttrEntityTransactionByAttrId(attrVo.getId()); - if (attrEntityTransactionVo == null) { - continue; - } - JSONArray valueList = attrEntityTransactionVo.getValueList(); - if (CollectionUtils.isNotEmpty(valueList)) { - List longValueList = new ArrayList<>(); - List stringValueList = new ArrayList<>(); - JSONArray tempList = new JSONArray(); - tempList.addAll(valueList); - for (int i = 0; i < tempList.size(); i++) { - Object valueObj = tempList.get(i); - if (valueObj instanceof Long) { - longValueList.add((Long) valueObj); - } else if (valueObj instanceof String) { - String valueStr = valueObj.toString(); - try { - Integer.valueOf(valueStr); - stringValueList.add(valueStr); - } catch (NumberFormatException e1) { - try { - longValueList.add(Long.valueOf(valueStr)); - } catch (NumberFormatException e2) { - stringValueList.add(valueStr); - } - } - } - } - JSONArray newValueList = new JSONArray(); - if (CollectionUtils.isNotEmpty(longValueList)) { - JSONArray array = new JSONArray(); - array.addAll(longValueList); - attrHandler.valid(attrVo, array); - newValueList.addAll(longValueList); - } - if (CollectionUtils.isNotEmpty(stringValueList)) { - CiVo ciVo = ciMapper.getCiById(attrVo.getTargetCiId()); - if (ciVo == null) { - throw new CiNotFoundException(attrVo.getTargetCiId()); - } - for (String valueStr : stringValueList) { - CiEntityVo search = new CiEntityVo(); - search.setName(valueStr); - if (ciVo.getIsVirtual().equals(0)) { - // 非虚拟模型 - List downwardCiList = ciMapper.getDownwardCiListByLR(ciVo.getLft(), ciVo.getRht()); - Map downwardCiMap = downwardCiList.stream().collect(Collectors.toMap(e -> e.getId(), e -> e)); - boolean isFind = false; - for (CiEntityTransactionVo ciEntityTransactionVo : ciEntityTransactionList) { - CiVo downwardCi = downwardCiMap.get(ciEntityTransactionVo.getCiId()); - if (downwardCi == null) { - continue; - } - if (downwardCi.getNameAttrId() == null) { - continue; - } - AttrEntityTransactionVo attrEntityTransaction = ciEntityTransactionVo.getAttrEntityTransactionByAttrId(downwardCi.getNameAttrId()); - if (attrEntityTransaction == null) { - continue; - } - if (CollectionUtils.isEmpty(attrEntityTransaction.getValueList())) { - continue; - } - for (Object value : attrEntityTransaction.getValueList()) { - if (Objects.equals(value, valueStr)) { - newValueList.add(ciEntityTransactionVo.getCiEntityId());// - isFind = true; - break; - } - } - } - if (!isFind) { - search.setIdList(new ArrayList<>(downwardCiMap.keySet())); - List ciEntityList = ciEntityMapper.getCiEntityListByCiIdListAndName(search); - if (CollectionUtils.isEmpty(ciEntityList)) { - throw new AttrValueIrregularException(attrVo, valueStr); - } - for (CiEntityVo ciEntity : ciEntityList) { - newValueList.add(ciEntity.getId()); - } - } - } else { - // 虚拟模型 - search.setCiId(ciVo.getId()); - List ciEntityList = ciEntityMapper.getVirtualCiEntityBaseInfoByName(search); - if (CollectionUtils.isEmpty(ciEntityList)) { - throw new AttrValueIrregularException(attrVo, valueStr); - } - for (CiEntityVo ciEntity : ciEntityList) { - newValueList.add(ciEntity.getId()); - } - } - } - } - valueList.clear(); - valueList.addAll(newValueList); - } - } - } - } - } - /** * 处理和校验下拉框类型和表格类型属性的值 * @@ -1313,26 +883,27 @@ public class CmdbSyncProcessComponent extends ProcessStepHandlerBase { /** * 遍历configList,将“批量操作”的配置信息根据表单数据转换成多条“单个操作”配置信息 * @param originalConfigList 原始配置信息列表 - * @param currentConfig 当前要处理的配置信息 + * @param currentConfigList 当前要处理的配置信息列表 * @param formAttributeDataMap 表单数据信息 * @param newConfigList 收集新产生的配置信息列表 * @param formConfig 表单配置信息 * @return */ - private List handleBatchDataSource( + private JSONArray handleBatchDataSource( List originalConfigList, - CiEntitySyncConfigVo currentConfig, + List currentConfigList, Map formAttributeDataMap, List newConfigList, String formConfig ) { + CiEntitySyncConfigVo currentConfig = currentConfigList.get(0); Long ciId = currentConfig.getCiId(); CiVo ciVo = ciMapper.getCiById(ciId); if (ciVo == null) { String ciName = currentConfig.getCiName(); throw new CiNotFoundException(ciName); } - List newUuidList = new ArrayList<>(); + JSONArray resultList = new JSONArray(); List batchDataColumnList = new ArrayList<>(); JSONArray batchDataList = new JSONArray(); if (Objects.equals(currentConfig.getCreatePolicy(), "batch")) { @@ -1341,95 +912,68 @@ public class CmdbSyncProcessComponent extends ProcessStepHandlerBase { String attributeUuid = batchDataSource.getAttributeUuid(); Object dataObj = formAttributeDataMap.get(attributeUuid); if (dataObj == null) { - return newUuidList; + return resultList; } if (dataObj instanceof JSONArray) { JSONArray dataList = (JSONArray) dataObj; List filterList = batchDataSource.getFilterList(); batchDataList = filterData(dataList, filterList, formConfig); if (CollectionUtils.isEmpty(batchDataList)) { - return newUuidList; + return resultList; } JSONObject rowDataObj = batchDataList.getJSONObject(0); batchDataColumnList.addAll(rowDataObj.keySet()); } else { - return newUuidList; + return resultList; } } - handleMappingFormComponent(currentConfig, batchDataColumnList, formAttributeDataMap); - List mappingList = currentConfig.getMappingList(); - if (CollectionUtils.isNotEmpty(batchDataList)) { - // 遍历批量操作表格数据 - for (int j = 0; j < batchDataList.size(); j++) { - JSONObject rowDataObj = batchDataList.getJSONObject(j); - if (MapUtils.isEmpty(rowDataObj)) { - continue; - } - List newMappingList = new ArrayList<>(); - for (CiEntitySyncMappingVo mappingObj : mappingList) { - JSONArray valueList = mappingObj.getValueList(); - if (CollectionUtils.isEmpty(valueList)) { + Map targetCiConfigMap = new HashMap<>(); + for (int i = 1; i < currentConfigList.size(); i++) { + CiEntitySyncConfigVo ciEntitySyncConfig = currentConfigList.get(i); + targetCiConfigMap.put(ciEntitySyncConfig.getCiId().toString(), ciEntitySyncConfig); + targetCiConfigMap.put(ciEntitySyncConfig.getCiName(), ciEntitySyncConfig); + targetCiConfigMap.put(ciEntitySyncConfig.getCiLabel(), ciEntitySyncConfig); + } + List mappingList = handleMappingFormComponent(currentConfig.getMappingList(), batchDataColumnList, formAttributeDataMap); + if (Objects.equals(currentConfig.getCreatePolicy(), "batch")) { + if (CollectionUtils.isNotEmpty(batchDataList)) { + // 遍历批量操作表格数据 + for (int j = 0; j < batchDataList.size(); j++) { + JSONObject rowDataObj = batchDataList.getJSONObject(j); + if (MapUtils.isEmpty(rowDataObj)) { continue; } - CiEntitySyncMappingVo newMappingObj = new CiEntitySyncMappingVo(mappingObj); - String mappingMode = mappingObj.getMappingMode(); - if (Objects.equals(mappingMode, "formTableComponent") || Objects.equals(mappingMode, "formSubassemblyComponent")) { - // 映射模式是表单表格组件 - String attributeUuid = valueList.getString(valueList.size() -1); - if (!batchDataColumnList.contains(attributeUuid)) { - continue; - } - JSONArray newValueList = new JSONArray(); - Object valueObj = rowDataObj.get(attributeUuid); - if (valueObj instanceof JSONArray) { - JSONArray valueArray = (JSONArray) valueObj; - newValueList.addAll(valueArray); - } else { - newValueList.add(valueObj); - } - if (CollectionUtils.isNotEmpty(newValueList)) { - newMappingObj.setMappingMode("constant"); - newMappingObj.setValueList(newValueList); - } else { - continue; - } - newMappingList.add(newMappingObj); - } else { - newMappingList.add(newMappingObj); - } + List newMappingList = new ArrayList<>(); + newMappingList.addAll(mappingList); + List list = handleMappingFormTableComponent(rowDataObj, batchDataColumnList, currentConfig.getMappingList()); + newMappingList.addAll(list); + CiEntitySyncConfigVo newConfigObj = createCiEntitySyncConfigVo(ciVo, currentConfig, newMappingList, batchDataColumnList, formAttributeDataMap, targetCiConfigMap, rowDataObj); + JSONObject resultObj = new JSONObject(); + resultObj.put("ciEntityUuid", newConfigObj.getUuid()); + resultObj.put("ciEntityName", newConfigObj.getCiName()); + resultObj.put("ciId", newConfigObj.getCiId()); + resultObj.put("type", "new"); + resultList.add(resultObj); + newConfigList.add(newConfigObj); + formAttributeDataMap.putAll(rowDataObj); + Map newFormAttributeDataMap = new HashMap<>(); + newFormAttributeDataMap.putAll(formAttributeDataMap); + newFormAttributeDataMap.putAll(rowDataObj); + handleMappingRelConfig(originalConfigList, newConfigObj, newFormAttributeDataMap, newConfigList, formConfig); } - CiEntitySyncConfigVo newConfigObj = new CiEntitySyncConfigVo(); - newConfigObj.setUuid(UuidUtil.randomUuid()); - newConfigObj.setCiId(currentConfig.getCiId()); - newConfigObj.setCiName(currentConfig.getCiName()); - newConfigObj.setCiLabel(currentConfig.getCiLabel()); - newConfigObj.setIsStart(currentConfig.getIsStart()); - newConfigObj.setCiIcon(currentConfig.getCiIcon()); - newConfigObj.setEditMode(currentConfig.getEditMode()); - newConfigObj.setMappingList(newMappingList); - newUuidList.add(newConfigObj.getUuid()); - newConfigList.add(newConfigObj); - formAttributeDataMap.putAll(rowDataObj); - Map newFormAttributeDataMap = new HashMap<>(); - newFormAttributeDataMap.putAll(formAttributeDataMap); - newFormAttributeDataMap.putAll(rowDataObj); - handleMappingRelConfig(originalConfigList, newConfigObj, newFormAttributeDataMap, newConfigList, formConfig); } } else { - CiEntitySyncConfigVo newConfigObj = new CiEntitySyncConfigVo(); - newConfigObj.setUuid(UuidUtil.randomUuid()); - newConfigObj.setCiId(currentConfig.getCiId()); - newConfigObj.setCiName(currentConfig.getCiName()); - newConfigObj.setCiLabel(currentConfig.getCiLabel()); - newConfigObj.setIsStart(currentConfig.getIsStart()); - newConfigObj.setCiIcon(currentConfig.getCiIcon()); - newConfigObj.setEditMode(currentConfig.getEditMode()); - newConfigObj.setMappingList(currentConfig.getMappingList()); - newUuidList.add(newConfigObj.getUuid()); + CiEntitySyncConfigVo newConfigObj = createCiEntitySyncConfigVo(ciVo, currentConfig, mappingList, batchDataColumnList, formAttributeDataMap, targetCiConfigMap, null); + JSONObject resultObj = new JSONObject(); + resultObj.put("ciEntityUuid", newConfigObj.getUuid()); + resultObj.put("ciEntityName", newConfigObj.getCiName()); + resultObj.put("ciId", newConfigObj.getCiId()); + resultObj.put("type", "new"); + resultList.add(resultObj); newConfigList.add(newConfigObj); handleMappingRelConfig(originalConfigList, newConfigObj, formAttributeDataMap, newConfigList, formConfig); } - return newUuidList; + return resultList; } /** @@ -1458,6 +1002,8 @@ public class CmdbSyncProcessComponent extends ProcessStepHandlerBase { continue; } JSONArray newValueList = new JSONArray(); + boolean isFrom = false; + List ciEntityUuidList = new ArrayList<>(); for (int i = 0; i < valueList.size(); i++) { JSONObject valueObj = valueList.getJSONObject(i); if (MapUtils.isEmpty(valueObj)) { @@ -1467,21 +1013,28 @@ public class CmdbSyncProcessComponent extends ProcessStepHandlerBase { if (StringUtils.isBlank(ciEntityUuid)) { continue; } + ciEntityUuidList.add(ciEntityUuid); String type = valueObj.getString("type"); - if (Objects.equals(type, "new")) { - CiEntitySyncConfigVo relConfigObj = originalConfigList.stream().filter(e -> Objects.equals(e.getUuid(), ciEntityUuid)).findFirst().get(); - replaceMappingRelFromCiEntityUuid(relConfigObj, currentConfig.getUuid()); - List newCiEntityUuidList = handleBatchDataSource(originalConfigList, relConfigObj, formAttributeDataMap, newConfigList, formConfig); - for (String newCiEntityUuid : newCiEntityUuidList) { - JSONObject newValueObj = new JSONObject(); - newValueObj.putAll(valueObj); - newValueObj.put("ciEntityUuid", newCiEntityUuid); - newValueList.add(newValueObj); - } - } else { + if (Objects.equals(type, "from")) { JSONObject newValueObj = new JSONObject(); newValueObj.putAll(valueObj); newValueList.add(newValueObj); + isFrom = true; + break; + } + } + if (!isFrom) { + List relConfigObjList = new ArrayList<>(); + for (CiEntitySyncConfigVo originalConfig : originalConfigList) { + if (ciEntityUuidList.contains(originalConfig.getUuid())) { + replaceMappingRelFromCiEntityUuid(originalConfig, currentConfig.getUuid()); + relConfigObjList.add(originalConfig); + } + } + if (CollectionUtils.isNotEmpty(relConfigObjList)) { + newValueList = handleBatchDataSource(originalConfigList, relConfigObjList, formAttributeDataMap, newConfigList, formConfig); + } else { + logger.warn("mappingObj = " + JSONObject.toJSONString(mappingObj)); } } mappingObj.setValueList(newValueList); @@ -1523,16 +1076,16 @@ public class CmdbSyncProcessComponent extends ProcessStepHandlerBase { /** * 遍历configList,将mappingList中映射模式为“表单普通组件”和“表单表格组件”的数据替换称表单组件对应的真实值 - * @param configObj 配置信息 + * @param mappingList 配置信息 * @param batchDataColumnList 遍历对象属性列表 * @param formAttributeDataMap 表单数据信息 */ - private void handleMappingFormComponent( - CiEntitySyncConfigVo configObj, + private List handleMappingFormComponent( + List mappingList, List batchDataColumnList, Map formAttributeDataMap) { - List mappingList = configObj.getMappingList(); + List newMappingList = new ArrayList<>(); for (CiEntitySyncMappingVo mappingObj : mappingList) { JSONArray valueList = mappingObj.getValueList(); if (CollectionUtils.isEmpty(valueList)) { @@ -1540,7 +1093,6 @@ public class CmdbSyncProcessComponent extends ProcessStepHandlerBase { } JSONArray newValueList = new JSONArray(); String mappingMode = mappingObj.getMappingMode(); - mappingObj.setValueList(newValueList); if (Objects.equals(mappingMode, "formTableComponent") || Objects.equals(mappingMode, "formSubassemblyComponent")) { String attributeUuid = valueList.getString(valueList.size() -1); @@ -1577,10 +1129,139 @@ public class CmdbSyncProcessComponent extends ProcessStepHandlerBase { newValueList.remove(i); } } + if (CollectionUtils.isNotEmpty(newValueList)) { + CiEntitySyncMappingVo newMappingObj = new CiEntitySyncMappingVo(mappingObj); + newMappingObj.setMappingMode("constant"); + newMappingObj.setValueList(newValueList); + newMappingList.add(newMappingObj); + } } + return newMappingList; } + private List handleMappingFormTableComponent( + JSONObject rowDataObj, + List batchDataColumnList, + List mappingList + ) { + List newMappingList = new ArrayList<>(); + for (CiEntitySyncMappingVo mappingObj : mappingList) { + JSONArray valueList = mappingObj.getValueList(); + if (CollectionUtils.isEmpty(valueList)) { + continue; + } + String mappingMode = mappingObj.getMappingMode(); + if (Objects.equals(mappingMode, "formTableComponent") || Objects.equals(mappingMode, "formSubassemblyComponent")) { + // 映射模式是表单表格组件 + String attributeUuid = valueList.getString(valueList.size() -1); + if (!batchDataColumnList.contains(attributeUuid)) { + continue; + } + JSONArray newValueList = new JSONArray(); + Object valueObj = rowDataObj.get(attributeUuid); + if (valueObj instanceof JSONArray) { + JSONArray valueArray = (JSONArray) valueObj; + newValueList.addAll(valueArray); + } else { + newValueList.add(valueObj); + } + for (int i = newValueList.size() - 1; i >= 0; i--) { + if (StringUtils.isBlank(newValueList.getString(i))){ + newValueList.remove(i); + } + } + if (CollectionUtils.isNotEmpty(newValueList)) { + CiEntitySyncMappingVo newMappingObj = new CiEntitySyncMappingVo(mappingObj); + newMappingObj.setMappingMode("constant"); + newMappingObj.setValueList(newValueList); + newMappingList.add(newMappingObj); + } + } + } + return newMappingList; + } + private CiEntitySyncConfigVo createCiEntitySyncConfigVo( + CiVo ciVo, + CiEntitySyncConfigVo currentConfig, + List newMappingList, + List batchDataColumnList, + Map formAttributeDataMap, + Map targetCiConfigMap, + JSONObject rowDataObj) { + CiEntitySyncConfigVo newConfigObj = new CiEntitySyncConfigVo(); + newConfigObj.setUuid(UuidUtil.randomUuid()); + if (Objects.equals(ciVo.getIsAbstract(), 1)) { + Optional targetCiIdMappingObj = newMappingList.stream().filter(e -> Objects.equals(e.getKey(), "targetCiId")).findFirst(); + if (!targetCiIdMappingObj.isPresent()) { + throw new AbstractCiTargetCiIdAttrNotFoundException(ciVo); + } + JSONArray valueList = targetCiIdMappingObj.get().getValueList(); + if (CollectionUtils.isEmpty(valueList)) { + throw new AbstractCiTargetCiIdAttrNotFoundException(ciVo); + } + String valueStr = valueList.getString(0); + CiEntitySyncConfigVo targetCiConfig = targetCiConfigMap.get(valueStr); + if (targetCiConfig == null) { + CiVo targetCi = null; + List downwardCiList = ciMapper.getDownwardCiListByLR(ciVo.getLft(), ciVo.getRht()); + for (CiVo downwardCi : downwardCiList) { + if (Objects.equals(downwardCi.getId().toString(), valueStr)) { + targetCi = downwardCi; + } else if (Objects.equals(downwardCi.getName(), valueStr)) { + targetCi = downwardCi; + } else if (Objects.equals(downwardCi.getLabel(), valueStr)) { + targetCi = downwardCi; + } + } + if (targetCi == null) { + throw new DownwardCiNotFoundException(ciVo, valueStr); + } else if (Objects.equals(targetCi.getIsAbstract(), 1)) { + throw new CiIsAbstractedException(CiIsAbstractedException.Type.DATA, targetCi.getLabel() + "(" + targetCi.getName() + ")"); + } + newConfigObj.setCiId(targetCi.getId()); + newConfigObj.setCiName(targetCi.getName()); + newConfigObj.setCiLabel(targetCi.getLabel()); + newConfigObj.setIsStart(currentConfig.getIsStart()); + newConfigObj.setCiIcon(targetCi.getIcon()); + newConfigObj.setEditMode(currentConfig.getEditMode()); + } else { + List mappingList = targetCiConfig.getMappingList(); + List list = handleMappingFormComponent(mappingList, batchDataColumnList, formAttributeDataMap); + newMappingList.addAll(list); + if (rowDataObj != null) { + list = handleMappingFormTableComponent(rowDataObj, batchDataColumnList, targetCiConfig.getMappingList()); + newMappingList.addAll(list); + } + for (CiEntitySyncMappingVo mapping : mappingList) { + if (Objects.equals(mapping.getMappingMode(), "new")) { + newMappingList.add(new CiEntitySyncMappingVo(mapping)); + } + } + newConfigObj.setCiId(targetCiConfig.getCiId()); + newConfigObj.setCiName(targetCiConfig.getCiName()); + newConfigObj.setCiLabel(targetCiConfig.getCiLabel()); + newConfigObj.setIsStart(targetCiConfig.getIsStart()); + newConfigObj.setCiIcon(targetCiConfig.getCiIcon()); + newConfigObj.setEditMode(targetCiConfig.getEditMode()); + } + } else { + newConfigObj.setCiId(currentConfig.getCiId()); + newConfigObj.setCiName(currentConfig.getCiName()); + newConfigObj.setCiLabel(currentConfig.getCiLabel()); + newConfigObj.setIsStart(currentConfig.getIsStart()); + newConfigObj.setCiIcon(currentConfig.getCiIcon()); + newConfigObj.setEditMode(currentConfig.getEditMode()); + } + List mappingList = currentConfig.getMappingList(); + for (CiEntitySyncMappingVo mapping : mappingList) { + if (Objects.equals(mapping.getMappingMode(), "new")) { + newMappingList.add(new CiEntitySyncMappingVo(mapping)); + } + } + newConfigObj.setMappingList(newMappingList); + return newConfigObj; + } /** * 表单数据适配CMDB数据