Search in sources :

Example 1 with Result.succeeded

use of org.folio.circulation.support.results.Result.succeeded in project mod-circulation by folio-org.

the class EventPublisher method publishItemCheckedOutEvent.

public CompletableFuture<Result<LoanAndRelatedRecords>> publishItemCheckedOutEvent(LoanAndRelatedRecords loanAndRelatedRecords, UserRepository userRepository) {
    if (loanAndRelatedRecords.getLoan() != null) {
        Loan loan = loanAndRelatedRecords.getLoan();
        JsonObject payloadJsonObject = new JsonObject();
        write(payloadJsonObject, USER_ID_FIELD, loan.getUserId());
        write(payloadJsonObject, LOAN_ID_FIELD, loan.getId());
        write(payloadJsonObject, DUE_DATE_FIELD, loan.getDueDate());
        ofNullable(loan.getLoanPolicy()).map(LoanPolicy::getGracePeriod).filter(Period::isValid).map(Period::asJson).ifPresent(json -> write(payloadJsonObject, GRACE_PERIOD_FIELD, json));
        runAsync(() -> userRepository.getUser(loanAndRelatedRecords.getLoggedInUserId()).thenApplyAsync(r -> r.after(loggedInUser -> CompletableFuture.completedFuture(Result.succeeded(pubSubPublishingService.publishEvent(LOG_RECORD.name(), mapToCheckOutLogEventContent(loanAndRelatedRecords, loggedInUser)))))));
        return pubSubPublishingService.publishEvent(ITEM_CHECKED_OUT.name(), payloadJsonObject.encode()).handle((result, error) -> handlePublishEventError(error, loanAndRelatedRecords));
    } else {
        logger.error(FAILED_TO_PUBLISH_LOG_TEMPLATE, ITEM_CHECKED_OUT.name());
    }
    return completedFuture(succeeded(loanAndRelatedRecords));
}
Also used : UserRepository(org.folio.circulation.infrastructure.storage.users.UserRepository) RenewalContext(org.folio.circulation.resources.context.RenewalContext) LoanRepository(org.folio.circulation.infrastructure.storage.loans.LoanRepository) LogContextActionResolver(org.folio.circulation.domain.representations.logs.LogContextActionResolver) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) RoutingContext(io.vertx.ext.web.RoutingContext) JsonPropertyWriter.write(org.folio.circulation.support.json.JsonPropertyWriter.write) CHECKED_IN(org.folio.circulation.domain.LoanAction.CHECKED_IN) DateFormatUtil.formatDateTimeOptional(org.folio.circulation.support.utils.DateFormatUtil.formatDateTimeOptional) LoanLogContext(org.folio.circulation.domain.representations.logs.LoanLogContext) LOAN_CLOSED(org.folio.circulation.domain.EventType.LOAN_CLOSED) LOG_EVENT_TYPE(org.folio.circulation.domain.representations.logs.LogEventPayloadField.LOG_EVENT_TYPE) LoanAndRelatedRecords(org.folio.circulation.domain.LoanAndRelatedRecords) Period(org.folio.circulation.domain.policy.Period) HttpFailure(org.folio.circulation.support.HttpFailure) CommonFailures.failedDueToServerError(org.folio.circulation.support.results.CommonFailures.failedDueToServerError) LoanAnonymizationRecords(org.folio.circulation.domain.anonymization.LoanAnonymizationRecords) JsonObject(io.vertx.core.json.JsonObject) DUE_DATE_CHANGED(org.folio.circulation.domain.LoanAction.DUE_DATE_CHANGED) LOAN(org.folio.circulation.domain.representations.logs.LogEventType.LOAN) CirculationCheckInCheckOutLogEventMapper.mapToCheckOutLogEventContent(org.folio.circulation.domain.representations.logs.CirculationCheckInCheckOutLogEventMapper.mapToCheckOutLogEventContent) Result.succeeded(org.folio.circulation.support.results.Result.succeeded) Loan(org.folio.circulation.domain.Loan) PAYLOAD(org.folio.circulation.domain.representations.logs.LogEventPayloadField.PAYLOAD) Result(org.folio.circulation.support.results.Result) LoanPolicy(org.folio.circulation.domain.policy.LoanPolicy) Logger(org.apache.logging.log4j.Logger) ITEM_CLAIMED_RETURNED(org.folio.circulation.domain.EventType.ITEM_CLAIMED_RETURNED) NoticeLogContext(org.folio.circulation.domain.representations.logs.NoticeLogContext) NOTICE(org.folio.circulation.domain.representations.logs.LogEventType.NOTICE) ClockUtil.getZonedDateTime(org.folio.circulation.support.utils.ClockUtil.getZonedDateTime) EventType(org.folio.circulation.domain.EventType) Request(org.folio.circulation.domain.Request) CompletableFuture(java.util.concurrent.CompletableFuture) ITEM_CHECKED_OUT(org.folio.circulation.domain.EventType.ITEM_CHECKED_OUT) CheckInContext(org.folio.circulation.domain.CheckInContext) AsyncCoordinationUtil.allOf(org.folio.circulation.support.AsyncCoordinationUtil.allOf) RequestUpdateLogEventMapper.mapToRequestLogEventJson(org.folio.circulation.domain.representations.logs.RequestUpdateLogEventMapper.mapToRequestLogEventJson) RequestAndRelatedRecords(org.folio.circulation.domain.RequestAndRelatedRecords) LOAN_DUE_DATE_CHANGED(org.folio.circulation.domain.EventType.LOAN_DUE_DATE_CHANGED) LogEventType(org.folio.circulation.domain.representations.logs.LogEventType) CompletableFuture.runAsync(java.util.concurrent.CompletableFuture.runAsync) Optional.ofNullable(java.util.Optional.ofNullable) NOTICE_ERROR(org.folio.circulation.domain.representations.logs.LogEventType.NOTICE_ERROR) CirculationCheckInCheckOutLogEventMapper.mapToCheckInLogEventContent(org.folio.circulation.domain.representations.logs.CirculationCheckInCheckOutLogEventMapper.mapToCheckInLogEventContent) Result.ofAsync(org.folio.circulation.support.results.Result.ofAsync) ITEM_AGED_TO_LOST(org.folio.circulation.domain.EventType.ITEM_AGED_TO_LOST) User(org.folio.circulation.domain.User) ITEM_CHECKED_IN(org.folio.circulation.domain.EventType.ITEM_CHECKED_IN) ITEM_DECLARED_LOST(org.folio.circulation.domain.EventType.ITEM_DECLARED_LOST) LOG_RECORD(org.folio.circulation.domain.EventType.LOG_RECORD) RECALLREQUESTED(org.folio.circulation.domain.LoanAction.RECALLREQUESTED) LogManager(org.apache.logging.log4j.LogManager) Loan(org.folio.circulation.domain.Loan) JsonObject(io.vertx.core.json.JsonObject) Period(org.folio.circulation.domain.policy.Period)

Example 2 with Result.succeeded

use of org.folio.circulation.support.results.Result.succeeded in project mod-circulation by folio-org.

the class EventPublisher method publishItemCheckedInEvents.

public CompletableFuture<Result<CheckInContext>> publishItemCheckedInEvents(CheckInContext checkInContext, UserRepository userRepository) {
    runAsync(() -> userRepository.getUser(checkInContext.getLoggedInUserId()).thenApplyAsync(r -> r.after(loggedInUser -> CompletableFuture.completedFuture(Result.succeeded(pubSubPublishingService.publishEvent(LOG_RECORD.name(), mapToCheckInLogEventContent(checkInContext, loggedInUser)))))));
    if (checkInContext.getLoan() != null) {
        Loan loan = checkInContext.getLoan();
        JsonObject payloadJsonObject = new JsonObject();
        write(payloadJsonObject, USER_ID_FIELD, loan.getUserId());
        write(payloadJsonObject, LOAN_ID_FIELD, loan.getId());
        write(payloadJsonObject, RETURN_DATE_FIELD, loan.getReturnDate());
        return pubSubPublishingService.publishEvent(ITEM_CHECKED_IN.name(), payloadJsonObject.encode()).handle((result, error) -> handlePublishEventError(error, checkInContext));
    } else {
        logger.error(FAILED_TO_PUBLISH_LOG_TEMPLATE, ITEM_CHECKED_IN.name());
    }
    return completedFuture(succeeded(checkInContext));
}
Also used : UserRepository(org.folio.circulation.infrastructure.storage.users.UserRepository) RenewalContext(org.folio.circulation.resources.context.RenewalContext) LoanRepository(org.folio.circulation.infrastructure.storage.loans.LoanRepository) LogContextActionResolver(org.folio.circulation.domain.representations.logs.LogContextActionResolver) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) RoutingContext(io.vertx.ext.web.RoutingContext) JsonPropertyWriter.write(org.folio.circulation.support.json.JsonPropertyWriter.write) CHECKED_IN(org.folio.circulation.domain.LoanAction.CHECKED_IN) DateFormatUtil.formatDateTimeOptional(org.folio.circulation.support.utils.DateFormatUtil.formatDateTimeOptional) LoanLogContext(org.folio.circulation.domain.representations.logs.LoanLogContext) LOAN_CLOSED(org.folio.circulation.domain.EventType.LOAN_CLOSED) LOG_EVENT_TYPE(org.folio.circulation.domain.representations.logs.LogEventPayloadField.LOG_EVENT_TYPE) LoanAndRelatedRecords(org.folio.circulation.domain.LoanAndRelatedRecords) Period(org.folio.circulation.domain.policy.Period) HttpFailure(org.folio.circulation.support.HttpFailure) CommonFailures.failedDueToServerError(org.folio.circulation.support.results.CommonFailures.failedDueToServerError) LoanAnonymizationRecords(org.folio.circulation.domain.anonymization.LoanAnonymizationRecords) JsonObject(io.vertx.core.json.JsonObject) DUE_DATE_CHANGED(org.folio.circulation.domain.LoanAction.DUE_DATE_CHANGED) LOAN(org.folio.circulation.domain.representations.logs.LogEventType.LOAN) CirculationCheckInCheckOutLogEventMapper.mapToCheckOutLogEventContent(org.folio.circulation.domain.representations.logs.CirculationCheckInCheckOutLogEventMapper.mapToCheckOutLogEventContent) Result.succeeded(org.folio.circulation.support.results.Result.succeeded) Loan(org.folio.circulation.domain.Loan) PAYLOAD(org.folio.circulation.domain.representations.logs.LogEventPayloadField.PAYLOAD) Result(org.folio.circulation.support.results.Result) LoanPolicy(org.folio.circulation.domain.policy.LoanPolicy) Logger(org.apache.logging.log4j.Logger) ITEM_CLAIMED_RETURNED(org.folio.circulation.domain.EventType.ITEM_CLAIMED_RETURNED) NoticeLogContext(org.folio.circulation.domain.representations.logs.NoticeLogContext) NOTICE(org.folio.circulation.domain.representations.logs.LogEventType.NOTICE) ClockUtil.getZonedDateTime(org.folio.circulation.support.utils.ClockUtil.getZonedDateTime) EventType(org.folio.circulation.domain.EventType) Request(org.folio.circulation.domain.Request) CompletableFuture(java.util.concurrent.CompletableFuture) ITEM_CHECKED_OUT(org.folio.circulation.domain.EventType.ITEM_CHECKED_OUT) CheckInContext(org.folio.circulation.domain.CheckInContext) AsyncCoordinationUtil.allOf(org.folio.circulation.support.AsyncCoordinationUtil.allOf) RequestUpdateLogEventMapper.mapToRequestLogEventJson(org.folio.circulation.domain.representations.logs.RequestUpdateLogEventMapper.mapToRequestLogEventJson) RequestAndRelatedRecords(org.folio.circulation.domain.RequestAndRelatedRecords) LOAN_DUE_DATE_CHANGED(org.folio.circulation.domain.EventType.LOAN_DUE_DATE_CHANGED) LogEventType(org.folio.circulation.domain.representations.logs.LogEventType) CompletableFuture.runAsync(java.util.concurrent.CompletableFuture.runAsync) Optional.ofNullable(java.util.Optional.ofNullable) NOTICE_ERROR(org.folio.circulation.domain.representations.logs.LogEventType.NOTICE_ERROR) CirculationCheckInCheckOutLogEventMapper.mapToCheckInLogEventContent(org.folio.circulation.domain.representations.logs.CirculationCheckInCheckOutLogEventMapper.mapToCheckInLogEventContent) Result.ofAsync(org.folio.circulation.support.results.Result.ofAsync) ITEM_AGED_TO_LOST(org.folio.circulation.domain.EventType.ITEM_AGED_TO_LOST) User(org.folio.circulation.domain.User) ITEM_CHECKED_IN(org.folio.circulation.domain.EventType.ITEM_CHECKED_IN) ITEM_DECLARED_LOST(org.folio.circulation.domain.EventType.ITEM_DECLARED_LOST) LOG_RECORD(org.folio.circulation.domain.EventType.LOG_RECORD) RECALLREQUESTED(org.folio.circulation.domain.LoanAction.RECALLREQUESTED) LogManager(org.apache.logging.log4j.LogManager) Loan(org.folio.circulation.domain.Loan) JsonObject(io.vertx.core.json.JsonObject)

Aggregations

JsonObject (io.vertx.core.json.JsonObject)2 RoutingContext (io.vertx.ext.web.RoutingContext)2 Optional.ofNullable (java.util.Optional.ofNullable)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 CompletableFuture.completedFuture (java.util.concurrent.CompletableFuture.completedFuture)2 CompletableFuture.runAsync (java.util.concurrent.CompletableFuture.runAsync)2 LogManager (org.apache.logging.log4j.LogManager)2 Logger (org.apache.logging.log4j.Logger)2 CheckInContext (org.folio.circulation.domain.CheckInContext)2 EventType (org.folio.circulation.domain.EventType)2 ITEM_AGED_TO_LOST (org.folio.circulation.domain.EventType.ITEM_AGED_TO_LOST)2 ITEM_CHECKED_IN (org.folio.circulation.domain.EventType.ITEM_CHECKED_IN)2 ITEM_CHECKED_OUT (org.folio.circulation.domain.EventType.ITEM_CHECKED_OUT)2 ITEM_CLAIMED_RETURNED (org.folio.circulation.domain.EventType.ITEM_CLAIMED_RETURNED)2 ITEM_DECLARED_LOST (org.folio.circulation.domain.EventType.ITEM_DECLARED_LOST)2 LOAN_CLOSED (org.folio.circulation.domain.EventType.LOAN_CLOSED)2 LOAN_DUE_DATE_CHANGED (org.folio.circulation.domain.EventType.LOAN_DUE_DATE_CHANGED)2 LOG_RECORD (org.folio.circulation.domain.EventType.LOG_RECORD)2 Loan (org.folio.circulation.domain.Loan)2 CHECKED_IN (org.folio.circulation.domain.LoanAction.CHECKED_IN)2