Search in sources :

Example 1 with PagingParameters

use of org.folio.inventory.common.api.request.PagingParameters 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 PagingParameters

use of org.folio.inventory.common.api.request.PagingParameters in project mod-inventory by folio-org.

the class ExternalItemCollectionExamples method allItemsCanBePaged.

@Test
public void allItemsCanBePaged() throws InterruptedException, ExecutionException, TimeoutException {
    WaitForAllFutures<Item> allAdded = new WaitForAllFutures<>();
    collection.add(smallAngryPlanet, allAdded.notifySuccess(), v -> {
    });
    collection.add(nod, allAdded.notifySuccess(), v -> {
    });
    collection.add(uprooted, allAdded.notifySuccess(), v -> {
    });
    collection.add(temeraire, allAdded.notifySuccess(), v -> {
    });
    collection.add(interestingTimes, allAdded.notifySuccess(), v -> {
    });
    allAdded.waitForCompletion();
    CompletableFuture<MultipleRecords<Item>> firstPageFuture = new CompletableFuture<>();
    CompletableFuture<MultipleRecords<Item>> secondPageFuture = new CompletableFuture<>();
    collection.findAll(new PagingParameters(3, 0), succeed(firstPageFuture), fail(secondPageFuture));
    collection.findAll(new PagingParameters(3, 3), succeed(secondPageFuture), fail(secondPageFuture));
    MultipleRecords<Item> firstPage = getOnCompletion(firstPageFuture);
    MultipleRecords<Item> secondPage = getOnCompletion(secondPageFuture);
    assertThat(firstPage.records.size(), is(3));
    assertThat(secondPage.records.size(), is(2));
    assertThat(firstPage.totalRecords, is(5));
    assertThat(secondPage.totalRecords, is(5));
}
Also used : Item(org.folio.inventory.domain.items.Item) CompletableFuture(java.util.concurrent.CompletableFuture) PagingParameters(org.folio.inventory.common.api.request.PagingParameters) WaitForAllFutures(org.folio.inventory.common.WaitForAllFutures) MultipleRecords(org.folio.inventory.common.domain.MultipleRecords) Test(org.junit.Test)

Example 3 with PagingParameters

use of org.folio.inventory.common.api.request.PagingParameters in project mod-inventory by folio-org.

the class ExternalItemCollectionExamples method itemsCanBeFoundByBarcode.

@Test
public void itemsCanBeFoundByBarcode() throws InterruptedException, ExecutionException, TimeoutException, UnsupportedEncodingException {
    CompletableFuture<Item> firstAddFuture = new CompletableFuture<>();
    CompletableFuture<Item> secondAddFuture = new CompletableFuture<>();
    CompletableFuture<Item> thirdAddFuture = new CompletableFuture<>();
    collection.add(smallAngryPlanet, succeed(firstAddFuture), fail(firstAddFuture));
    collection.add(nod, succeed(secondAddFuture), fail(secondAddFuture));
    collection.add(uprooted, succeed(thirdAddFuture), fail(thirdAddFuture));
    CompletableFuture<Void> allAddsFuture = CompletableFuture.allOf(firstAddFuture, secondAddFuture, thirdAddFuture);
    getOnCompletion(allAddsFuture);
    Item addedSmallAngryPlanet = getOnCompletion(firstAddFuture);
    CompletableFuture<MultipleRecords<Item>> findFuture = new CompletableFuture<>();
    collection.findByCql("barcode==036000291452", new PagingParameters(10, 0), succeed(findFuture), fail(findFuture));
    MultipleRecords<Item> wrappedItems = getOnCompletion(findFuture);
    assertThat(wrappedItems.records.size(), is(1));
    assertThat(wrappedItems.totalRecords, is(1));
    assertThat(wrappedItems.records.stream().findFirst().get().id, is(addedSmallAngryPlanet.id));
}
Also used : Item(org.folio.inventory.domain.items.Item) CompletableFuture(java.util.concurrent.CompletableFuture) PagingParameters(org.folio.inventory.common.api.request.PagingParameters) MultipleRecords(org.folio.inventory.common.domain.MultipleRecords) Test(org.junit.Test)

Example 4 with PagingParameters

use of org.folio.inventory.common.api.request.PagingParameters in project mod-inventory by folio-org.

the class ExternalInstanceCollectionFailureExamples method serverErrorWhenFindingItemsTriggersFailureCallback.

@Test
public void serverErrorWhenFindingItemsTriggersFailureCallback() throws InterruptedException, ExecutionException, TimeoutException, UnsupportedEncodingException {
    InstanceCollection collection = createCollection();
    CompletableFuture<Failure> failureCalled = new CompletableFuture<>();
    collection.findByCql("title=\"*Small Angry*\"", new PagingParameters(10, 0), success -> fail("Completion callback should not be called"), failureCalled::complete);
    Failure failure = failureCalled.get(1000, TimeUnit.MILLISECONDS);
    check(failure);
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) PagingParameters(org.folio.inventory.common.api.request.PagingParameters) InstanceCollection(org.folio.inventory.domain.instances.InstanceCollection) Failure(org.folio.inventory.common.domain.Failure) Test(org.junit.Test)

Example 5 with PagingParameters

use of org.folio.inventory.common.api.request.PagingParameters in project mod-inventory by folio-org.

the class Instances method getAll.

private void getAll(RoutingContext routingContext) {
    WebContext context = new WebContext(routingContext);
    String search = context.getStringParameter("query", null);
    PagingParameters pagingParameters = PagingParameters.from(context);
    if (pagingParameters == null) {
        ClientErrorResponse.badRequest(routingContext.response(), "limit and offset must be numeric when supplied");
        return;
    }
    if (search == null) {
        storage.getInstanceCollection(context).findAll(pagingParameters, (Success<MultipleRecords<Instance>> success) -> makeInstancesResponse(success, routingContext, context), FailureResponseConsumer.serverError(routingContext.response()));
    } else {
        try {
            storage.getInstanceCollection(context).findByCql(search, pagingParameters, success -> makeInstancesResponse(success, routingContext, context), FailureResponseConsumer.serverError(routingContext.response()));
        } catch (UnsupportedEncodingException e) {
            ServerErrorResponse.internalError(routingContext.response(), e.toString());
        }
    }
}
Also used : PagingParameters(org.folio.inventory.common.api.request.PagingParameters) WebContext(org.folio.inventory.common.WebContext) Instance(org.folio.inventory.domain.instances.Instance) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Success(org.folio.inventory.common.domain.Success)

Aggregations

PagingParameters (org.folio.inventory.common.api.request.PagingParameters)10 CompletableFuture (java.util.concurrent.CompletableFuture)7 Test (org.junit.Test)7 MultipleRecords (org.folio.inventory.common.domain.MultipleRecords)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 WebContext (org.folio.inventory.common.WebContext)3 Failure (org.folio.inventory.common.domain.Failure)3 Instance (org.folio.inventory.domain.instances.Instance)3 WaitForAllFutures (org.folio.inventory.common.WaitForAllFutures)2 Item (org.folio.inventory.domain.items.Item)2 HttpClient (io.vertx.core.http.HttpClient)1 JsonArray (io.vertx.core.json.JsonArray)1 JsonObject (io.vertx.core.json.JsonObject)1 Router (io.vertx.ext.web.Router)1 RoutingContext (io.vertx.ext.web.RoutingContext)1 WebClient (io.vertx.ext.web.client.WebClient)1 String.format (java.lang.String.format)1 MethodHandles (java.lang.invoke.MethodHandles)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1