use of org.folio.inventory.common.WebContext in project mod-inventory by folio-org.
the class Items method deleteAll.
private void deleteAll(RoutingContext routingContext) {
WebContext context = new WebContext(routingContext);
storage.getItemCollection(context).empty(v -> SuccessResponse.noContent(routingContext.response()), FailureResponseConsumer.serverError(routingContext.response()));
}
use of org.folio.inventory.common.WebContext in project mod-inventory by folio-org.
the class Items method update.
private void update(RoutingContext routingContext) {
WebContext context = new WebContext(routingContext);
JsonObject itemRequest = routingContext.getBodyAsJson();
Optional<ValidationError> validationError = itemHasCorrectStatus(itemRequest);
if (validationError.isPresent()) {
unprocessableEntity(routingContext.response(), validationError.get());
return;
}
Item newItem = ItemUtil.jsonToItem(itemRequest);
ItemCollection itemCollection = storage.getItemCollection(context);
UserCollection userCollection = storage.getUserCollection(context);
final String itemId = routingContext.request().getParam("id");
final CompletableFuture<Success<Item>> getItemFuture = new CompletableFuture<>();
itemCollection.findById(itemId, getItemFuture::complete, FailureResponseConsumer.serverError(routingContext.response()));
getItemFuture.thenApply(Success::getResult).thenCompose(ItemsValidator::refuseWhenItemNotFound).thenCompose(oldItem -> hridChanged(oldItem, newItem)).thenCompose(oldItem -> claimedReturnedMarkedAsMissing(oldItem, newItem)).thenAccept(oldItem -> {
if (hasSameBarcode(newItem, oldItem)) {
findUserAndUpdateItem(routingContext, newItem, oldItem, userCollection, itemCollection);
} else {
try {
checkForNonUniqueBarcode(routingContext, newItem, oldItem, itemCollection, userCollection);
} catch (UnsupportedEncodingException e) {
ServerErrorResponse.internalError(routingContext.response(), e.toString());
}
}
}).exceptionally(doExceptionally(routingContext));
}
use of org.folio.inventory.common.WebContext in project mod-inventory by folio-org.
the class Items method create.
private void create(RoutingContext routingContext) {
WebContext context = new WebContext(routingContext);
JsonObject item = routingContext.getBodyAsJson();
Optional<ValidationError> validationError = itemHasCorrectStatus(item);
if (validationError.isPresent()) {
unprocessableEntity(routingContext.response(), validationError.get());
return;
}
Item newItem = ItemUtil.jsonToItem(item);
ItemCollection itemCollection = storage.getItemCollection(context);
UserCollection userCollection = storage.getUserCollection(context);
if (newItem.getBarcode() != null) {
try {
itemCollection.findByCql(CqlHelper.barcodeIs(newItem.getBarcode()), PagingParameters.defaults(), findResult -> {
if (findResult.getResult().records.isEmpty()) {
findUserAndAddItem(routingContext, context, newItem, userCollection, itemCollection);
} else {
ClientErrorResponse.badRequest(routingContext.response(), String.format("Barcode must be unique, %s is already assigned to another item", newItem.getBarcode()));
}
}, FailureResponseConsumer.serverError(routingContext.response()));
} catch (UnsupportedEncodingException e) {
ServerErrorResponse.internalError(routingContext.response(), e.toString());
}
} else {
findUserAndAddItem(routingContext, context, newItem, userCollection, itemCollection);
}
}
use of org.folio.inventory.common.WebContext in project mod-inventory by folio-org.
the class Items 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.getItemCollection(context).findAll(pagingParameters, success -> respondWithManyItems(routingContext, context, success.getResult()), FailureResponseConsumer.serverError(routingContext.response()));
} else {
try {
storage.getItemCollection(context).findByCql(search, pagingParameters, success -> respondWithManyItems(routingContext, context, success.getResult()), FailureResponseConsumer.serverError(routingContext.response()));
} catch (UnsupportedEncodingException e) {
ServerErrorResponse.internalError(routingContext.response(), e.toString());
}
}
}
use of org.folio.inventory.common.WebContext in project mod-inventory by folio-org.
the class Items method respondWithManyItems.
protected void respondWithManyItems(RoutingContext routingContext, WebContext context, MultipleRecords<Item> wrappedItems) {
CollectionResourceClient holdingsClient;
CollectionResourceClient instancesClient;
CollectionResourceClient materialTypesClient;
CollectionResourceClient loanTypesClient;
CollectionResourceClient locationsClient;
CollectionResourceClient boundWithPartsClient;
try {
OkapiHttpClient okapiClient = createHttpClient(routingContext, context);
holdingsClient = createHoldingsClient(okapiClient, context);
instancesClient = createInstancesClient(okapiClient, context);
materialTypesClient = createMaterialTypesClient(okapiClient, context);
loanTypesClient = createLoanTypesClient(okapiClient, context);
locationsClient = createLocationsClient(okapiClient, context);
boundWithPartsClient = createBoundWithPartsClient(okapiClient, context);
} catch (MalformedURLException e) {
invalidOkapiUrlResponse(routingContext, context);
return;
}
ArrayList<CompletableFuture<Response>> allMaterialTypeFutures = new ArrayList<>();
ArrayList<CompletableFuture<Response>> allLoanTypeFutures = new ArrayList<>();
ArrayList<CompletableFuture<Response>> allLocationsFutures = new ArrayList<>();
ArrayList<CompletableFuture<Response>> allFutures = new ArrayList<>();
List<String> holdingsIds = wrappedItems.records.stream().map(Item::getHoldingId).filter(Objects::nonNull).distinct().collect(Collectors.toList());
CompletableFuture<Response> holdingsFetched = new CompletableFuture<>();
String holdingsQuery = multipleRecordsCqlQuery(holdingsIds);
holdingsClient.getMany(holdingsQuery, holdingsIds.size(), 0, holdingsFetched::complete);
holdingsFetched.thenAccept(holdingsResponse -> {
if (holdingsResponse.getStatusCode() != 200) {
ServerErrorResponse.internalError(routingContext.response(), String.format("Holdings request (%s) failed %s: %s", holdingsQuery, holdingsResponse.getStatusCode(), holdingsResponse.getBody()));
}
final List<JsonObject> holdings = JsonArrayHelper.toList(holdingsResponse.getJson().getJsonArray("holdingsRecords"));
List<String> instanceIds = holdings.stream().map(holding -> holding.getString("instanceId")).filter(Objects::nonNull).distinct().collect(Collectors.toList());
CompletableFuture<Response> instancesFetched = new CompletableFuture<>();
String instancesQuery = multipleRecordsCqlQuery(instanceIds);
instancesClient.getMany(instancesQuery, instanceIds.size(), 0, instancesFetched::complete);
instancesFetched.thenAccept(instancesResponse -> {
if (instancesResponse.getStatusCode() != 200) {
ServerErrorResponse.internalError(routingContext.response(), String.format("Instances request (%s) failed %s: %s", instancesQuery, instancesResponse.getStatusCode(), instancesResponse.getBody()));
}
final List<JsonObject> instances = JsonArrayHelper.toList(instancesResponse.getJson().getJsonArray("instances"));
List<String> materialTypeIds = wrappedItems.records.stream().map(Item::getMaterialTypeId).filter(Objects::nonNull).distinct().collect(Collectors.toList());
materialTypeIds.forEach(id -> {
CompletableFuture<Response> newFuture = new CompletableFuture<>();
allFutures.add(newFuture);
allMaterialTypeFutures.add(newFuture);
materialTypesClient.get(id, newFuture::complete);
});
List<String> permanentLoanTypeIds = wrappedItems.records.stream().map(Item::getPermanentLoanTypeId).filter(Objects::nonNull).distinct().collect(Collectors.toList());
List<String> temporaryLoanTypeIds = wrappedItems.records.stream().map(Item::getTemporaryLoanTypeId).filter(Objects::nonNull).distinct().collect(Collectors.toList());
Stream.concat(permanentLoanTypeIds.stream(), temporaryLoanTypeIds.stream()).distinct().forEach(id -> {
CompletableFuture<Response> newFuture = new CompletableFuture<>();
allFutures.add(newFuture);
allLoanTypeFutures.add(newFuture);
loanTypesClient.get(id, newFuture::complete);
});
List<String> effectiveLocationIds = wrappedItems.records.stream().map(Item::getEffectiveLocationId).filter(Objects::nonNull).distinct().collect(Collectors.toList());
List<String> permanentLocationIds = wrappedItems.records.stream().map(Item::getPermanentLocationId).filter(Objects::nonNull).distinct().collect(Collectors.toList());
List<String> temporaryLocationIds = wrappedItems.records.stream().map(Item::getTemporaryLocationId).filter(Objects::nonNull).distinct().collect(Collectors.toList());
Stream.concat(Stream.concat(permanentLocationIds.stream(), temporaryLocationIds.stream()), effectiveLocationIds.stream()).distinct().forEach(id -> {
CompletableFuture<Response> newFuture = new CompletableFuture<>();
allFutures.add(newFuture);
allLocationsFutures.add(newFuture);
locationsClient.get(id, newFuture::complete);
});
CompletableFuture<Response> boundWithPartsFuture = getBoundWithPartsForMultipleItemsFuture(wrappedItems, boundWithPartsClient);
allFutures.add(boundWithPartsFuture);
CompletableFuture<Void> allDoneFuture = allOf(allFutures);
allDoneFuture.thenAccept(v -> {
log.info("GET all items: all futures completed");
try {
Map<String, JsonObject> foundMaterialTypes = allMaterialTypeFutures.stream().map(CompletableFuture::join).filter(response -> response.getStatusCode() == 200).map(Response::getJson).collect(Collectors.toMap(r -> r.getString("id"), r -> r));
Map<String, JsonObject> foundLoanTypes = allLoanTypeFutures.stream().map(CompletableFuture::join).filter(response -> response.getStatusCode() == 200).map(Response::getJson).collect(Collectors.toMap(r -> r.getString("id"), r -> r));
Map<String, JsonObject> foundLocations = allLocationsFutures.stream().map(CompletableFuture::join).filter(response -> response.getStatusCode() == 200).map(Response::getJson).collect(Collectors.toMap(r -> r.getString("id"), r -> r));
setBoundWithFlagsOnItems(wrappedItems, boundWithPartsFuture);
JsonResponse.success(routingContext.response(), new ItemRepresentation(RELATIVE_ITEMS_PATH).toJson(wrappedItems, holdings, instances, foundMaterialTypes, foundLoanTypes, foundLocations, context));
} catch (Exception e) {
ServerErrorResponse.internalError(routingContext.response(), e.toString());
}
});
});
});
}
Aggregations