Search in sources :

Example 1 with WebContext

use of org.folio.inventory.common.WebContext in project mod-inventory by folio-org.

the class ItemsByHoldingsRecordId method joinAndRespondWithManyItems.

private void joinAndRespondWithManyItems(RoutingContext routingContext, WebContext webContext, Response boundWithParts, String holdingsRecordId, String relationsParam) {
    String itemQuery = "id==(NOOP)";
    List<String> itemIds = new ArrayList<>();
    boolean onlyBoundWiths = relationsParam != null && (relationsParam.equals(RELATION_PARAM_ONLY_BOUND_WITHS) || relationsParam.equals(RELATION_PARAM_ONLY_BOUND_WITHS_SKIP_DIRECTLY_LINKED_ITEM));
    boolean skipDirectlyLinkedItem = relationsParam != null && relationsParam.equals(RELATION_PARAM_ONLY_BOUND_WITHS_SKIP_DIRECTLY_LINKED_ITEM);
    JsonObject boundWithPartsJson = boundWithParts.getJson();
    JsonArray boundWithPartRecords = boundWithPartsJson.getJsonArray(BOUND_WITH_PARTS_JSON_ARRAY);
    itemIds = boundWithPartRecords.stream().map(o -> (JsonObject) o).map(part -> part.getString("itemId")).collect(Collectors.toList());
    boolean boundWithsFound = itemIds.size() > 0;
    if (boundWithsFound) {
        itemQuery = buildQueryByIds(itemIds);
        if (skipDirectlyLinkedItem) {
            itemQuery += " and holdingsRecordId <>" + holdingsRecordId;
        } else if (!onlyBoundWiths) {
            itemQuery += " or holdingsRecordId==" + holdingsRecordId;
        }
    } else {
        if (onlyBoundWiths) {
            itemQuery = "id==(NOOP)";
        } else {
            itemQuery = "holdingsRecordId==" + holdingsRecordId;
        }
    }
    try {
        storage.getItemCollection(webContext).findByCql(itemQuery, new PagingParameters(1000, 0), success -> respondWithManyItems(routingContext, webContext, success.getResult()), FailureResponseConsumer.serverError(routingContext.response()));
    } catch (UnsupportedEncodingException e) {
        ServerErrorResponse.internalError(routingContext.response(), e.toString());
    }
}
Also used : JsonArray(io.vertx.core.json.JsonArray) WebContext(org.folio.inventory.common.WebContext) Arrays(java.util.Arrays) WebClient(io.vertx.ext.web.client.WebClient) FailureResponseConsumer(org.folio.inventory.support.http.server.FailureResponseConsumer) URL(java.net.URL) ClientErrorResponse(org.folio.inventory.support.http.server.ClientErrorResponse) Router(io.vertx.ext.web.Router) Response(org.folio.inventory.support.http.client.Response) RoutingContext(io.vertx.ext.web.RoutingContext) ArrayList(java.util.ArrayList) ServerErrorResponse(org.folio.inventory.support.http.server.ServerErrorResponse) CollectionResourceClient(org.folio.inventory.storage.external.CollectionResourceClient) CqlHelper.buildQueryByIds(org.folio.inventory.support.CqlHelper.buildQueryByIds) JsonObject(io.vertx.core.json.JsonObject) MalformedURLException(java.net.MalformedURLException) MethodHandles(java.lang.invoke.MethodHandles) OkapiHttpClient(org.folio.inventory.support.http.client.OkapiHttpClient) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) Storage(org.folio.inventory.storage.Storage) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) Logger(org.apache.logging.log4j.Logger) LogManager(org.apache.logging.log4j.LogManager) PagingParameters(org.folio.inventory.common.api.request.PagingParameters) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpClient(io.vertx.core.http.HttpClient) PagingParameters(org.folio.inventory.common.api.request.PagingParameters) ArrayList(java.util.ArrayList) JsonObject(io.vertx.core.json.JsonObject) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 2 with WebContext

use of org.folio.inventory.common.WebContext in project mod-inventory by folio-org.

the class MoveApi method updateItems.

private void updateItems(RoutingContext routingContext, WebContext context, List<String> idsToUpdate, List<Item> itemsToUpdate) {
    ItemCollection storageItemCollection = storage.getItemCollection(context);
    List<CompletableFuture<Item>> updates = itemsToUpdate.stream().map(storageItemCollection::update).collect(Collectors.toList());
    CompletableFuture.allOf(updates.toArray(new CompletableFuture[0])).handle((vVoid, throwable) -> updates.stream().filter(future -> !future.isCompletedExceptionally()).map(CompletableFuture::join).map(Item::getId).collect(toList())).thenAccept(updatedIds -> respond(routingContext, idsToUpdate, updatedIds));
}
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) CompletableFuture(java.util.concurrent.CompletableFuture) ItemCollection(org.folio.inventory.domain.items.ItemCollection)

Example 3 with WebContext

use of org.folio.inventory.common.WebContext in project mod-inventory by folio-org.

the class MoveApi method updateHoldings.

private void updateHoldings(RoutingContext routingContext, WebContext context, List<String> idsToUpdate, List<HoldingsRecord> holdingsToUpdate) {
    HoldingsRecordCollection storageHoldingsRecordsCollection = storage.getHoldingsRecordCollection(context);
    List<CompletableFuture<HoldingsRecord>> updateFutures = holdingsToUpdate.stream().map(storageHoldingsRecordsCollection::update).collect(Collectors.toList());
    CompletableFuture.allOf(updateFutures.toArray(new CompletableFuture[0])).handle((vVoid, throwable) -> updateFutures.stream().filter(future -> !future.isCompletedExceptionally()).map(CompletableFuture::join).map(HoldingsRecord::getId).collect(toList())).thenAccept(updatedIds -> respond(routingContext, idsToUpdate, updatedIds));
}
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) CompletableFuture(java.util.concurrent.CompletableFuture) HoldingsRecordCollection(org.folio.inventory.domain.HoldingsRecordCollection)

Example 4 with WebContext

use of org.folio.inventory.common.WebContext 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 5 with WebContext

use of org.folio.inventory.common.WebContext in project mod-inventory by folio-org.

the class ModsIngestion method status.

private void status(RoutingContext routingContext) {
    Context context = new WebContext(routingContext);
    storage.getIngestJobCollection(context).findById(routingContext.request().getParam("id"), it -> JsonResponse.success(routingContext.response(), new JsonObject().put("status", it.getResult().state.toString())), FailureResponseConsumer.serverError(routingContext.response()));
}
Also used : Context(org.folio.inventory.common.Context) WebContext(org.folio.inventory.common.WebContext) RoutingContext(io.vertx.ext.web.RoutingContext) WebContext(org.folio.inventory.common.WebContext) JsonObject(io.vertx.core.json.JsonObject)

Aggregations

WebContext (org.folio.inventory.common.WebContext)36 JsonObject (io.vertx.core.json.JsonObject)27 RoutingContext (io.vertx.ext.web.RoutingContext)20 Router (io.vertx.ext.web.Router)19 Collectors (java.util.stream.Collectors)17 JsonArray (io.vertx.core.json.JsonArray)16 ServerErrorResponse (org.folio.inventory.support.http.server.ServerErrorResponse)16 BodyHandler (io.vertx.ext.web.handler.BodyHandler)15 MalformedURLException (java.net.MalformedURLException)15 URL (java.net.URL)15 ArrayList (java.util.ArrayList)15 List (java.util.List)15 CollectionResourceClient (org.folio.inventory.storage.external.CollectionResourceClient)15 HttpClient (io.vertx.core.http.HttpClient)14 CompletableFuture (java.util.concurrent.CompletableFuture)14 Storage (org.folio.inventory.storage.Storage)14 JsonResponse (org.folio.inventory.support.http.server.JsonResponse)14 UnsupportedEncodingException (java.io.UnsupportedEncodingException)12 ClientErrorResponse (org.folio.inventory.support.http.server.ClientErrorResponse)12 PagingParameters (org.folio.inventory.common.api.request.PagingParameters)11