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
  • Loading branch information
Dzmitry_Butramyou committed Nov 3, 2023
1 parent 2f65a97 commit b27696d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 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 @@ -109,10 +120,17 @@ private String buildPoLineQuery(VendorEdiOrdersExportConfig ediConfig) {
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=\"\"))";
if (Boolean.TRUE.equals(ediConfig.getIsDefaultConfig())) {
var configQuery = String.format("%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 empty or undefined
vendorAccountFilter = " AND (vendorDetail.vendorAccount==\"\" OR " +
"(cql.allRecords=1 NOT vendorDetail.vendorAccount=\"\"))";
}
} else {
// vendorAccount in the config account number list
vendorAccountFilter = fieldInListFilter("vendorDetail.vendorAccount",
Expand All @@ -125,6 +143,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 +172,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'

0 comments on commit b27696d

Please sign in to comment.