From 4db9a14075de8fbb906067b168472b8201d0c1e5 Mon Sep 17 00:00:00 2001 From: psmagin Date: Fri, 29 Nov 2024 17:54:53 +0200 Subject: [PATCH] - refactor --- .../extractor/ChildResourceExtractor.java | 2 +- .../impl/CallNumberResourceExtractor.java | 21 +++--- .../impl/ClassificationResourceExtractor.java | 5 +- .../reindex/jdbc/CallNumberRepository.java | 70 ++++++++++++------- 4 files changed, 61 insertions(+), 37 deletions(-) diff --git a/src/main/java/org/folio/search/service/converter/preprocessor/extractor/ChildResourceExtractor.java b/src/main/java/org/folio/search/service/converter/preprocessor/extractor/ChildResourceExtractor.java index 0ec101dc3..86d0dda14 100644 --- a/src/main/java/org/folio/search/service/converter/preprocessor/extractor/ChildResourceExtractor.java +++ b/src/main/java/org/folio/search/service/converter/preprocessor/extractor/ChildResourceExtractor.java @@ -71,7 +71,7 @@ private List> extractEntities(ResourceEvent event) { var entities = getChildResources(getNewAsMap(event)); return entities.stream() .map(this::constructEntity) - .filter(Objects::nonNull) + .filter(obj -> Objects.nonNull(obj) && !obj.isEmpty()) .toList(); } diff --git a/src/main/java/org/folio/search/service/converter/preprocessor/extractor/impl/CallNumberResourceExtractor.java b/src/main/java/org/folio/search/service/converter/preprocessor/extractor/impl/CallNumberResourceExtractor.java index 7847be001..3441d85af 100644 --- a/src/main/java/org/folio/search/service/converter/preprocessor/extractor/impl/CallNumberResourceExtractor.java +++ b/src/main/java/org/folio/search/service/converter/preprocessor/extractor/impl/CallNumberResourceExtractor.java @@ -75,12 +75,13 @@ public ResourceType resourceType() { @Override protected List> constructRelations(boolean shared, ResourceEvent event, List> entities) { + var resourceMap = getNewAsMap(event); return entities.stream() .map(entity -> InstanceCallNumberEntity.builder() .callNumberId(getString(entity, "id")) - .itemId(getString(getNewAsMap(event), "id")) - .instanceId(getString(getNewAsMap(event), "instanceId")) - .locationId(getString(getNewAsMap(event), "effectiveLocationId")) + .itemId(getString(resourceMap, "id")) + .instanceId(getString(resourceMap, "instanceId")) + .locationId(getString(resourceMap, "effectiveLocationId")) .tenantId(event.getTenant()) .build()) .map(jsonConverter::convertToMap) @@ -90,11 +91,9 @@ protected List> constructRelations(boolean shared, ResourceE @Override protected Map constructEntity(Map entityProperties) { if (!featureConfigService.isEnabled(TenantConfiguredFeature.BROWSE_CALL_NUMBERS)) { - return null; + return Collections.emptyMap(); } - var callNumberComponents = - (Map) getMap(entityProperties, EFFECTIVE_CALL_NUMBER_COMPONENTS_FIELD, - Collections.emptyMap()); + var callNumberComponents = getCallNumberComponents(entityProperties); var callNumber = getString(callNumberComponents, CALL_NUMBER_FIELD); if (callNumber != null) { var callNumberEntity = CallNumberEntity.builder() @@ -109,7 +108,13 @@ protected Map constructEntity(Map entityProperti .build(); return jsonConverter.convertToMap(callNumberEntity); } - return null; + return Collections.emptyMap(); + } + + @SuppressWarnings("unchecked") + private Map getCallNumberComponents(Map entityProperties) { + return (Map) getMap(entityProperties, EFFECTIVE_CALL_NUMBER_COMPONENTS_FIELD, + Collections.emptyMap()); } @Override diff --git a/src/main/java/org/folio/search/service/converter/preprocessor/extractor/impl/ClassificationResourceExtractor.java b/src/main/java/org/folio/search/service/converter/preprocessor/extractor/impl/ClassificationResourceExtractor.java index 873b164d9..0957f36c0 100644 --- a/src/main/java/org/folio/search/service/converter/preprocessor/extractor/impl/ClassificationResourceExtractor.java +++ b/src/main/java/org/folio/search/service/converter/preprocessor/extractor/impl/ClassificationResourceExtractor.java @@ -13,6 +13,7 @@ import static org.folio.search.utils.SearchUtils.prepareForExpectedFormat; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -137,11 +138,11 @@ protected List> constructRelations(boolean shared, ResourceE @Override protected Map constructEntity(Map entityProperties) { if (!featureConfigService.isEnabled(TenantConfiguredFeature.BROWSE_CLASSIFICATIONS)) { - return null; + return Collections.emptyMap(); } var classificationNumber = prepareForExpectedFormat(entityProperties.get(CLASSIFICATION_NUMBER_FIELD), 50); if (classificationNumber.isEmpty()) { - return null; + return Collections.emptyMap(); } var classificationTypeId = entityProperties.get(CLASSIFICATION_TYPE_FIELD); diff --git a/src/main/java/org/folio/search/service/reindex/jdbc/CallNumberRepository.java b/src/main/java/org/folio/search/service/reindex/jdbc/CallNumberRepository.java index 3f439845d..1f24e315a 100644 --- a/src/main/java/org/folio/search/service/reindex/jdbc/CallNumberRepository.java +++ b/src/main/java/org/folio/search/service/reindex/jdbc/CallNumberRepository.java @@ -15,6 +15,7 @@ import org.folio.search.model.types.ReindexEntityType; import org.folio.search.utils.JsonConverter; import org.folio.spring.FolioExecutionContext; +import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.stereotype.Repository; @@ -94,36 +95,53 @@ protected RowMapper> rowToMapMapper() { return null; } - private void saveRelationshipEntities(ChildResourceEntityBatch entityBatch) { - var instanceCallNumberTable = getFullTableName(context, INSTANCE_CALL_NUMBER_TABLE); - var instanceCallNumberSql = INSERT_RELATIONS_SQL.formatted(instanceCallNumberTable); - - jdbcTemplate.batchUpdate(instanceCallNumberSql, entityBatch.relationshipEntities(), BATCH_OPERATION_SIZE, - (statement, entity) -> { - statement.setString(1, getCallNumberId(entity)); - statement.setString(2, getItemId(entity)); - statement.setString(3, getInstanceId(entity)); - statement.setString(4, getTenantId(entity)); - statement.setString(5, getLocationId(entity)); - }); - } - private void saveResourceEntities(ChildResourceEntityBatch entityBatch) { var callNumberTable = getFullTableName(context, entityTable()); var callNumberSql = INSERT_ENTITIES_SQL.formatted(callNumberTable); - jdbcTemplate.batchUpdate(callNumberSql, entityBatch.resourceEntities(), BATCH_OPERATION_SIZE, - (statement, entity) -> { - statement.setString(1, getId(entity)); - statement.setString(2, getCallNumber(entity)); - statement.setString(3, getPrefix(entity)); - statement.setString(4, getSuffix(entity)); - statement.setString(5, getTypeId(entity)); - statement.setString(6, getVolume(entity)); - statement.setString(7, getEnumeration(entity)); - statement.setString(8, getChronology(entity)); - statement.setString(9, getCopyNumber(entity)); - }); + try { + jdbcTemplate.batchUpdate(callNumberSql, entityBatch.resourceEntities(), BATCH_OPERATION_SIZE, + (statement, entity) -> { + statement.setString(1, getId(entity)); + statement.setString(2, getCallNumber(entity)); + statement.setString(3, getPrefix(entity)); + statement.setString(4, getSuffix(entity)); + statement.setString(5, getTypeId(entity)); + statement.setString(6, getVolume(entity)); + statement.setString(7, getEnumeration(entity)); + statement.setString(8, getChronology(entity)); + statement.setString(9, getCopyNumber(entity)); + }); + } catch (DataAccessException e) { + log.warn("saveAll::Failed to save entities batch. Starting processing one-by-one", e); + for (var entity : entityBatch.resourceEntities()) { + jdbcTemplate.update(callNumberSql, + getId(entity), getCallNumber(entity), getPrefix(entity), getSuffix(entity), getTypeId(entity), + getVolume(entity), getEnumeration(entity), getChronology(entity), getCopyNumber(entity)); + } + } + } + + private void saveRelationshipEntities(ChildResourceEntityBatch entityBatch) { + var instanceCallNumberTable = getFullTableName(context, INSTANCE_CALL_NUMBER_TABLE); + var instanceCallNumberSql = INSERT_RELATIONS_SQL.formatted(instanceCallNumberTable); + + try { + jdbcTemplate.batchUpdate(instanceCallNumberSql, entityBatch.relationshipEntities(), BATCH_OPERATION_SIZE, + (statement, entity) -> { + statement.setString(1, getCallNumberId(entity)); + statement.setString(2, getItemId(entity)); + statement.setString(3, getInstanceId(entity)); + statement.setString(4, getTenantId(entity)); + statement.setString(5, getLocationId(entity)); + }); + } catch (DataAccessException e) { + log.warn("saveAll::Failed to save relations batch. Starting processing one-by-one", e); + for (var entityRelation : entityBatch.relationshipEntities()) { + jdbcTemplate.update(instanceCallNumberSql, getCallNumberId(entityRelation), getItemId(entityRelation), + getInstanceId(entityRelation), getTenantId(entityRelation), getLocationId(entityRelation)); + } + } } private String getCallNumber(Map callNumberComponents) {