use of org.folio.inventory.common.WebContext in project mod-inventory by folio-org.
the class ItemsByHoldingsRecordId method getBoundWithItems.
private void getBoundWithItems(RoutingContext routingContext) {
WebContext context = new WebContext(routingContext);
String queryByHoldingsRecordId = context.getStringParameter("query", null);
String relationsParam = context.getStringParameter("relations", null);
if (queryByHoldingsRecordId == null || !queryByHoldingsRecordId.contains("holdingsRecordId")) {
ClientErrorResponse.badRequest(routingContext.response(), "Items must be retrieved by 'holdingsRecordId' from this API. Query was: " + queryByHoldingsRecordId);
return;
}
if (relationsParam != null) {
if (!Arrays.asList(RELATION_PARAM_ONLY_BOUND_WITHS, RELATION_PARAM_ONLY_BOUND_WITHS_SKIP_DIRECTLY_LINKED_ITEM).contains(relationsParam)) {
ClientErrorResponse.badRequest(routingContext.response(), "The only valid values of the request parameter 'relations' are: '" + RELATION_PARAM_ONLY_BOUND_WITHS + "' and '" + RELATION_PARAM_ONLY_BOUND_WITHS_SKIP_DIRECTLY_LINKED_ITEM + "'");
return;
}
}
String[] keyVal = queryByHoldingsRecordId.replaceAll("[()]", "").split("[=]{1,2}");
if (keyVal.length != 2) {
ClientErrorResponse.badRequest(routingContext.response(), "Items must be retrieved by 'holdingsRecordId' from this API: query=holdingsRecordId==[a UUID]. Query was: " + queryByHoldingsRecordId);
return;
}
String holdingsRecordId = keyVal[1];
CollectionResourceClient itemsClient = getCollectionResourceRepository(routingContext, context, ITEM_STORAGE_PATH);
itemsClient.getMany("holdingsRecordId==" + holdingsRecordId, 1000, 0, response -> {
List<String> holdingsRecordsItemIds = response.getJson().getJsonArray("items").stream().map(item -> ((JsonObject) item).getString("id")).collect(Collectors.toList());
String queryByHoldingsRecordsItemIds = "";
if (holdingsRecordsItemIds.size() > 0) {
queryByHoldingsRecordsItemIds += " or " + buildQueryByItemIds(holdingsRecordsItemIds);
}
CollectionResourceClient boundWithPartsClient = getCollectionResourceRepository(routingContext, context, BOUND_WITH_PARTS_STORAGE_PATH);
boundWithPartsClient.getMany(queryByHoldingsRecordId + queryByHoldingsRecordsItemIds, 1000, 0, response2 -> {
joinAndRespondWithManyItems(routingContext, context, response2, holdingsRecordId, relationsParam);
});
});
}
use of org.folio.inventory.common.WebContext in project mod-inventory by folio-org.
the class MoveApi method moveHoldings.
private void moveHoldings(RoutingContext routingContext) {
WebContext context = new WebContext(routingContext);
JsonObject holdingsMoveJsonRequest = routingContext.getBodyAsJson();
Optional<ValidationError> validationError = holdingsMoveHasRequiredFields(holdingsMoveJsonRequest);
if (validationError.isPresent()) {
unprocessableEntity(routingContext.response(), validationError.get());
return;
}
String toInstanceId = holdingsMoveJsonRequest.getString(TO_INSTANCE_ID);
List<String> holdingsRecordsIdsToUpdate = toListOfStrings(holdingsMoveJsonRequest.getJsonArray(HOLDINGS_RECORD_IDS));
storage.getInstanceCollection(context).findById(toInstanceId).thenAccept(instance -> {
if (instance == null) {
JsonResponse.unprocessableEntity(routingContext.response(), String.format("Instance with id=%s not found", toInstanceId));
return;
}
try {
CollectionResourceClient holdingsStorageClient = createHoldingsStorageClient(createHttpClient(routingContext, context), context);
MultipleRecordsFetchClient holdingsRecordFetchClient = createHoldingsRecordsFetchClient(holdingsStorageClient);
holdingsRecordFetchClient.find(holdingsRecordsIdsToUpdate, this::fetchByIdCql).thenAccept(jsons -> {
List<HoldingsRecord> holdingsRecordsToUpdate = updateInstanceIdForHoldings(toInstanceId, jsons);
updateHoldings(routingContext, context, holdingsRecordsIdsToUpdate, holdingsRecordsToUpdate);
}).exceptionally(e -> {
ServerErrorResponse.internalError(routingContext.response(), e);
return null;
});
} catch (Exception e) {
ServerErrorResponse.internalError(routingContext.response(), e);
}
}).exceptionally(e -> {
ServerErrorResponse.internalError(routingContext.response(), e);
return null;
});
}
use of org.folio.inventory.common.WebContext in project mod-inventory by folio-org.
the class TenantApi method delete.
public void delete(RoutingContext routingContext) {
WebContext context = new WebContext(routingContext);
deleteSchemaForTenant(context.getTenantId()).onSuccess(result -> routingContext.response().setStatusCode(HttpStatus.HTTP_NO_CONTENT.toInt()).end()).onFailure(fail -> routingContext.response().setStatusCode(HttpStatus.HTTP_INTERNAL_SERVER_ERROR.toInt()).end(fail.toString()));
}
use of org.folio.inventory.common.WebContext in project mod-inventory by folio-org.
the class TenantApi method create.
public void create(RoutingContext routingContext) {
WebContext context = new WebContext(routingContext);
initializeSchemaForTenant(context.getTenantId()).onSuccess(result -> routingContext.response().setStatusCode(HttpStatus.HTTP_NO_CONTENT.toInt()).end()).onFailure(fail -> routingContext.response().setStatusCode(HttpStatus.HTTP_INTERNAL_SERVER_ERROR.toInt()).end(fail.toString()));
}
use of org.folio.inventory.common.WebContext in project mod-inventory by folio-org.
the class Instances method create.
private void create(RoutingContext routingContext) {
WebContext context = new WebContext(routingContext);
JsonObject instanceRequest = routingContext.getBodyAsJson();
if (StringUtils.isBlank(instanceRequest.getString(Instance.TITLE_KEY))) {
ClientErrorResponse.badRequest(routingContext.response(), "Title must be provided for an instance");
return;
}
Instance newInstance = Instance.fromJson(instanceRequest);
completedFuture(newInstance).thenCompose(InstancePrecedingSucceedingTitleValidators::refuseWhenUnconnectedHasNoTitle).thenCompose(instance -> storage.getInstanceCollection(context).add(instance)).thenCompose(response -> {
response.setParentInstances(newInstance.getParentInstances());
response.setChildInstances(newInstance.getChildInstances());
response.setPrecedingTitles(newInstance.getPrecedingTitles());
response.setSucceedingTitles(newInstance.getSucceedingTitles());
return updateRelatedRecords(routingContext, context, response).thenApply(notUsed -> response);
}).thenAccept(response -> {
try {
URL url = context.absoluteUrl(format("%s/%s", INSTANCES_PATH, response.getId()));
RedirectResponse.created(routingContext.response(), url.toString());
} catch (MalformedURLException e) {
log.warn(format("Failed to create self link for instance: %s", e.toString()));
}
}).exceptionally(doExceptionally(routingContext));
}
Aggregations