Search in sources :

Example 6 with Student

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

use of ca.bc.gov.educ.penreg.api.struct.Student 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 8 with Student

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

the class ResponseFileGeneratorService method getIDSBlob.

/**
 * Create an IDS file for a pen request batch entity
 *
 * @param penRequestBatchEntity the pen request batch entity
 * @return the pen web blob entity
 */
public PENWebBlobEntity getIDSBlob(final PenRequestBatchEntity penRequestBatchEntity, final List<PenRequestBatchStudent> penRequestBatchStudentEntities, final List<Student> students) {
    Map<String, Student> studentsMap = students.stream().collect(Collectors.toMap(Student::getStudentID, student -> student));
    final List<PenRequestBatchStudent> filteredStudents = penRequestBatchStudentEntities.stream().filter(x -> (x.getPenRequestBatchStudentStatusCode().equals(PenRequestBatchStudentStatusCodes.SYS_NEW_PEN.getCode()) || x.getPenRequestBatchStudentStatusCode().equals(PenRequestBatchStudentStatusCodes.USR_NEW_PEN.getCode()) || x.getPenRequestBatchStudentStatusCode().equals(PenRequestBatchStudentStatusCodes.SYS_MATCHED.getCode()) || x.getPenRequestBatchStudentStatusCode().equals(PenRequestBatchStudentStatusCodes.USR_MATCHED.getCode())) && x.getLocalID() != null).collect(Collectors.toList());
    byte[] bFile;
    if (!filteredStudents.isEmpty()) {
        final StringBuilder idsFile = new StringBuilder();
        for (final PenRequestBatchStudent entity : filteredStudents) {
            final var student = studentsMap.get(entity.getStudentID());
            if (student != null) {
                idsFile.append("E03").append(entity.getMincode()).append(String.format("%-12s", entity.getLocalID())).append(student.getPen()).append(" ").append(String.format("%-25s", student.getLegalLastName())).append("\r\n");
            } else {
                log.error("StudentId was not found. This should not have happened.");
            }
        }
        bFile = idsFile.toString().getBytes();
    } else {
        bFile = "No NEW PENS have been assigned by this PEN request".getBytes();
    }
    return PENWebBlobEntity.builder().mincode(penRequestBatchEntity.getMincode()).sourceApplication(getSourceApplication(penRequestBatchEntity.getSourceApplication())).fileName(penRequestBatchEntity.getMincode() + ".IDS").fileType("IDS").fileContents(bFile).insertDateTime(LocalDateTime.now()).submissionNumber(penRequestBatchEntity.getSubmissionNumber()).build();
}
Also used : PenRequestBatchReportData(ca.bc.gov.educ.penreg.api.struct.v1.reportstructs.PenRequestBatchReportData) SpringTemplateEngine(org.thymeleaf.spring5.SpringTemplateEngine) java.util(java.util) Getter(lombok.Getter) LocalDateTime(java.time.LocalDateTime) Autowired(org.springframework.beans.factory.annotation.Autowired) PenRequestBatchStudentRepository(ca.bc.gov.educ.penreg.api.repository.PenRequestBatchStudentRepository) PenRequestBatchStudent(ca.bc.gov.educ.penreg.api.struct.v1.PenRequestBatchStudent) StringUtils(org.apache.commons.lang3.StringUtils) PenRequestBatchEntity(ca.bc.gov.educ.penreg.api.model.v1.PenRequestBatchEntity) PENWebBlobEntity(ca.bc.gov.educ.penreg.api.model.v1.PENWebBlobEntity) Propagation(org.springframework.transaction.annotation.Propagation) Service(org.springframework.stereotype.Service) SchoolGroupCodes(ca.bc.gov.educ.penreg.api.constants.SchoolGroupCodes) RestUtils(ca.bc.gov.educ.penreg.api.rest.RestUtils) PenCoordinator(ca.bc.gov.educ.penreg.api.struct.v1.PenCoordinator) PenRequestBatchStudentStatusCodes(ca.bc.gov.educ.penreg.api.constants.PenRequestBatchStudentStatusCodes) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Slf4j(lombok.extern.slf4j.Slf4j) Student(ca.bc.gov.educ.penreg.api.struct.Student) DateTimeFormatter(java.time.format.DateTimeFormatter) PenWebBlobRepository(ca.bc.gov.educ.penreg.api.repository.PenWebBlobRepository) PenRegAPIRuntimeException(ca.bc.gov.educ.penreg.api.exception.PenRegAPIRuntimeException) Context(org.thymeleaf.context.Context) Transactional(org.springframework.transaction.annotation.Transactional) PRIVATE(lombok.AccessLevel.PRIVATE) 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)

Example 9 with Student

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

Example 10 with Student

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

the class PenRequestBatchArchiveAndReturnOrchestrator method archivePenRequestBatch.

private void archivePenRequestBatch(Event event, Saga saga, PenRequestBatchArchiveAndReturnSagaData penRequestBatchArchiveAndReturnSagaData) throws IOException {
    SagaEvent eventStates = this.createEventState(saga, event.getEventType(), event.getEventOutcome(), event.getEventPayload());
    saga.setSagaState(ARCHIVE_PEN_REQUEST_BATCH.toString());
    List<Student> students = obMapper.readValue(event.getEventPayload(), new TypeReference<>() {
    });
    penRequestBatchArchiveAndReturnSagaData.setStudents(event, students);
    // save the updated payload to DB...
    saga.setPayload(JsonUtil.getJsonStringFromObject(penRequestBatchArchiveAndReturnSagaData));
    this.getSagaService().updateAttachedSagaWithEvents(saga, eventStates);
    var penRequestBatchArchive = PenRequestBatchArchive.builder().penRequestBatchID(penRequestBatchArchiveAndReturnSagaData.getPenRequestBatchID()).updateUser(penRequestBatchArchiveAndReturnSagaData.getUpdateUser()).build();
    Event nextEvent = Event.builder().sagaId(saga.getSagaId()).eventType(EventType.ARCHIVE_PEN_REQUEST_BATCH).replyTo(this.getTopicToSubscribe()).eventPayload(JsonUtil.getJsonStringFromObject(penRequestBatchArchive)).build();
    this.postMessageToTopic(PEN_REQUEST_BATCH_API_TOPIC.toString(), nextEvent);
    log.info("message sent to PEN_REQUEST_BATCH_API_TOPIC for ARCHIVE_PEN_REQUEST_BATCH Event. :: {}", saga.getSagaId());
}
Also used : SagaEvent(ca.bc.gov.educ.penreg.api.model.v1.SagaEvent) Event(ca.bc.gov.educ.penreg.api.struct.Event) Student(ca.bc.gov.educ.penreg.api.struct.Student) SagaEvent(ca.bc.gov.educ.penreg.api.model.v1.SagaEvent)

Aggregations

Student (ca.bc.gov.educ.penreg.api.struct.Student)44 Test (org.junit.Test)26 lombok.val (lombok.val)24 Event (ca.bc.gov.educ.penreg.api.struct.Event)17 PenRequestBatchStudent (ca.bc.gov.educ.penreg.api.struct.v1.PenRequestBatchStudent)16 LocalDateTime (java.time.LocalDateTime)14 Collectors (java.util.stream.Collectors)14 PenRequestBatchEntity (ca.bc.gov.educ.penreg.api.model.v1.PenRequestBatchEntity)13 RestUtils (ca.bc.gov.educ.penreg.api.rest.RestUtils)13 JsonUtil (ca.bc.gov.educ.penreg.api.util.JsonUtil)13 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)13 BasePenRegAPITest (ca.bc.gov.educ.penreg.api.BasePenRegAPITest)12 PenRequestBatchStudentEntity (ca.bc.gov.educ.penreg.api.model.v1.PenRequestBatchStudentEntity)12 SagaEvent (ca.bc.gov.educ.penreg.api.model.v1.SagaEvent)12 SagaService (ca.bc.gov.educ.penreg.api.service.SagaService)12 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)12 EventOutcome (ca.bc.gov.educ.penreg.api.constants.EventOutcome)11 PenRequestBatchStudentStatusCodes (ca.bc.gov.educ.penreg.api.constants.PenRequestBatchStudentStatusCodes)11 PenMatchRecord (ca.bc.gov.educ.penreg.api.struct.PenMatchRecord)11 PenMatchResult (ca.bc.gov.educ.penreg.api.struct.PenMatchResult)11