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);
}
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);
}
}
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);
}
});
}
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()));
}
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);
}
Aggregations