use of ca.bc.gov.educ.penreg.api.model.v1.Saga in project EDUC-PEN-REG-BATCH-API by bcgov.
the class PenRequestBatchSagaControllerTest method testGetSagaPaginated_givenNoSearchCriteria_shouldReturnAllWithStatusOk.
@Test
@SuppressWarnings("java:S100")
public void testGetSagaPaginated_givenNoSearchCriteria_shouldReturnAllWithStatusOk() throws Exception {
final File file = new File(Objects.requireNonNull(this.getClass().getClassLoader().getResource("mock_multiple_sagas.json")).getFile());
final List<Saga> sagaStructs = new ObjectMapper().readValue(file, new TypeReference<>() {
});
final List<ca.bc.gov.educ.penreg.api.model.v1.Saga> sagaEntities = sagaStructs.stream().map(mapper::toModel).collect(Collectors.toList());
for (val saga : sagaEntities) {
saga.setSagaId(null);
saga.setCreateDate(LocalDateTime.now());
saga.setUpdateDate(LocalDateTime.now());
}
this.repository.saveAll(sagaEntities);
final MvcResult result = this.mockMvc.perform(get("/api/v1/pen-request-batch-saga/paginated").with(jwt().jwt((jwt) -> jwt.claim("scope", "PEN_REQUEST_BATCH_READ_SAGA"))).contentType(APPLICATION_JSON)).andReturn();
this.mockMvc.perform(asyncDispatch(result)).andDo(print()).andExpect(status().isOk()).andExpect(jsonPath("$.content", hasSize(3)));
}
use of ca.bc.gov.educ.penreg.api.model.v1.Saga in project EDUC-PEN-REG-BATCH-API by bcgov.
the class PenRegBatchProcessor method processLoadedRecordsInBatchFile.
/**
* System was able to process the file successfully, now the data is persisted and saga data is created for further processing.
* Process loaded records in batch file set.
* this method will convert from batch file to header and student record,
* send them to service for persistence and then return the set for further processing.
*
* @param guid the guid
* @param batchFile the batch file
* @param penWebBlobEntity the pen web blob entity
*/
private void processLoadedRecordsInBatchFile(@NonNull final String guid, @NonNull final BatchFile batchFile, @NonNull final PENWebBlobEntity penWebBlobEntity) {
var counter = 1;
log.info("going to persist data for batch :: {}", guid);
// batch file can be processed further and persisted.
final PenRequestBatchEntity entity = mapper.toPenReqBatchEntityLoaded(penWebBlobEntity, batchFile);
final Optional<School> school = this.restUtils.getSchoolByMincode(penWebBlobEntity.getMincode());
school.ifPresent(value -> entity.setSchoolName(value.getSchoolName()));
for (final var student : batchFile.getStudentDetails()) {
// set the object so that PK/FK relationship will be auto established by hibernate.
final var penRequestBatchStudentEntity = mapper.toPenRequestBatchStudentEntity(student, entity);
penRequestBatchStudentEntity.setRecordNumber(counter++);
entity.getPenRequestBatchStudentEntities().add(penRequestBatchStudentEntity);
}
this.getPenRequestBatchFileService().markInitialLoadComplete(entity, penWebBlobEntity);
if (entity.getPenRequestBatchID() != null) {
// this could happen when the same submission number is picked up again, system should not process the same submission.
// the entity was saved in propagation new context , so system needs to get it again from DB to have an attached entity bound to the current thread.
this.getPenRequestBatchFileService().filterDuplicatesAndRepeatRequests(guid, entity);
}
}
use of ca.bc.gov.educ.penreg.api.model.v1.Saga in project EDUC-PEN-REG-BATCH-API by bcgov.
the class PenRequestBatchArchiveAndReturnOrchestrator method generatePDFReport.
private void generatePDFReport(Event event, Saga saga, PenRequestBatchArchiveAndReturnSagaData penRequestBatchArchiveAndReturnSagaData) throws JsonProcessingException {
SagaEvent eventStates = this.createEventState(saga, event.getEventType(), event.getEventOutcome(), event.getEventPayload());
saga.setSagaState(GENERATE_PEN_REQUEST_BATCH_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());
}
}
Event nextEvent = Event.builder().sagaId(saga.getSagaId()).eventType(GENERATE_PEN_REQUEST_BATCH_REPORTS).replyTo(this.getTopicToSubscribe()).eventPayload(JsonUtil.getJsonStringFromObject(ReportGenerationEventPayload.builder().reportType("PEN_REG_BATCH_RESPONSE_REPORT").reportExtension("pdf").reportName(penRequestBatchArchiveAndReturnSagaData.getPenRequestBatch().getSubmissionNumber()).data(reportMapper.toReportData(penRequestBatchArchiveAndReturnSagaData)).build())).build();
this.postMessageToTopic(SagaTopicsEnum.PEN_REPORT_GENERATION_API_TOPIC.toString(), nextEvent);
log.info("message sent to PEN_REPORT_GENERATION_API_TOPIC for {} Event. :: {}", GENERATE_PEN_REQUEST_BATCH_REPORTS.toString(), saga.getSagaId());
}
use of ca.bc.gov.educ.penreg.api.model.v1.Saga in project EDUC-PEN-REG-BATCH-API by bcgov.
the class PenRequestBatchRepostReportsOrchestrator method saveReportsWithoutPDF.
private void saveReportsWithoutPDF(Event event, Saga saga, PenRequestBatchRepostReportsFilesSagaData penRequestBatchRepostReportsFilesSagaData) throws IOException, TimeoutException, InterruptedException {
SagaEvent eventStates = this.createEventState(saga, event.getEventType(), event.getEventOutcome(), event.getEventPayload());
saga.setSagaState(SAVE_REPORTS.toString());
List<Student> students = obMapper.readValue(event.getEventPayload(), new TypeReference<>() {
});
penRequestBatchRepostReportsFilesSagaData.setStudents(event, students);
// save the updated payload to DB...
saga.setPayload(JsonUtil.getJsonStringFromObject(penRequestBatchRepostReportsFilesSagaData));
this.getSagaService().updateAttachedSagaWithEvents(saga, eventStates);
this.getResponseFileGeneratorService().saveReports(mapper.toModel(penRequestBatchRepostReportsFilesSagaData.getPenRequestBatch()), penRequestBatchRepostReportsFilesSagaData.getPenRequestBatchStudents(), penRequestBatchRepostReportsFilesSagaData.getStudents(), reportMapper.toReportData(penRequestBatchRepostReportsFilesSagaData));
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.model.v1.Saga in project EDUC-PEN-REG-BATCH-API by bcgov.
the class BaseOrchestrator method replayFromLastEvent.
/**
* This method will restart the saga process from where it was left the last time. which could occur due to various reasons
*
* @param saga the model object.
* @param eventStates the event states corresponding to the saga
* @param t the payload string as an object
* @throws InterruptedException if thread is interrupted.
* @throws TimeoutException if connection to messaging system times out.
* @throws IOException if there is connectivity problem
*/
private void replayFromLastEvent(final Saga saga, final List<SagaEvent> eventStates, final T t) throws InterruptedException, TimeoutException, IOException {
val sagaEventOptional = this.findTheLastEventOccurred(eventStates);
if (sagaEventOptional.isPresent()) {
val sagaEvent = sagaEventOptional.get();
log.trace(sagaEventOptional.toString());
final EventType currentEvent = EventType.valueOf(sagaEvent.getSagaEventState());
final EventOutcome eventOutcome = EventOutcome.valueOf(sagaEvent.getSagaEventOutcome());
final Event event = Event.builder().eventOutcome(eventOutcome).eventType(currentEvent).eventPayload(sagaEvent.getSagaEventResponse()).build();
final Optional<SagaEventState<T>> sagaEventState = this.findNextSagaEventState(currentEvent, eventOutcome, t);
if (sagaEventState.isPresent()) {
log.trace(SYSTEM_IS_GOING_TO_EXECUTE_NEXT_EVENT_FOR_CURRENT_EVENT, sagaEventState.get().getNextEventType(), event.toString(), saga.getSagaId());
this.invokeNextEvent(event, saga, t, sagaEventState.get());
}
}
}
Aggregations