Skip to content

Commit

Permalink
[MODEXPW-529] Add title extraction logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Saba-Zedginidze-EPAM committed Dec 16, 2024
1 parent ff8bbab commit 6eb70ad
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<CompositePurchaseOrder> compPOs, List<Piece> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<T, String> field) {
return Optional.ofNullable(field.extract(entry))
.orElse("");
}

protected abstract ExtractableField<T, String>[] getFields();

protected abstract String[] getHeaders();
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Pair<CompositePoLine, Piece>> {

private final OrdersService ordersService;

public ClaimCsvConverter(OrdersService ordersService) {
this.ordersService = ordersService;
}

@Override
protected ExtractableField<Pair<CompositePoLine, Piece>, String>[] getFields() {
return ClaimCsvFields.values();
Expand All @@ -20,4 +28,15 @@ protected String[] getHeaders() {
.toArray(String[]::new);
}

@Override
protected String extractField(Pair<CompositePoLine, Piece> entry, ExtractableField<Pair<CompositePoLine, Piece>, String> field) {
return Optional.ofNullable(field.extract(entry))
.orElseGet(() -> {
if (field == ClaimCsvFields.TITLE) {
return ordersService.getTitleById(entry.getValue().getTitleId()).getTitle();
}
return "";
});
}

}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -18,7 +19,7 @@ public enum ClaimCsvFields implements ExtractableField<Pair<CompositePoLine, Pie
POL_NUMBER("POL number", pair -> 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()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -71,4 +73,12 @@ public List<Piece> getPiecesByIdsAndReceivingStatus(List<String> 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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,8 @@ public static String getVendorAccountNumber(CompositePoLine poLine) {
.orElse(null);
}

public static String getTitle(CompositePoLine poLine) {
return poLine.getIsPackage() ? null : poLine.getTitleOrPackage();
}

}
5 changes: 5 additions & 0 deletions src/main/java/org/folio/dew/client/OrdersStorageClient.java
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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);

}
2 changes: 2 additions & 0 deletions src/main/resources/swagger.api/order-export.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand Down
14 changes: 7 additions & 7 deletions src/test/resources/edifact/acquisitions/csv_claims_result.csv
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6eb70ad

Please sign in to comment.