use of org.folio.orders.utils.HelperUtils in project mod-orders by folio-org.
the class CheckinReceivePiecesHelper method checkIfAllItemsFound.
/**
* Checks if all expected piece records found in the storage. If any is
* missing, remove from piece reference to item and exclude piece from {@param piecesWithItems}
*
* @param expectedItemIds
* list of expected item id's
* @param piecesWithItems
* map with item id as a key and piece record as a value
* @param items
* found item records
*/
private void checkIfAllItemsFound(List<String> expectedItemIds, List<JsonObject> items, Map<String, Piece> piecesWithItems) {
// Handle the case when for some reason some items are not found
if (items.size() < expectedItemIds.size()) {
List<String> foundItemIds = StreamEx.of(items).map(HelperUtils::extractId).toList();
expectedItemIds.stream().filter(id -> !foundItemIds.contains(id)).forEach(itemId -> {
Piece piece = piecesWithItems.get(itemId);
piece.setItemId(null);
piecesWithItems.remove(itemId);
});
}
}
use of org.folio.orders.utils.HelperUtils in project mod-orders by folio-org.
the class PurchaseOrderHelper method getCompositeOrder.
/**
* Gets purchase order by id
*
* @param orderId purchase order uuid
* @return completable future with {@link CompositePurchaseOrder} on success or an exception if processing fails
*/
public CompletableFuture<CompositePurchaseOrder> getCompositeOrder(String orderId, RequestContext requestContext) {
CompletableFuture<CompositePurchaseOrder> future = new CompletableFuture<>();
purchaseOrderStorageService.getPurchaseOrderByIdAsJson(orderId, requestContext).thenApply(HelperUtils::convertToCompositePurchaseOrder).thenAccept(compPO -> protectionService.isOperationRestricted(compPO.getAcqUnitIds(), ProtectedOperationType.READ, requestContext).thenAccept(ok -> purchaseOrderLineService.populateOrderLines(compPO, requestContext).thenCompose(compPOWithLines -> titlesService.fetchNonPackageTitles(compPOWithLines, requestContext)).thenAccept(linesIdTitles -> populateInstanceId(linesIdTitles, compPO.getCompositePoLines())).thenCompose(po -> combinedPopulateService.populate(new CompositeOrderRetrieveHolder(compPO), requestContext)).thenApply(CompositeOrderRetrieveHolder::getOrder).thenAccept(future::complete).exceptionally(t -> {
logger.error("Failed to get lines for order with id={}", orderId, t.getCause());
future.completeExceptionally(t);
return null;
})).exceptionally(t -> {
logger.error("User with id={} is forbidden to view order with id={}", getCurrentUserId(requestContext), orderId, t.getCause());
future.completeExceptionally(t);
return null;
})).exceptionally(t -> {
logger.error("Failed to build composite purchase order with id={}", orderId, t.getCause());
future.completeExceptionally(t);
return null;
});
return future;
}
use of org.folio.orders.utils.HelperUtils in project mod-orders by folio-org.
the class RestClient method put.
public CompletableFuture<Void> put(RequestEntry requestEntry, JsonObject recordData, RequestContext requestContext) {
CompletableFuture<Void> future = new CompletableFuture<>();
String endpoint = requestEntry.buildEndpoint();
if (logger.isDebugEnabled()) {
logger.debug("Sending 'PUT {}' with body: {}", endpoint, recordData.encodePrettily());
}
HttpClientInterface client = getHttpClient(requestContext.getHeaders());
setDefaultHeaders(client);
try {
client.request(HttpMethod.PUT, recordData.toBuffer(), endpoint, requestContext.getHeaders()).thenAccept(HelperUtils::verifyResponse).thenAccept(avoid -> {
client.closeClient();
future.complete(null);
}).exceptionally(t -> {
client.closeClient();
future.completeExceptionally(t.getCause());
logger.error("'PUT {}' request failed. Request body: {}", endpoint, recordData.encodePrettily(), t.getCause());
return null;
});
} catch (Exception e) {
logger.error("'PUT {}' request failed. Request body: {}", endpoint, recordData.encodePrettily(), e);
client.closeClient();
future.completeExceptionally(e);
}
return future;
}
use of org.folio.orders.utils.HelperUtils in project mod-orders by folio-org.
the class RestClient method post.
public <T> CompletableFuture<T> post(RequestEntry requestEntry, T entity, RequestContext requestContext, Class<T> responseType) {
CompletableFuture<T> future = new CompletableFuture<>();
String endpoint = requestEntry.buildEndpoint();
JsonObject recordData = JsonObject.mapFrom(entity);
if (logger.isDebugEnabled()) {
logger.debug("Sending 'POST {}' with body: {}", endpoint, recordData.encodePrettily());
}
HttpClientInterface client = getHttpClient(requestContext.getHeaders());
try {
client.request(HttpMethod.POST, recordData.toBuffer(), endpoint, requestContext.getHeaders()).thenApply(HelperUtils::verifyAndExtractBody).thenAccept(body -> {
client.closeClient();
T responseEntity = body.mapTo(responseType);
if (logger.isDebugEnabled()) {
logger.debug("'POST {}' request successfully processed. Record with '{}' id has been created", endpoint, body);
}
future.complete(responseEntity);
}).exceptionally(t -> {
client.closeClient();
logger.error("'POST {}' request failed. Request body: {}", endpoint, recordData.encodePrettily(), t.getCause());
future.completeExceptionally(t.getCause());
return null;
});
} catch (Exception e) {
logger.error("'POST {}' request failed. Request body: {}", endpoint, recordData.encodePrettily(), e);
client.closeClient();
future.completeExceptionally(e);
}
return future;
}
use of org.folio.orders.utils.HelperUtils in project mod-orders by folio-org.
the class AbstractHelper method handleUpdateRequest.
/**
* A common method to update an entry in the storage
*
* @param endpoint endpoint
* @param recordData json to use for update operation
*/
protected CompletableFuture<Void> handleUpdateRequest(String endpoint, Object recordData) {
CompletableFuture<Void> future = new CompletableFuture<>();
try {
JsonObject json = convertToJson(recordData);
if (logger.isDebugEnabled()) {
logger.debug(CALLING_ENDPOINT_WITH_BODY_MSG, HttpMethod.PUT, endpoint, json.encodePrettily());
}
httpClient.request(HttpMethod.PUT, json.toBuffer(), endpoint, okapiHeaders).thenApply(HelperUtils::verifyAndExtractBody).thenAccept(response -> {
logger.debug("'PUT {}' request successfully processed", endpoint);
future.complete(null);
}).exceptionally(e -> {
future.completeExceptionally(e);
logger.error(EXCEPTION_CALLING_ENDPOINT_WITH_BODY_MSG, e, HttpMethod.PUT, endpoint, json.encodePrettily());
return null;
});
} catch (Exception e) {
logger.error(EXCEPTION_CALLING_ENDPOINT_WITH_BODY_MSG, e, HttpMethod.PUT, endpoint, recordData);
future.completeExceptionally(e);
}
return future;
}
Aggregations