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.");
}
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.");
}
}
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();
}
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);
}
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());
}
Aggregations