Search in sources :

Example 11 with Saga

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

the class PenReqBatchStudentOrchestrator method processPenMatch.

/**
 * Process pen match.
 *
 * @param event                          the event
 * @param saga                           the saga
 * @param penRequestBatchStudentSagaData the pen request batch student saga data
 */
protected void processPenMatch(final Event event, final Saga saga, final PenRequestBatchStudentSagaData penRequestBatchStudentSagaData) {
    final SagaEvent eventStates = this.createEventState(saga, event.getEventType(), event.getEventOutcome(), event.getEventPayload());
    saga.setSagaState(PROCESS_PEN_MATCH.toString());
    this.getSagaService().updateAttachedSagaWithEvents(saga, eventStates);
    // need to persist the validation warnings from response payload.
    this.saveDemogValidationResults(event, penRequestBatchStudentSagaData);
    final var eventPayload = JsonUtil.getJsonString(penMatchSagaMapper.toPenMatchStudent(penRequestBatchStudentSagaData));
    if (eventPayload.isPresent()) {
        final Event nextEvent = Event.builder().sagaId(saga.getSagaId()).eventType(PROCESS_PEN_MATCH).replyTo(this.getTopicToSubscribe()).eventPayload(eventPayload.get()).build();
        this.postMessageToTopic(PEN_MATCH_API_TOPIC.toString(), nextEvent);
        log.info("message sent to PEN_MATCH_API_TOPIC for PROCESS_PEN_MATCH Event. :: {}", saga.getSagaId());
    } else {
        log.error("event payload is not present this should not have happened. :: {}", saga.getSagaId());
    }
}
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 12 with Saga

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

the class PenReqBatchUserMatchOrchestrator method updateStudent.

/**
 * the following attributes on the matched student record get updated based on the incoming PEN Request
 * mincode
 * Local ID
 * Student Grade Code
 * Postal Code
 *
 * @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 updateStudent(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(UPDATE_STUDENT.toString());
    final Student studentDataFromEventResponse = JsonUtil.getJsonObjectFromString(Student.class, event.getEventPayload());
    studentDataFromEventResponse.setUpdateUser(penRequestBatchUserActionsSagaData.getUpdateUser());
    studentDataFromEventResponse.setMincode(penRequestBatchUserActionsSagaData.getMincode());
    studentDataFromEventResponse.setLocalID(penRequestBatchUserActionsSagaData.getLocalID());
    updateGradeCodeAndGradeYear(studentDataFromEventResponse, penRequestBatchUserActionsSagaData);
    updateUsualNameFields(studentDataFromEventResponse, penRequestBatchUserActionsSagaData);
    studentDataFromEventResponse.setPostalCode(penRequestBatchUserActionsSagaData.getPostalCode());
    studentDataFromEventResponse.setHistoryActivityCode(REQ_MATCH.getCode());
    penRequestBatchUserActionsSagaData.setStudentID(studentDataFromEventResponse.getStudentID());
    this.getSagaService().updateAttachedSagaWithEvents(saga, eventStates);
    final Event nextEvent = Event.builder().sagaId(saga.getSagaId()).eventType(UPDATE_STUDENT).replyTo(this.getTopicToSubscribe()).eventPayload(JsonUtil.getJsonStringFromObject(studentDataFromEventResponse)).build();
    this.postMessageToTopic(STUDENT_API_TOPIC.toString(), nextEvent);
    log.info("message sent to STUDENT_API_TOPIC for UPDATE_STUDENT Event.");
}
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) Student(ca.bc.gov.educ.penreg.api.struct.Student) SagaEvent(ca.bc.gov.educ.penreg.api.model.v1.SagaEvent)

Example 13 with Saga

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

the class PenReqBatchUserUnmatchOrchestrator method revertStudentInformation.

/**
 * This function will revert the student information to the previous state in history before the latest REQ_MATCH in the following steps:
 * 1) It will sort the student's Audit History in DESC order.
 * 2) It will find the record right before REQ_MATCH
 * 3) It will then revert the student record by updating it to the record right before REQ_MATCH
 *
 * the following attributes on the unmatched student record will be updated
 * First usual name
 * Middle usual name
 * Last usual name
 * Mincode
 * Local ID
 * Student Grade Code
 * Grade Year
 * Postal Code
 *
 * @param event                              the event
 * @param saga                               the saga
 * @param penRequestBatchUnmatchSagaData the pen request batch user actions unmatch saga data
 * @throws JsonProcessingException the json processing exception
 */
protected void revertStudentInformation(final Event event, final Saga saga, final PenRequestBatchUnmatchSagaData penRequestBatchUnmatchSagaData) throws JsonProcessingException, IOException, InterruptedException, TimeoutException {
    StudentHistory studentHistoryForRevert = null;
    final SagaEvent eventStates = this.createEventState(saga, event.getEventType(), event.getEventOutcome(), event.getEventPayload());
    // set current event as saga state.
    saga.setSagaState(UPDATE_STUDENT.toString());
    this.getSagaService().updateAttachedSagaWithEvents(saga, eventStates);
    // convert payload to StudentHistory List
    final ObjectMapper objectMapper = new ObjectMapper();
    final JavaType type = objectMapper.getTypeFactory().constructCollectionType(List.class, StudentHistory.class);
    final List<StudentHistory> historyList = objectMapper.readValue(event.getEventPayload(), type);
    // sort list in DESC order by CreateDate
    Collections.sort(historyList, Collections.reverseOrder(Comparator.comparing(StudentHistory::getCreateDate)));
    // find the most recent student history record before the REQ MATCH.
    for (int i = 0; i < historyList.size(); i++) {
        if (StringUtils.equals(historyList.get(i).getHistoryActivityCode(), StudentHistoryActivityCode.REQ_MATCH.getCode())) {
            studentHistoryForRevert = historyList.get(i + 1);
            log.debug("reverting student with this student audit history record ::{}", studentHistoryForRevert);
            break;
        }
    }
    if (studentHistoryForRevert == null) {
        log.debug("student audit history did not contain a REQ_MATCH. Student record will not be updated, but we need to complete SAGA");
        this.handleEvent(Event.builder().sagaId(saga.getSagaId()).eventType(UPDATE_STUDENT).eventOutcome(STUDENT_UPDATED).build());
    } else {
        // grab the student's most recent record to update.
        final Student studentInformation = this.restUtils.getStudentByStudentID(penRequestBatchUnmatchSagaData.getStudentID());
        studentInformation.setUpdateUser(penRequestBatchUnmatchSagaData.getUpdateUser());
        studentInformation.setUsualFirstName(studentHistoryForRevert.getUsualFirstName());
        studentInformation.setUsualMiddleNames(studentHistoryForRevert.getUsualMiddleNames());
        studentInformation.setUsualLastName(studentHistoryForRevert.getUsualLastName());
        studentInformation.setMincode(studentHistoryForRevert.getMincode());
        studentInformation.setLocalID(studentHistoryForRevert.getLocalID());
        studentInformation.setGradeCode(studentHistoryForRevert.getGradeCode());
        studentInformation.setGradeYear(studentHistoryForRevert.getGradeYear());
        studentInformation.setPostalCode(studentHistoryForRevert.getPostalCode());
        studentInformation.setHistoryActivityCode(StudentHistoryActivityCode.REQ_UNMATCH.getCode());
        final Event nextEvent = Event.builder().sagaId(saga.getSagaId()).eventType(UPDATE_STUDENT).replyTo(this.getTopicToSubscribe()).eventPayload(JsonUtil.getJsonStringFromObject(studentInformation)).build();
        this.postMessageToTopic(STUDENT_API_TOPIC.toString(), nextEvent);
        log.info("message sent to STUDENT_API_TOPIC for UPDATE_STUDENT Event.");
    }
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) StudentHistory(ca.bc.gov.educ.penreg.api.struct.v1.StudentHistory) 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) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SagaEvent(ca.bc.gov.educ.penreg.api.model.v1.SagaEvent)

Example 14 with Saga

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

the class PenReqBatchUserUnmatchOrchestrator method deletePossibleMatchesFromStudent.

/**
 * this method expects that the twin ids provided in the payload here is already validated.
 * Delete twin records to student.
 *
 * @param event                          the event
 * @param saga                           the saga
 * @param penRequestBatchUnmatchSagaData the pen request batch user actions saga data
 * @throws JsonProcessingException the json processing exception
 */
protected void deletePossibleMatchesFromStudent(final Event event, final Saga saga, final PenRequestBatchUnmatchSagaData penRequestBatchUnmatchSagaData) throws JsonProcessingException {
    final SagaEvent eventStates = this.createEventState(saga, event.getEventType(), event.getEventOutcome(), event.getEventPayload());
    // set current event as saga state.
    saga.setSagaState(DELETE_POSSIBLE_MATCH.toString());
    this.getSagaService().updateAttachedSagaWithEvents(saga, eventStates);
    final List<PossibleMatch> possibleMatches = new ArrayList<>();
    penRequestBatchUnmatchSagaData.getMatchedStudentIDList().forEach(element -> {
        final PossibleMatch possibleMatch = new PossibleMatch();
        possibleMatch.setStudentID(penRequestBatchUnmatchSagaData.getStudentID());
        possibleMatch.setMatchedStudentID(element);
        possibleMatch.setCreateUser(penRequestBatchUnmatchSagaData.getCreateUser());
        possibleMatch.setUpdateUser(penRequestBatchUnmatchSagaData.getUpdateUser());
        possibleMatches.add(possibleMatch);
    });
    final Event nextEvent = Event.builder().sagaId(saga.getSagaId()).eventType(DELETE_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 DELETE_POSSIBLE_MATCH Event.");
}
Also used : PossibleMatch(ca.bc.gov.educ.penreg.api.struct.v1.PossibleMatch) ArrayList(java.util.ArrayList) 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 15 with Saga

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

the class PenRequestBatchArchiveAndReturnOrchestrator method saveReportsWithoutPDF.

private void saveReportsWithoutPDF(Event event, Saga saga, PenRequestBatchArchiveAndReturnSagaData penRequestBatchArchiveAndReturnSagaData) throws IOException, InterruptedException, TimeoutException {
    SagaEvent eventStates = this.createEventState(saga, event.getEventType(), event.getEventOutcome(), event.getEventPayload());
    saga.setSagaState(SAVE_REPORTS.toString());
    penRequestBatchArchiveAndReturnSagaData.setPenRequestBatch(JsonUtil.getJsonObjectFromString(PenRequestBatch.class, event.getEventPayload()));
    // save the updated payload to DB...
    saga.setPayload(JsonUtil.getJsonStringFromObject(penRequestBatchArchiveAndReturnSagaData));
    this.getSagaService().updateAttachedSagaWithEvents(saga, eventStates);
    if (penRequestBatchArchiveAndReturnSagaData.getStudents() == null) {
        log.info("students in saga data is null or empty for batch id :: {} and saga id :: {}, setting it from event states table", penRequestBatchArchiveAndReturnSagaData.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<>() {
            });
            penRequestBatchArchiveAndReturnSagaData.setStudents(event, students);
        } else {
            throw new PenRegAPIRuntimeException("students not found in event states table for saga id :: " + saga.getSagaId());
        }
    }
    this.getResponseFileGeneratorService().saveReports(mapper.toModel(penRequestBatchArchiveAndReturnSagaData.getPenRequestBatch()), penRequestBatchArchiveAndReturnSagaData.getPenRequestBatchStudents(), penRequestBatchArchiveAndReturnSagaData.getStudents(), reportMapper.toReportData(penRequestBatchArchiveAndReturnSagaData));
    val nextEvent = Event.builder().sagaId(saga.getSagaId()).eventType(SAVE_REPORTS).eventOutcome(REPORTS_SAVED).build();
    this.handleEvent(nextEvent);
}
Also used : lombok.val(lombok.val) PenRequestBatch(ca.bc.gov.educ.penreg.api.struct.v1.PenRequestBatch) 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)

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)16 Student (ca.bc.gov.educ.penreg.api.struct.Student)11 PenRegAPIRuntimeException (ca.bc.gov.educ.penreg.api.exception.PenRegAPIRuntimeException)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 SagaService (ca.bc.gov.educ.penreg.api.service.SagaService)5 PenRequestBatchStudent (ca.bc.gov.educ.penreg.api.struct.v1.PenRequestBatchStudent)5 JsonUtil (ca.bc.gov.educ.penreg.api.util.JsonUtil)5 LocalDateTime (java.time.LocalDateTime)5 Collectors (java.util.stream.Collectors)5 BasePenRegAPITest (ca.bc.gov.educ.penreg.api.BasePenRegAPITest)4 PenRequestBatchEntity (ca.bc.gov.educ.penreg.api.model.v1.PenRequestBatchEntity)4 Saga (ca.bc.gov.educ.penreg.api.struct.v1.Saga)4 TypeReference (com.fasterxml.jackson.core.type.TypeReference)4 Autowired (org.springframework.beans.factory.annotation.Autowired)4 EventOutcome (ca.bc.gov.educ.penreg.api.constants.EventOutcome)3 EventType (ca.bc.gov.educ.penreg.api.constants.EventType)3 SagaEnum (ca.bc.gov.educ.penreg.api.constants.SagaEnum)3 FilterOperation (ca.bc.gov.educ.penreg.api.filter.FilterOperation)3