Search in sources :

Example 6 with Saga

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

the class PenReqBatchNewPenOrchestrator method getNextPenNumber.

/**
 * Get the next PEN number.
 *
 * @param event                              the event
 * @param saga                               the saga
 * @param penRequestBatchUserActionsSagaData the pen request batch student saga data
 */
public void getNextPenNumber(final Event event, final Saga saga, final PenRequestBatchUserActionsSagaData penRequestBatchUserActionsSagaData) {
    final SagaEvent eventStates = this.createEventState(saga, event.getEventType(), event.getEventOutcome(), event.getEventPayload());
    saga.setSagaState(GET_NEXT_PEN_NUMBER.toString());
    this.getSagaService().updateAttachedSagaWithEvents(saga, eventStates);
    final var transactionID = saga.getSagaId().toString();
    final Event nextEvent = Event.builder().sagaId(saga.getSagaId()).eventType(GET_NEXT_PEN_NUMBER).replyTo(this.getTopicToSubscribe()).eventPayload(transactionID).build();
    this.postMessageToTopic(PEN_SERVICES_API_TOPIC.toString(), nextEvent);
    log.info("message sent to PEN_SERVICES_API_TOPIC for GET_NEXT_PEN_NUMBER Event. :: {}", 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 7 with Saga

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

the class PenReqBatchNewPenOrchestrator method createStudent.

/**
 * Create 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
 */
public void createStudent(final Event event, final Saga saga, final PenRequestBatchUserActionsSagaData penRequestBatchUserActionsSagaData) throws JsonProcessingException {
    final var pen = event.getEventPayload();
    final var student = studentMapper.toStudent(penRequestBatchUserActionsSagaData);
    student.setPen(pen);
    student.setDemogCode("A");
    student.setHistoryActivityCode(REQ_NEW.getCode());
    final var gradeCodes = this.restUtils.getGradeCodes();
    val isGradeCodeValid = gradeCodes.stream().anyMatch(gradeCode1 -> LocalDateTime.now().isAfter(gradeCode1.getEffectiveDate()) && LocalDateTime.now().isBefore(gradeCode1.getExpiryDate()) && StringUtils.equalsIgnoreCase(penRequestBatchUserActionsSagaData.getGradeCode(), gradeCode1.getGradeCode()));
    penRequestBatchUserActionsSagaData.setAssignedPEN(pen);
    student.setGradeCode(isGradeCodeValid ? student.getGradeCode() : null);
    saga.setSagaState(CREATE_STUDENT.toString());
    saga.setPayload(JsonUtil.getJsonStringFromObject(penRequestBatchUserActionsSagaData));
    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(CREATE_STUDENT).replyTo(this.getTopicToSubscribe()).eventPayload(JsonUtil.getJsonStringFromObject(student)).build();
    this.postMessageToTopic(STUDENT_API_TOPIC.toString(), nextEvent);
    log.info("message sent to STUDENT_API_TOPIC for CREATE_STUDENT Event. :: {}", saga.getSagaId());
}
Also used : lombok.val(lombok.val) 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 8 with Saga

use of ca.bc.gov.educ.penreg.api.model.v1.Saga 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 9 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 processPenMatchResults.

/**
 * it will hand off the request to downstream service class to process the results.
 * please see
 * {@link PenRequestBatchStudentOrchestratorService#processPenMatchResult(Saga, PenRequestBatchStudentSagaData, PenMatchResult)}
 *
 * @param event                          the event
 * @param saga                           the saga
 * @param penRequestBatchStudentSagaData the pen request batch student saga data
 * @throws IOException          the io exception
 * @throws InterruptedException the interrupted exception
 * @throws TimeoutException     the timeout exception
 */
protected void processPenMatchResults(final Event event, final Saga saga, final PenRequestBatchStudentSagaData penRequestBatchStudentSagaData) throws IOException, InterruptedException, TimeoutException {
    final Optional<Event> eventOptional;
    if (penRequestBatchStudentSagaData.getIsPENMatchResultsProcessed() == null || !penRequestBatchStudentSagaData.getIsPENMatchResultsProcessed()) {
        // this is necessary to check, to avoid duplicate execution during replay process.
        final SagaEvent eventStates = this.createEventState(saga, event.getEventType(), event.getEventOutcome(), event.getEventPayload());
        saga.setSagaState(PROCESS_PEN_MATCH_RESULTS.toString());
        final var penMatchResult = JsonUtil.getJsonObjectFromString(PenMatchResult.class, event.getEventPayload());
        // update the original payload with response from PEN_MATCH_API
        penRequestBatchStudentSagaData.setPenMatchResult(penMatchResult);
        // save the updated payload to DB...
        saga.setPayload(JsonUtil.getJsonStringFromObject(penRequestBatchStudentSagaData));
        this.getSagaService().updateAttachedSagaWithEvents(saga, eventStates);
        eventOptional = this.getPenRequestBatchStudentOrchestratorService().processPenMatchResult(saga, penRequestBatchStudentSagaData, penMatchResult);
        penRequestBatchStudentSagaData.setIsPENMatchResultsProcessed(true);
        // save the updated payload to DB...
        saga.setPayload(JsonUtil.getJsonStringFromObject(penRequestBatchStudentSagaData));
        this.getSagaService().updateAttachedEntityDuringSagaProcess(saga);
    } else {
        eventOptional = Optional.of(Event.builder().sagaId(saga.getSagaId()).eventType(PROCESS_PEN_MATCH_RESULTS).eventOutcome(PEN_MATCH_RESULTS_PROCESSED).eventPayload(penRequestBatchStudentSagaData.getPenMatchResult().getPenStatus()).build());
    }
    if (eventOptional.isPresent()) {
        this.handleEvent(eventOptional.get());
    } else {
        this.handleEvent(Event.builder().sagaId(saga.getSagaId()).eventType(PROCESS_PEN_MATCH_RESULTS).eventOutcome(PEN_MATCH_RESULTS_PROCESSED).build());
    }
}
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 10 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 validateStudentDemographics.

/**
 * Validate student demographics.
 *
 * @param event                          the event
 * @param saga                           the saga
 * @param penRequestBatchStudentSagaData the pen request batch student saga data
 * @throws JsonProcessingException the json processing exception
 */
protected void validateStudentDemographics(final Event event, final Saga saga, final PenRequestBatchStudentSagaData penRequestBatchStudentSagaData) throws IOException, InterruptedException, TimeoutException {
    final var scrubbedSagaData = this.getPenRequestBatchStudentOrchestratorService().scrubPayload(penRequestBatchStudentSagaData);
    final SagaEvent eventStates = this.createEventState(saga, event.getEventType(), event.getEventOutcome(), event.getEventPayload());
    saga.setSagaState(VALIDATE_STUDENT_DEMOGRAPHICS.toString());
    // update the payload with scrubbed values to use it in the saga process...
    saga.setPayload(JsonUtil.getJsonStringFromObject(scrubbedSagaData));
    this.getSagaService().updateAttachedSagaWithEvents(saga, eventStates);
    final var validationPayload = validationMapper.toStudentDemogValidationPayload(scrubbedSagaData);
    validationPayload.setTransactionID(saga.getSagaId().toString());
    final var eventPayload = JsonUtil.getJsonString(validationPayload);
    if (eventPayload.isPresent()) {
        final Event nextEvent = Event.builder().sagaId(saga.getSagaId()).eventType(VALIDATE_STUDENT_DEMOGRAPHICS).replyTo(this.getTopicToSubscribe()).eventPayload(eventPayload.get()).build();
        this.postMessageToTopic(PEN_SERVICES_API_TOPIC.toString(), nextEvent);
        log.info("message sent to PEN_SERVICES_API_TOPIC for VALIDATE_STUDENT_DEMOGRAPHICS 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)

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