use of org.folio.orders.utils.HelperUtils.extractId in project mod-orders by folio-org.
the class InventoryManager method getOrCreateHoldingsRecord.
public CompletableFuture<String> getOrCreateHoldingsRecord(String instanceId, Location location, RequestContext requestContext) {
if (location.getHoldingId() != null) {
Context ctx = requestContext.getContext();
String tenantId = TenantTool.tenantId(requestContext.getHeaders());
String holdingId = location.getHoldingId();
RequestEntry requestEntry = new RequestEntry(INVENTORY_LOOKUP_ENDPOINTS.get(HOLDINGS_RECORDS_BY_ID_ENDPOINT)).withId(holdingId);
CompletableFuture<String> holdingIdFuture;
var holdingIdKey = String.format(TENANT_SPECIFIC_KEY_FORMAT, tenantId, "getOrCreateHoldingsRecord", holdingId);
String holdingIdCached = ctx.get(holdingIdKey);
if (holdingIdCached != null) {
holdingIdFuture = CompletableFuture.completedFuture(holdingIdCached);
} else {
holdingIdFuture = restClient.getAsJsonObject(requestEntry, requestContext).whenComplete((id, err) -> ctx.put(holdingIdKey, id)).thenApply(holdingJson -> {
var id = HelperUtils.extractId(holdingJson);
ctx.put(holdingIdKey, id);
return id;
});
}
return holdingIdFuture.exceptionally(throwable -> {
if (throwable.getCause() instanceof HttpException && ((HttpException) throwable.getCause()).getCode() == 404) {
String msg = String.format(HOLDINGS_BY_ID_NOT_FOUND.getDescription(), holdingId);
Error error = new Error().withCode(HOLDINGS_BY_ID_NOT_FOUND.getCode()).withMessage(msg);
throw new CompletionException(new HttpException(NOT_FOUND, error));
} else {
throw new CompletionException(throwable.getCause());
}
});
} else {
return createHoldingsRecord(instanceId, location.getLocationId(), PostResponseType.UUID, String.class, requestContext);
}
}
Aggregations