Search in sources :

Example 11 with WebContext

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

the class Instances method withPrecedingSucceedingTitles.

private CompletableFuture<Instance> withPrecedingSucceedingTitles(RoutingContext routingContext, WebContext context, Instance instance, Response result) {
    if (result.getStatusCode() == 200) {
        JsonObject json = result.getJson();
        List<JsonObject> relationsList = JsonArrayHelper.toList(json.getJsonArray("precedingSucceedingTitles"));
        List<CompletableFuture<PrecedingSucceedingTitle>> precedingTitleCompletableFutures = relationsList.stream().filter(rel -> isPrecedingTitle(instance, rel)).map(rel -> getPrecedingSucceedingTitle(routingContext, context, rel, PrecedingSucceedingTitle.PRECEDING_INSTANCE_ID_KEY)).collect(Collectors.toList());
        List<CompletableFuture<PrecedingSucceedingTitle>> succeedingTitleCompletableFutures = relationsList.stream().filter(rel -> isSucceedingTitle(instance, rel)).map(rel -> getPrecedingSucceedingTitle(routingContext, context, rel, PrecedingSucceedingTitle.SUCCEEDING_INSTANCE_ID_KEY)).collect(Collectors.toList());
        return completedFuture(instance).thenCompose(r -> withPrecedingTitles(instance, precedingTitleCompletableFutures)).thenCompose(r -> withSucceedingTitles(instance, succeedingTitleCompletableFutures));
    }
    return completedFuture(null);
}
Also used : FailureResponseConsumer(org.folio.inventory.support.http.server.FailureResponseConsumer) URL(java.net.URL) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Router(io.vertx.ext.web.Router) JsonArrayHelper(org.folio.inventory.support.JsonArrayHelper) RoutingContext(io.vertx.ext.web.RoutingContext) BodyHandler(io.vertx.ext.web.handler.BodyHandler) StringUtils(org.apache.commons.lang3.StringUtils) EndpointFailureHandler.handleFailure(org.folio.inventory.support.EndpointFailureHandler.handleFailure) ServerErrorResponse(org.folio.inventory.support.http.server.ServerErrorResponse) CollectionResourceClient(org.folio.inventory.storage.external.CollectionResourceClient) Map(java.util.Map) JsonObject(io.vertx.core.json.JsonObject) EndpointFailureHandler.doExceptionally(org.folio.inventory.support.EndpointFailureHandler.doExceptionally) SuccessResponse.noContent(org.folio.inventory.support.http.server.SuccessResponse.noContent) EndpointFailureHandler.getKnownException(org.folio.inventory.support.EndpointFailureHandler.getKnownException) RedirectResponse(org.folio.inventory.support.http.server.RedirectResponse) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) Storage(org.folio.inventory.storage.Storage) InstancePrecedingSucceedingTitleValidators(org.folio.inventory.validation.InstancePrecedingSucceedingTitleValidators) Objects(java.util.Objects) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) InstanceRelationshipToChild(org.folio.inventory.domain.instances.InstanceRelationshipToChild) UnprocessableEntityException(org.folio.inventory.exceptions.UnprocessableEntityException) Instance(org.folio.inventory.domain.instances.Instance) CompletableFutures.failedFuture(org.folio.inventory.support.CompletableFutures.failedFuture) PagingParameters(org.folio.inventory.common.api.request.PagingParameters) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpClient(io.vertx.core.http.HttpClient) WebContext(org.folio.inventory.common.WebContext) ClientErrorResponse(org.folio.inventory.support.http.server.ClientErrorResponse) InstancesValidators.refuseWhenHridChanged(org.folio.inventory.validation.InstancesValidators.refuseWhenHridChanged) InstancesValidators(org.folio.inventory.validation.InstancesValidators) Response(org.folio.inventory.support.http.client.Response) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) InstanceCollection(org.folio.inventory.domain.instances.InstanceCollection) MultipleRecords(org.folio.inventory.common.domain.MultipleRecords) ArrayList(java.util.ArrayList) JsonResponse(org.folio.inventory.support.http.server.JsonResponse) MultipleRecordsFetchClient(org.folio.inventory.storage.external.MultipleRecordsFetchClient) ObjectUtils(org.apache.commons.lang3.ObjectUtils) InstanceRelationship(org.folio.inventory.domain.instances.InstanceRelationship) SourceStorageRecordsClient(org.folio.rest.client.SourceStorageRecordsClient) MalformedURLException(java.net.MalformedURLException) COMMA(io.netty.util.internal.StringUtil.COMMA) InstanceRelationshipToParent(org.folio.inventory.domain.instances.InstanceRelationshipToParent) PrecedingSucceedingTitle(org.folio.inventory.domain.instances.titles.PrecedingSucceedingTitle) JsonArray(io.vertx.core.json.JsonArray) InstanceRelationshipsService(org.folio.inventory.services.InstanceRelationshipsService) HttpStatus(org.folio.HttpStatus) Success(org.folio.inventory.common.domain.Success) CqlQuery(org.folio.inventory.storage.external.CqlQuery) Collections(java.util.Collections) CompletableFuture(java.util.concurrent.CompletableFuture) JsonObject(io.vertx.core.json.JsonObject)

Example 12 with WebContext

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

the class InstancesBatch method updateRelatedRecords.

/**
 * Updates relationships for specified created instances.
 *
 * @param newInstances     the new instances containing relationship arrays to persist.
 * @param createdInstances instances from storage whose relationships will be updated.
 * @param routingContext   routingContext
 * @param webContext       webContext
 */
private Future<CompositeFuture> updateRelatedRecords(List<JsonObject> newInstances, List<Instance> createdInstances, RoutingContext routingContext, WebContext webContext) {
    try {
        Map<String, Instance> mapInstanceById = newInstances.stream().collect(Collectors.toMap(instance -> instance.getString("id"), Instance::fromJson));
        List<Future> updateRelationshipsFutures = new ArrayList<>();
        for (Instance createdInstance : createdInstances) {
            Instance newInstance = mapInstanceById.get(createdInstance.getId());
            if (newInstance != null) {
                createdInstance.setParentInstances(newInstance.getParentInstances());
                createdInstance.setChildInstances(newInstance.getChildInstances());
                createdInstance.setPrecedingTitles(newInstance.getPrecedingTitles());
                createdInstance.setSucceedingTitles(newInstance.getSucceedingTitles());
                Promise<Void> updatePromise = Promise.promise();
                updateRelationshipsFutures.add(updatePromise.future());
                updateRelatedRecords(routingContext, webContext, createdInstance).whenComplete((result, ex) -> {
                    if (ex == null) {
                        updatePromise.complete();
                    } else {
                        log.warn("Exception occurred", ex);
                        handleFailure(getKnownException(ex), routingContext);
                    }
                });
            }
        }
        return CompositeFuture.join(updateRelationshipsFutures);
    } catch (IllegalStateException e) {
        log.error("Can not update instances relationships cause: " + e);
        return Future.failedFuture(e);
    }
}
Also used : WebContext(org.folio.inventory.common.WebContext) InstancePrecedingSucceedingTitleValidators.isTitleMissingForUnconnectedPrecedingSucceeding(org.folio.inventory.validation.InstancePrecedingSucceedingTitleValidators.isTitleMissingForUnconnectedPrecedingSucceeding) Router(io.vertx.ext.web.Router) RoutingContext(io.vertx.ext.web.RoutingContext) BodyHandler(io.vertx.ext.web.handler.BodyHandler) StringUtils(org.apache.commons.lang3.StringUtils) EndpointFailureHandler.handleFailure(org.folio.inventory.support.EndpointFailureHandler.handleFailure) ArrayList(java.util.ArrayList) CompositeFuture(io.vertx.core.CompositeFuture) Pair(org.apache.commons.lang3.tuple.Pair) Map(java.util.Map) JsonObject(io.vertx.core.json.JsonObject) StringUtils.isEmpty(org.apache.commons.lang3.StringUtils.isEmpty) JsonArrayHelper.toList(org.folio.inventory.support.JsonArrayHelper.toList) EndpointFailureHandler.getKnownException(org.folio.inventory.support.EndpointFailureHandler.getKnownException) RedirectResponse(org.folio.inventory.support.http.server.RedirectResponse) Promise(io.vertx.core.Promise) BatchResult(org.folio.inventory.domain.BatchResult) UUID(java.util.UUID) Future(io.vertx.core.Future) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) PrecedingSucceedingTitle(org.folio.inventory.domain.instances.titles.PrecedingSucceedingTitle) Storage(org.folio.inventory.storage.Storage) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) Instance(org.folio.inventory.domain.instances.Instance) HttpClient(io.vertx.core.http.HttpClient) Instance(org.folio.inventory.domain.instances.Instance) ArrayList(java.util.ArrayList) CompositeFuture(io.vertx.core.CompositeFuture) Future(io.vertx.core.Future)

Example 13 with WebContext

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

the class Items method deleteById.

private void deleteById(RoutingContext routingContext) {
    WebContext context = new WebContext(routingContext);
    CollectionResourceClient itemsStorageClient;
    try {
        OkapiHttpClient okapiClient = createHttpClient(routingContext, context);
        itemsStorageClient = createItemsStorageClient(okapiClient, context);
    } catch (MalformedURLException e) {
        invalidOkapiUrlResponse(routingContext, context);
        return;
    }
    String id = routingContext.request().getParam("id");
    itemsStorageClient.delete(id, response -> {
        if (response.getStatusCode() == 204) {
            SuccessResponse.noContent(routingContext.response());
        } else {
            ForwardResponse.forward(routingContext.response(), response);
        }
    });
}
Also used : CollectionResourceClient(org.folio.inventory.storage.external.CollectionResourceClient) MalformedURLException(java.net.MalformedURLException) WebContext(org.folio.inventory.common.WebContext) OkapiHttpClient(org.folio.inventory.support.http.client.OkapiHttpClient)

Example 14 with WebContext

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

the class Items method addItem.

private void addItem(RoutingContext routingContext, WebContext webContext, Item newItem, User user, ItemCollection itemCollection) {
    List<CirculationNote> notes = newItem.getCirculationNotes().stream().map(note -> note.withId(UUID.randomUUID().toString())).map(note -> note.withSource(user)).map(note -> note.withDate(dateTimeFormatter.format(ZonedDateTime.now()))).collect(Collectors.toList());
    itemCollection.add(newItem.withCirculationNotes(notes), success -> {
        Item item = success.getResult();
        respondWithItemRepresentation(item, STATUS_CREATED, routingContext, webContext);
    }, FailureResponseConsumer.serverError(routingContext.response()));
}
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) Item(org.folio.inventory.domain.items.Item) CirculationNote(org.folio.inventory.domain.items.CirculationNote)

Example 15 with WebContext

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

the class FakeStorageModule method getMany.

private void getMany(RoutingContext routingContext) {
    WebContext context = new WebContext(routingContext);
    Integer limit = context.getIntegerParameter("limit", 10);
    Integer offset = context.getIntegerParameter("offset", 0);
    String query = context.getStringParameter("query", null);
    System.out.printf("Handling %s%n", routingContext.request().uri());
    Map<String, JsonObject> resourcesForTenant = getResourcesForTenant(context);
    List<JsonObject> filteredItems = new FakeCQLToJSONInterpreter(false).execute(resourcesForTenant.values(), query);
    List<JsonObject> pagedItems = filteredItems.stream().skip(offset).limit(limit).collect(Collectors.toList());
    JsonObject result = new JsonObject();
    result.put(collectionPropertyName, new JsonArray(pagedItems));
    result.put("totalRecords", filteredItems.size());
    System.out.printf("Found %s resources: %s%n", recordTypeName, result.encodePrettily());
    JsonResponse.success(routingContext.response(), result);
}
Also used : JsonArray(io.vertx.core.json.JsonArray) 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