Search in sources :

Example 1 with Saga

use of ca.bc.gov.educ.penreg.api.struct.v1.Saga 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 Saga

use of ca.bc.gov.educ.penreg.api.struct.v1.Saga in project EDUC-PEN-REG-BATCH-API by bcgov.

the class BaseUserActionsOrchestrator method updatePenRequestBatchStudent.

/**
 * Update PEN Request Batch record and PRB Student record.
 *
 * @param event the event
 * @param saga  the saga
 * @param t     the pen request batch student saga data
 * @throws JsonProcessingException the json processing exception
 */
protected void updatePenRequestBatchStudent(final Event event, final Saga saga, final T t) throws JsonProcessingException {
    final PenRequestBatchStudent prbStudent = this.createPRBStudent(event, t);
    saga.setSagaState(UPDATE_PEN_REQUEST_BATCH_STUDENT.toString());
    final SagaEvent eventStates = this.createEventState(saga, event.getEventType(), event.getEventOutcome(), event.getEventPayload());
    this.getSagaService().updateAttachedSagaWithEvents(saga, eventStates);
    final Event nextEvent = Event.builder().sagaId(saga.getSagaId()).eventType(UPDATE_PEN_REQUEST_BATCH_STUDENT).replyTo(this.getTopicToSubscribe()).eventPayload(JsonUtil.getJsonStringFromObject(prbStudent)).build();
    this.postMessageToTopic(PEN_REQUEST_BATCH_API_TOPIC.toString(), nextEvent);
    log.info("message sent to PEN_REQUEST_BATCH_API_TOPIC for UPDATE_PEN_REQUEST_BATCH_STUDENT Event. :: {}", saga.getSagaId());
}
Also used : SagaEvent(ca.bc.gov.educ.penreg.api.model.v1.SagaEvent) Event(ca.bc.gov.educ.penreg.api.struct.Event) PenRequestBatchStudent(ca.bc.gov.educ.penreg.api.struct.v1.PenRequestBatchStudent) SagaEvent(ca.bc.gov.educ.penreg.api.model.v1.SagaEvent)

Example 3 with Saga

use of ca.bc.gov.educ.penreg.api.struct.v1.Saga in project EDUC-PEN-REG-BATCH-API by bcgov.

the class PenReqBatchNewPenOrchestrator method addPossibleMatchesToStudent.

private void addPossibleMatchesToStudent(final Event event, final Saga saga, final PenRequestBatchUserActionsSagaData penRequestBatchUserActionsSagaData) throws JsonProcessingException {
    final String studentID;
    // this scenario might occur during replay when message could not reach batch api from student-api and batch api retried the same flow.
    if (event.getEventOutcome() == STUDENT_ALREADY_EXIST) {
        studentID = event.getEventPayload();
    } else {
        final Student student = JsonUtil.getJsonObjectFromString(Student.class, event.getEventPayload());
        studentID = student.getStudentID();
    }
    penRequestBatchUserActionsSagaData.setStudentID(studentID);
    final SagaEvent eventStates = this.createEventState(saga, event.getEventType(), event.getEventOutcome(), event.getEventPayload());
    // set current event as saga state.
    saga.setSagaState(ADD_POSSIBLE_MATCH.toString());
    // resave the updated saga data.
    saga.setPayload(JsonUtil.getJsonStringFromObject(penRequestBatchUserActionsSagaData));
    this.getSagaService().updateAttachedSagaWithEvents(saga, eventStates);
    final var possibleMatches = penRequestBatchUserActionsSagaData.getMatchedStudentIDList().stream().map(matchedStudentID -> PossibleMatch.builder().createUser(penRequestBatchUserActionsSagaData.getCreateUser()).updateUser(penRequestBatchUserActionsSagaData.getUpdateUser()).studentID(studentID).matchedStudentID(matchedStudentID).matchReasonCode(TwinReasonCodes.PENCREATE.getCode()).build()).collect(Collectors.toList());
    final Event nextEvent = Event.builder().sagaId(saga.getSagaId()).eventType(ADD_POSSIBLE_MATCH).replyTo(this.getTopicToSubscribe()).eventPayload(JsonUtil.getJsonStringFromObject(possibleMatches)).build();
    this.postMessageToTopic(PEN_MATCH_API_TOPIC.toString(), nextEvent);
    log.info("message sent to PEN_MATCH_API_TOPIC for ADD_POSSIBLE_MATCH Event.");
}
Also used : EventType(ca.bc.gov.educ.penreg.api.constants.EventType) LocalDateTime(java.time.LocalDateTime) Autowired(org.springframework.beans.factory.annotation.Autowired) PenRequestBatchStudent(ca.bc.gov.educ.penreg.api.struct.v1.PenRequestBatchStudent) JsonUtil(ca.bc.gov.educ.penreg.api.util.JsonUtil) StringUtils(org.apache.commons.lang3.StringUtils) TypeReference(com.fasterxml.jackson.core.type.TypeReference) RestUtils(ca.bc.gov.educ.penreg.api.rest.RestUtils) SagaTopicsEnum(ca.bc.gov.educ.penreg.api.constants.SagaTopicsEnum) SagaEvent(ca.bc.gov.educ.penreg.api.model.v1.SagaEvent) PenRequestBatchUserActionsSagaData(ca.bc.gov.educ.penreg.api.struct.PenRequestBatchUserActionsSagaData) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) lombok.val(lombok.val) SagaService(ca.bc.gov.educ.penreg.api.service.SagaService) EventOutcome(ca.bc.gov.educ.penreg.api.constants.EventOutcome) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) USR_NEW_PEN(ca.bc.gov.educ.penreg.api.constants.PenRequestBatchStudentStatusCodes.USR_NEW_PEN) Collectors(java.util.stream.Collectors) Event(ca.bc.gov.educ.penreg.api.struct.Event) PEN_REQUEST_BATCH_NEW_PEN_PROCESSING_SAGA(ca.bc.gov.educ.penreg.api.constants.SagaEnum.PEN_REQUEST_BATCH_NEW_PEN_PROCESSING_SAGA) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) List(java.util.List) Student(ca.bc.gov.educ.penreg.api.struct.Student) TwinReasonCodes(ca.bc.gov.educ.penreg.api.constants.TwinReasonCodes) Saga(ca.bc.gov.educ.penreg.api.model.v1.Saga) CollectionUtils(org.springframework.util.CollectionUtils) PossibleMatch(ca.bc.gov.educ.penreg.api.struct.v1.PossibleMatch) REQ_NEW(ca.bc.gov.educ.penreg.api.constants.StudentHistoryActivityCode.REQ_NEW) PenRegAPIRuntimeException(ca.bc.gov.educ.penreg.api.exception.PenRegAPIRuntimeException) MessagePublisher(ca.bc.gov.educ.penreg.api.messaging.MessagePublisher) SagaEvent(ca.bc.gov.educ.penreg.api.model.v1.SagaEvent) Event(ca.bc.gov.educ.penreg.api.struct.Event) PenRequestBatchStudent(ca.bc.gov.educ.penreg.api.struct.v1.PenRequestBatchStudent) Student(ca.bc.gov.educ.penreg.api.struct.Student) SagaEvent(ca.bc.gov.educ.penreg.api.model.v1.SagaEvent)

Example 4 with Saga

use of ca.bc.gov.educ.penreg.api.struct.v1.Saga in project EDUC-PEN-REG-BATCH-API by bcgov.

the class PenReqBatchNewPenOrchestrator method updateSagaData.

/**
 * Update saga data.
 *
 * @param event                              the event
 * @param penRequestBatchUserActionsSagaData the pen request batch user actions saga data
 * @return the pen request batch user actions saga data
 * @throws JsonProcessingException the json processing exception
 */
protected PenRequestBatchUserActionsSagaData updateSagaData(final Event event, final PenRequestBatchUserActionsSagaData penRequestBatchUserActionsSagaData) throws JsonProcessingException {
    if (event.getEventType() == CREATE_STUDENT) {
        if (event.getEventOutcome() == STUDENT_ALREADY_EXIST) {
            penRequestBatchUserActionsSagaData.setStudentID(event.getEventPayload());
        } else {
            final Student student = JsonUtil.getJsonObjectFromString(Student.class, event.getEventPayload());
            penRequestBatchUserActionsSagaData.setStudentID(student.getStudentID());
        }
    }
    return penRequestBatchUserActionsSagaData;
}
Also used : PenRequestBatchStudent(ca.bc.gov.educ.penreg.api.struct.v1.PenRequestBatchStudent) Student(ca.bc.gov.educ.penreg.api.struct.Student)

Example 5 with Saga

use of ca.bc.gov.educ.penreg.api.struct.v1.Saga in project EDUC-PEN-REG-BATCH-API by bcgov.

the class PenReqBatchNewPenOrchestrator method updatePenRequestBatchStudent.

/**
 * Update PEN Request Batch record and PRB Student record.
 *
 * @param event the event
 * @param saga  the saga
 * @param penRequestBatchUserActionsSagaData     the pen request batch student saga data
 * @throws JsonProcessingException the json processing exception
 */
@Override
protected void updatePenRequestBatchStudent(final Event event, final Saga saga, final PenRequestBatchUserActionsSagaData penRequestBatchUserActionsSagaData) throws JsonProcessingException {
    this.updateSagaData(event, penRequestBatchUserActionsSagaData);
    saga.setSagaState(UPDATE_PEN_REQUEST_BATCH_STUDENT.toString());
    saga.setPayload(JsonUtil.getJsonStringFromObject(penRequestBatchUserActionsSagaData));
    final SagaEvent eventStates = this.createEventState(saga, event.getEventType(), event.getEventOutcome(), event.getEventPayload());
    this.getSagaService().updateAttachedSagaWithEvents(saga, eventStates);
    if (penRequestBatchUserActionsSagaData.getStudentID() == null) {
        log.info("studentID in saga data is null for batch student id :: {} and saga id :: {}, setting it from event states table", penRequestBatchUserActionsSagaData.getPenRequestBatchStudentID(), saga.getSagaId());
        val sagaEventOptional = this.getSagaService().findSagaEvent(saga, CREATE_STUDENT.toString(), 3);
        if (sagaEventOptional.isPresent()) {
            val sagaEvent = sagaEventOptional.get();
            if (STUDENT_CREATED.toString().equals(sagaEvent.getSagaEventOutcome())) {
                final Student student = JsonUtil.getJsonObjectFromString(Student.class, sagaEvent.getSagaEventResponse());
                penRequestBatchUserActionsSagaData.setStudentID(student.getStudentID());
            } else if (STUDENT_ALREADY_EXIST.toString().equals(sagaEvent.getSagaEventOutcome())) {
                penRequestBatchUserActionsSagaData.setStudentID(sagaEvent.getSagaEventResponse());
            } else {
                throw new PenRegAPIRuntimeException("CREATE_STUDENT event with outcome not found in event states table for saga id :: " + saga.getSagaId());
            }
        } else {
            throw new PenRegAPIRuntimeException("CREATE_STUDENT event not found in event states table for saga id :: " + saga.getSagaId());
        }
    }
    final PenRequestBatchStudent prbStudent = this.createPRBStudent(event, penRequestBatchUserActionsSagaData);
    final Event nextEvent = Event.builder().sagaId(saga.getSagaId()).eventType(UPDATE_PEN_REQUEST_BATCH_STUDENT).replyTo(this.getTopicToSubscribe()).eventPayload(JsonUtil.getJsonStringFromObject(prbStudent)).build();
    this.postMessageToTopic(PEN_REQUEST_BATCH_API_TOPIC.toString(), nextEvent);
    log.info("message sent to PEN_REQUEST_BATCH_API_TOPIC for UPDATE_PEN_REQUEST_BATCH_STUDENT Event. :: {}", saga.getSagaId());
}
Also used : lombok.val(lombok.val) PenRegAPIRuntimeException(ca.bc.gov.educ.penreg.api.exception.PenRegAPIRuntimeException) SagaEvent(ca.bc.gov.educ.penreg.api.model.v1.SagaEvent) Event(ca.bc.gov.educ.penreg.api.struct.Event) PenRequestBatchStudent(ca.bc.gov.educ.penreg.api.struct.v1.PenRequestBatchStudent) Student(ca.bc.gov.educ.penreg.api.struct.Student) PenRequestBatchStudent(ca.bc.gov.educ.penreg.api.struct.v1.PenRequestBatchStudent) SagaEvent(ca.bc.gov.educ.penreg.api.model.v1.SagaEvent)

Aggregations

lombok.val (lombok.val)11 SagaEvent (ca.bc.gov.educ.penreg.api.model.v1.SagaEvent)9 Student (ca.bc.gov.educ.penreg.api.struct.Student)9 Event (ca.bc.gov.educ.penreg.api.struct.Event)8 PenRequestBatchStudent (ca.bc.gov.educ.penreg.api.struct.v1.PenRequestBatchStudent)8 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)7 SagaService (ca.bc.gov.educ.penreg.api.service.SagaService)6 JsonUtil (ca.bc.gov.educ.penreg.api.util.JsonUtil)6 LocalDateTime (java.time.LocalDateTime)6 Collectors (java.util.stream.Collectors)6 TypeReference (com.fasterxml.jackson.core.type.TypeReference)5 Autowired (org.springframework.beans.factory.annotation.Autowired)5 BasePenRegAPITest (ca.bc.gov.educ.penreg.api.BasePenRegAPITest)4 SagaEnum (ca.bc.gov.educ.penreg.api.constants.SagaEnum)4 SagaMapper (ca.bc.gov.educ.penreg.api.mappers.v1.SagaMapper)4 java.util (java.util)4 PenRegAPIRuntimeException (ca.bc.gov.educ.penreg.api.exception.PenRegAPIRuntimeException)3 FilterOperation (ca.bc.gov.educ.penreg.api.filter.FilterOperation)3 PenRequestBatchEntity (ca.bc.gov.educ.penreg.api.model.v1.PenRequestBatchEntity)3 Saga (ca.bc.gov.educ.penreg.api.struct.v1.Saga)3