Search in sources :

Example 1 with InvoiceLineHelper

use of org.folio.rest.impl.InvoiceLineHelper in project mod-invoice by folio-org.

the class CreateInvoiceEventHandler method saveInvoiceLines.

private CompletableFuture<List<Pair<InvoiceLine, String>>> saveInvoiceLines(List<InvoiceLine> invoiceLines, Map<String, String> okapiHeaders) {
    ArrayList<CompletableFuture<InvoiceLine>> futures = new ArrayList<>();
    CompletableFuture<InvoiceLine> future = completedFuture(null);
    InvoiceLineHelper helper = new InvoiceLineHelper(okapiHeaders, Vertx.currentContext(), DEFAULT_LANG);
    for (InvoiceLine invoiceLine : invoiceLines) {
        future = future.thenCompose(v -> helper.createInvoiceLine(invoiceLine));
        futures.add(future);
    }
    List<Pair<InvoiceLine, String>> savingResults = new ArrayList<>();
    return CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).handle((v, throwable) -> {
        for (int i = 0; i < futures.size(); i++) {
            InvoiceLine mappedInvoiceLine = invoiceLines.get(i);
            int invLineNumber = i + 1;
            futures.get(i).whenComplete((savedInvLine, e) -> {
                if (e != null) {
                    logger.error("Error to create invoice line with number {}", invLineNumber, e);
                }
                Pair<InvoiceLine, String> invoiceLineToMsg = e == null ? Pair.of(savedInvLine, null) : Pair.of(mappedInvoiceLine, e.getMessage());
                savingResults.add(invoiceLineToMsg);
            });
        }
        return savingResults;
    });
}
Also used : ORDER_LINES(org.folio.invoices.utils.ResourcePathResolver.ORDER_LINES) CREATE(org.folio.ActionProfile.Action.CREATE) EventHandler(org.folio.processing.events.services.handler.EventHandler) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) PoLine(org.folio.rest.acq.model.orders.PoLine) ACTION_PROFILE(org.folio.rest.jaxrs.model.ProfileSnapshotWrapper.ContentType.ACTION_PROFILE) StreamEx.ofSubLists(one.util.streamex.StreamEx.ofSubLists) Pair(org.apache.commons.lang3.tuple.Pair) Map(java.util.Map) JsonObject(io.vertx.core.json.JsonObject) OPEN(org.folio.rest.jaxrs.model.InvoiceLine.InvoiceLineStatus.OPEN) HelperUtils.collectResultsOnSuccess(org.folio.invoices.utils.HelperUtils.collectResultsOnSuccess) MappingProfile(org.folio.MappingProfile) InvoiceLine(org.folio.rest.jaxrs.model.InvoiceLine) DataImportEventPayload(org.folio.DataImportEventPayload) EdifactParsedContent(org.folio.EdifactParsedContent) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) Objects(java.util.Objects) List(java.util.List) CollectionUtils.isNotEmpty(org.apache.commons.collections4.CollectionUtils.isNotEmpty) Logger(org.apache.logging.log4j.Logger) DI_INVOICE_CREATED(org.folio.DataImportEventTypes.DI_INVOICE_CREATED) Optional(java.util.Optional) ParsedRecord(org.folio.ParsedRecord) Pattern(java.util.regex.Pattern) Invoice(org.folio.rest.jaxrs.model.Invoice) RestClient(org.folio.rest.core.RestClient) FundDistribution(org.folio.rest.acq.model.orders.FundDistribution) Json(io.vertx.core.json.Json) INVOICE(org.folio.ActionProfile.FolioRecord.INVOICE) Record(org.folio.Record) MappingManager(org.folio.processing.mapping.MappingManager) PoLineCollection(org.folio.rest.acq.model.orders.PoLineCollection) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) EDIFACT_INVOICE(org.folio.rest.jaxrs.model.EntityType.EDIFACT_INVOICE) ArrayList(java.util.ArrayList) InvoiceLineHelper(org.folio.rest.impl.InvoiceLineHelper) InvoiceLineCollection(org.folio.rest.jaxrs.model.InvoiceLineCollection) RequestContext(org.folio.rest.core.models.RequestContext) ActionProfile(org.folio.ActionProfile) ResourcePathResolver.resourcesPath(org.folio.invoices.utils.ResourcePathResolver.resourcesPath) MappingContext(org.folio.processing.mapping.mapper.MappingContext) DataImportUtils(org.folio.dataimport.utils.DataImportUtils) Vertx(io.vertx.core.Vertx) RequestEntry(org.folio.rest.core.models.RequestEntry) InvoiceHelper(org.folio.rest.impl.InvoiceHelper) JsonArray(io.vertx.core.json.JsonArray) EventProcessingException(org.folio.processing.exceptions.EventProcessingException) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) EdifactRecordReader(org.folio.processing.mapping.mapper.reader.record.edifact.EdifactRecordReader) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) CompletableFuture(java.util.concurrent.CompletableFuture) InvoiceLine(org.folio.rest.jaxrs.model.InvoiceLine) InvoiceLineHelper(org.folio.rest.impl.InvoiceLineHelper) ArrayList(java.util.ArrayList) Pair(org.apache.commons.lang3.tuple.Pair)

Aggregations

Vertx (io.vertx.core.Vertx)1 Json (io.vertx.core.json.Json)1 JsonArray (io.vertx.core.json.JsonArray)1 JsonObject (io.vertx.core.json.JsonObject)1 String.format (java.lang.String.format)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CompletableFuture.completedFuture (java.util.concurrent.CompletableFuture.completedFuture)1 Pattern (java.util.regex.Pattern)1 Collectors (java.util.stream.Collectors)1 StreamEx.ofSubLists (one.util.streamex.StreamEx.ofSubLists)1 CollectionUtils.isNotEmpty (org.apache.commons.collections4.CollectionUtils.isNotEmpty)1 StringUtils.isBlank (org.apache.commons.lang3.StringUtils.isBlank)1 Pair (org.apache.commons.lang3.tuple.Pair)1