From 6eb70ad6fa69f7932ea0be7bb753605511c0f1bc Mon Sep 17 00:00:00 2001 From: saba_zedginidze Date: Mon, 16 Dec 2024 15:23:22 +0400 Subject: [PATCH] [MODEXPW-529] Add title extraction logic --- .../edifact/config/ExportConfig.java | 11 ++++++-- .../edifact/mapper/CsvMapper.java | 11 +++++--- .../converter/AbstractCsvConverter.java | 9 +++++-- .../mapper/converter/ClaimCsvConverter.java | 19 +++++++++++++ .../mapper/converter/ClaimCsvFields.java | 3 ++- .../edifact/services/OrdersService.java | 10 +++++++ .../edifact/utils/ExportUtils.java | 4 +++ .../folio/dew/client/OrdersStorageClient.java | 5 ++++ .../resources/swagger.api/order-export.yaml | 2 ++ .../edifact/mapper/CsvMapperTest.java | 27 +++++++------------ .../acquisitions/csv_claims_result.csv | 14 +++++----- 11 files changed, 82 insertions(+), 33 deletions(-) diff --git a/src/main/java/org/folio/dew/batch/acquisitions/edifact/config/ExportConfig.java b/src/main/java/org/folio/dew/batch/acquisitions/edifact/config/ExportConfig.java index e67051c17..a33044efa 100644 --- a/src/main/java/org/folio/dew/batch/acquisitions/edifact/config/ExportConfig.java +++ b/src/main/java/org/folio/dew/batch/acquisitions/edifact/config/ExportConfig.java @@ -2,6 +2,7 @@ import org.folio.dew.batch.acquisitions.edifact.mapper.CsvMapper; import org.folio.dew.batch.acquisitions.edifact.mapper.ExportResourceMapper; +import org.folio.dew.batch.acquisitions.edifact.mapper.converter.ClaimCsvConverter; import org.folio.dew.batch.acquisitions.edifact.mapper.converter.CompOrderEdiConverter; import org.folio.dew.batch.acquisitions.edifact.mapper.converter.CompPoLineEdiConverter; import org.folio.dew.batch.acquisitions.edifact.mapper.EdifactMapper; @@ -11,6 +12,7 @@ import org.folio.dew.batch.acquisitions.edifact.services.IdentifierTypeService; import org.folio.dew.batch.acquisitions.edifact.services.LocationService; import org.folio.dew.batch.acquisitions.edifact.services.MaterialTypeService; +import org.folio.dew.batch.acquisitions.edifact.services.OrdersService; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @@ -36,8 +38,13 @@ ExportResourceMapper edifactMapper(CompOrderEdiConverter compOrderEdiConverter) } @Bean - ExportResourceMapper csvMapper() { - return new CsvMapper(); + ClaimCsvConverter claimCsvConverter(OrdersService ordersService) { + return new ClaimCsvConverter(ordersService); + } + + @Bean + ExportResourceMapper csvMapper(ClaimCsvConverter claimCsvConverter) { + return new CsvMapper(claimCsvConverter); } } diff --git a/src/main/java/org/folio/dew/batch/acquisitions/edifact/mapper/CsvMapper.java b/src/main/java/org/folio/dew/batch/acquisitions/edifact/mapper/CsvMapper.java index 81aaf82ad..c4e01e62b 100644 --- a/src/main/java/org/folio/dew/batch/acquisitions/edifact/mapper/CsvMapper.java +++ b/src/main/java/org/folio/dew/batch/acquisitions/edifact/mapper/CsvMapper.java @@ -14,12 +14,17 @@ public class CsvMapper implements ExportResourceMapper { + private final ClaimCsvConverter claimCsvConverter; + + public CsvMapper(ClaimCsvConverter claimCsvConverter) { + this.claimCsvConverter = claimCsvConverter; + } + @Override public String convertForExport(List compPOs, List pieces, VendorEdiOrdersExportConfig ediExportConfig, String jobName) { - var csvConverter = new ClaimCsvConverter(); - var csvResult = new StringBuilder(csvConverter.getCsvHeaders()).append(LINE_BREAK); + var csvResult = new StringBuilder(claimCsvConverter.getCsvHeaders()).append(LINE_BREAK); getClaimEntries(compPOs, pieces).stream() - .map(csvConverter::convertEntryToCsv) + .map(claimCsvConverter::convertEntryToCsv) .map(line -> line.concat(LINE_BREAK)) .forEachOrdered(csvResult::append); return csvResult.toString(); diff --git a/src/main/java/org/folio/dew/batch/acquisitions/edifact/mapper/converter/AbstractCsvConverter.java b/src/main/java/org/folio/dew/batch/acquisitions/edifact/mapper/converter/AbstractCsvConverter.java index 768c5d3fc..f4b1c4aff 100644 --- a/src/main/java/org/folio/dew/batch/acquisitions/edifact/mapper/converter/AbstractCsvConverter.java +++ b/src/main/java/org/folio/dew/batch/acquisitions/edifact/mapper/converter/AbstractCsvConverter.java @@ -4,6 +4,7 @@ import static org.folio.dew.utils.Constants.QUOTE; import java.util.Arrays; +import java.util.Optional; import org.folio.dew.utils.CsvHelper; import org.springframework.batch.item.file.transform.DelimitedLineAggregator; @@ -29,12 +30,16 @@ public String getCsvHeaders() { public String convertEntryToCsv(T entry) { return lineAggregator.doAggregate(Arrays.stream(getFields()) - .map(field -> field.extract(entry)) - .map(field -> field == null ? "" : field) + .map(field -> extractField(entry, field)) .map(field -> CsvHelper.escapeDelimiter(field, delimiter, QUOTE)) .toArray()); } + protected String extractField(T entry, ExtractableField field) { + return Optional.ofNullable(field.extract(entry)) + .orElse(""); + } + protected abstract ExtractableField[] getFields(); protected abstract String[] getHeaders(); diff --git a/src/main/java/org/folio/dew/batch/acquisitions/edifact/mapper/converter/ClaimCsvConverter.java b/src/main/java/org/folio/dew/batch/acquisitions/edifact/mapper/converter/ClaimCsvConverter.java index 06a1689ad..bcfa96c56 100644 --- a/src/main/java/org/folio/dew/batch/acquisitions/edifact/mapper/converter/ClaimCsvConverter.java +++ b/src/main/java/org/folio/dew/batch/acquisitions/edifact/mapper/converter/ClaimCsvConverter.java @@ -1,13 +1,21 @@ package org.folio.dew.batch.acquisitions.edifact.mapper.converter; import java.util.Arrays; +import java.util.Optional; import org.apache.commons.lang3.tuple.Pair; +import org.folio.dew.batch.acquisitions.edifact.services.OrdersService; import org.folio.dew.domain.dto.CompositePoLine; import org.folio.dew.domain.dto.Piece; public class ClaimCsvConverter extends AbstractCsvConverter> { + private final OrdersService ordersService; + + public ClaimCsvConverter(OrdersService ordersService) { + this.ordersService = ordersService; + } + @Override protected ExtractableField, String>[] getFields() { return ClaimCsvFields.values(); @@ -20,4 +28,15 @@ protected String[] getHeaders() { .toArray(String[]::new); } + @Override + protected String extractField(Pair entry, ExtractableField, String> field) { + return Optional.ofNullable(field.extract(entry)) + .orElseGet(() -> { + if (field == ClaimCsvFields.TITLE) { + return ordersService.getTitleById(entry.getValue().getTitleId()).getTitle(); + } + return ""; + }); + } + } diff --git a/src/main/java/org/folio/dew/batch/acquisitions/edifact/mapper/converter/ClaimCsvFields.java b/src/main/java/org/folio/dew/batch/acquisitions/edifact/mapper/converter/ClaimCsvFields.java index c4e5b6176..d9c763386 100644 --- a/src/main/java/org/folio/dew/batch/acquisitions/edifact/mapper/converter/ClaimCsvFields.java +++ b/src/main/java/org/folio/dew/batch/acquisitions/edifact/mapper/converter/ClaimCsvFields.java @@ -1,5 +1,6 @@ package org.folio.dew.batch.acquisitions.edifact.mapper.converter; +import static org.folio.dew.batch.acquisitions.edifact.utils.ExportUtils.getTitle; import static org.folio.dew.batch.acquisitions.edifact.utils.ExportUtils.getVendorAccountNumber; import static org.folio.dew.batch.acquisitions.edifact.utils.ExportUtils.getVendorOrderNumber; @@ -18,7 +19,7 @@ public enum ClaimCsvFields implements ExtractableField pair.getKey().getPoLineNumber()), ORDER_NUMBER("Vendor order number", pair -> getVendorOrderNumber(pair.getKey())), ACCOUNT_NUMBER("Account number", pair -> getVendorAccountNumber(pair.getKey())), - TITLE("Title from piece", pair -> "CHANGEME"), // FIXME: piece.titleId | poLine.titleOrPackage | title from inventory by piece.titleId + TITLE("Title from piece", pair -> getTitle(pair.getKey())), DISPLAY_SUMMARY("Display summary", pair -> pair.getValue().getDisplaySummary()), CHRONOLOGY("Chronology", pair -> pair.getValue().getChronology()), ENUMERATION("Enumeration", pair -> pair.getValue().getEnumeration()), diff --git a/src/main/java/org/folio/dew/batch/acquisitions/edifact/services/OrdersService.java b/src/main/java/org/folio/dew/batch/acquisitions/edifact/services/OrdersService.java index fe0ba2d2f..a9ce66d87 100644 --- a/src/main/java/org/folio/dew/batch/acquisitions/edifact/services/OrdersService.java +++ b/src/main/java/org/folio/dew/batch/acquisitions/edifact/services/OrdersService.java @@ -4,10 +4,12 @@ import static org.folio.dew.utils.QueryUtils.convertIdsToCqlQuery; import org.folio.dew.client.OrdersStorageClient; +import org.folio.dew.domain.dto.OrdersTitle; import org.folio.dew.domain.dto.Piece; import org.folio.dew.domain.dto.PoLine; import org.folio.dew.domain.dto.PurchaseOrder; import org.folio.dew.utils.QueryUtils; +import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import lombok.RequiredArgsConstructor; @@ -71,4 +73,12 @@ public List getPiecesByIdsAndReceivingStatus(List pieceIds, Piece return pieces; } + @Cacheable("titleIds") + public OrdersTitle getTitleById(String titleId) { + log.debug("getTitleById: Fetching title: {}", titleId); + var title = ordersStorageClient.getTitleById(titleId); + log.debug("getTitleById:: Fetched title: {}", title); + return title; + } + } diff --git a/src/main/java/org/folio/dew/batch/acquisitions/edifact/utils/ExportUtils.java b/src/main/java/org/folio/dew/batch/acquisitions/edifact/utils/ExportUtils.java index 03b05d55d..036030886 100644 --- a/src/main/java/org/folio/dew/batch/acquisitions/edifact/utils/ExportUtils.java +++ b/src/main/java/org/folio/dew/batch/acquisitions/edifact/utils/ExportUtils.java @@ -39,4 +39,8 @@ public static String getVendorAccountNumber(CompositePoLine poLine) { .orElse(null); } + public static String getTitle(CompositePoLine poLine) { + return poLine.getIsPackage() ? null : poLine.getTitleOrPackage(); + } + } diff --git a/src/main/java/org/folio/dew/client/OrdersStorageClient.java b/src/main/java/org/folio/dew/client/OrdersStorageClient.java index a77edbf96..a3c6ddff8 100644 --- a/src/main/java/org/folio/dew/client/OrdersStorageClient.java +++ b/src/main/java/org/folio/dew/client/OrdersStorageClient.java @@ -1,11 +1,13 @@ package org.folio.dew.client; +import org.folio.dew.domain.dto.OrdersTitle; import org.folio.dew.domain.dto.PieceCollection; import org.folio.dew.domain.dto.PoLineCollection; import org.folio.dew.domain.dto.PurchaseOrderCollection; 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 org.springframework.web.bind.annotation.RequestParam; @FeignClient(name = "orders-storage") @@ -32,4 +34,7 @@ PieceCollection getPiecesByQuery( @RequestParam("limit") int limit ); + @GetMapping(value = "/titles/{id}", produces = MediaType.APPLICATION_JSON_VALUE) + OrdersTitle getTitleById(@PathVariable("id") String id); + } diff --git a/src/main/resources/swagger.api/order-export.yaml b/src/main/resources/swagger.api/order-export.yaml index 31ee4e1fa..062524990 100644 --- a/src/main/resources/swagger.api/order-export.yaml +++ b/src/main/resources/swagger.api/order-export.yaml @@ -65,3 +65,5 @@ components: $ref: '../../../../folio-export-common/schemas/acquisitions/mod-orders-storage/piece.json#/Piece' pieceCollection: $ref: '../../../../folio-export-common/schemas/acquisitions/mod-orders-storage/piece_collection.json#/PieceCollection' + ordersTitle: + $ref: '../../../../folio-export-common/schemas/acquisitions/mod-orders-storage/title.json#/OrdersTitle' diff --git a/src/test/java/org/folio/dew/batch/acquisitions/edifact/mapper/CsvMapperTest.java b/src/test/java/org/folio/dew/batch/acquisitions/edifact/mapper/CsvMapperTest.java index dbc7215d9..dddf6b8b1 100644 --- a/src/test/java/org/folio/dew/batch/acquisitions/edifact/mapper/CsvMapperTest.java +++ b/src/test/java/org/folio/dew/batch/acquisitions/edifact/mapper/CsvMapperTest.java @@ -5,6 +5,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.when; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.IOException; @@ -14,15 +16,12 @@ import java.util.Map; import lombok.extern.log4j.Log4j2; -import org.folio.dew.batch.acquisitions.edifact.services.ConfigurationService; -import org.folio.dew.batch.acquisitions.edifact.services.ExpenseClassService; -import org.folio.dew.batch.acquisitions.edifact.services.HoldingService; -import org.folio.dew.batch.acquisitions.edifact.services.IdentifierTypeService; -import org.folio.dew.batch.acquisitions.edifact.services.LocationService; -import org.folio.dew.batch.acquisitions.edifact.services.MaterialTypeService; +import org.folio.dew.batch.acquisitions.edifact.mapper.converter.ClaimCsvConverter; +import org.folio.dew.batch.acquisitions.edifact.services.OrdersService; import org.folio.dew.config.JacksonConfiguration; import org.folio.dew.domain.dto.CompositePurchaseOrder; import org.folio.dew.domain.dto.ExportType; +import org.folio.dew.domain.dto.OrdersTitle; import org.folio.dew.domain.dto.Piece; import org.folio.dew.domain.dto.PieceCollection; import org.folio.dew.domain.dto.VendorEdiOrdersExportConfig; @@ -45,22 +44,14 @@ class CsvMapperTest { private ExportResourceMapper csvMapper; @Mock - private IdentifierTypeService identifierTypeService; - @Mock - private MaterialTypeService materialTypeService; - @Mock - private ExpenseClassService expenseClassService; - @Mock - private LocationService locationService; - @Mock - private HoldingService holdingService; - @Mock - private ConfigurationService configurationService; + private OrdersService ordersService; @BeforeEach void setUp() { - csvMapper = new CsvMapper(); + csvMapper = new CsvMapper(new ClaimCsvConverter(ordersService)); objectMapper = new JacksonConfiguration().objectMapper(); + + when(ordersService.getTitleById(anyString())).thenReturn(new OrdersTitle().title("Test title")); } @ParameterizedTest diff --git a/src/test/resources/edifact/acquisitions/csv_claims_result.csv b/src/test/resources/edifact/acquisitions/csv_claims_result.csv index e68ed42a6..e85369618 100644 --- a/src/test/resources/edifact/acquisitions/csv_claims_result.csv +++ b/src/test/resources/edifact/acquisitions/csv_claims_result.csv @@ -1,8 +1,8 @@ POL number,Vendor order number,Account number,Title from piece,Display summary,Chronology,Enumeration,Quantity -10000-1,ORD1000,BRXXXXX-01,CHANGEME,S1,C1,E1,CHANGEME -10000-1,ORD1000,BRXXXXX-01,CHANGEME,S2,C2,,CHANGEME -10001-1,ORD1001,BRXXXXX-01,CHANGEME,S3,,E3,CHANGEME -10001-2,ORD1002,BRXXXXX-01,CHANGEME,,C4,E4,CHANGEME -10002-1,,,CHANGEME,S5,,E5,CHANGEME -10002-1,,,CHANGEME,,C5,,CHANGEME -10002-1,,,CHANGEME,,,,CHANGEME +10000-1,ORD1000,BRXXXXX-01,Test title,S1,C1,E1,CHANGEME +10000-1,ORD1000,BRXXXXX-01,Test title,S2,C2,,CHANGEME +10001-1,ORD1001,BRXXXXX-01,Test title,S3,,E3,CHANGEME +10001-2,ORD1002,BRXXXXX-01,Test title,,C4,E4,CHANGEME +10002-1,,,Test title,S5,,E5,CHANGEME +10002-1,,,Test title,,C5,,CHANGEME +10002-1,,,Test title,,,,CHANGEME