use of ca.bc.gov.educ.penreg.api.struct.Event 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());
}
use of ca.bc.gov.educ.penreg.api.struct.Event 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.");
}
use of ca.bc.gov.educ.penreg.api.struct.Event 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());
}
use of ca.bc.gov.educ.penreg.api.struct.Event 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());
}
use of ca.bc.gov.educ.penreg.api.struct.Event 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());
}
Aggregations