Search in sources :

Example 1 with MultipleRecordsFetchClient

use of org.folio.inventory.storage.external.MultipleRecordsFetchClient 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 2 with MultipleRecordsFetchClient

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

the class Instances method fetchHoldingsRecordsForInstanceRecordSet.

private CompletableFuture<List<JsonObject>> fetchHoldingsRecordsForInstanceRecordSet(InstancesResponse instancesResponse, RoutingContext routingContext, WebContext context) {
    List<String> instanceIds = getInstanceIdsFromInstanceResult(instancesResponse.getSuccess());
    MultipleRecordsFetchClient holdingsFetcher = MultipleRecordsFetchClient.builder().withCollectionPropertyName("holdingsRecords").withExpectedStatus(200).withCollectionResourceClient(createHoldingsStorageClient(routingContext, context)).build();
    return holdingsFetcher.find(instanceIds, this::cqlMatchAnyByInstanceIds);
}
Also used : MultipleRecordsFetchClient(org.folio.inventory.storage.external.MultipleRecordsFetchClient)

Example 3 with MultipleRecordsFetchClient

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

the class MoveApi method moveHoldings.

private void moveHoldings(RoutingContext routingContext) {
    WebContext context = new WebContext(routingContext);
    JsonObject holdingsMoveJsonRequest = routingContext.getBodyAsJson();
    Optional<ValidationError> validationError = holdingsMoveHasRequiredFields(holdingsMoveJsonRequest);
    if (validationError.isPresent()) {
        unprocessableEntity(routingContext.response(), validationError.get());
        return;
    }
    String toInstanceId = holdingsMoveJsonRequest.getString(TO_INSTANCE_ID);
    List<String> holdingsRecordsIdsToUpdate = toListOfStrings(holdingsMoveJsonRequest.getJsonArray(HOLDINGS_RECORD_IDS));
    storage.getInstanceCollection(context).findById(toInstanceId).thenAccept(instance -> {
        if (instance == null) {
            JsonResponse.unprocessableEntity(routingContext.response(), String.format("Instance with id=%s not found", toInstanceId));
            return;
        }
        try {
            CollectionResourceClient holdingsStorageClient = createHoldingsStorageClient(createHttpClient(routingContext, context), context);
            MultipleRecordsFetchClient holdingsRecordFetchClient = createHoldingsRecordsFetchClient(holdingsStorageClient);
            holdingsRecordFetchClient.find(holdingsRecordsIdsToUpdate, this::fetchByIdCql).thenAccept(jsons -> {
                List<HoldingsRecord> holdingsRecordsToUpdate = updateInstanceIdForHoldings(toInstanceId, jsons);
                updateHoldings(routingContext, context, holdingsRecordsIdsToUpdate, holdingsRecordsToUpdate);
            }).exceptionally(e -> {
                ServerErrorResponse.internalError(routingContext.response(), e);
                return null;
            });
        } catch (Exception e) {
            ServerErrorResponse.internalError(routingContext.response(), e);
        }
    }).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) HoldingsRecord(org.folio.HoldingsRecord) 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)

Aggregations

MultipleRecordsFetchClient (org.folio.inventory.storage.external.MultipleRecordsFetchClient)3 HttpClient (io.vertx.core.http.HttpClient)2 HttpServerResponse (io.vertx.core.http.HttpServerResponse)2 JsonObject (io.vertx.core.json.JsonObject)2 Router (io.vertx.ext.web.Router)2 RoutingContext (io.vertx.ext.web.RoutingContext)2 WebClient (io.vertx.ext.web.client.WebClient)2 BodyHandler (io.vertx.ext.web.handler.BodyHandler)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Objects (java.util.Objects)2 Optional (java.util.Optional)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 Collectors (java.util.stream.Collectors)2 Collectors.toList (java.util.stream.Collectors.toList)2 ListUtils (org.apache.commons.collections15.ListUtils)2 HoldingsRecord (org.folio.HoldingsRecord)2 WebContext (org.folio.inventory.common.WebContext)2