From 230d325042564ff67cf0173699237e127c72981b Mon Sep 17 00:00:00 2001 From: Yaroslav_Kiriak Date: Thu, 18 Jan 2024 13:38:46 +0200 Subject: [PATCH] MODSOURMAN-1063: fix sonar issues --- .../org/folio/dao/JournalRecordDaoImpl.java | 69 +++++++++---------- .../folio/services/journal/JournalUtil.java | 4 +- 2 files changed, 33 insertions(+), 40 deletions(-) diff --git a/mod-source-record-manager-server/src/main/java/org/folio/dao/JournalRecordDaoImpl.java b/mod-source-record-manager-server/src/main/java/org/folio/dao/JournalRecordDaoImpl.java index 4f9475c84..929df5715 100644 --- a/mod-source-record-manager-server/src/main/java/org/folio/dao/JournalRecordDaoImpl.java +++ b/mod-source-record-manager-server/src/main/java/org/folio/dao/JournalRecordDaoImpl.java @@ -45,6 +45,7 @@ import java.util.stream.Collectors; import static java.lang.String.format; +import static java.util.stream.Collectors.toList; import static org.apache.commons.lang3.StringUtils.EMPTY; import static org.apache.commons.lang3.StringUtils.isEmpty; import static org.folio.dao.util.JournalRecordsColumns.ACTION_DATE; @@ -177,7 +178,7 @@ public Future>> saveBatch(List journalRecords, S LOGGER.info("saveBatch:: Trying to save list of JournalRecord entities to the {} table", JOURNAL_RECORDS_TABLE); Promise>> promise = Promise.promise(); try { - List tupleList = journalRecords.stream().map(this::prepareInsertQueryParameters).collect(Collectors.toList()); + List tupleList = journalRecords.stream().map(this::prepareInsertQueryParameters).collect(toList()); String query = format(INSERT_SQL, convertToPsqlStandard(tenantId), JOURNAL_RECORDS_TABLE); LOGGER.trace("saveBatch:: JournalRecordDaoImpl::saveBatch query = {}; tuples = {}", query, tupleList); pgClientFactory.createInstance(tenantId).execute(query, tupleList, promise); @@ -546,45 +547,39 @@ private EntityProcessingSummary mapToEntityProcessingSummary(Row row, String tot private static RecordProcessingLogDtoCollection processMultipleHoldingsAndItemsIfNeeded(RecordProcessingLogDtoCollection recordProcessingLogDto) { List entries = recordProcessingLogDto.getEntries(); - boolean needToMerge = ifNeedToMerge(entries); - - if (needToMerge) { - Map> relatedHoldingsInfoBySourceRecordId = - entries.stream() - .collect(Collectors.groupingBy( - RecordProcessingLogDto::getSourceRecordId, - Collectors.mapping(RecordProcessingLogDto::getRelatedHoldingsInfo, - Collectors.flatMapping(List::stream, Collectors.toList()) - ))); - - Map> relatedItemInfoBySourceId = - entries.stream() - .collect(Collectors.groupingBy( - RecordProcessingLogDto::getSourceRecordId, - Collectors.mapping(RecordProcessingLogDto::getRelatedItemInfo, - Collectors.flatMapping(List::stream, Collectors.toList()) - ))); - - List mergedEntries = new ArrayList<>(); - for (String sourceRecordId : relatedHoldingsInfoBySourceRecordId.keySet()) { - List relatedHoldingsInfos = relatedHoldingsInfoBySourceRecordId.get(sourceRecordId); + if (!ifNeedToMerge(entries)) { + return recordProcessingLogDto; + } + Map> relatedHoldingsInfoBySourceRecordId = + entries.stream() + .collect(Collectors.groupingBy( + RecordProcessingLogDto::getSourceRecordId, + Collectors.mapping(RecordProcessingLogDto::getRelatedHoldingsInfo, + Collectors.flatMapping(List::stream, toList()) + ))); + + Map> relatedItemInfoBySourceId = + entries.stream() + .collect(Collectors.groupingBy( + RecordProcessingLogDto::getSourceRecordId, + Collectors.mapping(RecordProcessingLogDto::getRelatedItemInfo, + Collectors.flatMapping(List::stream, toList()) + ))); + + List mergedEntries = relatedHoldingsInfoBySourceRecordId.entrySet() + .stream().map(e -> { + String sourceRecordId = e.getKey(); List relatedItemInfos = relatedItemInfoBySourceId.get(sourceRecordId); - Optional optionalRecord = entries.stream() - .filter(record -> record.getSourceRecordId().equals(sourceRecordId)).findFirst(); - RecordProcessingLogDto firstRecordWithCurrentSourceId = new RecordProcessingLogDto(); - if (optionalRecord.isPresent()) { - firstRecordWithCurrentSourceId = optionalRecord.get(); - } - RecordProcessingLogDto newRecord = firstRecordWithCurrentSourceId - .withRelatedHoldingsInfo(relatedHoldingsInfos.stream().distinct().toList()) + RecordProcessingLogDto firstRecordWithCurrentSourceId = entries.stream() + .filter(record -> record.getSourceRecordId().equals(sourceRecordId)) + .findFirst().orElseGet(RecordProcessingLogDto::new); + + return firstRecordWithCurrentSourceId + .withRelatedHoldingsInfo(e.getValue().stream().distinct().toList()) .withRelatedItemInfo(relatedItemInfos.stream().distinct().toList()); - mergedEntries.add(newRecord); - } - return recordProcessingLogDto.withEntries(mergedEntries); - } else { - return recordProcessingLogDto; - } + }).collect(toList()); + return recordProcessingLogDto.withEntries(mergedEntries); } private static boolean ifNeedToMerge(List entries) { diff --git a/mod-source-record-manager-server/src/main/java/org/folio/services/journal/JournalUtil.java b/mod-source-record-manager-server/src/main/java/org/folio/services/journal/JournalUtil.java index 4174bb071..cbde89685 100644 --- a/mod-source-record-manager-server/src/main/java/org/folio/services/journal/JournalUtil.java +++ b/mod-source-record-manager-server/src/main/java/org/folio/services/journal/JournalUtil.java @@ -27,11 +27,9 @@ import static org.apache.commons.lang3.StringUtils.isEmpty; import static org.folio.rest.jaxrs.model.DataImportEventTypes.DI_ERROR; import static org.folio.rest.jaxrs.model.DataImportEventTypes.DI_INVENTORY_INSTANCE_CREATED; -import static org.folio.rest.jaxrs.model.DataImportEventTypes.*; +import static org.folio.rest.jaxrs.model.DataImportEventTypes.DI_LOG_SRS_MARC_BIB_RECORD_UPDATED; import static org.folio.rest.jaxrs.model.DataImportEventTypes.DI_SRS_MARC_BIB_RECORD_UPDATED; import static org.folio.rest.jaxrs.model.JournalRecord.EntityType.AUTHORITY; -import static org.folio.rest.jaxrs.model.DataImportEventTypes.DI_INVENTORY_INSTANCE_CREATED; -import static org.folio.rest.jaxrs.model.DataImportEventTypes.DI_LOG_SRS_MARC_BIB_RECORD_UPDATED; import static org.folio.rest.jaxrs.model.JournalRecord.EntityType.HOLDINGS; import static org.folio.rest.jaxrs.model.JournalRecord.EntityType.INSTANCE; import static org.folio.rest.jaxrs.model.JournalRecord.EntityType.ITEM;