Search in sources :

Example 1 with PrecedingSucceedingTitle

use of org.folio.inventory.domain.instances.titles.PrecedingSucceedingTitle in project mod-inventory by folio-org.

the class PrecedingSucceedingTitlesHelper method prepareSucceedingTitles.

private void prepareSucceedingTitles(Instance instance, List<PrecedingSucceedingTitle> preparedTitles) {
    if (instance.getSucceedingTitles() != null) {
        for (PrecedingSucceedingTitle child : instance.getSucceedingTitles()) {
            PrecedingSucceedingTitle precedingSucceedingTitle = new PrecedingSucceedingTitle(UUID.randomUUID().toString(), instance.getId(), child.succeedingInstanceId, child.title, child.hrid, child.identifiers);
            preparedTitles.add(precedingSucceedingTitle);
        }
    }
}
Also used : PrecedingSucceedingTitle(org.folio.inventory.domain.instances.titles.PrecedingSucceedingTitle)

Example 2 with PrecedingSucceedingTitle

use of org.folio.inventory.domain.instances.titles.PrecedingSucceedingTitle in project mod-inventory by folio-org.

the class AbstractInstances method toRepresentation.

/**
 * Populates multiple Instances representation (downwards)
 *
 * @param instancesResponse Set of Instances to transform to representations
 * @param context
 * @return Result set as JSON object
 */
protected JsonObject toRepresentation(InstancesResponse instancesResponse, WebContext context) {
    JsonObject representation = new JsonObject();
    JsonArray results = new JsonArray();
    MultipleRecords<Instance> wrappedInstances = instancesResponse.getSuccess().getResult();
    List<Instance> instances = wrappedInstances.records;
    instances.forEach(instance -> {
        List<InstanceRelationshipToParent> parentInstances = instancesResponse.getParentInstanceMap().get(instance.getId());
        List<InstanceRelationshipToChild> childInstances = instancesResponse.getChildInstanceMap().get(instance.getId());
        List<PrecedingSucceedingTitle> precedingTitles = instancesResponse.getPrecedingTitlesMap().get(instance.getId());
        List<PrecedingSucceedingTitle> succeedingTitles = instancesResponse.getSucceedingTitlesMap().get(instance.getId());
        results.add(instance.setParentInstances(parentInstances).setChildInstances(childInstances).setPrecedingTitles(precedingTitles).setSucceedingTitles(succeedingTitles).getJsonForResponse(context));
    });
    representation.put("instances", results).put("totalRecords", wrappedInstances.totalRecords);
    return representation;
}
Also used : JsonArray(io.vertx.core.json.JsonArray) Instance(org.folio.inventory.domain.instances.Instance) PrecedingSucceedingTitle(org.folio.inventory.domain.instances.titles.PrecedingSucceedingTitle) JsonObject(io.vertx.core.json.JsonObject) InstanceRelationshipToChild(org.folio.inventory.domain.instances.InstanceRelationshipToChild) InstanceRelationshipToParent(org.folio.inventory.domain.instances.InstanceRelationshipToParent)

Example 3 with PrecedingSucceedingTitle

use of org.folio.inventory.domain.instances.titles.PrecedingSucceedingTitle 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 4 with PrecedingSucceedingTitle

use of org.folio.inventory.domain.instances.titles.PrecedingSucceedingTitle in project mod-inventory by folio-org.

the class AbstractInstances method updatePrecedingTitles.

private void updatePrecedingTitles(Instance instance, Map<String, PrecedingSucceedingTitle> updatingPrecedingSucceedingTitles) {
    if (instance.getPrecedingTitles() != null) {
        instance.getPrecedingTitles().forEach(parent -> {
            String id = (parent.id == null ? UUID.randomUUID().toString() : parent.id);
            PrecedingSucceedingTitle precedingSucceedingTitle = new PrecedingSucceedingTitle(id, parent.precedingInstanceId, instance.getId(), parent.title, parent.hrid, parent.identifiers);
            updatingPrecedingSucceedingTitles.put(id, precedingSucceedingTitle);
        });
    }
}
Also used : PrecedingSucceedingTitle(org.folio.inventory.domain.instances.titles.PrecedingSucceedingTitle)

Example 5 with PrecedingSucceedingTitle

use of org.folio.inventory.domain.instances.titles.PrecedingSucceedingTitle in project mod-inventory by folio-org.

the class AbstractInstances method updatePrecedingSucceedingTitles.

private CompletableFuture<List<Response>> updatePrecedingSucceedingTitles(Instance instance, CollectionResourceRepository precedingSucceedingTitlesClient, Response result) {
    JsonObject json = result.getJson();
    List<JsonObject> relationsList = JsonArrayHelper.toList(json.getJsonArray("precedingSucceedingTitles"));
    Map<String, PrecedingSucceedingTitle> existingPrecedingSucceedingTitles = getExistedPrecedingSucceedingTitles(relationsList);
    Map<String, PrecedingSucceedingTitle> updatingPrecedingSucceedingTitles = getUpdatingPrecedingSucceedingTitles(instance);
    List<CompletableFuture<Response>> allFutures = update(precedingSucceedingTitlesClient, existingPrecedingSucceedingTitles, updatingPrecedingSucceedingTitles);
    return allResultsOf(allFutures);
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) PrecedingSucceedingTitle(org.folio.inventory.domain.instances.titles.PrecedingSucceedingTitle) JsonObject(io.vertx.core.json.JsonObject)

Aggregations

PrecedingSucceedingTitle (org.folio.inventory.domain.instances.titles.PrecedingSucceedingTitle)8 JsonObject (io.vertx.core.json.JsonObject)4 Instance (org.folio.inventory.domain.instances.Instance)3 JsonArray (io.vertx.core.json.JsonArray)2 String.format (java.lang.String.format)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 InstanceRelationshipToChild (org.folio.inventory.domain.instances.InstanceRelationshipToChild)2 InstanceRelationshipToParent (org.folio.inventory.domain.instances.InstanceRelationshipToParent)2 CollectionResourceClient (org.folio.inventory.storage.external.CollectionResourceClient)2 COMMA (io.netty.util.internal.StringUtil.COMMA)1 Future (io.vertx.core.Future)1 Promise (io.vertx.core.Promise)1 HttpClient (io.vertx.core.http.HttpClient)1 Router (io.vertx.ext.web.Router)1 RoutingContext (io.vertx.ext.web.RoutingContext)1 WebClient (io.vertx.ext.web.client.WebClient)1