Skip to content

Commit

Permalink
- refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
psmagin committed Nov 29, 2024
1 parent 1c77113 commit 4db9a14
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private List<Map<String, Object>> 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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,13 @@ public ResourceType resourceType() {
@Override
protected List<Map<String, Object>> constructRelations(boolean shared, ResourceEvent event,
List<Map<String, Object>> 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)
Expand All @@ -90,11 +91,9 @@ protected List<Map<String, Object>> constructRelations(boolean shared, ResourceE
@Override
protected Map<String, Object> constructEntity(Map<String, Object> entityProperties) {
if (!featureConfigService.isEnabled(TenantConfiguredFeature.BROWSE_CALL_NUMBERS)) {
return null;
return Collections.emptyMap();
}
var callNumberComponents =
(Map<String, Object>) getMap(entityProperties, EFFECTIVE_CALL_NUMBER_COMPONENTS_FIELD,
Collections.<String, Object>emptyMap());
var callNumberComponents = getCallNumberComponents(entityProperties);
var callNumber = getString(callNumberComponents, CALL_NUMBER_FIELD);
if (callNumber != null) {
var callNumberEntity = CallNumberEntity.builder()
Expand All @@ -109,7 +108,13 @@ protected Map<String, Object> constructEntity(Map<String, Object> entityProperti
.build();
return jsonConverter.convertToMap(callNumberEntity);
}
return null;
return Collections.emptyMap();
}

@SuppressWarnings("unchecked")
private Map<String, Object> getCallNumberComponents(Map<String, Object> entityProperties) {
return (Map<String, Object>) getMap(entityProperties, EFFECTIVE_CALL_NUMBER_COMPONENTS_FIELD,
Collections.<String, Object>emptyMap());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -137,11 +138,11 @@ protected List<Map<String, Object>> constructRelations(boolean shared, ResourceE
@Override
protected Map<String, Object> constructEntity(Map<String, Object> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -94,36 +95,53 @@ protected RowMapper<Map<String, Object>> 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<String, Object> callNumberComponents) {
Expand Down

0 comments on commit 4db9a14

Please sign in to comment.