Search in sources :

Example 1 with SagaRuntimeException

use of ca.bc.gov.educ.penreg.api.exception.SagaRuntimeException in project EDUC-PEN-REG-BATCH-API by bcgov.

the class PenRequestBatchSagaController method processBatchRequest.

private ResponseEntity<List<ArchiveAndReturnSagaResponse>> processBatchRequest(final SagaEnum sagaName, final PenRequestBatchArchiveAndReturnAllSagaData penRequestBatchArchiveAndReturnAllSagaData) {
    final var penRequestBatchIDs = penRequestBatchArchiveAndReturnAllSagaData.getPenRequestBatchArchiveAndReturnSagaData().stream().map(PenRequestBatchArchiveAndReturnSagaData::getPenRequestBatchID).collect(Collectors.toList());
    final var sagaInProgress = !this.getSagaService().findAllByPenRequestBatchIDInAndStatusIn(penRequestBatchIDs, this.getStatusesFilter()).isEmpty();
    if (sagaInProgress) {
        return ResponseEntity.status(HttpStatus.CONFLICT).build();
    }
    try {
        final var updateUser = penRequestBatchArchiveAndReturnAllSagaData.getUpdateUser();
        final var payloads = penRequestBatchArchiveAndReturnAllSagaData.getPenRequestBatchArchiveAndReturnSagaData().stream().map(sagaData -> {
            sagaData.setUpdateUser(updateUser);
            try {
                val payload = JsonUtil.getJsonStringFromObject(sagaData);
                return Pair.of(sagaData.getPenRequestBatchID(), payload);
            } catch (final JsonProcessingException e) {
                throw new InvalidParameterException(e.getMessage());
            }
        }).collect(Collectors.toList());
        final var sagas = this.getOrchestratorMap().get(sagaName.toString()).saveMultipleSagas(payloads, penRequestBatchArchiveAndReturnAllSagaData.getCreateUser());
        for (val saga : sagas) {
            this.getOrchestratorMap().get(sagaName.toString()).startSaga(saga);
        }
        return ResponseEntity.ok(sagas.stream().map(archiveAndReturnSagaResponseMapper::toStruct).collect(Collectors.toList()));
    } catch (final Exception e) {
        throw new SagaRuntimeException(e.getMessage());
    }
}
Also used : ApiError(ca.bc.gov.educ.penreg.api.exception.errors.ApiError) PenRequestBatchUnmatchSagaData(ca.bc.gov.educ.penreg.api.struct.PenRequestBatchUnmatchSagaData) java.util(java.util) Getter(lombok.Getter) PenRequestBatchSagaEndpoint(ca.bc.gov.educ.penreg.api.endpoint.v1.PenRequestBatchSagaEndpoint) InvalidParameterException(ca.bc.gov.educ.penreg.api.exception.InvalidParameterException) LocalDateTime(java.time.LocalDateTime) Autowired(org.springframework.beans.factory.annotation.Autowired) CompletableFuture(java.util.concurrent.CompletableFuture) JsonUtil(ca.bc.gov.educ.penreg.api.util.JsonUtil) StringUtils(org.apache.commons.lang3.StringUtils) SagaMapper(ca.bc.gov.educ.penreg.api.mappers.v1.SagaMapper) SagaEnum(ca.bc.gov.educ.penreg.api.constants.SagaEnum) ca.bc.gov.educ.penreg.api.struct.v1(ca.bc.gov.educ.penreg.api.struct.v1) Pair(org.springframework.data.util.Pair) Orchestrator(ca.bc.gov.educ.penreg.api.orchestrator.base.Orchestrator) Sort(org.springframework.data.domain.Sort) TypeReference(com.fasterxml.jackson.core.type.TypeReference) InvalidPayloadException(ca.bc.gov.educ.penreg.api.exception.InvalidPayloadException) BAD_REQUEST(org.springframework.http.HttpStatus.BAD_REQUEST) PenRequestBatchUserActionsSagaData(ca.bc.gov.educ.penreg.api.struct.PenRequestBatchUserActionsSagaData) BasePenRequestBatchStudentSagaData(ca.bc.gov.educ.penreg.api.struct.BasePenRequestBatchStudentSagaData) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) lombok.val(lombok.val) SagaService(ca.bc.gov.educ.penreg.api.service.SagaService) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) SagaFilterSpecs(ca.bc.gov.educ.penreg.api.filter.SagaFilterSpecs) Page(org.springframework.data.domain.Page) RestController(org.springframework.web.bind.annotation.RestController) Collectors(java.util.stream.Collectors) SagaRuntimeException(ca.bc.gov.educ.penreg.api.exception.SagaRuntimeException) HttpStatus(org.springframework.http.HttpStatus) Slf4j(lombok.extern.slf4j.Slf4j) Specification(org.springframework.data.jpa.domain.Specification) ArchiveAndReturnSagaResponseMapper(ca.bc.gov.educ.penreg.api.mappers.v1.ArchiveAndReturnSagaResponseMapper) ResponseEntity(org.springframework.http.ResponseEntity) SagaStatusEnum(ca.bc.gov.educ.penreg.api.constants.SagaStatusEnum) Transactional(org.springframework.transaction.annotation.Transactional) PRIVATE(lombok.AccessLevel.PRIVATE) lombok.val(lombok.val) InvalidParameterException(ca.bc.gov.educ.penreg.api.exception.InvalidParameterException) SagaRuntimeException(ca.bc.gov.educ.penreg.api.exception.SagaRuntimeException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) InvalidParameterException(ca.bc.gov.educ.penreg.api.exception.InvalidParameterException) InvalidPayloadException(ca.bc.gov.educ.penreg.api.exception.InvalidPayloadException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) SagaRuntimeException(ca.bc.gov.educ.penreg.api.exception.SagaRuntimeException)

Example 2 with SagaRuntimeException

use of ca.bc.gov.educ.penreg.api.exception.SagaRuntimeException in project EDUC-PEN-REG-BATCH-API by bcgov.

the class PenRequestBatchSagaController method processStudentRequest.

private ResponseEntity<String> processStudentRequest(final SagaEnum sagaName, final BasePenRequestBatchStudentSagaData penRequestBatchStudentSagaData) {
    final var penRequestBatchStudentID = penRequestBatchStudentSagaData.getPenRequestBatchStudentID();
    final var penRequestBatchID = penRequestBatchStudentSagaData.getPenRequestBatchID();
    final var sagaInProgress = !this.getSagaService().findAllByPenRequestBatchStudentIDAndStatusIn(penRequestBatchStudentID, this.getStatusesFilter()).isEmpty();
    final var parentSagaInProgress = !this.getSagaService().findAllByPenRequestBatchIDInAndStatusIn(List.of(penRequestBatchID), this.getStatusesFilter()).isEmpty();
    if (sagaInProgress || parentSagaInProgress) {
        return ResponseEntity.status(HttpStatus.CONFLICT).build();
    }
    final var orchestrator = this.getOrchestratorMap().get(sagaName.toString());
    try {
        final var saga = orchestrator.createSaga(JsonUtil.getJsonStringFromObject(penRequestBatchStudentSagaData), penRequestBatchStudentID, penRequestBatchStudentSagaData.getPenRequestBatchID(), penRequestBatchStudentSagaData.getCreateUser());
        orchestrator.startSaga(saga);
        return ResponseEntity.ok(saga.getSagaId().toString());
    } catch (final JsonProcessingException e) {
        log.error("JsonProcessingException while processStudentRequest", e);
        throw new SagaRuntimeException(e.getMessage());
    }
}
Also used : SagaRuntimeException(ca.bc.gov.educ.penreg.api.exception.SagaRuntimeException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 3 with SagaRuntimeException

use of ca.bc.gov.educ.penreg.api.exception.SagaRuntimeException in project EDUC-PEN-REG-BATCH-API by bcgov.

the class EventTaskSchedulerAsyncService method startArchiveAndReturnSaga.

private void startArchiveAndReturnSaga(final PenRequestBatchEntity penRequestBatchEntity) {
    final var sagaData = PenRequestBatchArchiveAndReturnSagaData.builder().penRequestBatchID(penRequestBatchEntity.getPenRequestBatchID()).schoolName(penRequestBatchEntity.getSchoolName()).createUser(penRequestBatchEntity.getCreateUser()).build();
    final var orchestrator = this.getSagaOrchestrators().get(PEN_REQUEST_BATCH_ARCHIVE_AND_RETURN_SAGA.toString());
    try {
        final var saga = orchestrator.createSaga(JsonUtil.getJsonStringFromObject(sagaData), null, penRequestBatchEntity.getPenRequestBatchID(), sagaData.getCreateUser());
        orchestrator.startSaga(saga);
    } catch (final JsonProcessingException e) {
        log.error("JsonProcessingException while startArchiveAndReturnSaga", e);
        throw new SagaRuntimeException(e.getMessage());
    }
}
Also used : SagaRuntimeException(ca.bc.gov.educ.penreg.api.exception.SagaRuntimeException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 4 with SagaRuntimeException

use of ca.bc.gov.educ.penreg.api.exception.SagaRuntimeException in project EDUC-PEN-REG-BATCH-API by bcgov.

the class PenRequestBatchSagaController method processBatchRequest.

private ResponseEntity<String> processBatchRequest(final SagaEnum sagaName, final PenRequestBatchRepostReportsFilesSagaData penRequestBatchSagaData) {
    final var penRequestBatchID = penRequestBatchSagaData.getPenRequestBatchID();
    final var sagaInProgress = !this.getSagaService().findAllByPenRequestBatchIDInAndStatusIn(List.of(penRequestBatchID), this.getStatusesFilter()).isEmpty();
    if (sagaInProgress) {
        return ResponseEntity.status(HttpStatus.CONFLICT).build();
    }
    final var orchestrator = this.getOrchestratorMap().get(sagaName.toString());
    try {
        final var saga = orchestrator.createSaga(JsonUtil.getJsonStringFromObject(penRequestBatchSagaData), null, penRequestBatchID, penRequestBatchSagaData.getCreateUser());
        orchestrator.startSaga(saga);
        return ResponseEntity.ok(saga.getSagaId().toString());
    } catch (final JsonProcessingException e) {
        log.error("JsonProcessingException while processStudentRequest", e);
        throw new SagaRuntimeException(e.getMessage());
    }
}
Also used : SagaRuntimeException(ca.bc.gov.educ.penreg.api.exception.SagaRuntimeException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

SagaRuntimeException (ca.bc.gov.educ.penreg.api.exception.SagaRuntimeException)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)4 SagaEnum (ca.bc.gov.educ.penreg.api.constants.SagaEnum)1 SagaStatusEnum (ca.bc.gov.educ.penreg.api.constants.SagaStatusEnum)1 PenRequestBatchSagaEndpoint (ca.bc.gov.educ.penreg.api.endpoint.v1.PenRequestBatchSagaEndpoint)1 InvalidParameterException (ca.bc.gov.educ.penreg.api.exception.InvalidParameterException)1 InvalidPayloadException (ca.bc.gov.educ.penreg.api.exception.InvalidPayloadException)1 ApiError (ca.bc.gov.educ.penreg.api.exception.errors.ApiError)1 SagaFilterSpecs (ca.bc.gov.educ.penreg.api.filter.SagaFilterSpecs)1 ArchiveAndReturnSagaResponseMapper (ca.bc.gov.educ.penreg.api.mappers.v1.ArchiveAndReturnSagaResponseMapper)1 SagaMapper (ca.bc.gov.educ.penreg.api.mappers.v1.SagaMapper)1 Orchestrator (ca.bc.gov.educ.penreg.api.orchestrator.base.Orchestrator)1 SagaService (ca.bc.gov.educ.penreg.api.service.SagaService)1 BasePenRequestBatchStudentSagaData (ca.bc.gov.educ.penreg.api.struct.BasePenRequestBatchStudentSagaData)1 PenRequestBatchUnmatchSagaData (ca.bc.gov.educ.penreg.api.struct.PenRequestBatchUnmatchSagaData)1 PenRequestBatchUserActionsSagaData (ca.bc.gov.educ.penreg.api.struct.PenRequestBatchUserActionsSagaData)1 ca.bc.gov.educ.penreg.api.struct.v1 (ca.bc.gov.educ.penreg.api.struct.v1)1 JsonUtil (ca.bc.gov.educ.penreg.api.util.JsonUtil)1 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1