Search in sources :

Example 1 with CollectionResourceClient

use of org.folio.inventory.storage.external.CollectionResourceClient in project mod-inventory by folio-org.

the class PrecedingSucceedingTitlesHelper method getExistingPrecedingSucceedingTitles.

public Future<List<JsonObject>> getExistingPrecedingSucceedingTitles(Instance instance, Context context) {
    CollectionResourceClient precedingSucceedingTitlesClient = createPrecedingSucceedingTitlesClient(context);
    Promise<List<JsonObject>> promise = Promise.promise();
    String instanceId = instance.getId();
    String queryForPrecedingSucceedingInstances = String.format("succeedingInstanceId==(%s) or precedingInstanceId==(%s)", instanceId, instanceId);
    precedingSucceedingTitlesClient.getAll(queryForPrecedingSucceedingInstances, response -> {
        if (response.getStatusCode() == 200) {
            JsonObject json = response.getJson();
            List<JsonObject> precedingSucceedingTitles = JsonArrayHelper.toList(json.getJsonArray("precedingSucceedingTitles"));
            promise.complete(precedingSucceedingTitles);
        } else {
            String msg = format("Error retrieving existing preceding and succeeding titles. Response status code: %s", response.getStatusCode());
            LOGGER.error(msg);
            promise.fail(msg);
        }
    });
    return promise.future();
}
Also used : CollectionResourceClient(org.folio.inventory.storage.external.CollectionResourceClient) JsonObject(io.vertx.core.json.JsonObject) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with CollectionResourceClient

use of org.folio.inventory.storage.external.CollectionResourceClient in project mod-inventory by folio-org.

the class ItemsByHoldingsRecordId method getCollectionResourceRepository.

private CollectionResourceClient getCollectionResourceRepository(RoutingContext routingContext, WebContext context, String path) {
    CollectionResourceClient collectionResourceClient = null;
    try {
        OkapiHttpClient okapiClient = createHttpClient(routingContext, context);
        collectionResourceClient = new CollectionResourceClient(okapiClient, new URL(context.getOkapiLocation() + path));
    } catch (MalformedURLException mfue) {
        log.error(mfue);
    }
    return collectionResourceClient;
}
Also used : CollectionResourceClient(org.folio.inventory.storage.external.CollectionResourceClient) MalformedURLException(java.net.MalformedURLException) OkapiHttpClient(org.folio.inventory.support.http.client.OkapiHttpClient) URL(java.net.URL)

Example 3 with CollectionResourceClient

use of org.folio.inventory.storage.external.CollectionResourceClient in project mod-inventory by folio-org.

the class MoveApi method moveItems.

private void moveItems(RoutingContext routingContext) {
    WebContext context = new WebContext(routingContext);
    JsonObject itemsMoveJsonRequest = routingContext.getBodyAsJson();
    Optional<ValidationError> validationError = itemsMoveHasRequiredFields(itemsMoveJsonRequest);
    if (validationError.isPresent()) {
        unprocessableEntity(routingContext.response(), validationError.get());
        return;
    }
    String toHoldingsRecordId = itemsMoveJsonRequest.getString(TO_HOLDINGS_RECORD_ID);
    List<String> itemIdsToUpdate = toListOfStrings(itemsMoveJsonRequest.getJsonArray(ITEM_IDS));
    storage.getHoldingCollection(context).findById(toHoldingsRecordId).thenAccept(holding -> {
        if (Objects.nonNull(holding)) {
            try {
                CollectionResourceClient itemsStorageClient = createItemStorageClient(createHttpClient(routingContext, context), context);
                MultipleRecordsFetchClient itemsFetchClient = createItemsFetchClient(itemsStorageClient);
                itemsFetchClient.find(itemIdsToUpdate, this::fetchByIdCql).thenAccept(jsons -> {
                    List<Item> itemsToUpdate = updateHoldingsRecordIdForItems(toHoldingsRecordId, jsons);
                    updateItems(routingContext, context, itemIdsToUpdate, itemsToUpdate);
                }).exceptionally(e -> {
                    ServerErrorResponse.internalError(routingContext.response(), e);
                    return null;
                });
            } catch (Exception e) {
                ServerErrorResponse.internalError(routingContext.response(), e);
            }
        } else {
            JsonResponse.unprocessableEntity(routingContext.response(), String.format("Holding with id=%s not found", toHoldingsRecordId));
        }
    }).exceptionally(e -> {
        ServerErrorResponse.internalError(routingContext.response(), e);
        return null;
    });
}
Also used : WebContext(org.folio.inventory.common.WebContext) ItemUtil(org.folio.inventory.support.ItemUtil) WebClient(io.vertx.ext.web.client.WebClient) URL(java.net.URL) Router(io.vertx.ext.web.Router) CompletableFuture(java.util.concurrent.CompletableFuture) Item(org.folio.inventory.domain.items.Item) RoutingContext(io.vertx.ext.web.RoutingContext) BodyHandler(io.vertx.ext.web.handler.BodyHandler) ArrayList(java.util.ArrayList) ServerErrorResponse(org.folio.inventory.support.http.server.ServerErrorResponse) JsonResponse(org.folio.inventory.support.http.server.JsonResponse) CollectionResourceClient(org.folio.inventory.storage.external.CollectionResourceClient) HoldingsRecordCollection(org.folio.inventory.domain.HoldingsRecordCollection) MultipleRecordsFetchClient(org.folio.inventory.storage.external.MultipleRecordsFetchClient) MoveValidator.holdingsMoveHasRequiredFields(org.folio.inventory.validation.MoveValidator.holdingsMoveHasRequiredFields) ItemCollection(org.folio.inventory.domain.items.ItemCollection) JsonObject(io.vertx.core.json.JsonObject) JsonArrayHelper.toListOfStrings(org.folio.inventory.support.JsonArrayHelper.toListOfStrings) MalformedURLException(java.net.MalformedURLException) OkapiHttpClient(org.folio.inventory.support.http.client.OkapiHttpClient) JsonResponse.unprocessableEntity(org.folio.inventory.support.http.server.JsonResponse.unprocessableEntity) JsonResponse.success(org.folio.inventory.support.http.server.JsonResponse.success) Collectors(java.util.stream.Collectors) HoldingsRecord(org.folio.HoldingsRecord) Storage(org.folio.inventory.storage.Storage) Objects(java.util.Objects) Collectors.toList(java.util.stream.Collectors.toList) MoveValidator.itemsMoveHasRequiredFields(org.folio.inventory.validation.MoveValidator.itemsMoveHasRequiredFields) List(java.util.List) ListUtils(org.apache.commons.collections15.ListUtils) HttpServerResponse(io.vertx.core.http.HttpServerResponse) ValidationError(org.folio.inventory.support.http.server.ValidationError) Optional(java.util.Optional) CqlQuery(org.folio.inventory.storage.external.CqlQuery) HttpClient(io.vertx.core.http.HttpClient) Item(org.folio.inventory.domain.items.Item) CollectionResourceClient(org.folio.inventory.storage.external.CollectionResourceClient) WebContext(org.folio.inventory.common.WebContext) JsonObject(io.vertx.core.json.JsonObject) MultipleRecordsFetchClient(org.folio.inventory.storage.external.MultipleRecordsFetchClient) ValidationError(org.folio.inventory.support.http.server.ValidationError) MalformedURLException(java.net.MalformedURLException)

Example 4 with CollectionResourceClient

use of org.folio.inventory.storage.external.CollectionResourceClient in project mod-inventory by folio-org.

the class AbstractInstances method updatePrecedingSucceedingTitles.

protected CompletableFuture<List<Response>> updatePrecedingSucceedingTitles(Instance instance, RoutingContext routingContext, WebContext context) {
    CollectionResourceClient precedingSucceedingTitlesClient = createPrecedingSucceedingTitlesClient(routingContext, context);
    CollectionResourceRepository precedingSucceedingTitlesRepository = new CollectionResourceRepository(precedingSucceedingTitlesClient);
    List<String> instanceId = List.of(instance.getId());
    String query = createQueryForPrecedingSucceedingInstances(instanceId);
    CompletableFuture<Response> future = new CompletableFuture<>();
    precedingSucceedingTitlesClient.getAll(query, future::complete);
    return future.thenCompose(result -> updatePrecedingSucceedingTitles(instance, precedingSucceedingTitlesRepository, result));
}
Also used : Response(org.folio.inventory.support.http.client.Response) ServerErrorResponse(org.folio.inventory.support.http.server.ServerErrorResponse) CollectionResourceClient(org.folio.inventory.storage.external.CollectionResourceClient) CompletableFuture(java.util.concurrent.CompletableFuture) CollectionResourceRepository(org.folio.inventory.storage.external.CollectionResourceRepository)

Example 5 with CollectionResourceClient

use of org.folio.inventory.storage.external.CollectionResourceClient in project mod-inventory by folio-org.

the class Instances method fetchPrecedingSucceedingTitles.

private CompletableFuture<Instance> fetchPrecedingSucceedingTitles(Success<Instance> success, RoutingContext routingContext, WebContext context) {
    Instance instance = success.getResult();
    List<String> instanceIds = getInstanceIdsFromInstanceResult(success);
    String queryForPrecedingSucceedingInstances = createQueryForPrecedingSucceedingInstances(instanceIds);
    CollectionResourceClient precedingSucceedingTitlesClient = createPrecedingSucceedingTitlesClient(routingContext, context);
    CompletableFuture<Response> precedingSucceedingTitlesFetched = new CompletableFuture<>();
    precedingSucceedingTitlesClient.getAll(queryForPrecedingSucceedingInstances, precedingSucceedingTitlesFetched::complete);
    return precedingSucceedingTitlesFetched.thenCompose(response -> withPrecedingSucceedingTitles(routingContext, context, instance, response));
}
Also used : ServerErrorResponse(org.folio.inventory.support.http.server.ServerErrorResponse) RedirectResponse(org.folio.inventory.support.http.server.RedirectResponse) ClientErrorResponse(org.folio.inventory.support.http.server.ClientErrorResponse) Response(org.folio.inventory.support.http.client.Response) JsonResponse(org.folio.inventory.support.http.server.JsonResponse) CollectionResourceClient(org.folio.inventory.storage.external.CollectionResourceClient) CompletableFuture(java.util.concurrent.CompletableFuture) Instance(org.folio.inventory.domain.instances.Instance)

Aggregations

CollectionResourceClient (org.folio.inventory.storage.external.CollectionResourceClient)16 MalformedURLException (java.net.MalformedURLException)11 OkapiHttpClient (org.folio.inventory.support.http.client.OkapiHttpClient)11 JsonObject (io.vertx.core.json.JsonObject)9 URL (java.net.URL)9 ServerErrorResponse (org.folio.inventory.support.http.server.ServerErrorResponse)9 CompletableFuture (java.util.concurrent.CompletableFuture)8 Response (org.folio.inventory.support.http.client.Response)7 WebClient (io.vertx.ext.web.client.WebClient)6 ArrayList (java.util.ArrayList)6 List (java.util.List)6 WebContext (org.folio.inventory.common.WebContext)6 JsonResponse (org.folio.inventory.support.http.server.JsonResponse)6 ClientErrorResponse (org.folio.inventory.support.http.server.ClientErrorResponse)5 HttpClient (io.vertx.core.http.HttpClient)4 Router (io.vertx.ext.web.Router)4 RoutingContext (io.vertx.ext.web.RoutingContext)4 Collectors (java.util.stream.Collectors)4 LogManager (org.apache.logging.log4j.LogManager)4 Logger (org.apache.logging.log4j.Logger)4