Search in sources :

Example 1 with ArchiveAndReturnSagaResponseMapper

use of ca.bc.gov.educ.penreg.api.mappers.v1.ArchiveAndReturnSagaResponseMapper 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)

Aggregations

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 SagaRuntimeException (ca.bc.gov.educ.penreg.api.exception.SagaRuntimeException)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 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1