Skip to content

Commit

Permalink
fix(call-number) fix issues after resolving merge conflict.
Browse files Browse the repository at this point in the history
  • Loading branch information
SvitlanaKovalova1 committed Jan 7, 2025
1 parent c6dc1cb commit cc8990e
Show file tree
Hide file tree
Showing 14 changed files with 184 additions and 232 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ public FolioIndexOperationResponse indexInstancesById(List<ResourceEvent> resour

private Map<String, List<SearchDocumentBody>> processIndexInstanceEvents(List<ResourceEvent> resourceEvents) {
var indexEvents = extractEventsForDataMove(resourceEvents);
preProcessEvents(indexEvents);
var fetchedInstances = Optional.ofNullable(consortiumTenantExecutor.execute(
() -> resourceFetchService.fetchInstancesByIds(indexEvents)))
.orElse(Collections.emptyList()).stream()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,23 @@
package org.folio.search.service.converter.preprocessor.extractor.impl;

import static java.util.Collections.emptyList;
import static java.util.Collections.emptySet;
import static org.apache.commons.collections4.MapUtils.getMap;
import static org.apache.commons.collections4.MapUtils.getString;
import static org.folio.search.utils.CollectionUtils.subtract;
import static org.folio.search.utils.SearchConverterUtils.getNewAsMap;
import static org.folio.search.utils.SearchConverterUtils.getOldAsMap;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.extern.log4j.Log4j2;
import org.folio.search.domain.dto.ResourceEvent;
import org.folio.search.domain.dto.ResourceEventType;
import org.folio.search.domain.dto.TenantConfiguredFeature;
import org.folio.search.model.entity.CallNumberEntity;
import org.folio.search.model.entity.InstanceCallNumberEntity;
import org.folio.search.model.entity.InstanceCallNumberEntityAgg;
import org.folio.search.model.index.CallNumberResource;
import org.folio.search.model.types.ResourceType;
import org.folio.search.service.FeatureConfigService;
import org.folio.search.service.consortium.ConsortiumTenantProvider;
import org.folio.search.service.converter.preprocessor.extractor.ChildResourceExtractor;
import org.folio.search.service.reindex.jdbc.CallNumberRepository;
import org.folio.search.utils.CollectionUtils;
import org.folio.search.utils.JsonConverter;
import org.springframework.stereotype.Component;

Expand All @@ -46,67 +35,15 @@ public class CallNumberResourceExtractor extends ChildResourceExtractor {
public static final String ENUMERATION_FIELD = "enumeration";
public static final String COPY_NUMBER_FIELD = "copyNumber";

private final CallNumberRepository repository;
private final JsonConverter jsonConverter;
private final FeatureConfigService featureConfigService;
private final ConsortiumTenantProvider tenantProvider;

public CallNumberResourceExtractor(CallNumberRepository repository,
JsonConverter jsonConverter,
FeatureConfigService featureConfigService,
ConsortiumTenantProvider tenantProvider) {
FeatureConfigService featureConfigService) {
super(repository);
this.repository = repository;
this.jsonConverter = jsonConverter;
this.featureConfigService = featureConfigService;
this.tenantProvider = tenantProvider;
}

@Override
public List<ResourceEvent> prepareEvents(ResourceEvent resource) {
if (!featureConfigService.isEnabled(TenantConfiguredFeature.BROWSE_CALL_NUMBERS)) {
return Collections.emptyList();
}
var oldCallNumbers = getCallNumberResources(getOldAsMap(resource));
var newCallNumbers = getCallNumberResources(getNewAsMap(resource));

if (oldCallNumbers.equals(newCallNumbers)) {
return emptyList();
}

var tenant = resource.getTenant();
var callNumbersForCreate = subtract(newCallNumbers, oldCallNumbers);
var callNumbersForDelete = subtract(oldCallNumbers, newCallNumbers);

var idsForCreate = toIds(callNumbersForCreate);
var idsForDelete = toIds(callNumbersForDelete);

List<String> idsForFetch = new ArrayList<>();
idsForFetch.addAll(idsForCreate);
idsForFetch.addAll(idsForDelete);

var entityAggList = repository.fetchByIds(idsForFetch);
var list = getResourceEventsForDeletion(idsForDelete, entityAggList, tenant);

var list1 = entityAggList.stream()
.map(entities -> toResourceEvent(entities, tenant))
.toList();
return CollectionUtils.mergeSafelyToList(list, list1);
}

@Override
public List<ResourceEvent> prepareEventsOnSharing(ResourceEvent resource) {
return emptyList();
}

@Override
public boolean hasChildResourceChanges(ResourceEvent resource) {
if (!featureConfigService.isEnabled(TenantConfiguredFeature.BROWSE_CALL_NUMBERS)) {
return false;
}
var oldCallNumber = toCallNumberEntity(getOldAsMap(resource));
var newCallNumber = toCallNumberEntity(getNewAsMap(resource));
return !oldCallNumber.equals(newCallNumber);
}

@Override
Expand All @@ -132,6 +69,9 @@ protected List<Map<String, Object>> constructRelations(boolean shared, ResourceE

@Override
protected Map<String, Object> constructEntity(Map<String, Object> entityProperties) {
if (entityProperties == null) {
return null;
}
if (!featureConfigService.isEnabled(TenantConfiguredFeature.BROWSE_CALL_NUMBERS)) {
return Collections.emptyMap();
}
Expand All @@ -150,61 +90,6 @@ protected Set<Map<String, Object>> getChildResources(Map<String, Object> event)
return Set.of(event);
}

private Set<CallNumberEntity> getCallNumberResources(Map<String, Object> event) {
return toCallNumberEntity(event)
.map(Set::of)
.orElse(emptySet());
}

private List<String> toIds(Set<CallNumberEntity> subtract) {
return subtract.stream()
.map(CallNumberEntity::getId)
.collect(Collectors.toCollection(ArrayList::new));
}

private List<ResourceEvent> getResourceEventsForDeletion(List<String> idsForDelete,
List<InstanceCallNumberEntityAgg> entityAggList,
String tenant) {
var notFoundEntitiesForDelete = new ArrayList<>(idsForDelete);
var iterator = notFoundEntitiesForDelete.iterator();
while (iterator.hasNext()) {
var callNumber = iterator.next();
for (var agg : entityAggList) {
if (agg.id().equals(callNumber)) {
iterator.remove();
}
}
}

return notFoundEntitiesForDelete.stream()
.map(callNumberId -> toResourceDeleteEvent(callNumberId, tenant))
.toList();
}

private ResourceEvent toResourceDeleteEvent(String id, String tenant) {
return new ResourceEvent()
.id(id)
.tenant(tenant)
.resourceName(ResourceType.INSTANCE_CALL_NUMBER.getName())
.type(ResourceEventType.DELETE);
}

private ResourceEvent toResourceEvent(InstanceCallNumberEntityAgg source, String tenant) {
var id = source.id();
var resource = new CallNumberResource(id, source.fullCallNumber(), source.callNumber(),
source.callNumberPrefix(), source.callNumberSuffix(), source.callNumberTypeId(), source.volume(),
source.enumeration(), source.chronology(), source.copyNumber(), source.instances());
for (var instance : source.instances()) {
instance.setShared(tenantProvider.isCentralTenant(instance.getTenantId()));
}
return new ResourceEvent()
.id(id)
.tenant(tenant)
.resourceName(ResourceType.INSTANCE_CALL_NUMBER.getName())
.type(ResourceEventType.UPDATE)
._new(jsonConverter.convertToMap(resource));
}

private Optional<CallNumberEntity> toCallNumberEntity(Map<String, Object> entityProperties) {
var callNumberComponents = getCallNumberComponents(entityProperties);
var callNumber = getString(callNumberComponents, CALL_NUMBER_FIELD);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public class ClassificationResourceExtractor extends ChildResourceExtractor {

private final FeatureConfigService featureConfigService;

public ClassificationResourceExtractor(ClassificationRepository repository, FeatureConfigService featureConfigService) {
public ClassificationResourceExtractor(ClassificationRepository repository,
FeatureConfigService featureConfigService) {
super(repository);
this.featureConfigService = featureConfigService;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.Objects;
import lombok.extern.log4j.Log4j2;
import org.folio.search.domain.dto.ResourceEvent;
import org.folio.search.model.types.ResourceType;
import org.folio.search.service.converter.preprocessor.extractor.ChildResourceExtractor;
import org.folio.search.service.reindex.jdbc.ContributorRepository;
import org.folio.search.utils.ShaUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.Objects;
import lombok.extern.log4j.Log4j2;
import org.folio.search.domain.dto.ResourceEvent;
import org.folio.search.model.types.ResourceType;
import org.folio.search.service.converter.preprocessor.extractor.ChildResourceExtractor;
import org.folio.search.service.reindex.jdbc.SubjectRepository;
import org.folio.search.utils.ShaUtils;
Expand Down
Loading

0 comments on commit cc8990e

Please sign in to comment.