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 all 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
3 changes: 3 additions & 0 deletions src/main/java/org/folio/dew/batch/JsonListFileWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import org.folio.dew.domain.dto.Formatable;
import org.folio.dew.domain.dto.HoldingsFormat;
import org.folio.dew.domain.dto.InstanceFormat;
import org.folio.dew.utils.WriterHelper;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.json.JacksonJsonObjectMarshaller;
Expand Down Expand Up @@ -34,6 +35,8 @@ public String doWrite(Chunk<? extends List<U>> lists) {
var item = iterator.next();
if (item instanceof HoldingsFormat hf) {
lines.append(WriterHelper.enrichHoldingsJson(hf, objectMapper));
} else if (item instanceof InstanceFormat instanceFormat) {
lines.append(WriterHelper.enrichInstancesJson(instanceFormat, objectMapper));
} else {
lines.append(marshaller.marshal(item.getOriginal()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,36 @@
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.FormatOfInstance;
import org.folio.dew.domain.dto.IdentifierType;
import org.folio.dew.domain.dto.Instance;
import org.folio.dew.domain.dto.InstanceFormat;
import org.folio.dew.domain.dto.ErrorServiceArgs;
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;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static org.folio.dew.domain.dto.IdentifierType.fromValue;

import static org.apache.commons.lang3.StringUtils.EMPTY;
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.ARRAY_DELIMITER_SPACED;
import static org.folio.dew.utils.Constants.ITEM_DELIMITER;
import static org.folio.dew.utils.Constants.ITEM_DELIMITER_SPACED;

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

private final InstanceReferenceService instanceReferenceService;
private final SpecialCharacterEscaper escaper;


@Value("#{jobParameters['identifierType']}")
Expand All @@ -46,7 +54,8 @@ public class BulkEditInstanceProcessor implements ItemProcessor<Instance, Instan

@Override
public InstanceFormat process(@NotNull Instance instance) {
var errorServiceArgs = new ErrorServiceArgs(jobId, getIdentifier(instance, identifierType), FilenameUtils.getName(fileName));
String identifierValue = getIdentifier(instance, identifierType);
var errorServiceArgs = new ErrorServiceArgs(jobId, identifierValue, FilenameUtils.getName(fileName));

var instanceFormat = InstanceFormat.builder()
.id(instance.getId())
Expand All @@ -55,63 +64,92 @@ public InstanceFormat process(@NotNull Instance instance) {
.previouslyHeld(isEmpty(instance.getPreviouslyHeld()) ? EMPTY : Boolean.toString(instance.getPreviouslyHeld()))
.hrid(instance.getHrid())
.source(instance.getSource())
.catalogedDate(instance.getCatalogedDate())
.catalogedDate(formatDate(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()))))
.instanceFormatIds(fetchInstanceFormats(instance.getInstanceFormatIds(), errorServiceArgs))
.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())))
.isbn(IdentifierType.ISBN == fromValue(identifierType) ? identifierValue : null)
.issn(IdentifierType.ISSN == fromValue(identifierType) ? identifierValue : null)
.build();


return instanceFormat.withOriginal(instance);
}

private String fetchInstanceFormats(List<FormatOfInstance> instanceFormats, ErrorServiceArgs errorServiceArgs) {
private String formatDate(String catalogedDateInput) {
if (isEmpty(catalogedDateInput)){
return EMPTY;
}

var inputFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
var outputFormatter = new SimpleDateFormat("yyyy-MM-dd");

Date date;
try {
date = inputFormatter.parse(catalogedDateInput);
} catch (ParseException e) {
log.error("Can't parse catalogedDate {}.", catalogedDateInput);
return catalogedDateInput;
}
return outputFormatter.format(date);
}

private String fetchInstanceFormats(List<String> instanceFormats, ErrorServiceArgs errorServiceArgs) {
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));
.map(iFormatId -> instanceReferenceService.getFormatOfInstanceNameById(iFormatId, errorServiceArgs))
.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)) {
return switch (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);
}
3 changes: 3 additions & 0 deletions src/main/java/org/folio/dew/domain/dto/InstanceFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ public class InstanceFormat implements Formatable<org.folio.dew.domain.dto.Insta
@CsvBindByPosition(position = 21)
private String publicationRange;

private String isbn;
private String issn;

public static String[] getInstanceFieldsArray() {
return FieldUtils.getFieldsListWithAnnotation(InstanceFormat.class, CsvBindByName.class).stream()
.map(Field::getName)
Expand Down
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
9 changes: 9 additions & 0 deletions src/main/java/org/folio/dew/utils/WriterHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;
import org.folio.dew.domain.dto.HoldingsFormat;
import org.folio.dew.domain.dto.InstanceFormat;

@UtilityClass
public class WriterHelper {
Expand All @@ -17,4 +18,12 @@ public static String enrichHoldingsJson(HoldingsFormat item, ObjectMapper object
holdingsJson.putIfAbsent("instanceTitle", holdingsFormatJson.get("instance"));
return objectMapper.writeValueAsString(holdingsJson);
}
@SneakyThrows
public static String enrichInstancesJson(InstanceFormat item, ObjectMapper objectMapper) {
var instancesFormatJson = objectMapper.valueToTree(item);
var instancesJson = (ObjectNode) objectMapper.valueToTree(item.getOriginal());
instancesJson.putIfAbsent("issn", instancesFormatJson.get("issn"));
instancesJson.putIfAbsent("isbn", instancesFormatJson.get("isbn"));
return objectMapper.writeValueAsString(instancesJson);
}
}
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
Loading