Search in sources :

Example 21 with SagaEvent

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

the class BaseOrchestrator method replayFromLastEvent.

/**
 * This method will restart the saga process from where it was left the last time. which could occur due to various reasons
 *
 * @param saga        the model object.
 * @param eventStates the event states corresponding to the saga
 * @param t           the payload string as an object
 * @throws InterruptedException if thread is interrupted.
 * @throws TimeoutException     if connection to messaging system times out.
 * @throws IOException          if there is connectivity problem
 */
private void replayFromLastEvent(final Saga saga, final List<SagaEvent> eventStates, final T t) throws InterruptedException, TimeoutException, IOException {
    val sagaEventOptional = this.findTheLastEventOccurred(eventStates);
    if (sagaEventOptional.isPresent()) {
        val sagaEvent = sagaEventOptional.get();
        log.trace(sagaEventOptional.toString());
        final EventType currentEvent = EventType.valueOf(sagaEvent.getSagaEventState());
        final EventOutcome eventOutcome = EventOutcome.valueOf(sagaEvent.getSagaEventOutcome());
        final Event event = Event.builder().eventOutcome(eventOutcome).eventType(currentEvent).eventPayload(sagaEvent.getSagaEventResponse()).build();
        final Optional<SagaEventState<T>> sagaEventState = this.findNextSagaEventState(currentEvent, eventOutcome, t);
        if (sagaEventState.isPresent()) {
            log.trace(SYSTEM_IS_GOING_TO_EXECUTE_NEXT_EVENT_FOR_CURRENT_EVENT, sagaEventState.get().getNextEventType(), event.toString(), saga.getSagaId());
            this.invokeNextEvent(event, saga, t, sagaEventState.get());
        }
    }
}
Also used : lombok.val(lombok.val) EventOutcome(ca.bc.gov.educ.penreg.api.constants.EventOutcome) EventType(ca.bc.gov.educ.penreg.api.constants.EventType) SagaEvent(ca.bc.gov.educ.penreg.api.model.v1.SagaEvent) Event(ca.bc.gov.educ.penreg.api.struct.Event) NotificationEvent(ca.bc.gov.educ.penreg.api.struct.NotificationEvent)

Example 22 with SagaEvent

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

the class PenReqBatchUserMatchOrchestrator method getStudentByPen.

/**
 * Gets student by pen.
 *
 * @param event                              the event
 * @param saga                               the saga
 * @param penRequestBatchUserActionsSagaData the pen request batch user actions saga data
 */
protected void getStudentByPen(final Event event, final Saga saga, final PenRequestBatchUserActionsSagaData penRequestBatchUserActionsSagaData) {
    final SagaEvent eventStates = this.createEventState(saga, event.getEventType(), event.getEventOutcome(), event.getEventPayload());
    saga.setStatus(IN_PROGRESS.toString());
    // set current event as saga state.
    saga.setSagaState(GET_STUDENT.toString());
    this.getSagaService().updateAttachedSagaWithEvents(saga, eventStates);
    final Event nextEvent = Event.builder().sagaId(saga.getSagaId()).eventType(GET_STUDENT).replyTo(this.getTopicToSubscribe()).eventPayload(penRequestBatchUserActionsSagaData.getAssignedPEN()).build();
    this.postMessageToTopic(STUDENT_API_TOPIC.toString(), nextEvent);
    log.info("message sent to STUDENT_API_TOPIC for GET_STUDENT Event.");
}
Also used : SagaEvent(ca.bc.gov.educ.penreg.api.model.v1.SagaEvent) Event(ca.bc.gov.educ.penreg.api.struct.Event) SagaEvent(ca.bc.gov.educ.penreg.api.model.v1.SagaEvent)

Example 23 with SagaEvent

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

the class PenReqBatchUserMatchOrchestrator method addPossibleMatchToStudent.

/**
 * this method expects that the twin ids provided in the payload here is already validated.
 * Add twin records to student.
 *
 * @param event                              the event
 * @param saga                               the saga
 * @param penRequestBatchUserActionsSagaData the pen request batch user actions saga data
 * @throws JsonProcessingException the json processing exception
 */
protected void addPossibleMatchToStudent(final Event event, final Saga saga, final PenRequestBatchUserActionsSagaData penRequestBatchUserActionsSagaData) throws JsonProcessingException {
    final SagaEvent eventStates = this.createEventState(saga, event.getEventType(), event.getEventOutcome(), event.getEventPayload());
    // set current event as saga state.
    saga.setSagaState(ADD_POSSIBLE_MATCH.toString());
    this.getSagaService().updateAttachedSagaWithEvents(saga, eventStates);
    final var possibleMatches = penRequestBatchUserActionsSagaData.getMatchedStudentIDList().stream().map(matchedStudentID -> PossibleMatch.builder().createUser(penRequestBatchUserActionsSagaData.getCreateUser()).updateUser(penRequestBatchUserActionsSagaData.getUpdateUser()).studentID(penRequestBatchUserActionsSagaData.getStudentID()).matchedStudentID(matchedStudentID).matchReasonCode(TwinReasonCodes.PEN_MATCH.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) 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) PenRequestValidationIssue(ca.bc.gov.educ.penreg.api.struct.PenRequestValidationIssue) REQ_MATCH(ca.bc.gov.educ.penreg.api.constants.StudentHistoryActivityCode.REQ_MATCH) 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) USR_MATCHED(ca.bc.gov.educ.penreg.api.constants.PenRequestBatchStudentStatusCodes.USR_MATCHED) PenRequestBatchUserActionsSagaData(ca.bc.gov.educ.penreg.api.struct.PenRequestBatchUserActionsSagaData) lombok.val(lombok.val) PEN_REQUEST_BATCH_USER_MATCH_PROCESSING_SAGA(ca.bc.gov.educ.penreg.api.constants.SagaEnum.PEN_REQUEST_BATCH_USER_MATCH_PROCESSING_SAGA) SagaService(ca.bc.gov.educ.penreg.api.service.SagaService) EventOutcome(ca.bc.gov.educ.penreg.api.constants.EventOutcome) IN_PROGRESS(ca.bc.gov.educ.penreg.api.constants.SagaStatusEnum.IN_PROGRESS) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Collectors(java.util.stream.Collectors) Event(ca.bc.gov.educ.penreg.api.struct.Event) 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) 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) SagaEvent(ca.bc.gov.educ.penreg.api.model.v1.SagaEvent)

Example 24 with SagaEvent

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

the class PenReqBatchUserUnmatchOrchestrator method readAuditHistory.

/**
 * Obtaining student history records.
 *
 * @param event                        the event
 * @param saga                         the saga
 * @param penRequestBatchUnmatchSagaData the pen request batch user actions unmatch saga data
 */
protected void readAuditHistory(final Event event, final Saga saga, final PenRequestBatchUnmatchSagaData penRequestBatchUnmatchSagaData) {
    final SagaEvent eventStates = this.createEventState(saga, event.getEventType(), event.getEventOutcome(), event.getEventPayload());
    // set current event as saga state.
    saga.setSagaState(GET_STUDENT_HISTORY.toString());
    this.getSagaService().updateAttachedSagaWithEvents(saga, eventStates);
    final Event nextEvent = Event.builder().sagaId(saga.getSagaId()).eventType(GET_STUDENT_HISTORY).replyTo(this.getTopicToSubscribe()).eventPayload(penRequestBatchUnmatchSagaData.getStudentID()).build();
    this.postMessageToTopic(STUDENT_API_TOPIC.toString(), nextEvent);
    log.info("message sent to STUDENT_API_TOPIC for GET_STUDENT_HISTORY Event.");
}
Also used : SagaEvent(ca.bc.gov.educ.penreg.api.model.v1.SagaEvent) Event(ca.bc.gov.educ.penreg.api.struct.Event) SagaEvent(ca.bc.gov.educ.penreg.api.model.v1.SagaEvent)

Example 25 with SagaEvent

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

the class BaseReturnFilesOrchestrator method getStudents.

protected void getStudents(final Event event, final Saga saga, final BasePenRequestBatchReturnFilesSagaData penRequestBatchReturnFilesSagaData) throws IOException, TimeoutException, InterruptedException {
    final SagaEvent eventStates = this.createEventState(saga, event.getEventType(), event.getEventOutcome(), event.getEventPayload());
    saga.setSagaState(GET_STUDENTS.toString());
    this.getSagaService().updateAttachedSagaWithEvents(saga, eventStates);
    val nextEvent = Event.builder().sagaId(saga.getSagaId()).eventType(EventType.GET_STUDENTS).replyTo(this.getTopicToSubscribe()).build();
    if (penRequestBatchReturnFilesSagaData.getPenRequestBatchStudents() == null) {
        throw new PenRegAPIRuntimeException("penRequestBatchReturnFilesSagaData.getPenRequestBatchStudents() is null which is not expected in this flow for batch id :: " + penRequestBatchReturnFilesSagaData.getPenRequestBatchID());
    }
    final List<String> studentIDs = penRequestBatchReturnFilesSagaData.getPenRequestBatchStudents().stream().map(PenRequestBatchStudent::getStudentID).filter(Objects::nonNull).collect(Collectors.toList());
    if (studentIDs.isEmpty()) {
        nextEvent.setEventOutcome(STUDENTS_FOUND);
        nextEvent.setEventPayload("[]");
        this.handleEvent(nextEvent);
    } else {
        nextEvent.setEventPayload(JsonUtil.getJsonStringFromObject(studentIDs));
        this.postMessageToTopic(SagaTopicsEnum.STUDENT_API_TOPIC.toString(), nextEvent);
        log.info("message sent to STUDENT_API_TOPIC for {} Event. :: {}", GET_STUDENTS, 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)

Aggregations

SagaEvent (ca.bc.gov.educ.penreg.api.model.v1.SagaEvent)24 Event (ca.bc.gov.educ.penreg.api.struct.Event)18 lombok.val (lombok.val)13 Student (ca.bc.gov.educ.penreg.api.struct.Student)11 PenRegAPIRuntimeException (ca.bc.gov.educ.penreg.api.exception.PenRegAPIRuntimeException)6 PenRequestBatchStudent (ca.bc.gov.educ.penreg.api.struct.v1.PenRequestBatchStudent)6 EventOutcome (ca.bc.gov.educ.penreg.api.constants.EventOutcome)3 EventType (ca.bc.gov.educ.penreg.api.constants.EventType)3 SagaService (ca.bc.gov.educ.penreg.api.service.SagaService)3 PossibleMatch (ca.bc.gov.educ.penreg.api.struct.v1.PossibleMatch)3 JsonUtil (ca.bc.gov.educ.penreg.api.util.JsonUtil)3 LocalDateTime (java.time.LocalDateTime)3 Collectors (java.util.stream.Collectors)3 SagaTopicsEnum (ca.bc.gov.educ.penreg.api.constants.SagaTopicsEnum)2 TwinReasonCodes (ca.bc.gov.educ.penreg.api.constants.TwinReasonCodes)2 MessagePublisher (ca.bc.gov.educ.penreg.api.messaging.MessagePublisher)2 Saga (ca.bc.gov.educ.penreg.api.model.v1.Saga)2 RestUtils (ca.bc.gov.educ.penreg.api.rest.RestUtils)2 PenRequestBatchUserActionsSagaData (ca.bc.gov.educ.penreg.api.struct.PenRequestBatchUserActionsSagaData)2 PenRequestBatch (ca.bc.gov.educ.penreg.api.struct.v1.PenRequestBatch)2