use of org.folio.circulation.domain.Item in project mod-circulation by folio-org.
the class RequestByInstanceIdResourceTests method getItems.
private static List<Item> getItems(int totalItems, UUID loanTypeId) {
LinkedList<Item> items = new LinkedList<>();
for (int i = 0; i < totalItems; i++) {
JsonObject itemJsonObject = ItemExamples.basedUponSmallAngryPlanet(UUID.randomUUID(), loanTypeId).create();
items.add(Item.from(itemJsonObject));
}
return items;
}
use of org.folio.circulation.domain.Item in project mod-circulation by folio-org.
the class RequestByInstanceIdResource method getLoanItems.
private CompletableFuture<Result<Map<Item, ZonedDateTime>>> getLoanItems(InstanceRequestRelatedRecords instanceRequestPackage, LoanRepository loanRepository) {
final List<Item> unsortedUnavailableItems = instanceRequestPackage.getUnsortedUnavailableItems();
if (unsortedUnavailableItems == null || unsortedUnavailableItems.isEmpty()) {
return CompletableFuture.completedFuture(succeeded(null));
}
Map<Item, CompletableFuture<Result<Loan>>> itemLoanFuturesMap = new HashMap<>();
// Find request queues and loan items for each item
for (Item item : unsortedUnavailableItems) {
itemLoanFuturesMap.put(item, loanRepository.findOpenLoanForItem(item));
}
final Collection<CompletableFuture<Result<Loan>>> loanFutures = itemLoanFuturesMap.values();
return CompletableFuture.allOf(loanFutures.toArray(new CompletableFuture[0])).thenApply(dd -> {
Map<Item, ZonedDateTime> itemDueDateMap = new HashMap<>();
List<Item> itemsWithoutLoansList = new ArrayList<>();
for (Map.Entry<Item, CompletableFuture<Result<Loan>>> entry : itemLoanFuturesMap.entrySet()) {
Result<Loan> aLoanResult = entry.getValue().join();
if (aLoanResult.succeeded() && aLoanResult.value() != null) {
itemDueDateMap.put(aLoanResult.value().getItem(), aLoanResult.value().getDueDate());
} else {
itemsWithoutLoansList.add(entry.getKey());
}
}
instanceRequestPackage.setItemsWithoutLoans(itemsWithoutLoansList);
return succeeded(itemDueDateMap);
});
}
use of org.folio.circulation.domain.Item in project mod-circulation by folio-org.
the class RequestByInstanceIdResource method instanceToItemRequests.
static Result<List<JsonObject>> instanceToItemRequests(InstanceRequestRelatedRecords requestRecords) {
final RequestByInstanceIdRequest requestByInstanceIdRequest = requestRecords.getInstanceLevelRequest();
final List<Item> combinedItems = requestRecords.getCombinedSortedItemsList();
if (combinedItems == null || combinedItems.isEmpty()) {
return failedValidation("Cannot create request objects when items list is null or empty", "items", "null");
}
LinkedList<JsonObject> requests = new LinkedList<>();
for (Item item : combinedItems) {
for (RequestType requestType : ORDERED_REQUEST_TYPES) {
JsonObject requestBody = new JsonObject();
write(requestBody, ITEM_ID, item.getItemId());
write(requestBody, "requestDate", requestByInstanceIdRequest.getRequestDate());
write(requestBody, REQUESTER_ID, requestByInstanceIdRequest.getRequesterId().toString());
write(requestBody, "pickupServicePointId", requestByInstanceIdRequest.getPickupServicePointId().toString());
write(requestBody, "fulfilmentPreference", DEFAULT_FULFILMENT_PREFERENCE.getValue());
write(requestBody, "requestType", requestType.getValue());
if (requestByInstanceIdRequest.getRequestExpirationDate() != null) {
write(requestBody, "requestExpirationDate", requestByInstanceIdRequest.getRequestExpirationDate());
}
write(requestBody, "patronComments", requestByInstanceIdRequest.getPatronComments());
write(requestBody, "instanceId", requestByInstanceIdRequest.getInstanceId());
write(requestBody, "holdingsRecordId", item.getHoldingsRecordId());
write(requestBody, "requestLevel", requestByInstanceIdRequest.getRequestLevel());
requests.add(requestBody);
}
}
return succeeded(requests);
}
use of org.folio.circulation.domain.Item 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.Item in project mod-circulation by folio-org.
the class RequestFromRepresentationService method validateRequestLevel.
private Result<Request> validateRequestLevel(Request request) {
JsonObject representation = request.getRequestRepresentation();
String requestLevelRaw = representation.getString(REQUEST_LEVEL);
RequestLevel requestLevel = RequestLevel.from(requestLevelRaw);
boolean tlrEnabled = request.getTlrSettingsConfiguration().isTitleLevelRequestsFeatureEnabled();
List<RequestLevel> allowedStatuses = tlrEnabled ? List.of(ITEM, TITLE) : List.of(ITEM);
if (!allowedStatuses.contains(requestLevel)) {
String allowedStatusesJoined = allowedStatuses.stream().map(existingLevel -> StringUtils.wrap(existingLevel.getValue(), '"')).collect(Collectors.joining(", "));
return failedValidation("requestLevel must be one of the following: " + allowedStatusesJoined, "requestLevel", requestLevelRaw);
}
return succeeded(request);
}
Aggregations