Search in sources :

Example 1 with AND

use of ca.bc.gov.educ.penreg.api.struct.v1.Condition.AND 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 2 with AND

use of ca.bc.gov.educ.penreg.api.struct.v1.Condition.AND 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 3 with AND

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

Example 4 with AND

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

the class PenReqBatchUserMatchOrchestrator method updateGradeCodeAndGradeYear.

/**
 * updated for https://gww.jira.educ.gov.bc.ca/browse/PEN-1348
 * When district number is <b> NOT </b> 102, apply the following logic for grade code & grade year.
 * If the student record grade is null, and the incoming batch grade is valid, take it and update the grade year
 * Set the STUD_GRADE_YEAR to the current year (if after June 30) or the previous year (if before June 30)
 *
 * @param studentDataFromEventResponse          the student from student api
 * @param penRequestBatchUserActionsSagaData the pen request batch student data
 */
protected void updateGradeCodeAndGradeYear(final Student studentDataFromEventResponse, final PenRequestBatchUserActionsSagaData penRequestBatchUserActionsSagaData) {
    final var gradeCodes = this.restUtils.getGradeCodes();
    var batchGradeCode = penRequestBatchUserActionsSagaData.getGradeCode();
    val isGradeCodeValid = StringUtils.isNotBlank(batchGradeCode) ? gradeCodes.stream().anyMatch(gradeCode1 -> LocalDateTime.now().isAfter(gradeCode1.getEffectiveDate()) && LocalDateTime.now().isBefore(gradeCode1.getExpiryDate()) && StringUtils.equalsIgnoreCase(batchGradeCode, gradeCode1.getGradeCode())) : false;
    if (isGradeCodeValid && StringUtils.isBlank(studentDataFromEventResponse.getGradeCode())) {
        studentDataFromEventResponse.setGradeCode(batchGradeCode);
        val localDateTime = LocalDateTime.now();
        if (localDateTime.getMonthValue() > 6) {
            studentDataFromEventResponse.setGradeYear(String.valueOf(localDateTime.getYear()));
        } else {
            studentDataFromEventResponse.setGradeYear(String.valueOf(localDateTime.getYear() - 1));
        }
    }
}
Also used : lombok.val(lombok.val) 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)

Example 5 with AND

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

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)18 BasePenRegAPITest (ca.bc.gov.educ.penreg.api.BasePenRegAPITest)17 Search (ca.bc.gov.educ.penreg.api.struct.v1.Search)17 SearchCriteria (ca.bc.gov.educ.penreg.api.struct.v1.SearchCriteria)17 Collectors (java.util.stream.Collectors)17 Test (org.junit.Test)17 MvcResult (org.springframework.test.web.servlet.MvcResult)17 PenRequestBatch (ca.bc.gov.educ.penreg.api.struct.v1.PenRequestBatch)16 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)16 TypeReference (com.fasterxml.jackson.core.type.TypeReference)16 lombok.val (lombok.val)16 Autowired (org.springframework.beans.factory.annotation.Autowired)16 FilterOperation (ca.bc.gov.educ.penreg.api.filter.FilterOperation)15 AND (ca.bc.gov.educ.penreg.api.struct.v1.Condition.AND)15 ValueType (ca.bc.gov.educ.penreg.api.struct.v1.ValueType)15 PenRequestBatchTestUtils (ca.bc.gov.educ.penreg.api.support.PenRequestBatchTestUtils)15 File (java.io.File)15 java.util (java.util)15 Before (org.junit.Before)15 MockitoAnnotations (org.mockito.MockitoAnnotations)15