Search in sources :

Example 1 with UserCollection

use of org.folio.inventory.domain.user.UserCollection in project mod-inventory by folio-org.

the class Items method update.

private void update(RoutingContext routingContext) {
    WebContext context = new WebContext(routingContext);
    JsonObject itemRequest = routingContext.getBodyAsJson();
    Optional<ValidationError> validationError = itemHasCorrectStatus(itemRequest);
    if (validationError.isPresent()) {
        unprocessableEntity(routingContext.response(), validationError.get());
        return;
    }
    Item newItem = ItemUtil.jsonToItem(itemRequest);
    ItemCollection itemCollection = storage.getItemCollection(context);
    UserCollection userCollection = storage.getUserCollection(context);
    final String itemId = routingContext.request().getParam("id");
    final CompletableFuture<Success<Item>> getItemFuture = new CompletableFuture<>();
    itemCollection.findById(itemId, getItemFuture::complete, FailureResponseConsumer.serverError(routingContext.response()));
    getItemFuture.thenApply(Success::getResult).thenCompose(ItemsValidator::refuseWhenItemNotFound).thenCompose(oldItem -> hridChanged(oldItem, newItem)).thenCompose(oldItem -> claimedReturnedMarkedAsMissing(oldItem, newItem)).thenAccept(oldItem -> {
        if (hasSameBarcode(newItem, oldItem)) {
            findUserAndUpdateItem(routingContext, newItem, oldItem, userCollection, itemCollection);
        } else {
            try {
                checkForNonUniqueBarcode(routingContext, newItem, oldItem, itemCollection, userCollection);
            } catch (UnsupportedEncodingException e) {
                ServerErrorResponse.internalError(routingContext.response(), e.toString());
            }
        }
    }).exceptionally(doExceptionally(routingContext));
}
Also used : ItemUtil(org.folio.inventory.support.ItemUtil) URL(java.net.URL) FailureResponseConsumer(org.folio.inventory.support.http.server.FailureResponseConsumer) ZonedDateTime(java.time.ZonedDateTime) MoveItemIntoStatusService(org.folio.inventory.services.MoveItemIntoStatusService) JsonArrayHelper(org.folio.inventory.support.JsonArrayHelper) Router(io.vertx.ext.web.Router) FutureAssistance.allOf(org.folio.inventory.common.FutureAssistance.allOf) Item(org.folio.inventory.domain.items.Item) RoutingContext(io.vertx.ext.web.RoutingContext) BodyHandler(io.vertx.ext.web.handler.BodyHandler) CirculationNote(org.folio.inventory.domain.items.CirculationNote) ServerErrorResponse(org.folio.inventory.support.http.server.ServerErrorResponse) CollectionResourceClient(org.folio.inventory.storage.external.CollectionResourceClient) ItemCollection(org.folio.inventory.domain.items.ItemCollection) JsonObject(io.vertx.core.json.JsonObject) ZoneOffset(java.time.ZoneOffset) ForwardResponse(org.folio.inventory.support.http.server.ForwardResponse) EndpointFailureHandler.doExceptionally(org.folio.inventory.support.EndpointFailureHandler.doExceptionally) UserCollection(org.folio.inventory.domain.user.UserCollection) MethodHandles(java.lang.invoke.MethodHandles) OkapiHttpClient(org.folio.inventory.support.http.client.OkapiHttpClient) Collectors(java.util.stream.Collectors) Storage(org.folio.inventory.storage.Storage) HTTP_OK(org.folio.HttpStatus.HTTP_OK) ItemsValidator(org.folio.inventory.validation.ItemsValidator) Stream(java.util.stream.Stream) Logger(org.apache.logging.log4j.Logger) ValidationError(org.folio.inventory.support.http.server.ValidationError) UnsupportedEncodingException(java.io.UnsupportedEncodingException) PagingParameters(org.folio.inventory.common.api.request.PagingParameters) HttpClient(io.vertx.core.http.HttpClient) CqlHelper.multipleRecordsCqlQuery(org.folio.inventory.support.CqlHelper.multipleRecordsCqlQuery) java.util(java.util) WebContext(org.folio.inventory.common.WebContext) ItemsValidator.claimedReturnedMarkedAsMissing(org.folio.inventory.validation.ItemsValidator.claimedReturnedMarkedAsMissing) WebClient(io.vertx.ext.web.client.WebClient) ClientErrorResponse(org.folio.inventory.support.http.server.ClientErrorResponse) ItemsValidator.hridChanged(org.folio.inventory.validation.ItemsValidator.hridChanged) ItemStatusValidator.itemHasCorrectStatus(org.folio.inventory.validation.ItemStatusValidator.itemHasCorrectStatus) CompletableFuture(java.util.concurrent.CompletableFuture) Response(org.folio.inventory.support.http.client.Response) Function(java.util.function.Function) MultipleRecords(org.folio.inventory.common.domain.MultipleRecords) JsonResponse(org.folio.inventory.support.http.server.JsonResponse) Clients(org.folio.inventory.storage.external.Clients) MalformedURLException(java.net.MalformedURLException) ItemStatusName(org.folio.inventory.domain.items.ItemStatusName) CqlHelper(org.folio.inventory.support.CqlHelper) User(org.folio.inventory.domain.user.User) JsonResponse.unprocessableEntity(org.folio.inventory.support.http.server.JsonResponse.unprocessableEntity) JsonArray(io.vertx.core.json.JsonArray) DateTimeFormatter(java.time.format.DateTimeFormatter) Success(org.folio.inventory.common.domain.Success) SuccessResponse(org.folio.inventory.support.http.server.SuccessResponse) LogManager(org.apache.logging.log4j.LogManager) WebContext(org.folio.inventory.common.WebContext) JsonObject(io.vertx.core.json.JsonObject) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ItemCollection(org.folio.inventory.domain.items.ItemCollection) Success(org.folio.inventory.common.domain.Success) ItemsValidator(org.folio.inventory.validation.ItemsValidator) Item(org.folio.inventory.domain.items.Item) CompletableFuture(java.util.concurrent.CompletableFuture) ValidationError(org.folio.inventory.support.http.server.ValidationError) UserCollection(org.folio.inventory.domain.user.UserCollection)

Example 2 with UserCollection

use of org.folio.inventory.domain.user.UserCollection in project mod-inventory by folio-org.

the class Items method create.

private void create(RoutingContext routingContext) {
    WebContext context = new WebContext(routingContext);
    JsonObject item = routingContext.getBodyAsJson();
    Optional<ValidationError> validationError = itemHasCorrectStatus(item);
    if (validationError.isPresent()) {
        unprocessableEntity(routingContext.response(), validationError.get());
        return;
    }
    Item newItem = ItemUtil.jsonToItem(item);
    ItemCollection itemCollection = storage.getItemCollection(context);
    UserCollection userCollection = storage.getUserCollection(context);
    if (newItem.getBarcode() != null) {
        try {
            itemCollection.findByCql(CqlHelper.barcodeIs(newItem.getBarcode()), PagingParameters.defaults(), findResult -> {
                if (findResult.getResult().records.isEmpty()) {
                    findUserAndAddItem(routingContext, context, newItem, userCollection, itemCollection);
                } else {
                    ClientErrorResponse.badRequest(routingContext.response(), String.format("Barcode must be unique, %s is already assigned to another item", newItem.getBarcode()));
                }
            }, FailureResponseConsumer.serverError(routingContext.response()));
        } catch (UnsupportedEncodingException e) {
            ServerErrorResponse.internalError(routingContext.response(), e.toString());
        }
    } else {
        findUserAndAddItem(routingContext, context, newItem, userCollection, itemCollection);
    }
}
Also used : Item(org.folio.inventory.domain.items.Item) WebContext(org.folio.inventory.common.WebContext) JsonObject(io.vertx.core.json.JsonObject) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ItemCollection(org.folio.inventory.domain.items.ItemCollection) ValidationError(org.folio.inventory.support.http.server.ValidationError) UserCollection(org.folio.inventory.domain.user.UserCollection)

Aggregations

JsonObject (io.vertx.core.json.JsonObject)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 WebContext (org.folio.inventory.common.WebContext)2 Item (org.folio.inventory.domain.items.Item)2 ItemCollection (org.folio.inventory.domain.items.ItemCollection)2 UserCollection (org.folio.inventory.domain.user.UserCollection)2 ValidationError (org.folio.inventory.support.http.server.ValidationError)2 HttpClient (io.vertx.core.http.HttpClient)1 JsonArray (io.vertx.core.json.JsonArray)1 Router (io.vertx.ext.web.Router)1 RoutingContext (io.vertx.ext.web.RoutingContext)1 WebClient (io.vertx.ext.web.client.WebClient)1 BodyHandler (io.vertx.ext.web.handler.BodyHandler)1 MethodHandles (java.lang.invoke.MethodHandles)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 ZoneOffset (java.time.ZoneOffset)1 ZonedDateTime (java.time.ZonedDateTime)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 java.util (java.util)1