-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MODINVSTOR-1243] Implement endpoint to retrieve items from multiple …
…tenants
- Loading branch information
1 parent
05423ff
commit 6dc9293
Showing
7 changed files
with
174 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-04/schema#", | ||
"description": "Pair of item and tenant IDs", | ||
"type": "object", | ||
"properties": { | ||
"itemId": { | ||
"type": "string", | ||
"description": "Unique ID (UUID) of the item", | ||
"$ref": "../../mod-inventory-storage/ramls/uuid.json" | ||
}, | ||
"tenantId": { | ||
"type": "string", | ||
"description": "Unique ID of the tenant where the item is located" | ||
} | ||
}, | ||
"additionalProperties": false, | ||
"required": ["itemId", "tenantId"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-04/schema#", | ||
"description": "Collection of pairs of item and tenant IDs", | ||
"type": "object", | ||
"properties": { | ||
"itemTenantPairs": { | ||
"type": "array", | ||
"description": "Unique ID (UUID) of the item", | ||
"items": { | ||
"type": "object", | ||
"$ref": "tenantItemPair.json" | ||
} | ||
} | ||
}, | ||
"additionalProperties": false, | ||
"required": ["itemTenantPairs"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
src/main/java/org/folio/inventory/resources/TenantItems.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package org.folio.inventory.resources; | ||
|
||
import static java.lang.String.format; | ||
import static java.util.stream.Collectors.groupingBy; | ||
import static java.util.stream.Collectors.mapping; | ||
import static java.util.stream.Collectors.toList; | ||
import static org.folio.inventory.support.CqlHelper.multipleRecordsCqlQuery; | ||
|
||
import java.lang.invoke.MethodHandles; | ||
import java.net.MalformedURLException; | ||
import java.net.URI; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.folio.inventory.common.WebContext; | ||
import org.folio.inventory.storage.Storage; | ||
import org.folio.inventory.storage.external.CollectionResourceClient; | ||
import org.folio.inventory.support.JsonArrayHelper; | ||
import org.folio.inventory.support.http.client.OkapiHttpClient; | ||
import org.folio.inventory.support.http.client.Response; | ||
import org.folio.inventory.support.http.server.JsonResponse; | ||
import org.folio.inventory.support.http.server.ServerErrorResponse; | ||
|
||
import io.vertx.core.http.HttpClient; | ||
import io.vertx.core.json.JsonArray; | ||
import io.vertx.core.json.JsonObject; | ||
import io.vertx.ext.web.Router; | ||
import io.vertx.ext.web.RoutingContext; | ||
import io.vertx.ext.web.client.WebClient; | ||
|
||
public class TenantItems extends Items { | ||
|
||
private static final Logger log = LogManager.getLogger(MethodHandles.lookup().lookupClass()); | ||
|
||
private static final String TENANT_ITEMS_PATH = "/inventory/tenant-items"; | ||
private static final String TENANT_ITEM_PAIRS_FIELD = "itemTenantPairs"; | ||
private static final String ITEMS_FIELD = "items"; | ||
private static final String TOTAL_RECORDS_FIELD = "items"; | ||
private static final String ITEM_ID_FIELD = "itemId"; | ||
private static final String TENANT_ID_FIELD = "tenantId"; | ||
|
||
public TenantItems(final Storage storage, final HttpClient client) { | ||
super(storage, client); | ||
} | ||
|
||
@Override | ||
public void register(Router router) { | ||
router.post(TENANT_ITEMS_PATH).handler(this::getItemsFromTenants); | ||
} | ||
|
||
/** | ||
* This API is meant to be used by UI to fetch different items from several | ||
* tenants together within one call | ||
* | ||
*/ | ||
private void getItemsFromTenants(RoutingContext routingContext) { | ||
var getItemsFutures = JsonArrayHelper.toList(routingContext.body().asJsonObject(), TENANT_ITEM_PAIRS_FIELD).stream() | ||
.collect(groupingBy(json -> json.getString(TENANT_ID_FIELD), mapping(json -> json.getString(ITEM_ID_FIELD), toList()))) | ||
.entrySet().stream() | ||
.map(tenantToItems -> getItemsWithTenantId(tenantToItems.getKey(), tenantToItems.getValue(), routingContext)) | ||
.toList(); | ||
|
||
CompletableFuture.allOf(getItemsFutures.toArray(new CompletableFuture[0])) | ||
.thenApply(v -> getItemsFutures.stream() | ||
.map(CompletableFuture::join) | ||
.flatMap(List::stream) | ||
.collect(toList())) | ||
.thenApply(this::constructResponse) | ||
.thenAccept(jsonObject -> JsonResponse.success(routingContext.response(), jsonObject)); | ||
} | ||
|
||
private CompletableFuture<List<JsonObject>> getItemsWithTenantId(String tenantId, List<String> itemIds, RoutingContext routingContext) { | ||
log.info("getItemsWithTenantId:: Fetching items - [{}] from tenant - {}", itemIds, tenantId); | ||
var context = new WebContext(routingContext); | ||
CollectionResourceClient itemsStorageClient; | ||
try { | ||
OkapiHttpClient okapiClient = createHttpClient(tenantId, context, routingContext); | ||
itemsStorageClient = createItemsStorageClient(okapiClient, context); | ||
} | ||
catch (MalformedURLException e) { | ||
invalidOkapiUrlResponse(routingContext, context); | ||
return CompletableFuture.completedFuture(List.of()); | ||
} | ||
|
||
var getByIdsQuery = multipleRecordsCqlQuery(itemIds); | ||
var itemsFetched = new CompletableFuture<Response>(); | ||
itemsStorageClient.getAll(getByIdsQuery, itemsFetched::complete); | ||
|
||
return itemsFetched.thenApplyAsync(response -> | ||
response.getStatusCode() == 200 && response.hasBody() | ||
? JsonArrayHelper.toList(response.getJson(), ITEMS_FIELD) | ||
: List.of()); | ||
} | ||
|
||
private JsonObject constructResponse(List<JsonObject> items) { | ||
return JsonObject.of( | ||
ITEMS_FIELD, JsonArray.of(items.toArray()), | ||
TOTAL_RECORDS_FIELD, items.size() | ||
); | ||
} | ||
|
||
private OkapiHttpClient createHttpClient(String tenantId, WebContext context, | ||
RoutingContext routingContext) throws MalformedURLException { | ||
return new OkapiHttpClient(WebClient.wrap(client), | ||
URI.create(context.getOkapiLocation()).toURL(), | ||
Optional.ofNullable(tenantId).orElse(context.getTenantId()), | ||
context.getToken(), | ||
context.getUserId(), | ||
context.getRequestId(), | ||
exception -> ServerErrorResponse.internalError(routingContext.response(), | ||
format("Failed to contact storage module: %s", exception.toString()))); | ||
} | ||
|
||
} |