From d411e34565e435dc5bcd90a336b96d065af4e9d3 Mon Sep 17 00:00:00 2001
From: "1437892690@qq.com" <1437892690@qq.com>
Date: Fri, 17 Oct 2025 18:38:06 +0800
Subject: [PATCH] =?UTF-8?q?[=E5=8A=9F=E8=83=BD]=20=E5=A2=9E=E5=8A=A0?=
=?UTF-8?q?=E4=B8=80=E4=B8=AA=E5=90=8C=E6=AD=A5balantflow=E8=87=AA?=
=?UTF-8?q?=E5=AE=9A=E4=B9=89=E8=A7=86=E5=9B=BE=E6=95=B0=E6=8D=AE=E5=88=B0?=
=?UTF-8?q?mongodb=E6=8E=A5=E5=8F=A3?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
关联 #[1421065807429632]增加一个同步balantflow自定义视图数据到mongodb接口 http://192.168.0.96:8090/demo/rdm.html#/story-detail/939050947543040/939050947543042/1421065807429632
---
...CiEntityDataToMongoDBForBalantFlowApi.java | 615 ++++++++++++++++++
1 file changed, 615 insertions(+)
create mode 100644 src/main/java/neatlogic/module/cmdb/api/synccientity/BatchSyncCiEntityDataToMongoDBForBalantFlowApi.java
diff --git a/src/main/java/neatlogic/module/cmdb/api/synccientity/BatchSyncCiEntityDataToMongoDBForBalantFlowApi.java b/src/main/java/neatlogic/module/cmdb/api/synccientity/BatchSyncCiEntityDataToMongoDBForBalantFlowApi.java
new file mode 100644
index 00000000..6ba29732
--- /dev/null
+++ b/src/main/java/neatlogic/module/cmdb/api/synccientity/BatchSyncCiEntityDataToMongoDBForBalantFlowApi.java
@@ -0,0 +1,615 @@
+/*
+ * Copyright (C) 2025 深圳极向量科技有限公司 All Rights Reserved.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see .
+ */
+
+package neatlogic.module.cmdb.api.synccientity;
+
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import com.alibaba.fastjson.serializer.SerializerFeature;
+import neatlogic.framework.auth.core.AuthAction;
+import neatlogic.framework.auth.label.ADMIN;
+import neatlogic.framework.cmdb.dto.ci.CiVo;
+import neatlogic.framework.cmdb.dto.sync.ObjectVo;
+import neatlogic.framework.cmdb.exception.sync.CollectionNameFieldValueRepeatException;
+import neatlogic.framework.common.constvalue.ApiParamType;
+import neatlogic.framework.exception.integration.IntegrationHandlerNotFoundException;
+import neatlogic.framework.integration.core.IIntegrationHandler;
+import neatlogic.framework.integration.core.IntegrationHandlerFactory;
+import neatlogic.framework.integration.dao.mapper.IntegrationMapper;
+import neatlogic.framework.integration.dto.IntegrationResultVo;
+import neatlogic.framework.integration.dto.IntegrationVo;
+import neatlogic.framework.matrix.exception.MatrixExternalAccessException;
+import neatlogic.framework.restful.annotation.*;
+import neatlogic.framework.restful.constvalue.OperationTypeEnum;
+import neatlogic.framework.restful.core.privateapi.PrivateApiComponentBase;
+import neatlogic.framework.util.HanyuPinyinUtil;
+import neatlogic.framework.util.Md5Util;
+import neatlogic.framework.util.TableResultUtil;
+import neatlogic.framework.util.UuidUtil;
+import neatlogic.module.cmdb.dao.mapper.ci.CiMapper;
+import neatlogic.module.cmdb.dao.mapper.sync.ObjectMapper;
+import neatlogic.module.framework.integration.handler.FrameworkRequestFrom;
+import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.collections4.MapUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.data.mongodb.core.MongoTemplate;
+import org.springframework.data.mongodb.core.query.Criteria;
+import org.springframework.data.mongodb.core.query.Query;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.*;
+
+@Service
+@AuthAction(action = ADMIN.class)
+@OperationType(type = OperationTypeEnum.OPERATE)
+public class BatchSyncCiEntityDataToMongoDBForBalantFlowApi extends PrivateApiComponentBase {
+
+ private final static Logger logger = LoggerFactory.getLogger(BatchSyncCiEntityDataToMongoDBForBalantFlowApi.class);
+
+ private final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
+ @Resource
+ private MongoTemplate mongoTemplate;
+
+ @Resource
+ private IntegrationMapper integrationMapper;
+
+ @Resource
+ private CiMapper ciMapper;
+
+ @Resource
+ private ObjectMapper objectMapper;
+
+ @Override
+ public String getName() {
+ return "批量同步CMDB配置项数据到mongodb";
+ }
+
+ @Override
+ public JSONObject example() {
+ JSONObject defaultJson = new JSONObject(true);
+ JSONArray dictionaryConfigList = new JSONArray();
+ {
+ JSONObject dictionaryConfigObj = new JSONObject(true);
+ dictionaryConfigObj.put("dictionaryName", "APP");
+ dictionaryConfigObj.put("dictionaryLabel", "应用系统");
+ dictionaryConfigObj.put("dictionaryCollection", "COLLECT_BALANTFLOWCIENTITY");
+ dictionaryConfigObj.put("_OBJ_CATEGORY", "APPLICATION");
+ dictionaryConfigObj.put("_OBJ_TYPE", "APPLICATION");
+ dictionaryConfigObj.put("neatLogicCiName", "APP");
+ dictionaryConfigObj.put("balantflowCiId", "99");
+ dictionaryConfigObj.put("balantflowCiName", "应用系统");
+ dictionaryConfigList.add(dictionaryConfigObj);
+ }
+ {
+ JSONObject dictionaryConfigObj = new JSONObject(true);
+ dictionaryConfigObj.put("dictionaryName", "APPComponent");
+ dictionaryConfigObj.put("dictionaryLabel", "应用模块");
+ dictionaryConfigObj.put("dictionaryCollection", "COLLECT_BALANTFLOWCIENTITY");
+ dictionaryConfigObj.put("_OBJ_CATEGORY", "APPLICATION");
+ dictionaryConfigObj.put("_OBJ_TYPE", "APPLICATION_MODULE");
+ dictionaryConfigObj.put("neatLogicCiName", "APPComponent");
+ dictionaryConfigObj.put("balantflowCiId", "99");
+ dictionaryConfigObj.put("balantflowCiName", "应用系统");
+ dictionaryConfigList.add(dictionaryConfigObj);
+ }
+ JSONArray batchList = new JSONArray();
+ JSONObject jsonObj = new JSONObject();
+ jsonObj.put("dictionaryConfigList", dictionaryConfigList);
+ jsonObj.put("balantflowViewId", 66);
+ jsonObj.put("balantflowViewName", "视图名称");
+ batchList.add(jsonObj);
+ defaultJson.put("batchList", batchList);
+ String integrationName = "获取数据的集成名称";
+ defaultJson.put("integrationName", integrationName);
+ defaultJson.put("integrationRequestMaxCount", 10000);
+ defaultJson.put("pageSize", 1000);
+ defaultJson.put("needSyncData", true);
+ return defaultJson;
+ }
+
+ @Input({
+ @Param(name = "batchList", type = ApiParamType.JSONARRAY, isRequired = true, minSize = 1, desc = "批量列表"),
+ @Param(name = "integrationName", type = ApiParamType.STRING, isRequired = true, desc = "集成名称"),
+ @Param(name = "pageSize", type = ApiParamType.INTEGER, defaultValue = "1000", desc = "common.pagesize"),
+ @Param(name = "integrationRequestMaxCount", type = ApiParamType.INTEGER, defaultValue = "10000", desc = "集成请求最大次数,默认是10000"),
+ @Param(name = "needSyncData", type = ApiParamType.BOOLEAN, defaultValue = "false", desc = "是否需要同步数据,值为false时只同步mongodb中的dictionary数据"),
+ })
+ @Output({
+
+ })
+ @Description(desc = "同步CMDB配置项数据,获取数据接口是balantflow/restservices/cmdb/glasnostViewQueryRestComponentApi?currentPage=1&pageSize=20&id=65")
+ @Override
+ public Object myDoService(JSONObject paramObj) throws Exception {
+ String integrationName = paramObj.getString("integrationName");
+ int integrationRequestMaxCount = paramObj.getInteger("integrationRequestMaxCount");
+ Integer pageSize = paramObj.getInteger("pageSize");
+ pageSize = pageSize != null ? pageSize : 1000;
+ JSONArray batchList = paramObj.getJSONArray("batchList");
+ Boolean needSyncData = paramObj.getBoolean("needSyncData");
+ needSyncData = needSyncData != null && needSyncData;
+ if (CollectionUtils.isNotEmpty(batchList)) {
+ JSONArray tbodyList = new JSONArray();
+ for (int i = 0; i < batchList.size(); i++) {
+ JSONObject jsonObj = batchList.getJSONObject(i);
+ if (MapUtils.isNotEmpty(jsonObj)) {
+ JSONArray dictionaryConfigList = jsonObj.getJSONArray("dictionaryConfigList");
+ Long balantflowViewId = jsonObj.getLong("balantflowViewId");
+ String balantflowViewName = jsonObj.getString("balantflowViewName");
+ JSONObject tbodyObj = singleProcess(dictionaryConfigList, balantflowViewId, balantflowViewName, integrationName, integrationRequestMaxCount, pageSize, needSyncData);
+ tbodyList.add(tbodyObj);
+ }
+ }
+ return TableResultUtil.getResult(tbodyList);
+ }
+ return new JSONObject();
+ }
+
+ private JSONObject singleProcess(JSONArray dictionaryConfigList, Long balantflowViewId, String balantflowViewName, String integrationName, int integrationRequestMaxCount, int pageSize, boolean needSyncData) {
+ JSONObject resultObj = new JSONObject();
+ resultObj.put("id", balantflowViewId);
+ resultObj.put("name", balantflowViewName);
+ resultObj.put("pageSize", pageSize);
+ resultObj.put("needSyncData", needSyncData);
+ IntegrationVo supplierIntegrationVo = integrationMapper.getIntegrationByName(integrationName);
+ if (supplierIntegrationVo != null) {
+ IIntegrationHandler handler = IntegrationHandlerFactory.getHandler(supplierIntegrationVo.getHandler());
+ if (handler == null) {
+ throw new IntegrationHandlerNotFoundException(supplierIntegrationVo.getHandler());
+ }
+ int rowNum = 0;
+ supplierIntegrationVo.getParamObj().put("id", balantflowViewId);
+ supplierIntegrationVo.getParamObj().put("name", balantflowViewName);
+ supplierIntegrationVo.getParamObj().put("pageSize", pageSize);
+ boolean flag = false;
+ Map attrId2CiIdMap = new HashMap<>();
+ JSONArray dictionaryList = new JSONArray();
+ LocalDateTime now = LocalDateTime.now();
+ for (int currentPage = 1; currentPage <= integrationRequestMaxCount; currentPage++) {
+ supplierIntegrationVo.getParamObj().put("currentPage", currentPage);
+ resultObj.put("requestCount", currentPage);
+ IntegrationResultVo resultVo = handler.sendRequest(supplierIntegrationVo, FrameworkRequestFrom.API);
+ if (StringUtils.isNotBlank(resultVo.getError())) {
+ logger.error(resultVo.getError());
+ throw new MatrixExternalAccessException(supplierIntegrationVo.getName());
+ }
+ handler.validate(resultVo);
+ JSONObject dataObj = JSONObject.parseObject(resultVo.getTransformedResult());
+ if (MapUtils.isNotEmpty(dataObj)) {
+ JSONObject returnObj = dataObj.getJSONObject("Return");
+ if (MapUtils.isNotEmpty(returnObj)) {
+ if (!flag) {
+ Long id = returnObj.getLong("id");
+ String name = returnObj.getString("name");
+ JSONArray columnList = returnObj.getJSONArray("columnList");
+ if (CollectionUtils.isNotEmpty(columnList)) {
+ for (int i = 0; i < columnList.size(); i++) {
+ JSONObject columnObj = columnList.getJSONObject(i);
+ if (MapUtils.isNotEmpty(columnObj)) {
+ Long ciId = columnObj.getLong("ciId");
+ Long attrId = columnObj.getLong("attrId");
+ String label = columnObj.getString("label");
+ attrId2CiIdMap.put(attrId, ciId);
+ }
+ }
+ }
+ dictionaryList = generateDictionaryList(id, name, columnList, dictionaryConfigList);
+ saveDictionaryList(dictionaryList, now);
+ flag = true;
+ }
+ if (needSyncData) {
+ JSONArray resultList = returnObj.getJSONArray("resultList");
+ if (CollectionUtils.isNotEmpty(resultList)) {
+ rowNum += resultList.size();
+ savePageData(resultList, attrId2CiIdMap, dictionaryList, now);
+ }
+ } else {
+ break;
+ }
+ } else {
+ break;
+ }
+ }
+ }
+ resultObj.put("rowNum", rowNum);
+ }
+ return resultObj;
+ }
+
+ private JSONArray generateDictionaryList(
+ Long customViewId,
+ String customViewName,
+ JSONArray columnList,
+ JSONArray dictionaryConfigList
+ ) {
+ JSONArray dictionaryList = new JSONArray();
+ Set balantflowCiIdSet = new HashSet<>();
+ Map attrId2AttrLabelMap = new HashMap<>();
+ Map ciId2AttrListMap = new LinkedHashMap<>();
+ if (CollectionUtils.isNotEmpty(columnList)) {
+ for (int i = 0; i < columnList.size(); i++) {
+ JSONObject columnObj = columnList.getJSONObject(i);
+ if (MapUtils.isNotEmpty(columnObj)) {
+ Long ciId = columnObj.getLong("ciId");
+ Long attrId = columnObj.getLong("attrId");
+ String label = columnObj.getString("label");
+ balantflowCiIdSet.add(ciId);
+ JSONObject attrObj = new JSONObject(true);
+ String name = HanyuPinyinUtil.format(label + "_" + attrId);
+ attrObj.put("name", name);
+ attrObj.put("desc", label + "_" + attrId);
+ attrObj.put("type", "String");
+ attrObj.put("attrId", attrId);
+ ciId2AttrListMap.computeIfAbsent(ciId, key -> new JSONArray()).add(attrObj);
+ attrId2AttrLabelMap.put(attrId, label);
+ }
+ }
+ }
+ if (CollectionUtils.isNotEmpty(dictionaryConfigList)) {
+ Map ciId2CiName = new HashMap<>();
+ for (int i = 0; i < dictionaryConfigList.size(); i++) {
+ JSONObject dictionaryConfigObj = dictionaryConfigList.getJSONObject(i);
+ if (MapUtils.isNotEmpty(dictionaryConfigObj)) {
+ Long balantflowCiId = dictionaryConfigObj.getLong("balantflowCiId");
+ String balantflowCiName = dictionaryConfigObj.getString("balantflowCiName");
+ if (balantflowCiId != null) {
+ if (StringUtils.isNotBlank(balantflowCiName)) {
+ ciId2CiName.put(balantflowCiId, balantflowCiName);
+ }
+ }
+ }
+ }
+ for (int i = 0; i < dictionaryConfigList.size(); i++) {
+ JSONObject dictionaryConfigObj = dictionaryConfigList.getJSONObject(i);
+ if (MapUtils.isNotEmpty(dictionaryConfigObj)) {
+ String dictionaryName = dictionaryConfigObj.getString("dictionaryName");
+ String dictionaryLabel = dictionaryConfigObj.getString("dictionaryLabel");
+ String dictionaryCollection = dictionaryConfigObj.getString("dictionaryCollection");
+ String dictionaryCollectionLabel = dictionaryConfigObj.getString("dictionaryCollectionLabel");
+ String _OBJ_CATEGORY = dictionaryConfigObj.getString("_OBJ_CATEGORY");
+ String _OBJ_TYPE = dictionaryConfigObj.getString("_OBJ_TYPE");
+ String neatLogicCiName = dictionaryConfigObj.getString("neatLogicCiName");
+ Long balantflowCiId = dictionaryConfigObj.getLong("balantflowCiId");
+ String balantflowCiName = dictionaryConfigObj.getString("balantflowCiName");
+ List multipleSelectTypeAttrIdList = new ArrayList<>();
+ JSONArray multipleSelectTypeAttrIdArray = dictionaryConfigObj.getJSONArray("multipleSelectTypeAttrIdList");
+ if (CollectionUtils.isNotEmpty(multipleSelectTypeAttrIdArray)) {
+ multipleSelectTypeAttrIdList = multipleSelectTypeAttrIdArray.toJavaList(Long.class);
+ }
+ if (StringUtils.isNotBlank(dictionaryName)) {
+ JSONObject dictionaryObj = new JSONObject();
+ String id = UuidUtil.getCustomUUID(dictionaryName);
+ dictionaryObj.put("_id", id);
+ dictionaryObj.put("name", dictionaryName);
+ dictionaryObj.put("label", dictionaryLabel);
+ dictionaryObj.put("collection", dictionaryCollection);
+ dictionaryObj.put("collectionLabel", dictionaryCollectionLabel);
+ dictionaryObj.put("_OBJ_CATEGORY", _OBJ_CATEGORY);
+ dictionaryObj.put("_OBJ_TYPE", _OBJ_TYPE);
+ dictionaryObj.put("neatLogicCiName", neatLogicCiName);
+ dictionaryObj.put("balantflowCiId", balantflowCiId);
+ dictionaryObj.put("balantflowCiName", balantflowCiName);
+ JSONArray fields = new JSONArray();
+ for (Long ciId : balantflowCiIdSet) {
+ String ciName = ciId2CiName.get(ciId);
+ JSONObject fieldObj = generateField(ciId, ciName, multipleSelectTypeAttrIdList, columnList);
+ if (MapUtils.isNotEmpty(fieldObj)) {
+ fields.add(fieldObj);
+ }
+ dictionaryObj.put("fields", fields);
+ }
+ JSONObject balantflowObj = new JSONObject();
+ balantflowObj.put("customViewId", customViewId);
+ balantflowObj.put("customViewName", customViewName);
+ dictionaryObj.put("balantflow", balantflowObj);
+ JSONObject filterObj = new JSONObject();
+ filterObj.put("dictionaryName", dictionaryName);
+ dictionaryObj.put("filter", filterObj);
+ dictionaryList.add(dictionaryObj);
+ }
+ }
+ }
+ }
+ return dictionaryList;
+ }
+
+ private JSONObject generateField(Long balantflowCiId, String balantflowCiName, List multipleSelectTypeAttrIdList, JSONArray columnList) {
+ Map ciId2AttrListMap = new HashMap<>();
+ if (CollectionUtils.isNotEmpty(columnList)) {
+ for (int i = 0; i < columnList.size(); i++) {
+ JSONObject columnObj = columnList.getJSONObject(i);
+ if (MapUtils.isNotEmpty(columnObj)) {
+ Long ciId = columnObj.getLong("ciId");
+ Long attrId = columnObj.getLong("attrId");
+ String label = columnObj.getString("label");
+ JSONObject attrObj = new JSONObject(true);
+ String name = HanyuPinyinUtil.format(label + "_" + attrId);
+ attrObj.put("name", name);
+ attrObj.put("desc", label + "_" + attrId);
+ attrObj.put("attrId", attrId);
+ if (multipleSelectTypeAttrIdList.contains(attrId)) {
+ attrObj.put("type", "JsonArray");
+ JSONArray subset = new JSONArray();
+ JSONObject subObj = new JSONObject();
+ subObj.put("name", name);
+ subObj.put("desc", label + "_" + attrId);
+ subObj.put("type", "String");
+ subset.add(subObj);
+ attrObj.put("subset", subset);
+ } else {
+ attrObj.put("type", "String");
+ }
+ ciId2AttrListMap.computeIfAbsent(ciId, key -> new JSONArray()).add(attrObj);
+ }
+ }
+ }
+ JSONArray subset = ciId2AttrListMap.get(balantflowCiId);
+ if (CollectionUtils.isNotEmpty(subset)) {
+ String desc = "模型_" + balantflowCiId;
+ if (StringUtils.isNotBlank(balantflowCiName)) {
+ desc = balantflowCiName;
+ }
+ String name = HanyuPinyinUtil.format(desc);
+ JSONObject fieldObj = new JSONObject(true);
+ fieldObj.put("ciId", balantflowCiId);
+ fieldObj.put("name", name);
+ fieldObj.put("desc", desc);
+ fieldObj.put("type", "JsonArray");
+ fieldObj.put("subset", subset);
+ return fieldObj;
+ }
+ return null;
+ }
+
+ private void saveDictionaryList(JSONArray dictionaryList, LocalDateTime localDateTime) {
+ if (CollectionUtils.isNotEmpty(dictionaryList)) {
+ JSONObject dictionaryObj = dictionaryList.getJSONObject(0);
+ String id = dictionaryObj.getString("_id");
+ String name = dictionaryObj.getString("name");
+ Query query = new Query();
+ query.addCriteria(Criteria.where("name").is(name));
+ List collectionVoList = mongoTemplate.find(query, JSONObject.class, "_dictionary");
+ if (CollectionUtils.isNotEmpty(collectionVoList)) {
+ if (collectionVoList.size() > 1) {
+ throw new CollectionNameFieldValueRepeatException("_dictionary", name);
+ }
+ JSONObject collectionObj = collectionVoList.get(0);
+ String _id = collectionObj.getString("_id");
+ if (!Objects.equals(_id, id)) {
+ throw new CollectionNameFieldValueRepeatException("_dictionary", name);
+ }
+ collectionObj = JSONObject.parseObject(collectionObj.toJSONString());
+ if (!Objects.equals(
+ Md5Util.encryptMD5(JSONObject.toJSONString(collectionObj, SerializerFeature.MapSortField)),
+ Md5Util.encryptMD5(JSONObject.toJSONString(dictionaryObj, SerializerFeature.MapSortField))
+ )
+ ) {
+ dictionaryObj.put("lcd", localDateTime.format(dateTimeFormatter));
+ mongoTemplate.findAndReplace(query, dictionaryObj, "_dictionary");
+ }
+ } else {
+ dictionaryObj.put("fcd", localDateTime.format(dateTimeFormatter));
+ mongoTemplate.insert(dictionaryObj, "_dictionary");
+ }
+ }
+ for (int i = 0; i < dictionaryList.size(); i++) {
+ JSONObject dictionaryObj = dictionaryList.getJSONObject(i);
+ String _OBJ_CATEGORY = dictionaryObj.getString("_OBJ_CATEGORY");
+ String _OBJ_TYPE = dictionaryObj.getString("_OBJ_TYPE");
+ String neatLogicCiName = dictionaryObj.getString("neatLogicCiName");
+ if (StringUtils.isNotBlank(neatLogicCiName)) {
+ CiVo ciVo = ciMapper.getCiByName(neatLogicCiName);
+ if (ciVo != null) {
+ ObjectVo oldObjectVo = objectMapper.getObjectByCategoryAndType(_OBJ_CATEGORY, _OBJ_TYPE);
+ if (oldObjectVo == null) {
+ ObjectVo objectVo = new ObjectVo();
+ objectVo.setObjCategory(_OBJ_CATEGORY);
+ objectVo.setObjType(_OBJ_TYPE);
+ objectVo.setCiId(ciVo.getId());
+ objectMapper.insertObject(objectVo);
+ } else {
+ if (!Objects.equals(oldObjectVo.getCiId(), ciVo.getId())) {
+ oldObjectVo.setCiId(ciVo.getId());
+ objectMapper.updateObject(oldObjectVo);
+ }
+ }
+ }
+ }
+ }
+ }
+
+
+ private void savePageData(
+ JSONArray resultList,
+ Map attrId2CiIdMap,
+ JSONArray dictionaryList,
+ LocalDateTime localDateTime
+ ) {
+ for (int i = 0; i < resultList.size(); i++) {
+ JSONObject row = resultList.getJSONObject(i);
+ Long balantflowCiEntityId = row.getLong("ciEntityId");
+ JSONArray attrList = row.getJSONArray("attrList");
+ if (CollectionUtils.isNotEmpty(attrList)) {
+ saveRowData(balantflowCiEntityId, attrList, attrId2CiIdMap, dictionaryList, localDateTime);
+ }
+ }
+ }
+
+ private void saveRowData(
+ Long balantflowCiEntityId,
+ JSONArray attrList,
+ Map attrId2CiIdMap,
+ JSONArray dictionaryList,
+ LocalDateTime localDateTime
+ ) {
+ for (int i = 0; i < attrList.size(); i++) {
+ JSONObject attrObj = attrList.getJSONObject(i);
+ if (MapUtils.isNotEmpty(attrObj)) {
+ Long attrId = attrObj.getLong("attrId");
+ Long ciId = attrId2CiIdMap.get(attrId);
+ attrObj.put("ciId", ciId);
+ }
+ }
+ Map ciId2DictionaryMap = new HashMap<>();
+ for (int i = 0; i < dictionaryList.size(); i++) {
+ JSONObject dictionaryObj = dictionaryList.getJSONObject(i);
+ Long ciId = dictionaryObj.getLong("balantflowCiId");
+ if (ciId != null) {
+ ciId2DictionaryMap.put(ciId, dictionaryObj);
+ }
+ }
+// for (int i = 0; i < dictionaryList.size(); i++) {
+ JSONObject dictionaryObj = dictionaryList.getJSONObject(0);
+ String collection = dictionaryObj.getString("collection");
+ if (StringUtils.isNotBlank(collection)) {
+ JSONObject dataObj = generateData(balantflowCiEntityId, attrList, dictionaryObj, ciId2DictionaryMap, localDateTime);
+ mongoTemplate.insert(dataObj, collection);
+ }
+// }
+ }
+
+ private JSONObject generateData(
+ Long balantflowCiEntityId,
+ JSONArray attrList,
+ JSONObject dictionaryObj,
+ Map ciId2DictionaryMap,
+ LocalDateTime localDateTime
+ ) {
+ JSONObject dataObj = new JSONObject();
+ JSONArray fields = dictionaryObj.getJSONArray("fields");
+ if (CollectionUtils.isNotEmpty(fields)) {
+ for (int i = 0; i < fields.size(); i++) {
+ JSONObject fieldObj = fields.getJSONObject(i);
+ if (MapUtils.isNotEmpty(fieldObj)) {
+ Long ciId = fieldObj.getLong("ciId");
+ String name = fieldObj.getString("name");
+ JSONArray subset = fieldObj.getJSONArray("subset");
+ if (CollectionUtils.isNotEmpty(subset)) {
+ JSONObject jsonObj = new JSONObject();
+ JSONObject subDictionaryObj = ciId2DictionaryMap.get(ciId);
+ if (MapUtils.isNotEmpty(subDictionaryObj)) {
+ String _OBJ_CATEGORY = subDictionaryObj.getString("_OBJ_CATEGORY");
+ jsonObj.put("_OBJ_CATEGORY", _OBJ_CATEGORY);
+ String _OBJ_TYPE = subDictionaryObj.getString("_OBJ_TYPE");
+ jsonObj.put("_OBJ_TYPE", _OBJ_TYPE);
+ }
+ JSONArray jsonArray = new JSONArray();
+ for (int j = 0; j < subset.size(); j++) {
+ JSONObject subObj = subset.getJSONObject(j);
+ if (MapUtils.isNotEmpty(subObj)) {
+ String subName = subObj.getString("name");
+ Long attrId = subObj.getLong("attrId");
+ String type = subObj.getString("type");
+ if (Objects.equals(type, "String")) {
+ for (int k = 0; k < attrList.size(); k++) {
+ JSONObject attrObj = attrList.getJSONObject(k);
+ if (MapUtils.isNotEmpty(attrObj)) {
+ if (Objects.equals(attrId, attrObj.getLong("attrId"))) {
+ String value = attrObj.getString("value");
+ if (StringUtils.isNotBlank(value)) {
+ jsonObj.put(subName, value.trim());
+ }
+ break;
+ }
+ }
+ }
+ } else if (Objects.equals(type, "JsonArray")) {
+ String subName1 = null;
+ JSONArray subset1 = subObj.getJSONArray("subset");
+ if (CollectionUtils.isNotEmpty(subset1)) {
+ JSONObject subObj1 = subset1.getJSONObject(0);
+ if (MapUtils.isNotEmpty(subObj1)) {
+ subName1 = subObj1.getString("name");
+ }
+ }
+ List valueList = new ArrayList<>();
+ for (int k = 0; k < attrList.size(); k++) {
+ JSONObject attrObj = attrList.getJSONObject(k);
+ if (MapUtils.isNotEmpty(attrObj)) {
+ if (Objects.equals(attrId, attrObj.getLong("attrId"))) {
+ Object value = attrObj.get("value");
+ if (value != null) {
+ if (value instanceof String) {
+ String valueStr = (String) value;
+ if (valueStr.contains("
")) {
+ String[] split = valueStr.split("
");
+ for (String str : split) {
+ if (StringUtils.isNotBlank(str)) {
+ valueList.add(str.trim());
+ }
+ }
+ } else {
+ if (StringUtils.isNotBlank(valueStr)) {
+ valueList.add(valueStr.trim());
+ }
+ }
+ } else {
+ valueList.add(value.toString());
+ }
+ }
+ break;
+ }
+ }
+ }
+ if (StringUtils.isNotBlank(subName1) && CollectionUtils.isNotEmpty(valueList)) {
+ JSONArray array = new JSONArray();
+ for (String value : valueList) {
+ JSONObject json = new JSONObject();
+ json.put(subName1, value);
+ array.add(json);
+ }
+ jsonObj.put(subName, array);
+ }
+ }
+ }
+ }
+ jsonArray.add(jsonObj);
+ dataObj.put(name, jsonArray);
+ } else {
+ Long attrId = fieldObj.getLong("attrId");
+ for (int k = 0; k < attrList.size(); k++) {
+ JSONObject attrObj = attrList.getJSONObject(k);
+ if (MapUtils.isNotEmpty(attrObj)) {
+ if (Objects.equals(attrId, attrObj.getLong("attrId"))) {
+ Object value = attrObj.get("value");
+ dataObj.put(name, value);
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ String _OBJ_CATEGORY = dictionaryObj.getString("_OBJ_CATEGORY");
+ dataObj.put("_OBJ_CATEGORY", _OBJ_CATEGORY);
+ String _OBJ_TYPE = dictionaryObj.getString("_OBJ_TYPE");
+ dataObj.put("_OBJ_TYPE", _OBJ_TYPE);
+ dataObj.put("dictionaryName", dictionaryObj.getString("name"));
+ dataObj.put("insertTime", localDateTime.format(dateTimeFormatter));
+ dataObj.put("balantflowCiEntityId", balantflowCiEntityId);
+ return dataObj;
+ }
+
+ @Override
+ public String getToken() {
+ return "cmdb/ciEntity/data/mongodb/batchsync";
+ }
+}
--
Gitee