Skip to content

Commit

Permalink
MODSOURMAN-1063: fix sonar issues
Browse files Browse the repository at this point in the history
  • Loading branch information
yaroslav-epam committed Jan 18, 2024
1 parent 2cafbf3 commit 230d325
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -177,7 +178,7 @@ public Future<List<RowSet<Row>>> saveBatch(List<JournalRecord> journalRecords, S
LOGGER.info("saveBatch:: Trying to save list of JournalRecord entities to the {} table", JOURNAL_RECORDS_TABLE);
Promise<List<RowSet<Row>>> promise = Promise.promise();
try {
List<Tuple> tupleList = journalRecords.stream().map(this::prepareInsertQueryParameters).collect(Collectors.toList());
List<Tuple> 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);
Expand Down Expand Up @@ -546,45 +547,39 @@ private EntityProcessingSummary mapToEntityProcessingSummary(Row row, String tot

private static RecordProcessingLogDtoCollection processMultipleHoldingsAndItemsIfNeeded(RecordProcessingLogDtoCollection recordProcessingLogDto) {
List<RecordProcessingLogDto> entries = recordProcessingLogDto.getEntries();
boolean needToMerge = ifNeedToMerge(entries);

if (needToMerge) {
Map<String, List<ProcessedHoldingsInfo>> relatedHoldingsInfoBySourceRecordId =
entries.stream()
.collect(Collectors.groupingBy(
RecordProcessingLogDto::getSourceRecordId,
Collectors.mapping(RecordProcessingLogDto::getRelatedHoldingsInfo,
Collectors.flatMapping(List::stream, Collectors.toList())
)));

Map<String, List<ProcessedItemInfo>> relatedItemInfoBySourceId =
entries.stream()
.collect(Collectors.groupingBy(
RecordProcessingLogDto::getSourceRecordId,
Collectors.mapping(RecordProcessingLogDto::getRelatedItemInfo,
Collectors.flatMapping(List::stream, Collectors.toList())
)));

List<RecordProcessingLogDto> mergedEntries = new ArrayList<>();
for (String sourceRecordId : relatedHoldingsInfoBySourceRecordId.keySet()) {
List<ProcessedHoldingsInfo> relatedHoldingsInfos = relatedHoldingsInfoBySourceRecordId.get(sourceRecordId);
if (!ifNeedToMerge(entries)) {
return recordProcessingLogDto;
}
Map<String, List<ProcessedHoldingsInfo>> relatedHoldingsInfoBySourceRecordId =
entries.stream()
.collect(Collectors.groupingBy(
RecordProcessingLogDto::getSourceRecordId,
Collectors.mapping(RecordProcessingLogDto::getRelatedHoldingsInfo,
Collectors.flatMapping(List::stream, toList())
)));

Map<String, List<ProcessedItemInfo>> relatedItemInfoBySourceId =
entries.stream()
.collect(Collectors.groupingBy(
RecordProcessingLogDto::getSourceRecordId,
Collectors.mapping(RecordProcessingLogDto::getRelatedItemInfo,
Collectors.flatMapping(List::stream, toList())
)));

List<RecordProcessingLogDto> mergedEntries = relatedHoldingsInfoBySourceRecordId.entrySet()
.stream().map(e -> {
String sourceRecordId = e.getKey();
List<ProcessedItemInfo> relatedItemInfos = relatedItemInfoBySourceId.get(sourceRecordId);

Optional<RecordProcessingLogDto> 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<RecordProcessingLogDto> entries) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 230d325

Please sign in to comment.