use of org.folio.circulation.domain.representations.RequestProperties.ITEM_ID in project mod-circulation by folio-org.
the class RequestByInstanceIdResource method validateRequester.
private Result<Collection<Item>> validateRequester(Map<Item, RequestQueue> itemRequestQueueMap, InstanceRequestRelatedRecords requestPackage) {
if (!itemRequestQueueMap.isEmpty()) {
Collection<RequestQueue> requestQueues = itemRequestQueueMap.values();
String requesterId = requestPackage.getInstanceLevelRequest().getRequesterId().toString();
for (RequestQueue queue : requestQueues) {
final Optional<Request> matchingRequest = queue.getRequests().stream().filter(request -> request.isOpen() && Objects.equals(request.getUserId(), requesterId)).findFirst();
if (matchingRequest.isPresent()) {
Map<String, String> parameters = new HashMap<>();
parameters.put(REQUESTER_ID, requesterId);
parameters.put(ITEM_ID, matchingRequest.get().getItemId());
parameters.put("instanceId", requestPackage.getInstanceLevelRequest().getInstanceId().toString());
String message = "This requester already has an open request for an item of this instance";
return failedValidation(new ValidationError(message, parameters));
}
}
}
return of(itemRequestQueueMap::keySet);
}
use of org.folio.circulation.domain.representations.RequestProperties.ITEM_ID 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