Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MODEXPW-452: Retrieve instance records based on provided list of ISBN/ISSN #503

Merged
merged 14 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion descriptors/ModuleDescriptor-template.json
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,12 @@
"usergroups.collection.get",
"usergroups.item.get",
"users.collection.get",
"users.item.get"
"users.item.get",
"inventory-storage.instance-statuses.item.get",
"inventory-storage.modes-of-issuance.item.get",
"inventory-storage.instance-types.item.get",
"inventory-storage.nature-of-content-terms.item.get",
"inventory-storage.instance-formats.item.get"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.io.FilenameUtils;
import org.folio.dew.domain.dto.*;
import org.folio.dew.domain.dto.InstanceFormat;
import org.folio.dew.domain.dto.ErrorServiceArgs;
import org.folio.dew.domain.dto.FormatOfInstance;
import org.folio.dew.domain.dto.Instance;
import org.folio.dew.domain.dto.InstanceContributorsInner;
import org.folio.dew.domain.dto.InstanceIdentifiersInner;
import org.folio.dew.domain.dto.InstanceSeriesInner;
import org.folio.dew.service.InstanceReferenceService;
import org.folio.dew.service.SpecialCharacterEscaper;
import org.jetbrains.annotations.NotNull;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemProcessor;
Expand All @@ -24,8 +25,9 @@
import java.util.stream.Collectors;

import static org.apache.commons.lang3.StringUtils.EMPTY;
import static org.folio.dew.utils.Constants.ARRAY_DELIMITER_SPACED;
import static org.folio.dew.utils.Constants.ITEM_DELIMITER;
import static org.folio.dew.domain.dto.eholdings.Identifier.TypeEnum.ISBN;
import static org.folio.dew.domain.dto.eholdings.Identifier.TypeEnum.ISSN;
import static org.folio.dew.utils.Constants.*;

@Component
@StepScope
Expand All @@ -34,7 +36,6 @@
public class BulkEditInstanceProcessor implements ItemProcessor<Instance, InstanceFormat> {

private final InstanceReferenceService instanceReferenceService;
private final SpecialCharacterEscaper escaper;


@Value("#{jobParameters['identifierType']}")
Expand All @@ -58,19 +59,19 @@ public InstanceFormat process(@NotNull Instance instance) {
.catalogedDate(instance.getCatalogedDate())
.statusId(instanceReferenceService.getInstanceStatusNameById(instance.getStatusId(), errorServiceArgs))
.modeOfIssuanceId(instanceReferenceService.getModeOfIssuanceNameById(instance.getModeOfIssuanceId(), errorServiceArgs))
.administrativeNotes(isEmpty(instance.getAdministrativeNotes()) ? EMPTY : String.join(ITEM_DELIMITER, escaper.escape(instance.getAdministrativeNotes())))
.administrativeNotes(isEmpty(instance.getAdministrativeNotes()) ? EMPTY : String.join(ITEM_DELIMITER, instance.getAdministrativeNotes()))
.title(instance.getTitle())
.indexTitle(instance.getIndexTitle())
.series(fetchSeries(instance.getSeries()))
.contributors(fetchContributorNames(instance.getContributors()))
.editions(isEmpty(instance.getEditions()) ? EMPTY : String.join(ITEM_DELIMITER, escaper.escape(new ArrayList<>(instance.getEditions()))))
.physicalDescriptions(isEmpty(instance.getPhysicalDescriptions()) ? EMPTY : String.join(ITEM_DELIMITER, escaper.escape(instance.getPhysicalDescriptions())))
.editions(isEmpty(instance.getEditions()) ? EMPTY : String.join(ITEM_DELIMITER, new ArrayList<>(instance.getEditions())))
.physicalDescriptions(isEmpty(instance.getPhysicalDescriptions()) ? EMPTY : String.join(ITEM_DELIMITER, instance.getPhysicalDescriptions()))
.instanceTypeId(instanceReferenceService.getInstanceTypeNameById(instance.getInstanceTypeId(), errorServiceArgs))
.natureOfContentTermIds(fetchNatureOfContentTerms(instance.getNatureOfContentTermIds(), errorServiceArgs))
.instanceFormatIds(fetchInstanceFormats(instance.getInstanceFormats(), errorServiceArgs))
.languages(isEmpty(instance.getLanguages()) ? EMPTY : String.join(ITEM_DELIMITER, escaper.escape(instance.getLanguages())))
.publicationFrequency(isEmpty(instance.getPublicationFrequency()) ? EMPTY : String.join(ITEM_DELIMITER, escaper.escape(new ArrayList<>(instance.getPublicationFrequency()))))
.publicationRange(isEmpty(instance.getPublicationRange()) ? EMPTY : String.join(ITEM_DELIMITER, escaper.escape(new ArrayList<>(instance.getPublicationRange()))))
.languages(isEmpty(instance.getLanguages()) ? EMPTY : String.join(ITEM_DELIMITER, instance.getLanguages()))
.publicationFrequency(isEmpty(instance.getPublicationFrequency()) ? EMPTY : String.join(ITEM_DELIMITER, new ArrayList<>(instance.getPublicationFrequency())))
.publicationRange(isEmpty(instance.getPublicationRange()) ? EMPTY : String.join(ITEM_DELIMITER, new ArrayList<>(instance.getPublicationRange())))
.build();


Expand All @@ -81,37 +82,46 @@ private String fetchInstanceFormats(List<FormatOfInstance> instanceFormats, Erro
return isEmpty(instanceFormats) ? EMPTY :
instanceFormats.stream()
.map(iFormat -> instanceReferenceService.getFormatOfInstanceNameById(iFormat.getId(), errorServiceArgs))
.map(iFormatName -> String.join(ITEM_DELIMITER, escaper.escape(iFormatName)))
.collect(Collectors.joining(ITEM_DELIMITER));
.collect(Collectors.joining(ITEM_DELIMITER_SPACED));
}

private String fetchNatureOfContentTerms(Set<String> natureOfContentTermIds, ErrorServiceArgs errorServiceArgs) {
return isEmpty(natureOfContentTermIds) ? EMPTY :
natureOfContentTermIds.stream()
.map(natId -> instanceReferenceService.getNatureOfContentTermNameById(natId, errorServiceArgs))
.map(natName -> String.join(ITEM_DELIMITER, escaper.escape(natName)))
.collect(Collectors.joining(ITEM_DELIMITER));
.collect(Collectors.joining(ITEM_DELIMITER_SPACED));
}

private String fetchContributorNames(List<InstanceContributorsInner> contributors) {
return isEmpty(contributors) ? EMPTY :
contributors.stream()
.map(c -> String.join(ARRAY_DELIMITER_SPACED, escaper.escape(c.getName())))
.map(InstanceContributorsInner::getName)
.collect(Collectors.joining(ARRAY_DELIMITER_SPACED));
}

private String fetchSeries(Set<InstanceSeriesInner> series) {
return isEmpty(series) ? EMPTY :
series.stream()
.map(instanceSeriesInner -> String.join(ITEM_DELIMITER, escaper.escape(instanceSeriesInner.getValue())))
.collect(Collectors.joining(ITEM_DELIMITER));
.map(InstanceSeriesInner::getValue)
.collect(Collectors.joining(ITEM_DELIMITER_SPACED));
}


private String getIdentifier(Instance instance, String identifierType) {
try {
return switch (org.folio.dew.domain.dto.IdentifierType.fromValue(identifierType)) {
case HRID -> instance.getHrid();
case ISSN -> instance.getIdentifiers().stream()
.filter(identifier -> instanceReferenceService.getTypeOfIdentifiersIdByName(ISSN.getValue()).equals(identifier.getIdentifierTypeId()))
.findFirst()
.map(InstanceIdentifiersInner::getValue)
.orElse(instance.getId());
case ISBN -> instance.getIdentifiers().stream()
.filter(identifier -> instanceReferenceService.getTypeOfIdentifiersIdByName(ISBN.getValue()).equals(identifier.getIdentifierTypeId()))
.findFirst()
.map(InstanceIdentifiersInner::getValue)
.orElse(instance.getId());

default -> instance.getId();
};
} catch (IllegalArgumentException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.folio.dew.domain.dto.InstanceCollection;
import org.folio.dew.domain.dto.ItemIdentifier;
import org.folio.dew.error.BulkEditException;
import org.folio.dew.service.InstanceReferenceService;
import org.folio.dew.utils.ExceptionHelper;
import org.jetbrains.annotations.NotNull;
import org.springframework.batch.core.configuration.annotation.StepScope;
Expand All @@ -28,6 +29,7 @@
@Log4j2
public class InstanceFetcher implements ItemProcessor<ItemIdentifier, InstanceCollection> {
private final InventoryInstancesClient inventoryInstancesClient;
private final InstanceReferenceService instanceReferenceService;

@Value("#{jobParameters['identifierType']}")
private String identifierType;
Expand All @@ -42,6 +44,11 @@ public InstanceCollection process(@NotNull ItemIdentifier itemIdentifier) throws
identifiersToCheckDuplication.add(itemIdentifier);
var limit = HOLDINGS_RECORD_ID == IdentifierType.fromValue(identifierType) ? Integer.MAX_VALUE : 1;
var idType = resolveIdentifier(identifierType);
if ("ISSN".equals(idType) || "ISBN".equals(idType)){
String typeOfIdentifiersId = instanceReferenceService.getTypeOfIdentifiersIdByName(idType);

return inventoryInstancesClient.getInstanceByQuery(String.format("(identifiers=/@identifierTypeId=%s %s)", typeOfIdentifiersId, itemIdentifier.getItemId()));
}
try {
return inventoryInstancesClient.getInstanceByQuery(String.format(getMatchPattern(identifierType), idType, itemIdentifier.getItemId()), limit);
} catch (DecodeException e) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/folio/dew/client/IdentifierTypeClient.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package org.folio.dew.client;

import org.folio.dew.domain.dto.IdentifierTypeReferenceCollection;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.web.bind.annotation.RequestParam;


@FeignClient(name = "identifier-types")
public interface IdentifierTypeClient {
@GetMapping(value = "/{identifierTypeId}", produces = MediaType.APPLICATION_JSON_VALUE) JsonNode getIdentifierType(@PathVariable String identifierTypeId);

@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
IdentifierTypeReferenceCollection getByQuery(@RequestParam String query);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@

import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.folio.dew.client.*;
import org.apache.commons.lang3.StringUtils;
import org.folio.dew.client.InstanceStatusesClient;
import org.folio.dew.client.InstanceModeOfIssuanceClient;
import org.folio.dew.client.InstanceTypesClient;
import org.folio.dew.client.NatureOfContentTermsClient;
import org.folio.dew.client.InstanceFormatsClient;
import org.folio.dew.client.IdentifierTypeClient;
import org.folio.dew.domain.dto.ErrorServiceArgs;
import org.folio.dew.domain.dto.IdentifierTypeReferenceCollection;
import org.folio.dew.error.BulkEditException;
import org.folio.dew.error.NotFoundException;
import org.springframework.cache.annotation.Cacheable;
Expand All @@ -18,12 +25,15 @@
@RequiredArgsConstructor
public class InstanceReferenceService {

private static final String QUERY_PATTERN_NAME = "name==\"%s\"";

private final BulkEditProcessingErrorsService errorsService;
private final InstanceStatusesClient instanceStatusesClient;
private final InstanceModeOfIssuanceClient instanceModeOfIssuanceClient;
private final InstanceTypesClient instanceTypesClient;
private final NatureOfContentTermsClient natureOfContentTermsClient;
private final InstanceFormatsClient instanceFormatsClient;
private final IdentifierTypeClient identifierTypeClient;


@Cacheable(cacheNames = "instanceStatusNames")
Expand Down Expand Up @@ -72,4 +82,17 @@ public String getFormatOfInstanceNameById(String instanceFormatId, ErrorServiceA
}
}

@Cacheable(cacheNames = "typeOfIdentifiersIds")
public String getTypeOfIdentifiersIdByName(String identifierName) {
if (StringUtils.isEmpty(identifierName)) {
return null;
}
IdentifierTypeReferenceCollection typeOfIdentifiers = identifierTypeClient.getByQuery(String.format(QUERY_PATTERN_NAME, identifierName));
if (typeOfIdentifiers.getIdentifierTypes().isEmpty()) {
log.error("Identifier type not found by identifierName={}", identifierName);
return identifierName;
}
return typeOfIdentifiers.getIdentifierTypes().get(0).getId();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import static org.folio.dew.domain.dto.IdentifierType.HRID;
import static org.folio.dew.domain.dto.IdentifierType.ID;
import static org.folio.dew.domain.dto.IdentifierType.USER_NAME;
import static org.folio.dew.domain.dto.IdentifierType.ISSN;
import static org.folio.dew.domain.dto.IdentifierType.ISBN;
import static org.folio.dew.utils.Constants.DATE_TIME_PATTERN;
import static org.folio.dew.utils.Constants.DATE_WITHOUT_TIME_PATTERN;

Expand Down Expand Up @@ -53,6 +55,8 @@ public class BulkEditProcessorHelper {
identifiersMap.put(HOLDINGS_RECORD_ID, "holdingsRecordId");
identifiersMap.put(USER_NAME, "username");
identifiersMap.put(EXTERNAL_SYSTEM_ID, "externalSystemId");
identifiersMap.put(ISSN, "ISSN");
identifiersMap.put(ISBN, "ISBN");
}

public static String dateToString(Date date) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/folio/dew/utils/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class Constants {
public static final String CSV_EXTENSION = ".csv";

public static final String ARRAY_DELIMITER = ";";
public static final String ARRAY_DELIMITER_SPACED = " ; ";
public static final String ARRAY_DELIMITER_SPACED = "; ";
public static final String ELECTRONIC_RELATIONSHIP_NAME_ID_DELIMITER = ARRAY_DELIMITER;
public static final String ITEM_DELIMITER = "|";
public static final String ITEM_DELIMITER_SPACED = " | ";
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/swagger.api/bulk-edit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,10 @@ components:
$ref: '../../../../folio-export-common/schemas/inventory/instanceType.json#/InstanceType'
natureOfContentTerm:
$ref: '../../../../folio-export-common/schemas/inventory/natureOfContentTerm.json#/NatureOfContentTerm'
identifierTypeReference:
$ref: '../../../../folio-export-common/schemas/inventory/identifierTypeReference.json#/IdentifierTypeReference'
IdentifierTypeReferenceCollection:
$ref: '../../../../folio-export-common/schemas/inventory/identifierTypeReferenceCollection.json#/IdentifierTypeReferenceCollection'
examples:
errors:
value:
Expand Down
22 changes: 20 additions & 2 deletions src/test/java/org/folio/dew/BulkEditTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ class BulkEditTest extends BaseBatchTest {
private static final String BARCODES_FOR_PROGRESS_CSV = "src/test/resources/upload/barcodes_for_progress.csv";
private static final String ITEM_BARCODES_CSV = "src/test/resources/upload/item_barcodes.csv";
private static final String INSTANCE_HRIDS_CSV = "src/test/resources/upload/instance_hrids.csv";
private static final String INSTANCE_ISSN_ISBN_CSV = "src/test/resources/upload/instance_ISSN_ISBN.csv";
private static final String ITEM_BARCODES_DOUBLE_QOUTES_CSV = "src/test/resources/upload/item_barcodes_double_qoutes.csv";
private static final String ITEM_HOLDINGS_CSV = "src/test/resources/upload/item_holdings.csv";
private static final String USER_RECORD_CSV = "src/test/resources/upload/bulk_edit_user_record.csv";
Expand All @@ -140,7 +141,9 @@ class BulkEditTest extends BaseBatchTest {
private static final String EXPECTED_BULK_EDIT_ITEM_OUTPUT = "src/test/resources/output/bulk_edit_item_identifiers_output.csv";
private static final String EXPECTED_BULK_EDIT_ITEM_JSON_OUTPUT = "src/test/resources/output/bulk_edit_item_identifiers_json_output.json";
private static final String EXPECTED_BULK_EDIT_INSTANCE_OUTPUT = "src/test/resources/output/bulk_edit_instance_identifiers_output.csv";
private static final String EXPECTED_BULK_EDIT_INSTANCE_BY_ISSN_ISBN_OUTPUT = "src/test/resources/output/bulk_edit_instance_by_issn_isbn_output.csv";
private static final String EXPECTED_BULK_EDIT_INSTANCE_JSON_OUTPUT = "src/test/resources/output/bulk_edit_instance_identifiers_json_output.json";
private static final String EXPECTED_BULK_EDIT_INSTANCE_BY_ISSN_ISBN_JSON_OUTPUT = "src/test/resources/output/bulk_edit_instance_by_issn_isbn_json_output.json";
private static final String EXPECTED_BULK_EDIT_ITEM_QUERY_JSON_OUTPUT = "src/test/resources/output/bulk_edit_item_query_json_output.json";

private static final String EXPECTED_BULK_EDIT_HOLDINGS_OUTPUT = "src/test/resources/output/bulk_edit_holdings_records_output.csv";
Expand Down Expand Up @@ -263,8 +266,8 @@ void uploadItemIdentifiersJobTest(IdentifierType identifierType) throws Exceptio

@ParameterizedTest
@EnumSource(value = IdentifierType.class, names = {"ID","HRID"}, mode = EnumSource.Mode.INCLUDE)
@DisplayName("Run bulk-edit (instance identifiers) successfully")
void uploadInstanceIdentifiersJobTest(IdentifierType identifierType) throws Exception {
@DisplayName("Run bulk-edit (instance identifiers ID or HRID) successfully")
void uploadInstanceIdentifiersJobTest_1(IdentifierType identifierType) throws Exception {

JobLauncherTestUtils testLauncher = createTestLauncher(bulkEditProcessInstanceIdentifiersJob);

Expand All @@ -276,6 +279,21 @@ void uploadInstanceIdentifiersJobTest(IdentifierType identifierType) throws Exce
assertThat(jobExecution.getExitStatus()).isEqualTo(ExitStatus.COMPLETED);
}

@ParameterizedTest
@EnumSource(value = IdentifierType.class, names = {"ISSN","ISBN"}, mode = EnumSource.Mode.INCLUDE)
@DisplayName("Run bulk-edit (instance identifiers ISSN or ISBN) successfully")
void uploadInstanceIdentifiersJobTest_2(IdentifierType identifierType) throws Exception {

JobLauncherTestUtils testLauncher = createTestLauncher(bulkEditProcessInstanceIdentifiersJob);

final JobParameters jobParameters = prepareJobParameters(BULK_EDIT_IDENTIFIERS, INSTANCE, identifierType, INSTANCE_ISSN_ISBN_CSV);
JobExecution jobExecution = testLauncher.launchJob(jobParameters);

verifyCsvAndJsonOutput(jobExecution, EXPECTED_BULK_EDIT_INSTANCE_BY_ISSN_ISBN_OUTPUT, EXPECTED_BULK_EDIT_INSTANCE_BY_ISSN_ISBN_JSON_OUTPUT);

assertThat(jobExecution.getExitStatus()).isEqualTo(ExitStatus.COMPLETED);
}

@Test
@DisplayName("Run bulk-edit (item identifiers) with wrong reference identifiers")
void shouldWriteErrorsWhenItemReferenceDataNotFoundAndContinueBulkEdit() throws Exception {
Expand Down
26 changes: 26 additions & 0 deletions src/test/resources/mappings/identifierTypes.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,32 @@
"Content-Type": "application/json"
}
}
},
{
"request": {
"method": "GET",
"url": "/identifier-types?query=name%3D%3D%22ISSN%22"
},
"response": {
"status": 200,
"body": "{\n \"identifierTypes\": [\n {\n \"id\": \"913300b2-03ed-469a-8179-c1092c991227\",\n \"name\": \"ISSN\",\n \"source\": \"folio\",\n \"metadata\": {\n \"createdDate\": \"2023-11-10T11:53:52.492+00:00\",\n \"createdByUserId\": \"cffb2565-07fc-470b-86c6-17d8ce14432e\",\n \"updatedDate\": \"2023-11-10T11:53:52.492+00:00\",\n \"updatedByUserId\": \"cffb2565-07fc-470b-86c6-17d8ce14432e\"\n }\n }\n ],\n \"totalRecords\": 1\n}",
"headers": {
"Content-Type": "application/json"
}
}
},
{
"request": {
"method": "GET",
"url": "/identifier-types?query=name%3D%3D%22ISBN%22"
},
"response": {
"status": 200,
"body": "{\n \"identifierTypes\": [\n {\n \"id\": \"8261054f-be78-422d-bd51-4ed9f33c3422\",\n \"name\": \"ISBN\",\n \"source\": \"folio\",\n \"metadata\": {\n \"createdDate\": \"2023-11-10T11:53:51.879+00:00\",\n \"createdByUserId\": \"cffb2565-07fc-470b-86c6-17d8ce14432e\",\n \"updatedDate\": \"2023-11-10T11:53:51.879+00:00\",\n \"updatedByUserId\": \"cffb2565-07fc-470b-86c6-17d8ce14432e\"\n }\n }\n ],\n \"totalRecords\": 1\n}",
"headers": {
"Content-Type": "application/json"
}
}
}
]
}
Loading