Search in sources :

Example 1 with INVOICE

use of org.folio.ActionProfile.FolioRecord.INVOICE 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)

Example 2 with INVOICE

use of org.folio.ActionProfile.FolioRecord.INVOICE in project mod-invoice by folio-org.

the class CreateInvoiceEventHandler method handle.

@Override
public CompletableFuture<DataImportEventPayload> handle(DataImportEventPayload dataImportEventPayload) {
    CompletableFuture<DataImportEventPayload> future = new CompletableFuture<>();
    dataImportEventPayload.setEventType(DI_INVOICE_CREATED.value());
    try {
        HashMap<String, String> payloadContext = dataImportEventPayload.getContext();
        if (payloadContext == null || isBlank(payloadContext.get(EDIFACT_INVOICE.value()))) {
            logger.error(PAYLOAD_HAS_NO_DATA_MSG);
            return CompletableFuture.failedFuture(new EventProcessingException(PAYLOAD_HAS_NO_DATA_MSG));
        }
        Map<String, String> okapiHeaders = DataImportUtils.getOkapiHeaders(dataImportEventPayload);
        CompletableFuture<Map<Integer, PoLine>> poLinesFuture = getAssociatedPoLines(dataImportEventPayload, okapiHeaders);
        poLinesFuture.thenAccept(invLineNoToPoLine -> ensureAdditionalData(dataImportEventPayload, invLineNoToPoLine)).thenAccept(v -> prepareEventPayloadForMapping(dataImportEventPayload)).thenAccept(v -> MappingManager.map(dataImportEventPayload, new MappingContext())).thenAccept(v -> prepareMappingResult(dataImportEventPayload)).thenCompose(v -> saveInvoice(dataImportEventPayload, okapiHeaders)).thenApply(savedInvoice -> prepareInvoiceLinesToSave(savedInvoice.getId(), dataImportEventPayload, poLinesFuture.join())).thenCompose(preparedInvoiceLines -> saveInvoiceLines(preparedInvoiceLines, okapiHeaders)).whenComplete((savedInvoiceLines, throwable) -> {
            makeLightweightReturnPayload(dataImportEventPayload);
            if (throwable == null) {
                List<InvoiceLine> invoiceLines = savedInvoiceLines.stream().map(Pair::getLeft).collect(Collectors.toList());
                InvoiceLineCollection invoiceLineCollection = new InvoiceLineCollection().withInvoiceLines(invoiceLines).withTotalRecords(invoiceLines.size());
                dataImportEventPayload.getContext().put(INVOICE_LINES_KEY, Json.encode(invoiceLineCollection));
                Map<Integer, String> invoiceLinesErrors = prepareInvoiceLinesErrors(savedInvoiceLines);
                if (!invoiceLinesErrors.isEmpty()) {
                    dataImportEventPayload.getContext().put(INVOICE_LINES_ERRORS_KEY, Json.encode(invoiceLinesErrors));
                    future.completeExceptionally(new EventProcessingException("Error during invoice lines creation"));
                    return;
                }
                future.complete(dataImportEventPayload);
            } else {
                preparePayloadWithMappedInvoiceLines(dataImportEventPayload);
                logger.error("Error during invoice creation", throwable);
                future.completeExceptionally(throwable);
            }
        });
    } catch (Exception e) {
        logger.error("Error during creation invoice and invoice lines", e);
        future.completeExceptionally(e);
    }
    return future;
}
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) InvoiceLine(org.folio.rest.jaxrs.model.InvoiceLine) EventProcessingException(org.folio.processing.exceptions.EventProcessingException) DataImportEventPayload(org.folio.DataImportEventPayload) MappingContext(org.folio.processing.mapping.mapper.MappingContext) CompletableFuture(java.util.concurrent.CompletableFuture) InvoiceLineCollection(org.folio.rest.jaxrs.model.InvoiceLineCollection) Map(java.util.Map) HashMap(java.util.HashMap) EventProcessingException(org.folio.processing.exceptions.EventProcessingException)

Aggregations

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