Search in sources :

Example 1 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 gatherReportData.

protected void gatherReportData(final Event event, final Saga saga, final BasePenRequestBatchReturnFilesSagaData penRequestBatchReturnFilesSagaData) throws IOException, InterruptedException, TimeoutException {
    final SagaEvent eventStates = this.createEventState(saga, event.getEventType(), event.getEventOutcome(), event.getEventPayload());
    saga.setSagaState(GATHER_REPORT_DATA.toString());
    this.getSagaService().updateAttachedSagaWithEvents(saga, eventStates);
    final var penRequestBatch = this.getPenRequestBatchService().findById(penRequestBatchReturnFilesSagaData.getPenRequestBatchID());
    val nextEvent = Event.builder().sagaId(saga.getSagaId()).eventType(GATHER_REPORT_DATA).replyTo(this.getTopicToSubscribe()).build();
    if (penRequestBatch.isPresent()) {
        penRequestBatchReturnFilesSagaData.setPenRequestBatchStudentValidationIssues(this.getValidationIssues(penRequestBatch.get()));
        final List<PenRequestBatchStudent> studentRequests = penRequestBatch.get().getPenRequestBatchStudentEntities().stream().map(studentMapper::toStructure).collect(Collectors.toList());
        penRequestBatchReturnFilesSagaData.setPenRequestBatchStudents(studentRequests);
        penRequestBatchReturnFilesSagaData.setPenRequestBatch(mapper.toStructure(penRequestBatch.get()));
        penRequestBatchReturnFilesSagaData.setPenCoordinator(this.getPenCoordinator(penRequestBatch.get()));
        penRequestBatchReturnFilesSagaData.setFromEmail(this.penCoordinatorProperties.getFromEmail());
        penRequestBatchReturnFilesSagaData.setTelephone(this.penCoordinatorProperties.getTelephone());
        penRequestBatchReturnFilesSagaData.setFacsimile(this.penCoordinatorProperties.getFacsimile());
        penRequestBatchReturnFilesSagaData.setMailingAddress(this.penCoordinatorProperties.getMailingAddress());
        // save the updated payload to DB...
        saga.setPayload(JsonUtil.getJsonStringFromObject(penRequestBatchReturnFilesSagaData));
        this.getSagaService().updateAttachedSagaWithEvents(saga, eventStates);
        nextEvent.setEventOutcome(REPORT_DATA_GATHERED);
    } else {
        log.error("PenRequestBatch not found during archive and return saga. This is not expected :: {}", penRequestBatchReturnFilesSagaData.getPenRequestBatchID());
        nextEvent.setEventOutcome(EventOutcome.PEN_REQUEST_BATCH_NOT_FOUND);
    }
    this.handleEvent(nextEvent);
}
Also used : lombok.val(lombok.val) SagaEvent(ca.bc.gov.educ.penreg.api.model.v1.SagaEvent)

Example 2 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 sendArchivedEmail.

protected void sendArchivedEmail(final Event event, final Saga saga, final BasePenRequestBatchReturnFilesSagaData penRequestBatchReturnFilesSagaData, final EventType eventType) throws JsonProcessingException {
    final SagaEvent eventStates = this.createEventState(saga, event.getEventType(), event.getEventOutcome(), event.getEventPayload());
    saga.setSagaState(eventType.toString());
    this.getSagaService().updateAttachedSagaWithEvents(saga, eventStates);
    val prbStudentStatusCodeMap = penRequestBatchReturnFilesSagaData.getPenRequestBatchStudents() == null ? new HashMap<String, Long>() : penRequestBatchReturnFilesSagaData.getPenRequestBatchStudents().stream().collect(groupingBy(PenRequestBatchStudent::getPenRequestBatchStudentStatusCode, counting()));
    log.debug("PRBStudent status code map :: {}", prbStudentStatusCodeMap);
    final PendingRecords pendingRecords;
    // if all PRBStudent records have ERROR status then all of them are pending.
    if (this.getValueFromMap(ERROR.getCode(), prbStudentStatusCodeMap) > 0 && (this.getValueFromMap(ERROR.getCode(), prbStudentStatusCodeMap) == penRequestBatchReturnFilesSagaData.getPenRequestBatchStudents().size())) {
        pendingRecords = PendingRecords.ALL;
    } else if (this.areSomeRecordsPending(prbStudentStatusCodeMap)) {
        pendingRecords = PendingRecords.SOME;
    } else {
        pendingRecords = PendingRecords.NONE;
    }
    log.debug("Pending Records value :: {}", pendingRecords);
    final PenRequestBatchArchivedEmailEvent penRequestBatchArchivedEmailEvent = PenRequestBatchArchivedEmailEvent.builder().fromEmail(this.getPenCoordinatorProperties().getFromEmail()).mincode(penRequestBatchReturnFilesSagaData.getPenRequestBatch().getMincode()).submissionNumber(penRequestBatchReturnFilesSagaData.getPenRequestBatch().getSubmissionNumber()).schoolName(penRequestBatchReturnFilesSagaData.getSchoolName()).pendingRecords(pendingRecords).build();
    val nextEvent = Event.builder().sagaId(saga.getSagaId()).replyTo(this.getTopicToSubscribe()).build();
    // set toEmail and email type depending on whether a penCoordinator exists for the mincode
    if (eventType.equals(NOTIFY_PEN_REQUEST_BATCH_ARCHIVE_HAS_NO_SCHOOL_CONTACT)) {
        nextEvent.setEventType(NOTIFY_PEN_REQUEST_BATCH_ARCHIVE_HAS_NO_SCHOOL_CONTACT);
        penRequestBatchArchivedEmailEvent.setToEmail(this.getPenCoordinatorProperties().getFromEmail());
    } else {
        nextEvent.setEventType(NOTIFY_PEN_REQUEST_BATCH_ARCHIVE_HAS_CONTACT);
        penRequestBatchArchivedEmailEvent.setToEmail(penRequestBatchReturnFilesSagaData.getPenCoordinator().getPenCoordinatorEmail());
    }
    nextEvent.setEventPayload(JsonUtil.getJsonStringFromObject(penRequestBatchArchivedEmailEvent));
    this.postMessageToTopic(SagaTopicsEnum.PROFILE_REQUEST_EMAIL_API_TOPIC.toString(), nextEvent);
    log.info("message sent to PROFILE_REQUEST_EMAIL_API_TOPIC for {} Event. :: {}", eventType, saga.getSagaId());
}
Also used : lombok.val(lombok.val) SagaEvent(ca.bc.gov.educ.penreg.api.model.v1.SagaEvent)

Example 3 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 saveReports.

protected void saveReports(final Event event, final Saga saga, final BasePenRequestBatchReturnFilesSagaData penRequestBatchReturnFilesSagaData) throws IOException, InterruptedException, TimeoutException {
    final SagaEvent eventStates = this.createEventState(saga, event.getEventType(), event.getEventOutcome(), event.getEventPayload());
    saga.setSagaState(SAVE_REPORTS.toString());
    this.getSagaService().updateAttachedSagaWithEvents(saga, eventStates);
    if (penRequestBatchReturnFilesSagaData.getStudents() == null) {
        log.info("students in saga data is null or empty for batch id :: {} and saga id :: {}, setting it from event states table", penRequestBatchReturnFilesSagaData.getPenRequestBatchID(), saga.getSagaId());
        SagaEvent sagaEvent = SagaEvent.builder().sagaEventState(GET_STUDENTS.toString()).sagaEventOutcome(STUDENTS_FOUND.toString()).sagaStepNumber(3).build();
        val sagaEventOptional = this.getSagaService().findSagaEvent(saga, sagaEvent);
        if (sagaEventOptional.isPresent()) {
            List<Student> students = obMapper.readValue(sagaEventOptional.get().getSagaEventResponse(), new TypeReference<>() {
            });
            penRequestBatchReturnFilesSagaData.setStudents(event, students);
        } else {
            throw new PenRegAPIRuntimeException("students not found in event states table for saga id :: " + saga.getSagaId());
        }
    }
    this.getResponseFileGeneratorService().saveReports(event.getEventPayload(), mapper.toModel(penRequestBatchReturnFilesSagaData.getPenRequestBatch()), penRequestBatchReturnFilesSagaData.getPenRequestBatchStudents(), penRequestBatchReturnFilesSagaData.getStudents(), reportMapper.toReportData(penRequestBatchReturnFilesSagaData));
    val nextEvent = Event.builder().sagaId(saga.getSagaId()).eventType(SAVE_REPORTS).eventOutcome(REPORTS_SAVED).build();
    this.handleEvent(nextEvent);
}
Also used : lombok.val(lombok.val) PenRegAPIRuntimeException(ca.bc.gov.educ.penreg.api.exception.PenRegAPIRuntimeException) Student(ca.bc.gov.educ.penreg.api.struct.Student) SagaEvent(ca.bc.gov.educ.penreg.api.model.v1.SagaEvent)

Example 4 with SagaEvent

use of ca.bc.gov.educ.penreg.api.model.v1.SagaEvent 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 5 with SagaEvent

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

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