use of org.folio.circulation.resources.handlers.error.FailFastErrorHandler in project mod-circulation by folio-org.
the class RequestCollectionResource method replace.
@Override
void replace(RoutingContext routingContext) {
final var context = new WebContext(routingContext);
final var clients = Clients.create(context, client);
final var representation = routingContext.getBodyAsJson();
write(representation, "id", getRequestId(routingContext));
final var itemRepository = new ItemRepository(clients);
final var userRepository = new UserRepository(clients);
final var loanRepository = new LoanRepository(clients, itemRepository, userRepository);
final var requestRepository = RequestRepository.using(clients, itemRepository, userRepository, loanRepository);
final var requestQueueRepository = new RequestQueueRepository(requestRepository);
final var updateRequestQueue = UpdateRequestQueue.using(clients, requestRepository, requestQueueRepository);
final var loanPolicyRepository = new LoanPolicyRepository(clients);
final var eventPublisher = new EventPublisher(routingContext);
final var requestNoticeSender = createRequestNoticeSender(clients, representation);
final var configurationRepository = new ConfigurationRepository(clients);
final var updateItem = new UpdateItem(itemRepository);
final var updateUponRequest = new UpdateUponRequest(updateItem, new UpdateLoan(clients, loanRepository, loanPolicyRepository), updateRequestQueue);
final var errorHandler = new FailFastErrorHandler();
final var createRequestService = new CreateRequestService(new CreateRequestRepositories(requestRepository, new RequestPolicyRepository(clients), configurationRepository), updateUponRequest, new RequestLoanValidator(new ItemByInstanceIdFinder(clients.holdingsStorage(), itemRepository), loanRepository), requestNoticeSender, regularRequestBlockValidators(clients), eventPublisher, errorHandler);
final var updateRequestService = new UpdateRequestService(requestRepository, updateRequestQueue, new ClosedRequestValidator(requestRepository), requestNoticeSender, updateItem, eventPublisher);
final var requestFromRepresentationService = new RequestFromRepresentationService(new InstanceRepository(clients), itemRepository, new RequestQueueRepository(requestRepository), userRepository, loanRepository, new ServicePointRepository(clients), configurationRepository, createProxyRelationshipValidator(representation, clients), new ServicePointPickupLocationValidator(), errorHandler, new ItemByInstanceIdFinder(clients.holdingsStorage(), itemRepository));
final var requestScheduledNoticeService = RequestScheduledNoticeService.using(clients);
fromFutureResult(requestFromRepresentationService.getRequestFrom(Request.Operation.REPLACE, representation)).flatMapFuture(when(requestRepository::exists, updateRequestService::replaceRequest, createRequestService::createRequest)).flatMapFuture(records -> eventPublisher.publishDueDateChangedEvent(records, loanRepository)).map(requestScheduledNoticeService::rescheduleRequestNotices).map(toFixedValue(NoContentResponse::noContent)).onComplete(context::write, context::write);
}
use of org.folio.circulation.resources.handlers.error.FailFastErrorHandler in project mod-circulation by folio-org.
the class RequestByInstanceIdResource method placeRequests.
private CompletableFuture<Result<RequestAndRelatedRecords>> placeRequests(Clients clients, EventPublisher eventPublisher, ItemRepository itemRepository, LoanRepository loanRepository, RequestRepository requestRepository, RequestQueueRepository requestQueueRepository, UserRepository userRepository, ItemByInstanceIdFinder itemFinder, TlrSettingsConfiguration tlrConfig, List<JsonObject> requestRepresentations) {
final LoanPolicyRepository loanPolicyRepository = new LoanPolicyRepository(clients);
final ConfigurationRepository configurationRepository = new ConfigurationRepository(clients);
final UpdateUponRequest updateUponRequest = new UpdateUponRequest(new UpdateItem(itemRepository), new UpdateLoan(clients, loanRepository, loanPolicyRepository), UpdateRequestQueue.using(clients, requestRepository, requestQueueRepository));
final RequestNoticeSender requestNoticeSender = tlrConfig.isTitleLevelRequestsFeatureEnabled() ? new TitleLevelRequestNoticeSender(clients) : new ItemLevelRequestNoticeSender(clients);
final CreateRequestService createRequestService = new CreateRequestService(new CreateRequestRepositories(requestRepository, new RequestPolicyRepository(clients), configurationRepository), updateUponRequest, new RequestLoanValidator(itemFinder, loanRepository), requestNoticeSender, regularRequestBlockValidators(clients), eventPublisher, new FailFastErrorHandler());
return placeRequest(requestRepresentations, 0, createRequestService, clients, loanRepository, new ArrayList<>(), itemRepository, userRepository, requestQueueRepository);
}
use of org.folio.circulation.resources.handlers.error.FailFastErrorHandler in project mod-circulation by folio-org.
the class RequestByInstanceIdResource method placeRequest.
private CompletableFuture<Result<RequestAndRelatedRecords>> placeRequest(List<JsonObject> itemRequests, int startIndex, CreateRequestService createRequestService, Clients clients, LoanRepository loanRepository, List<String> errors, ItemRepository itemRepository, UserRepository userRepository, RequestQueueRepository requestQueueRepository) {
log.debug("RequestByInstanceIdResource.placeRequest, startIndex={}, itemRequestSize={}", startIndex, itemRequests.size());
if (startIndex >= itemRequests.size()) {
String aggregateFailures = String.format("%n%s", String.join("%n", errors));
return CompletableFuture.completedFuture(failedDueToServerError("Failed to place a request for the instance. Reasons: " + aggregateFailures));
}
JsonObject currentItemRequest = itemRequests.get(startIndex);
final RequestFromRepresentationService requestFromRepresentationService = new RequestFromRepresentationService(new InstanceRepository(clients), itemRepository, requestQueueRepository, userRepository, loanRepository, new ServicePointRepository(clients), new ConfigurationRepository(clients), createProxyRelationshipValidator(currentItemRequest, clients), new ServicePointPickupLocationValidator(), new FailFastErrorHandler(), new ItemByInstanceIdFinder(clients.holdingsStorage(), itemRepository));
return requestFromRepresentationService.getRequestFrom(Request.Operation.CREATE, currentItemRequest).thenCompose(r -> r.after(createRequestService::createRequest)).thenCompose(r -> {
if (r.succeeded()) {
log.debug("RequestByInstanceIdResource.placeRequest: succeeded creating request for item {}", currentItemRequest.getString(ITEM_ID));
return CompletableFuture.completedFuture(r);
} else {
String reason = getErrorMessage(r.cause());
errors.add(reason);
log.debug("Failed to create request for item {} with reason: {}", currentItemRequest.getString(ITEM_ID), reason);
return placeRequest(itemRequests, startIndex + 1, createRequestService, clients, loanRepository, errors, itemRepository, userRepository, requestQueueRepository);
}
});
}
Aggregations