Search in sources :

Example 6 with Clients

use of org.folio.circulation.support.Clients in project mod-circulation by folio-org.

the class CirculationRulesResource method proceedWithUpdate.

private void proceedWithUpdate(Map<String, Set<String>> existingPoliciesIds, RoutingContext routingContext, Clients clients) {
    final WebContext webContext = new WebContext(routingContext);
    JsonObject rulesInput;
    try {
        // try to convert, do not safe if conversion fails
        rulesInput = routingContext.getBodyAsJson();
        Text2Drools.convert(rulesInput.getString("rulesAsText"), (policyType, policies, token) -> validatePolicy(existingPoliciesIds, policyType, policies, token));
    } catch (CirculationRulesException e) {
        processingError(routingContext.response(), e);
        return;
    } catch (DecodeException e) {
        decodingError(routingContext.response(), e);
        return;
    } catch (Exception e) {
        internalError(routingContext.response(), getStackTrace(e));
        return;
    }
    clients.circulationRulesStorage().put(rulesInput.copy()).thenApply(this::failWhenResponseOtherThanNoContent).thenApply(result -> result.map(response -> noContent())).thenAccept(webContext::writeResultToHttpResponse);
    CirculationRulesCache.getInstance().clearCache(webContext.getTenantId());
}
Also used : Clients(org.folio.circulation.support.Clients) Response(org.folio.circulation.support.http.client.Response) DecodeException(io.vertx.core.json.DecodeException) WebContext(org.folio.circulation.support.http.server.WebContext) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) Token(org.antlr.v4.runtime.Token) Router(io.vertx.ext.web.Router) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) CollectionResourceClient(org.folio.circulation.support.CollectionResourceClient) RoutingContext(io.vertx.ext.web.RoutingContext) BodyHandler(io.vertx.ext.web.handler.BodyHandler) Function(java.util.function.Function) JsonHttpResponse.ok(org.folio.circulation.support.http.server.JsonHttpResponse.ok) ForwardOnFailure(org.folio.circulation.support.ForwardOnFailure) CirculationRulesParser(org.folio.circulation.rules.CirculationRulesParser) Text2Drools(org.folio.circulation.rules.Text2Drools) Collectors.mapping(java.util.stream.Collectors.mapping) Map(java.util.Map) PageLimit(org.folio.circulation.support.http.client.PageLimit) JsonObject(io.vertx.core.json.JsonObject) Collectors.toSet(java.util.stream.Collectors.toSet) MapUtils(org.apache.commons.collections4.MapUtils) CirculationRulesCache(org.folio.circulation.rules.cache.CirculationRulesCache) MultipleRecords(org.folio.circulation.domain.MultipleRecords) Result.of(org.folio.circulation.support.results.Result.of) MethodHandles(java.lang.invoke.MethodHandles) ExceptionUtils.getStackTrace(org.apache.commons.lang3.exception.ExceptionUtils.getStackTrace) Set(java.util.Set) Result(org.folio.circulation.support.results.Result) ForwardResponse(org.folio.circulation.support.http.server.ForwardResponse) NoContentResponse.noContent(org.folio.circulation.support.http.server.NoContentResponse.noContent) List(java.util.List) CollectionUtils.isNotEmpty(org.apache.commons.collections4.CollectionUtils.isNotEmpty) Logger(org.apache.logging.log4j.Logger) CirculationRulesException(org.folio.circulation.rules.CirculationRulesException) HttpServerResponse(io.vertx.core.http.HttpServerResponse) JsonHttpResponse.unprocessableEntity(org.folio.circulation.support.http.server.JsonHttpResponse.unprocessableEntity) LogManager(org.apache.logging.log4j.LogManager) JsonPropertyFetcher.getProperty(org.folio.circulation.support.json.JsonPropertyFetcher.getProperty) ServerErrorResponse.internalError(org.folio.circulation.support.http.server.ServerErrorResponse.internalError) HttpClient(io.vertx.core.http.HttpClient) WebContext(org.folio.circulation.support.http.server.WebContext) CirculationRulesException(org.folio.circulation.rules.CirculationRulesException) JsonObject(io.vertx.core.json.JsonObject) DecodeException(io.vertx.core.json.DecodeException) DecodeException(io.vertx.core.json.DecodeException) CirculationRulesException(org.folio.circulation.rules.CirculationRulesException)

Example 7 with Clients

use of org.folio.circulation.support.Clients in project mod-circulation by folio-org.

the class CirculationRulesResource method getPolicyIdsByType.

private CompletableFuture<Result<Map<String, Set<String>>>> getPolicyIdsByType(Clients clients) {
    CollectionResourceClient loanPolicyClient = clients.loanPoliciesStorage();
    CollectionResourceClient noticePolicyClient = clients.patronNoticePolicesStorageClient();
    CollectionResourceClient requestPolicyClient = clients.requestPoliciesStorage();
    CollectionResourceClient overdueFinePolicyClient = clients.overdueFinesPoliciesStorage();
    CollectionResourceClient lostItemFeePolicyClient = clients.lostItemPoliciesStorage();
    Map<String, Set<String>> ids = new HashMap<>();
    return Result.ofAsync(() -> ids).thenCombineAsync(getPolicyIdsByType(loanPolicyClient, "loanPolicies", "l"), (resultTotalIds, resultNewIds) -> resultTotalIds.combine(resultNewIds, this::getTotalMap)).thenCombineAsync(getPolicyIdsByType(noticePolicyClient, "patronNoticePolicies", "n"), (resultTotalIds, resultNewIds) -> resultTotalIds.combine(resultNewIds, this::getTotalMap)).thenCombineAsync(getPolicyIdsByType(requestPolicyClient, "requestPolicies", "r"), (resultTotalIds, resultNewIds) -> resultTotalIds.combine(resultNewIds, this::getTotalMap)).thenCombineAsync(getPolicyIdsByType(overdueFinePolicyClient, "overdueFinePolicies", "o"), (resultTotalIds, resultNewIds) -> resultTotalIds.combine(resultNewIds, this::getTotalMap)).thenCombineAsync(getPolicyIdsByType(lostItemFeePolicyClient, "lostItemFeePolicies", "i"), (resultTotalIds, resultNewIds) -> resultTotalIds.combine(resultNewIds, this::getTotalMap));
}
Also used : Clients(org.folio.circulation.support.Clients) Response(org.folio.circulation.support.http.client.Response) DecodeException(io.vertx.core.json.DecodeException) WebContext(org.folio.circulation.support.http.server.WebContext) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) Token(org.antlr.v4.runtime.Token) Router(io.vertx.ext.web.Router) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) CollectionResourceClient(org.folio.circulation.support.CollectionResourceClient) RoutingContext(io.vertx.ext.web.RoutingContext) BodyHandler(io.vertx.ext.web.handler.BodyHandler) Function(java.util.function.Function) JsonHttpResponse.ok(org.folio.circulation.support.http.server.JsonHttpResponse.ok) ForwardOnFailure(org.folio.circulation.support.ForwardOnFailure) CirculationRulesParser(org.folio.circulation.rules.CirculationRulesParser) Text2Drools(org.folio.circulation.rules.Text2Drools) Collectors.mapping(java.util.stream.Collectors.mapping) Map(java.util.Map) PageLimit(org.folio.circulation.support.http.client.PageLimit) JsonObject(io.vertx.core.json.JsonObject) Collectors.toSet(java.util.stream.Collectors.toSet) MapUtils(org.apache.commons.collections4.MapUtils) CirculationRulesCache(org.folio.circulation.rules.cache.CirculationRulesCache) MultipleRecords(org.folio.circulation.domain.MultipleRecords) Result.of(org.folio.circulation.support.results.Result.of) MethodHandles(java.lang.invoke.MethodHandles) ExceptionUtils.getStackTrace(org.apache.commons.lang3.exception.ExceptionUtils.getStackTrace) Set(java.util.Set) Result(org.folio.circulation.support.results.Result) ForwardResponse(org.folio.circulation.support.http.server.ForwardResponse) NoContentResponse.noContent(org.folio.circulation.support.http.server.NoContentResponse.noContent) List(java.util.List) CollectionUtils.isNotEmpty(org.apache.commons.collections4.CollectionUtils.isNotEmpty) Logger(org.apache.logging.log4j.Logger) CirculationRulesException(org.folio.circulation.rules.CirculationRulesException) HttpServerResponse(io.vertx.core.http.HttpServerResponse) JsonHttpResponse.unprocessableEntity(org.folio.circulation.support.http.server.JsonHttpResponse.unprocessableEntity) LogManager(org.apache.logging.log4j.LogManager) JsonPropertyFetcher.getProperty(org.folio.circulation.support.json.JsonPropertyFetcher.getProperty) ServerErrorResponse.internalError(org.folio.circulation.support.http.server.ServerErrorResponse.internalError) HttpClient(io.vertx.core.http.HttpClient) CollectionResourceClient(org.folio.circulation.support.CollectionResourceClient) Collectors.toSet(java.util.stream.Collectors.toSet) Set(java.util.Set) HashMap(java.util.HashMap)

Example 8 with Clients

use of org.folio.circulation.support.Clients in project mod-circulation by folio-org.

the class CirculationRulesResource method put.

// Cannot combine exception catching as cannot resolve overloaded method for error
@SuppressWarnings("squid:S2147")
private void put(RoutingContext routingContext) {
    final Clients clients = Clients.create(new WebContext(routingContext), client);
    CollectionResourceClient loansRulesClient = clients.circulationRulesStorage();
    if (loansRulesClient == null) {
        internalError(routingContext.response(), "Cannot initialise client to storage interface");
        return;
    }
    getPolicyIdsByType(clients).thenAccept(result -> proceedWithUpdate(result.value(), routingContext, clients));
}
Also used : CollectionResourceClient(org.folio.circulation.support.CollectionResourceClient) WebContext(org.folio.circulation.support.http.server.WebContext) Clients(org.folio.circulation.support.Clients)

Example 9 with Clients

use of org.folio.circulation.support.Clients in project mod-circulation by folio-org.

the class DeclareLostResource method declareLost.

private void declareLost(RoutingContext routingContext) {
    final WebContext context = new WebContext(routingContext);
    final Clients clients = Clients.create(context, client);
    final EventPublisher eventPublisher = new EventPublisher(routingContext);
    validateDeclaredLostRequest(routingContext).after(request -> declareItemLost(request, clients, context)).thenComposeAsync(r -> r.after(loan -> publishEvent(loan, eventPublisher))).thenApply(r -> r.map(toFixedValue(NoContentResponse::noContent))).thenAccept(context::writeResultToHttpResponse);
}
Also used : UserRepository(org.folio.circulation.infrastructure.storage.users.UserRepository) Clients(org.folio.circulation.support.Clients) DeclareItemLostRequest(org.folio.circulation.domain.representations.DeclareItemLostRequest) LoanRepository(org.folio.circulation.infrastructure.storage.loans.LoanRepository) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) WebContext(org.folio.circulation.support.http.server.WebContext) MappingFunctions.when(org.folio.circulation.support.results.MappingFunctions.when) Router(io.vertx.ext.web.Router) CompletableFuture(java.util.concurrent.CompletableFuture) ValidationErrorFailure.singleValidationError(org.folio.circulation.support.ValidationErrorFailure.singleValidationError) LostItemFeeChargingService(org.folio.circulation.services.LostItemFeeChargingService) RoutingContext(io.vertx.ext.web.RoutingContext) Function(java.util.function.Function) NotesRepository(org.folio.circulation.infrastructure.storage.notes.NotesRepository) LoanValidator(org.folio.circulation.domain.validation.LoanValidator) ItemRepository(org.folio.circulation.infrastructure.storage.inventory.ItemRepository) LostItemFeeRefundService(org.folio.circulation.services.LostItemFeeRefundService) NoteCreator(org.folio.circulation.domain.notes.NoteCreator) Result.succeeded(org.folio.circulation.support.results.Result.succeeded) Loan(org.folio.circulation.domain.Loan) MappingFunctions.toFixedValue(org.folio.circulation.support.results.MappingFunctions.toFixedValue) Result(org.folio.circulation.support.results.Result) Result.ofAsync(org.folio.circulation.support.results.Result.ofAsync) EventPublisher(org.folio.circulation.services.EventPublisher) StringUtils.defaultIfBlank(org.apache.commons.lang.StringUtils.defaultIfBlank) CompletionStage(java.util.concurrent.CompletionStage) StoreLoanAndItem(org.folio.circulation.StoreLoanAndItem) NoContentResponse(org.folio.circulation.support.http.server.NoContentResponse) HttpClient(io.vertx.core.http.HttpClient) WebContext(org.folio.circulation.support.http.server.WebContext) EventPublisher(org.folio.circulation.services.EventPublisher) Clients(org.folio.circulation.support.Clients)

Example 10 with Clients

use of org.folio.circulation.support.Clients in project mod-circulation by folio-org.

the class DeclareLostResource method declareItemLostWhenClaimedReturned.

private CompletableFuture<Result<Loan>> declareItemLostWhenClaimedReturned(Loan loan, DeclareItemLostRequest request, Clients clients) {
    final NotesRepository notesRepository = NotesRepository.createUsing(clients);
    final NoteCreator creator = new NoteCreator(notesRepository);
    return ofAsync(() -> declareItemLost(loan, request)).thenCompose(r -> r.after(l -> creator.createGeneralUserNote(loan.getUserId(), "Claimed returned item marked declared lost"))).thenCompose(r -> r.after(note -> completedFuture(succeeded(loan))));
}
Also used : UserRepository(org.folio.circulation.infrastructure.storage.users.UserRepository) Clients(org.folio.circulation.support.Clients) DeclareItemLostRequest(org.folio.circulation.domain.representations.DeclareItemLostRequest) LoanRepository(org.folio.circulation.infrastructure.storage.loans.LoanRepository) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) WebContext(org.folio.circulation.support.http.server.WebContext) MappingFunctions.when(org.folio.circulation.support.results.MappingFunctions.when) Router(io.vertx.ext.web.Router) CompletableFuture(java.util.concurrent.CompletableFuture) ValidationErrorFailure.singleValidationError(org.folio.circulation.support.ValidationErrorFailure.singleValidationError) LostItemFeeChargingService(org.folio.circulation.services.LostItemFeeChargingService) RoutingContext(io.vertx.ext.web.RoutingContext) Function(java.util.function.Function) NotesRepository(org.folio.circulation.infrastructure.storage.notes.NotesRepository) LoanValidator(org.folio.circulation.domain.validation.LoanValidator) ItemRepository(org.folio.circulation.infrastructure.storage.inventory.ItemRepository) LostItemFeeRefundService(org.folio.circulation.services.LostItemFeeRefundService) NoteCreator(org.folio.circulation.domain.notes.NoteCreator) Result.succeeded(org.folio.circulation.support.results.Result.succeeded) Loan(org.folio.circulation.domain.Loan) MappingFunctions.toFixedValue(org.folio.circulation.support.results.MappingFunctions.toFixedValue) Result(org.folio.circulation.support.results.Result) Result.ofAsync(org.folio.circulation.support.results.Result.ofAsync) EventPublisher(org.folio.circulation.services.EventPublisher) StringUtils.defaultIfBlank(org.apache.commons.lang.StringUtils.defaultIfBlank) CompletionStage(java.util.concurrent.CompletionStage) StoreLoanAndItem(org.folio.circulation.StoreLoanAndItem) NoContentResponse(org.folio.circulation.support.http.server.NoContentResponse) HttpClient(io.vertx.core.http.HttpClient) NotesRepository(org.folio.circulation.infrastructure.storage.notes.NotesRepository) NoteCreator(org.folio.circulation.domain.notes.NoteCreator)

Aggregations

Clients (org.folio.circulation.support.Clients)41 WebContext (org.folio.circulation.support.http.server.WebContext)38 HttpClient (io.vertx.core.http.HttpClient)36 RoutingContext (io.vertx.ext.web.RoutingContext)36 ItemRepository (org.folio.circulation.infrastructure.storage.inventory.ItemRepository)33 UserRepository (org.folio.circulation.infrastructure.storage.users.UserRepository)32 LoanRepository (org.folio.circulation.infrastructure.storage.loans.LoanRepository)31 Result (org.folio.circulation.support.results.Result)31 Router (io.vertx.ext.web.Router)30 CompletableFuture (java.util.concurrent.CompletableFuture)29 JsonObject (io.vertx.core.json.JsonObject)26 EventPublisher (org.folio.circulation.services.EventPublisher)26 Result.succeeded (org.folio.circulation.support.results.Result.succeeded)23 RouteRegistration (org.folio.circulation.support.RouteRegistration)21 ValidationErrorFailure.singleValidationError (org.folio.circulation.support.ValidationErrorFailure.singleValidationError)21 Loan (org.folio.circulation.domain.Loan)19 RequestRepository (org.folio.circulation.infrastructure.storage.requests.RequestRepository)19 JsonHttpResponse (org.folio.circulation.support.http.server.JsonHttpResponse)19 NoContentResponse (org.folio.circulation.support.http.server.NoContentResponse)19 MappingFunctions.toFixedValue (org.folio.circulation.support.results.MappingFunctions.toFixedValue)19