Search in sources :

Example 1 with Item

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;
}
Also used : Item(org.folio.circulation.domain.Item) JsonObject(io.vertx.core.json.JsonObject) LinkedList(java.util.LinkedList)

Example 2 with Item

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);
    });
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) UpdateItem(org.folio.circulation.domain.UpdateItem) Item(org.folio.circulation.domain.Item) CompletableFuture(java.util.concurrent.CompletableFuture) Loan(org.folio.circulation.domain.Loan) UpdateLoan(org.folio.circulation.domain.UpdateLoan) ZonedDateTime(java.time.ZonedDateTime) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with Item

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);
}
Also used : RequestByInstanceIdRequest(org.folio.circulation.domain.representations.RequestByInstanceIdRequest) UpdateItem(org.folio.circulation.domain.UpdateItem) Item(org.folio.circulation.domain.Item) JsonObject(io.vertx.core.json.JsonObject) LinkedList(java.util.LinkedList) RequestType(org.folio.circulation.domain.RequestType)

Example 4 with Item

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);
}
Also used : HOLD(org.folio.circulation.domain.RequestType.HOLD) UserRepository(org.folio.circulation.infrastructure.storage.users.UserRepository) UpdateRequestQueue(org.folio.circulation.domain.UpdateRequestQueue) CreateRequestService(org.folio.circulation.domain.CreateRequestService) LoanRepository(org.folio.circulation.infrastructure.storage.loans.LoanRepository) ConfigurationRepository(org.folio.circulation.infrastructure.storage.ConfigurationRepository) InstanceRequestRelatedRecords(org.folio.circulation.domain.InstanceRequestRelatedRecords) ZonedDateTime(java.time.ZonedDateTime) RequestQueueRepository(org.folio.circulation.infrastructure.storage.requests.RequestQueueRepository) LoanPolicyRepository(org.folio.circulation.infrastructure.storage.loans.LoanPolicyRepository) ServerErrorResponse(org.folio.circulation.support.http.server.ServerErrorResponse) Router(io.vertx.ext.web.Router) ValidationErrorFailure.singleValidationError(org.folio.circulation.support.ValidationErrorFailure.singleValidationError) UpdateItem(org.folio.circulation.domain.UpdateItem) RoutingContext(io.vertx.ext.web.RoutingContext) JsonPropertyWriter.write(org.folio.circulation.support.json.JsonPropertyWriter.write) Item(org.folio.circulation.domain.Item) REQUEST_LEVEL(org.folio.circulation.domain.representations.RequestProperties.REQUEST_LEVEL) ServicePointRepository(org.folio.circulation.infrastructure.storage.ServicePointRepository) HttpFailure(org.folio.circulation.support.HttpFailure) Map(java.util.Map) CommonFailures.failedDueToServerError(org.folio.circulation.support.results.CommonFailures.failedDueToServerError) JsonObject(io.vertx.core.json.JsonObject) ItemRepository(org.folio.circulation.infrastructure.storage.inventory.ItemRepository) RequestRepresentation(org.folio.circulation.domain.RequestRepresentation) RequestBlockValidators.regularRequestBlockValidators(org.folio.circulation.resources.RequestBlockValidators.regularRequestBlockValidators) Result.of(org.folio.circulation.support.results.Result.of) Result.succeeded(org.folio.circulation.support.results.Result.succeeded) MethodHandles(java.lang.invoke.MethodHandles) Collection(java.util.Collection) Loan(org.folio.circulation.domain.Loan) RequestLoanValidator(org.folio.circulation.domain.validation.RequestLoanValidator) RequestPolicyRepository(org.folio.circulation.infrastructure.storage.requests.RequestPolicyRepository) UUID(java.util.UUID) ValidationError(org.folio.circulation.support.http.server.ValidationError) Result(org.folio.circulation.support.results.Result) Collectors(java.util.stream.Collectors) EventPublisher(org.folio.circulation.services.EventPublisher) Objects(java.util.Objects) PROXY_USER_ID(org.folio.circulation.domain.representations.RequestProperties.PROXY_USER_ID) ServicePointPickupLocationValidator(org.folio.circulation.domain.validation.ServicePointPickupLocationValidator) RouteRegistration(org.folio.circulation.support.RouteRegistration) List(java.util.List) PAGE(org.folio.circulation.domain.RequestType.PAGE) Logger(org.apache.logging.log4j.Logger) RECALL(org.folio.circulation.domain.RequestType.RECALL) HOLD_SHELF(org.folio.circulation.domain.RequestFulfilmentPreference.HOLD_SHELF) InstanceRepository(org.folio.circulation.infrastructure.storage.inventory.InstanceRepository) REQUEST_TYPE(org.folio.circulation.domain.representations.RequestProperties.REQUEST_TYPE) Optional(java.util.Optional) BadRequestFailure(org.folio.circulation.support.BadRequestFailure) FULFILMENT_PREFERENCE(org.folio.circulation.domain.representations.RequestProperties.FULFILMENT_PREFERENCE) CreateRequestRepositories(org.folio.circulation.domain.CreateRequestRepositories) RequestRepository(org.folio.circulation.infrastructure.storage.requests.RequestRepository) HttpClient(io.vertx.core.http.HttpClient) Clients(org.folio.circulation.support.Clients) WebContext(org.folio.circulation.support.http.server.WebContext) Request(org.folio.circulation.domain.Request) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) FailFastErrorHandler(org.folio.circulation.resources.handlers.error.FailFastErrorHandler) JsonHttpResponse(org.folio.circulation.support.http.server.JsonHttpResponse) UpdateUponRequest(org.folio.circulation.domain.UpdateUponRequest) ArrayList(java.util.ArrayList) TlrSettingsConfiguration(org.folio.circulation.domain.configuration.TlrSettingsConfiguration) ForwardOnFailure(org.folio.circulation.support.ForwardOnFailure) RequestAndRelatedRecords(org.folio.circulation.domain.RequestAndRelatedRecords) RequestQueue(org.folio.circulation.domain.RequestQueue) REQUESTER_ID(org.folio.circulation.domain.representations.RequestProperties.REQUESTER_ID) LinkedList(java.util.LinkedList) RequestType(org.folio.circulation.domain.RequestType) RequestFulfilmentPreference(org.folio.circulation.domain.RequestFulfilmentPreference) ItemByInstanceIdFinder(org.folio.circulation.storage.ItemByInstanceIdFinder) ValidationErrorFailure(org.folio.circulation.support.ValidationErrorFailure) ServerErrorFailure(org.folio.circulation.support.ServerErrorFailure) Result.ofAsync(org.folio.circulation.support.results.Result.ofAsync) ITEM_ID(org.folio.circulation.domain.representations.RequestProperties.ITEM_ID) ValidationErrorFailure.failedValidation(org.folio.circulation.support.ValidationErrorFailure.failedValidation) InstanceRequestItemsComparer.sortRequestQueues(org.folio.circulation.domain.InstanceRequestItemsComparer.sortRequestQueues) UpdateLoan(org.folio.circulation.domain.UpdateLoan) Collectors.toList(java.util.stream.Collectors.toList) RequestLevel(org.folio.circulation.domain.RequestLevel) ProxyRelationshipValidator(org.folio.circulation.domain.validation.ProxyRelationshipValidator) RequestByInstanceIdRequest(org.folio.circulation.domain.representations.RequestByInstanceIdRequest) LogManager(org.apache.logging.log4j.LogManager) HashMap(java.util.HashMap) UpdateRequestQueue(org.folio.circulation.domain.UpdateRequestQueue) RequestQueue(org.folio.circulation.domain.RequestQueue) Request(org.folio.circulation.domain.Request) UpdateUponRequest(org.folio.circulation.domain.UpdateUponRequest) RequestByInstanceIdRequest(org.folio.circulation.domain.representations.RequestByInstanceIdRequest) ValidationErrorFailure.singleValidationError(org.folio.circulation.support.ValidationErrorFailure.singleValidationError) ValidationError(org.folio.circulation.support.http.server.ValidationError)

Example 5 with Item

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);
}
Also used : UserRepository(org.folio.circulation.infrastructure.storage.users.UserRepository) INSTANCE_DOES_NOT_EXIST(org.folio.circulation.resources.handlers.error.CirculationErrorType.INSTANCE_DOES_NOT_EXIST) LoanRepository(org.folio.circulation.infrastructure.storage.loans.LoanRepository) JsonPropertyFetcher.getDateTimeProperty(org.folio.circulation.support.json.JsonPropertyFetcher.getDateTimeProperty) ConfigurationRepository(org.folio.circulation.infrastructure.storage.ConfigurationRepository) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) MappingFunctions.when(org.folio.circulation.support.results.MappingFunctions.when) RequestQueueRepository(org.folio.circulation.infrastructure.storage.requests.RequestQueueRepository) ItemStatus(org.folio.circulation.domain.ItemStatus) StringUtils(org.apache.commons.lang3.StringUtils) Item(org.folio.circulation.domain.Item) REQUEST_LEVEL(org.folio.circulation.domain.representations.RequestProperties.REQUEST_LEVEL) ServicePointRepository(org.folio.circulation.infrastructure.storage.ServicePointRepository) Map(java.util.Map) JsonObject(io.vertx.core.json.JsonObject) ItemRepository(org.folio.circulation.infrastructure.storage.inventory.ItemRepository) REQUEST_DATE(org.folio.circulation.domain.representations.RequestProperties.REQUEST_DATE) Result.of(org.folio.circulation.support.results.Result.of) AsynchronousResult.fromFutureResult(org.folio.circulation.support.results.AsynchronousResult.fromFutureResult) Result.succeeded(org.folio.circulation.support.results.Result.succeeded) Collection(java.util.Collection) Loan(org.folio.circulation.domain.Loan) ATTEMPT_TO_CREATE_TLR_LINKED_TO_AN_ITEM(org.folio.circulation.resources.handlers.error.CirculationErrorType.ATTEMPT_TO_CREATE_TLR_LINKED_TO_AN_ITEM) INVALID_INSTANCE_ID(org.folio.circulation.resources.handlers.error.CirculationErrorType.INVALID_INSTANCE_ID) PageLimit.limit(org.folio.circulation.support.http.client.PageLimit.limit) UUID(java.util.UUID) Result(org.folio.circulation.support.results.Result) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) INVALID_ITEM_ID(org.folio.circulation.resources.handlers.error.CirculationErrorType.INVALID_ITEM_ID) ServicePointPickupLocationValidator(org.folio.circulation.domain.validation.ServicePointPickupLocationValidator) List(java.util.List) StringUtils.isNotBlank(org.apache.commons.lang3.StringUtils.isNotBlank) TITLE(org.folio.circulation.domain.RequestLevel.TITLE) InstanceRepository(org.folio.circulation.infrastructure.storage.inventory.InstanceRepository) RequestStatus(org.folio.circulation.domain.RequestStatus) Optional(java.util.Optional) BadRequestFailure(org.folio.circulation.support.BadRequestFailure) Request(org.folio.circulation.domain.Request) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ATTEMPT_HOLD_OR_RECALL_TLR_FOR_AVAILABLE_ITEM(org.folio.circulation.resources.handlers.error.CirculationErrorType.ATTEMPT_HOLD_OR_RECALL_TLR_FOR_AVAILABLE_ITEM) ITEM(org.folio.circulation.domain.RequestLevel.ITEM) INVALID_HOLDINGS_RECORD_ID(org.folio.circulation.resources.handlers.error.CirculationErrorType.INVALID_HOLDINGS_RECORD_ID) RequestAndRelatedRecords(org.folio.circulation.domain.RequestAndRelatedRecords) String.join(java.lang.String.join) PageLimit(org.folio.circulation.support.http.client.PageLimit) INSTANCE_ID(org.folio.circulation.domain.representations.RequestProperties.INSTANCE_ID) Result.failed(org.folio.circulation.support.results.Result.failed) INVALID_PICKUP_SERVICE_POINT(org.folio.circulation.resources.handlers.error.CirculationErrorType.INVALID_PICKUP_SERVICE_POINT) MultipleRecords(org.folio.circulation.domain.MultipleRecords) RequestFulfilmentPreference(org.folio.circulation.domain.RequestFulfilmentPreference) ItemByInstanceIdFinder(org.folio.circulation.storage.ItemByInstanceIdFinder) NO_AVAILABLE_ITEMS_FOR_TLR(org.folio.circulation.resources.handlers.error.CirculationErrorType.NO_AVAILABLE_ITEMS_FOR_TLR) Result.ofAsync(org.folio.circulation.support.results.Result.ofAsync) ITEM_ID(org.folio.circulation.domain.representations.RequestProperties.ITEM_ID) ValidationErrorFailure.failedValidation(org.folio.circulation.support.ValidationErrorFailure.failedValidation) User(org.folio.circulation.domain.User) Collectors.toList(java.util.stream.Collectors.toList) CirculationErrorHandler(org.folio.circulation.resources.handlers.error.CirculationErrorHandler) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) RequestLevel(org.folio.circulation.domain.RequestLevel) ProxyRelationshipValidator(org.folio.circulation.domain.validation.ProxyRelationshipValidator) INVALID_PROXY_RELATIONSHIP(org.folio.circulation.resources.handlers.error.CirculationErrorType.INVALID_PROXY_RELATIONSHIP) AllArgsConstructor(lombok.AllArgsConstructor) JsonPropertyFetcher.getProperty(org.folio.circulation.support.json.JsonPropertyFetcher.getProperty) RequestLevel(org.folio.circulation.domain.RequestLevel) JsonObject(io.vertx.core.json.JsonObject)

Aggregations

Item (org.folio.circulation.domain.Item)29 JsonObject (io.vertx.core.json.JsonObject)17 Loan (org.folio.circulation.domain.Loan)13 UUID (java.util.UUID)12 Result (org.folio.circulation.support.results.Result)12 CompletableFuture (java.util.concurrent.CompletableFuture)11 Map (java.util.Map)10 Request (org.folio.circulation.domain.Request)10 LoanRepository (org.folio.circulation.infrastructure.storage.loans.LoanRepository)10 HashMap (java.util.HashMap)9 ZonedDateTime (java.time.ZonedDateTime)8 ArrayList (java.util.ArrayList)8 RequestQueue (org.folio.circulation.domain.RequestQueue)8 UserRepository (org.folio.circulation.infrastructure.storage.users.UserRepository)8 Result.succeeded (org.folio.circulation.support.results.Result.succeeded)8 List (java.util.List)7 UpdateItem (org.folio.circulation.domain.UpdateItem)7 RequestByInstanceIdRequest (org.folio.circulation.domain.representations.RequestByInstanceIdRequest)7 ItemRepository (org.folio.circulation.infrastructure.storage.inventory.ItemRepository)7 Test (org.junit.jupiter.api.Test)7