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;
});
}
Aggregations