Search in sources :

Example 1 with ValidationError

use of org.folio.inventory.support.http.server.ValidationError 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 ValidationError

use of org.folio.inventory.support.http.server.ValidationError in project mod-inventory by folio-org.

the class FakeStorageModule method checkRequiredProperties.

private void checkRequiredProperties(RoutingContext routingContext) {
    JsonObject body = getJsonFromBody(routingContext);
    List<ValidationError> errors = new ArrayList<>();
    requiredProperties.forEach(requiredProperty -> {
        if (getPropertyValue(body, requiredProperty) == null) {
            errors.add(new ValidationError("Required property missing", requiredProperty, ""));
        }
    });
    if (errors.isEmpty()) {
        routingContext.next();
    } else {
        JsonResponse.unprocessableEntity(routingContext.response(), errors);
    }
}
Also used : ArrayList(java.util.ArrayList) JsonObject(io.vertx.core.json.JsonObject) ValidationError(org.folio.inventory.support.http.server.ValidationError)

Example 3 with ValidationError

use of org.folio.inventory.support.http.server.ValidationError in project mod-inventory by folio-org.

the class InstancePrecedingSucceedingTitleValidators method refuseWhenUnconnectedHasNoTitle.

public static CompletableFuture<Instance> refuseWhenUnconnectedHasNoTitle(Instance instance) {
    final ValidationError succeedingError = new ValidationError("Title is required for unconnected succeeding title", "succeedingTitles.title", null);
    final ValidationError precedingError = new ValidationError("Title is required for unconnected preceding title", "precedingTitles.title", null);
    return refuseWhenUnconnectedHasNoTitle(instance, instance.getSucceedingTitles(), succeedingError).thenCompose(prev -> refuseWhenUnconnectedHasNoTitle(instance, instance.getPrecedingTitles(), precedingError));
}
Also used : ValidationError(org.folio.inventory.support.http.server.ValidationError)

Example 4 with ValidationError

use of org.folio.inventory.support.http.server.ValidationError in project mod-inventory by folio-org.

the class MoveValidator method itemsMoveHasRequiredFields.

public static Optional<ValidationError> itemsMoveHasRequiredFields(JsonObject itemsMoveRequest) {
    final String toHoldingsRecordId = JsonHelper.getString(itemsMoveRequest, TO_HOLDINGS_RECORD_ID);
    if (StringUtils.isBlank(toHoldingsRecordId)) {
        return Optional.of(new ValidationError("toHoldingsRecordId is a required field", TO_HOLDINGS_RECORD_ID, null));
    }
    List<String> itemIds = JsonArrayHelper.toListOfStrings(itemsMoveRequest.getJsonArray(ITEM_IDS));
    if (itemIds.isEmpty()) {
        return Optional.of(new ValidationError("Item ids aren't specified", "itemIds", null));
    }
    return Optional.empty();
}
Also used : ValidationError(org.folio.inventory.support.http.server.ValidationError)

Example 5 with ValidationError

use of org.folio.inventory.support.http.server.ValidationError in project mod-inventory by folio-org.

the class MoveValidator method holdingsMoveHasRequiredFields.

public static Optional<ValidationError> holdingsMoveHasRequiredFields(JsonObject holdingsMoveRequest) {
    final String toInstanceId = JsonHelper.getString(holdingsMoveRequest, TO_INSTANCE_ID);
    if (StringUtils.isBlank(toInstanceId)) {
        return Optional.of(new ValidationError("toInstanceId is a required field", TO_INSTANCE_ID, null));
    }
    List<String> holdingsRecordIds = JsonArrayHelper.toListOfStrings(holdingsMoveRequest.getJsonArray(HOLDINGS_RECORD_IDS));
    if (holdingsRecordIds.isEmpty()) {
        return Optional.of(new ValidationError("Holdings record ids aren't specified", HOLDINGS_RECORD_IDS, null));
    }
    return Optional.empty();
}
Also used : ValidationError(org.folio.inventory.support.http.server.ValidationError)

Aggregations

ValidationError (org.folio.inventory.support.http.server.ValidationError)9 JsonObject (io.vertx.core.json.JsonObject)6 WebContext (org.folio.inventory.common.WebContext)5 Router (io.vertx.ext.web.Router)4 RoutingContext (io.vertx.ext.web.RoutingContext)4 BodyHandler (io.vertx.ext.web.handler.BodyHandler)4 ArrayList (java.util.ArrayList)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 Collectors (java.util.stream.Collectors)4 Item (org.folio.inventory.domain.items.Item)4 ItemCollection (org.folio.inventory.domain.items.ItemCollection)4 JsonResponse (org.folio.inventory.support.http.server.JsonResponse)4 ServerErrorResponse (org.folio.inventory.support.http.server.ServerErrorResponse)4 HttpClient (io.vertx.core.http.HttpClient)3 WebClient (io.vertx.ext.web.client.WebClient)3 MalformedURLException (java.net.MalformedURLException)3 URL (java.net.URL)3 List (java.util.List)3 Storage (org.folio.inventory.storage.Storage)3 CollectionResourceClient (org.folio.inventory.storage.external.CollectionResourceClient)3