Skip to content

Commit

Permalink
[MODEXPW-445] - Updated the "OrderLine" search for the DefaultConfig (#…
Browse files Browse the repository at this point in the history
…495)

* [MODEXPW-445] - Updated the "OrderLine" search for the DefaultConfig

* [MODEXPW-445] - test updated

* [MODEXPW-445] - test updated

* [MODEXPW-445] - configQuery updated

* [MODEXPW-445] - refactor

* [MODEXPW-445] - tests updated
  • Loading branch information
Dmitriy-Butramyou authored Nov 3, 2023
1 parent 2f65a97 commit 42e78a9
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,31 @@
import static java.util.stream.Collectors.groupingBy;
import static org.folio.dew.domain.dto.JobParameterNames.EDIFACT_ORDERS_EXPORT;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.folio.dew.batch.ExecutionContextUtils;
import org.folio.dew.batch.acquisitions.edifact.PurchaseOrdersToEdifactMapper;
import org.folio.dew.batch.acquisitions.edifact.exceptions.CompositeOrderMappingException;
import org.folio.dew.batch.acquisitions.edifact.exceptions.EdifactException;
import org.folio.dew.batch.acquisitions.edifact.exceptions.OrderNotFoundException;
import org.folio.dew.batch.acquisitions.edifact.services.OrdersService;
import org.folio.dew.client.DataExportSpringClient;
import org.folio.dew.domain.dto.CompositePoLine;
import org.folio.dew.domain.dto.CompositePurchaseOrder;
import org.folio.dew.domain.dto.JobParameterNames;
import org.folio.dew.domain.dto.EdiConfig;
import org.folio.dew.domain.dto.ExportConfig;
import org.folio.dew.domain.dto.ExportConfigCollection;
import org.folio.dew.domain.dto.ExportType;
import org.folio.dew.domain.dto.PoLine;
import org.folio.dew.domain.dto.PurchaseOrder;
import org.folio.dew.domain.dto.VendorEdiOrdersExportConfig;
Expand All @@ -43,6 +53,7 @@ public class MapToEdifactTasklet implements Tasklet {
private final ObjectMapper ediObjectMapper;

private final OrdersService ordersService;
private final DataExportSpringClient dataExportSpringClient;
private final PurchaseOrdersToEdifactMapper purchaseOrdersToEdifactMapper;

@Override
Expand Down Expand Up @@ -108,11 +119,14 @@ private String buildPoLineQuery(VendorEdiOrdersExportConfig ediConfig) {
var ediExportDateFilter = " AND (cql.allRecords=1 NOT lastEDIExportDate=\"\")"; // has not been exported yet
var acqMethodsFilter = fieldInListFilter("acquisitionMethod",
ediConfig.getEdiConfig().getDefaultAcquisitionMethods()); // acquisitionMethod in default list
String vendorAccountFilter;
if (ediConfig.getIsDefaultConfig() != null && ediConfig.getIsDefaultConfig()) {
// vendorAccount empty or undefined
vendorAccountFilter = " AND (vendorDetail.vendorAccount==\"\" OR " +
"(cql.allRecords=1 NOT vendorDetail.vendorAccount=\"\"))";
String vendorAccountFilter = "";
if (Boolean.TRUE.equals(ediConfig.getIsDefaultConfig())) {
var configQuery = String.format("configName==%s_%s*", ExportType.EDIFACT_ORDERS_EXPORT, ediConfig.getVendorId());
var configs = dataExportSpringClient.getExportConfigs(configQuery);
if (configs.getTotalRecords() > 1) {
var accountNoSetForExclude = getAccountNoSet(configs);
vendorAccountFilter = fieldNotInListFilter("vendorDetail.vendorAccount", accountNoSetForExclude);
}
} else {
// vendorAccount in the config account number list
vendorAccountFilter = fieldInListFilter("vendorDetail.vendorAccount",
Expand All @@ -125,6 +139,20 @@ private String buildPoLineQuery(VendorEdiOrdersExportConfig ediConfig) {
return resultQuery;
}

private Set<String> getAccountNoSet(ExportConfigCollection configs) {
Set<String> accountNoSet = new HashSet<>();
for (ExportConfig exportConfig : configs.getConfigs()) {
EdiConfig ediConfig = exportConfig.getExportTypeSpecificParameters().getVendorEdiOrdersExportConfig().getEdiConfig();
if (Objects.nonNull(ediConfig)) {
List<String> currentAccountNoList = ediConfig.getAccountNoList();
if (CollectionUtils.isNotEmpty(currentAccountNoList)) {
accountNoSet.addAll(currentAccountNoList);
}
}
}
return accountNoSet;
}

private void persistPoLineIds(ChunkContext chunkContext, List<CompositePurchaseOrder> compOrders) throws JsonProcessingException {
var polineIds = compOrders.stream()
.flatMap(ord -> ord.getCompositePoLines().stream())
Expand All @@ -140,6 +168,13 @@ private String fieldInListFilter(String fieldName, List<?> list) {
.collect(Collectors.joining(" OR ", "(", ")")));
}

private static String fieldNotInListFilter(String fieldName, Collection<?> list) {
return String.format(" AND cql.allRecords=1 NOT %s==%s", fieldName,
list.stream()
.map(item -> String.format("\"%s\"", item.toString()))
.collect(Collectors.joining(" OR ", "(", ")")));
}

private List<CompositePurchaseOrder> assembleCompositeOrders(List<PurchaseOrder> orders, List<PoLine> poLines) {
Map<String, List<CompositePoLine>> orderIdToCompositePoLines = poLines.stream()
.map(poLine -> convertTo(poLine, CompositePoLine.class))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package org.folio.dew.client;

import org.folio.dew.domain.dto.ExportConfigCollection;
import org.folio.dew.domain.dto.Job;
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 = "data-export-spring")
public interface DataExportSpringClient {

@GetMapping(value = "/jobs/{jobId}", produces = MediaType.APPLICATION_JSON_VALUE)
Job getJobById(@PathVariable String jobId);

@GetMapping(value = "/configs", produces = MediaType.APPLICATION_JSON_VALUE)
ExportConfigCollection getExportConfigs(@RequestParam("query") String query);

}
4 changes: 4 additions & 0 deletions src/main/resources/swagger.api/bursar-export.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@ components:
$ref: '../../../../folio-export-common/schemas/acquisitions/vendorEdiOrdersExportConfig.json#/VendorEdiOrdersExportConfig'
eHoldingsExportConfig:
$ref: '../../../../folio-export-common/schemas/eholdings/eHoldingsExportConfig.json#/EHoldingsExportConfig'
exportConfig:
$ref: '../../../../folio-export-common/schemas/exportConfig.json#/ExportConfig'
exportConfigCollection:
$ref: '../../../../folio-export-common/schemas/exportConfigCollection.json#/ExportConfigCollection'
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.folio.dew.BaseBatchTest;
import org.folio.dew.batch.acquisitions.edifact.PurchaseOrdersToEdifactMapper;
import org.folio.dew.batch.acquisitions.edifact.services.OrdersService;
import org.folio.dew.client.DataExportSpringClient;
import org.folio.dew.domain.dto.ExportConfigCollection;
import org.folio.dew.domain.dto.PoLine;
import org.folio.dew.domain.dto.PoLineCollection;
import org.folio.dew.domain.dto.PurchaseOrder;
Expand All @@ -42,6 +44,8 @@ class MapToEdifactTaskletTest extends BaseBatchTest {
@MockBean
private OrdersService ordersService;
@MockBean
DataExportSpringClient dataExportSpringClient;
@MockBean
private PurchaseOrdersToEdifactMapper purchaseOrdersToEdifactMapper;

@Autowired
Expand Down Expand Up @@ -98,6 +102,35 @@ void testShouldReturnEdifactExceptionBecauseFtpPortIsNull() throws Exception {

@Test
void edifactExportJobIfDefaultConfigTestSuccess() throws Exception {
JobLauncherTestUtils testLauncher = createTestLauncher(edifactExportJob);
List<PurchaseOrder> orders = objectMapper.readValue(getMockData(
"edifact/acquisitions/purchase_order_collection.json"), PurchaseOrderCollection.class).getPurchaseOrders();
List<PoLine> poLines = objectMapper.readValue(getMockData("edifact/acquisitions/po_line_collection.json"),
PoLineCollection.class).getPoLines();
String cqlString = "purchaseOrder.workflowStatus==Open" +
" AND purchaseOrder.vendor==d0fb5aa0-cdf1-11e8-a8d5-f2801f1b9fd1" +
" AND (cql.allRecords=1 NOT purchaseOrder.manualPo==true)" +
" AND automaticExport==true" +
" AND (cql.allRecords=1 NOT lastEDIExportDate=\"\")" +
" AND acquisitionMethod==(\"306489dd-0053-49ee-a068-c316444a8f55\")";
String configSql = "configName==EDIFACT_ORDERS_EXPORT_d0fb5aa0-cdf1-11e8-a8d5-f2801f1b9fd1*";
ExportConfigCollection exportConfigCollection = new ExportConfigCollection();
exportConfigCollection.setTotalRecords(1);
poLines.get(0).getVendorDetail().setVendorAccount(null);
doReturn(poLines).when(ordersService).getPoLinesByQuery(cqlString);
doReturn(exportConfigCollection).when(dataExportSpringClient).getExportConfigs(configSql);
doReturn(orders).when(ordersService).getPurchaseOrdersByIds(anyList());
doReturn("test1").when(purchaseOrdersToEdifactMapper).convertOrdersToEdifact(any(), any(), anyString());

JobExecution jobExecution = testLauncher.launchStep("mapToEdifactStep", getJobParameters(true));

Assertions.assertThat(jobExecution.getExitStatus()).isEqualTo(ExitStatus.COMPLETED);
verify(ordersService).getPoLinesByQuery(cqlString);
verify(ordersService).getPurchaseOrdersByIds(anyList());
}

@Test
void edifactExportJobIfDefaultConfigNotOneTestSuccess() throws Exception {
JobLauncherTestUtils testLauncher = createTestLauncher(edifactExportJob);
List<PurchaseOrder> orders = objectMapper.readValue(getMockData(
"edifact/acquisitions/purchase_order_collection.json"), PurchaseOrderCollection.class).getPurchaseOrders();
Expand All @@ -109,9 +142,12 @@ void edifactExportJobIfDefaultConfigTestSuccess() throws Exception {
" AND automaticExport==true" +
" AND (cql.allRecords=1 NOT lastEDIExportDate=\"\")" +
" AND acquisitionMethod==(\"306489dd-0053-49ee-a068-c316444a8f55\")" +
" AND (vendorDetail.vendorAccount==\"\" OR (cql.allRecords=1 NOT vendorDetail.vendorAccount=\"\"))";
" AND cql.allRecords=1 NOT vendorDetail.vendorAccount==(\"org1\" OR \"org2\")";
String configSql = "configName==EDIFACT_ORDERS_EXPORT_d0fb5aa0-cdf1-11e8-a8d5-f2801f1b9fd1*";
ExportConfigCollection exportConfigCollection = objectMapper.readValue(getMockData("edifact/dataExportConfigs.json"), ExportConfigCollection.class);
poLines.get(0).getVendorDetail().setVendorAccount(null);
doReturn(poLines).when(ordersService).getPoLinesByQuery(cqlString);
doReturn(exportConfigCollection).when(dataExportSpringClient).getExportConfigs(configSql);
doReturn(orders).when(ordersService).getPurchaseOrdersByIds(anyList());
doReturn("test1").when(purchaseOrdersToEdifactMapper).convertOrdersToEdifact(any(), any(), anyString());

Expand Down
106 changes: 106 additions & 0 deletions src/test/resources/edifact/dataExportConfigs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
{
"configs": [
{
"id": "a96b3caa-f534-4f64-83a3-ed705665062e",
"type": "EDIFACT_ORDERS_EXPORT",
"tenant": "consortium",
"exportTypeSpecificParameters": {
"vendorEdiOrdersExportConfig": {
"exportConfigId": "a96b3caa-f534-4f64-83a3-ed705665062e",
"vendorId": "7baa459e-b99d-4f25-b918-9e367e1aa430",
"configName": "default",
"ediConfig": {
"defaultAcquisitionMethods": [
"86d12634-b848-4968-adf0-5a95ce41c41b",
"df26d81b-9d63-4ff8-bf41-49bf75cfa70e"
],
"ediNamingConvention": "{organizationCode}-{integrationName}-{exportJobEndDate}",
"libEdiCode": "default",
"libEdiType": "31B/US-SAN",
"vendorEdiCode": "default",
"vendorEdiType": "31B/US-SAN",
"sendAccountNumber": false,
"supportOrder": true,
"supportInvoice": false
},
"ediFtp": {
"ftpConnMode": "Active",
"ftpFormat": "FTP",
"ftpMode": "ASCII",
"ftpPort": 22,
"invoiceDirectory": "/files",
"orderDirectory": "/files",
"password": "Ffx29%pu",
"serverAddress": "ftp://ftp.ci.folio.org",
"username": "folio"
},
"ediSchedule": {
"enableScheduledExport": true,
"scheduleParameters": {
"id": "a96b3caa-f534-4f64-83a3-ed705665062e",
"scheduleFrequency": 1,
"schedulePeriod": "DAY",
"scheduleTime": "14:24:00",
"timeZone": "UTC"
}
},
"isDefaultConfig": true
}
},
"schedulePeriod": "NONE"
},
{
"id": "411ca1a1-4547-4e4b-a873-0f3852cdf0e5",
"type": "EDIFACT_ORDERS_EXPORT",
"tenant": "consortium",
"exportTypeSpecificParameters": {
"vendorEdiOrdersExportConfig": {
"exportConfigId": "411ca1a1-4547-4e4b-a873-0f3852cdf0e5",
"vendorId": "7baa459e-b99d-4f25-b918-9e367e1aa430",
"configName": "neworg1",
"ediConfig": {
"accountNoList": [
"org1",
"org2"
],
"defaultAcquisitionMethods": [
"796596c4-62b5-4b64-a2ce-524c747afaa2"
],
"ediNamingConvention": "{organizationCode}-{integrationName}-{exportJobEndDate}",
"libEdiCode": "neworg1",
"libEdiType": "31B/US-SAN",
"vendorEdiCode": "neworg1",
"vendorEdiType": "31B/US-SAN",
"sendAccountNumber": false,
"supportOrder": true,
"supportInvoice": false
},
"ediFtp": {
"ftpConnMode": "Active",
"ftpFormat": "FTP",
"ftpMode": "ASCII",
"ftpPort": 22,
"invoiceDirectory": "/files",
"orderDirectory": "/files",
"password": "Ffx29%pu",
"serverAddress": "ftp://ftp.ci.folio.org",
"username": "folio"
},
"ediSchedule": {
"enableScheduledExport": true,
"scheduleParameters": {
"id": "411ca1a1-4547-4e4b-a873-0f3852cdf0e5",
"scheduleFrequency": 1,
"schedulePeriod": "DAY",
"scheduleTime": "14:26:00",
"timeZone": "UTC"
}
},
"isDefaultConfig": false
}
},
"schedulePeriod": "NONE"
}
],
"totalRecords": 2
}

0 comments on commit 42e78a9

Please sign in to comment.