use of ca.bc.gov.educ.penreg.api.model.v1.PenRequestBatchStudentEntity in project EDUC-PEN-REG-BATCH-API by bcgov.
the class PenRequestBatchAPIController method findAllStudents.
/**
* Find all students completable future.
*
* @param pageNumber the page number
* @param pageSize the page size
* @param sortCriteriaJson the sort criteria json
* @param searchCriteriaListJson the search criteria list json
* @return the completable future
*/
@Override
public CompletableFuture<Page<PenRequestBatchStudent>> findAllStudents(final Integer pageNumber, final Integer pageSize, final String sortCriteriaJson, final String searchCriteriaListJson) {
final List<Sort.Order> sorts = new ArrayList<>();
Specification<PenRequestBatchStudentEntity> penRequestBatchStudentEntitySpecification = null;
try {
final var associationNames = this.getSortCriteria(sortCriteriaJson, JsonUtil.mapper, sorts);
if (StringUtils.isNotBlank(searchCriteriaListJson)) {
final List<Search> searches = JsonUtil.mapper.readValue(searchCriteriaListJson, new TypeReference<>() {
});
this.getAssociationNamesFromSearchCriterias(associationNames, searches);
int i = 0;
for (final var search : searches) {
penRequestBatchStudentEntitySpecification = this.getSpecifications(penRequestBatchStudentEntitySpecification, i, search, associationNames, this.penRegBatchStudentFilterSpecs);
i++;
}
}
} catch (final JsonProcessingException e) {
throw new InvalidParameterException(e.getMessage());
}
return this.getStudentService().findAll(penRequestBatchStudentEntitySpecification, pageNumber, pageSize, sorts).thenApplyAsync(penRegBatchEntities -> penRegBatchEntities.map(studentMapper::toStructure));
}
use of ca.bc.gov.educ.penreg.api.model.v1.PenRequestBatchStudentEntity in project EDUC-PEN-REG-BATCH-API by bcgov.
the class PenRequestBatchFileService method checkBatchForRepeatRequests.
private void checkBatchForRepeatRequests(String guid, PenRequestBatchEntity penRequestBatchEntity, Set<PenRequestBatchStudentEntity> studentEntities, Set<PenRequestBatchStudentEntity> filteredStudentEntities) {
long numRepeats = 0;
final Map<String, List<PenRequestBatchStudentEntity>> repeatCheckMap = this.penRequestBatchStudentService.populateRepeatCheckMap(penRequestBatchEntity);
if (repeatCheckMap.size() > 0) {
for (final PenRequestBatchStudentEntity penRequestBatchStudent : studentEntities) {
if (PenRequestBatchStudentStatusCodes.DUPLICATE.getCode().equals(penRequestBatchStudent.getPenRequestBatchStudentStatusCode())) {
// no need to do any checking for DUPLICATE student requests, continue further.
continue;
}
final String repeatCheckKey = this.penRequestBatchStudentService.constructKeyGivenBatchStudent(penRequestBatchStudent);
if (repeatCheckMap.containsKey(repeatCheckKey)) {
// if it is a repeat remove it from the list to be further processed.
filteredStudentEntities.remove(penRequestBatchStudent);
this.updatePenRequestBatchStudentRequest(repeatCheckMap.get(repeatCheckKey), penRequestBatchStudent);
numRepeats++;
}
}
}
log.debug("{} :: Found {} total repeats", guid, numRepeats);
penRequestBatchEntity.setRepeatCount(numRepeats);
}
use of ca.bc.gov.educ.penreg.api.model.v1.PenRequestBatchStudentEntity in project EDUC-PEN-REG-BATCH-API by bcgov.
the class PenRegBatchProcessorTest method testProcessPenRegBatchFileFromTSW_Given30RowValidFile_ShouldCreateRecordsInDB.
/**
* Test process pen reg batch file from tsw given 30 row valid file should create records in db.
*
* @throws IOException the io exception
*/
@Test
@Transactional
public void testProcessPenRegBatchFileFromTSW_Given30RowValidFile_ShouldCreateRecordsInDB() throws IOException {
when(this.restUtils.getSchoolByMincode(anyString())).thenReturn(Optional.of(this.createMockSchool()));
final File file = new File(Objects.requireNonNull(this.getClass().getClassLoader().getResource("sample_30_records_OK.txt")).getFile());
final byte[] bFile = Files.readAllBytes(file.toPath());
final var randomNum = (new Random().nextLong() * (MAX - MIN + 1) + MIN);
var tsw = PENWebBlobEntity.builder().penWebBlobId(1L).mincode("66510518").sourceApplication("MYED").tswAccount((randomNum + "").substring(0, 8)).fileName("sample_30_records_OK").fileType("PEN").fileContents(bFile).insertDateTime(LocalDateTime.now()).submissionNumber(("T" + randomNum).substring(0, 8)).build();
tsw = this.penRequestBatchTestUtils.savePenWebBlob(tsw);
this.penRegBatchProcessor.processPenRegBatchFileFromPenWebBlob(tsw);
final var result = this.repository.findAll();
assertThat(result.size()).isEqualTo(1);
final var entity = result.get(0);
assertThat(entity.getPenRequestBatchID()).isNotNull();
assertThat(entity.getPenRequestBatchStatusCode()).isEqualTo(REPEATS_CHECKED.getCode());
assertThat(entity.getSchoolGroupCode()).isEqualTo(K12.getCode());
assertThat(entity.getPenRequestBatchStatusReason()).isNull();
assertThat(entity.getRepeatCount()).isZero();
final var students = this.studentRepository.findAllByPenRequestBatchEntity(result.get(0));
assertThat(entity.getPenRequestBatchHistoryEntities().size()).isEqualTo(2);
final Optional<PenRequestBatchHistoryEntity> penRequestBatchHistoryEntityOptional = entity.getPenRequestBatchHistoryEntities().stream().min(new PenRequestBatchHistoryComparator());
assertThat(penRequestBatchHistoryEntityOptional).isPresent();
assertThat(penRequestBatchHistoryEntityOptional.get().getPenRequestBatchEventCode()).isEqualTo(STATUS_CHANGED.getCode());
assertThat(penRequestBatchHistoryEntityOptional.get().getPenRequestBatchStatusReason()).isNull();
assertThat(students.size()).isEqualTo(30);
students.sort(Comparator.comparing(PenRequestBatchStudentEntity::getRecordNumber));
log.error("students {}", students);
var counter = 1;
for (final PenRequestBatchStudentEntity student : students) {
assertThat(counter++).isEqualTo(student.getRecordNumber());
}
}
use of ca.bc.gov.educ.penreg.api.model.v1.PenRequestBatchStudentEntity in project EDUC-PEN-REG-BATCH-API by bcgov.
the class PenRegBatchProcessorTest method testProcessPenRegBatchFileFromTSW_Given30RowValidFileAndExistingRecords_ShouldShowRepeats.
/**
* Test process pen reg batch file from tsw given 30 row valid file should create records in db.
*
* @throws IOException the io exception
*/
@Test
@Transactional
public void testProcessPenRegBatchFileFromTSW_Given30RowValidFileAndExistingRecords_ShouldShowRepeats() throws IOException {
when(this.restUtils.getSchoolByMincode(anyString())).thenReturn(Optional.of(this.createMockSchool()));
this.penRequestBatchTestUtils.createBatchStudentsInSingleTransaction(this.repository, "mock_pen_req_batch_repeat.json", "mock_pen_req_batch_student_repeat.json", 1, (batch) -> batch.setProcessDate(LocalDateTime.now().minusDays(3)));
final File file = new File(Objects.requireNonNull(this.getClass().getClassLoader().getResource("sample_5_K12_OK.txt")).getFile());
final byte[] bFile = Files.readAllBytes(file.toPath());
final var randomNum = (new Random().nextLong() * (MAX - MIN + 1) + MIN);
var tsw = PENWebBlobEntity.builder().penWebBlobId(1L).mincode("66510518").sourceApplication("MYED").tswAccount((randomNum + "").substring(0, 8)).fileName("sample_5_K12_OK").fileType("PEN").fileContents(bFile).insertDateTime(LocalDateTime.now()).submissionNumber(("T" + randomNum).substring(0, 8)).build();
tsw = this.penRequestBatchTestUtils.savePenWebBlob(tsw);
this.penRegBatchProcessor.processPenRegBatchFileFromPenWebBlob(tsw);
final var result = this.repository.findAll();
assertThat(result.size()).isEqualTo(2);
final var entity = result.get(1);
assertThat(entity.getPenRequestBatchID()).isNotNull();
assertThat(entity.getPenRequestBatchStatusCode()).isEqualTo(REPEATS_CHECKED.getCode());
assertThat(entity.getSchoolGroupCode()).isEqualTo(K12.getCode());
assertThat(entity.getPenRequestBatchHistoryEntities().size()).isEqualTo(2);
assertThat(entity.getRepeatCount()).isEqualTo(1);
final Optional<PenRequestBatchHistoryEntity> penRequestBatchHistoryEntityOptional = entity.getPenRequestBatchHistoryEntities().stream().min(new PenRequestBatchHistoryComparator());
assertThat(penRequestBatchHistoryEntityOptional).isPresent();
assertThat(penRequestBatchHistoryEntityOptional.get().getPenRequestBatchEventCode()).isEqualTo(STATUS_CHANGED.getCode());
assertThat(penRequestBatchHistoryEntityOptional.get().getPenRequestBatchStatusReason()).isNull();
final var students = this.studentRepository.findAllByPenRequestBatchEntity(result.get(1));
assertThat(students.stream().filter(s -> PenRequestBatchStudentStatusCodes.REPEAT.getCode().equals(s.getPenRequestBatchStudentStatusCode())).count()).isEqualTo(1);
assertThat(students.size()).isEqualTo(5);
students.sort(Comparator.comparing(PenRequestBatchStudentEntity::getRecordNumber));
log.error("students {}", students);
var counter = 1;
for (final PenRequestBatchStudentEntity student : students) {
assertThat(counter++).isEqualTo(student.getRecordNumber());
}
}
use of ca.bc.gov.educ.penreg.api.model.v1.PenRequestBatchStudentEntity in project EDUC-PEN-REG-BATCH-API by bcgov.
the class PenRequestBatchEventService method updatePenRequestBatchStudent.
@Transactional(propagation = Propagation.REQUIRES_NEW)
public PenRequestBatchEvent updatePenRequestBatchStudent(final Event event) throws JsonProcessingException {
val penRequestBatchEventOptional = this.getPenRequestBatchEventRepository().findBySagaIdAndEventType(event.getSagaId(), event.getEventType().toString());
final PenRequestBatchEvent penRequestBatchEvent;
if (penRequestBatchEventOptional.isEmpty()) {
log.info(NO_RECORD_SAGA_ID_EVENT_TYPE);
log.trace(EVENT_PAYLOAD, event);
final var prbStudent = JsonUtil.getJsonObjectFromString(PenRequestBatchStudent.class, event.getEventPayload());
final PenRequestBatchStudentEntity entity = prbStudentMapper.toModel(prbStudent);
this.populateAuditColumnsForUpdateStudent(entity);
try {
this.prbStudentService.updateStudent(entity, UUID.fromString(prbStudent.getPenRequestBatchID()), UUID.fromString(prbStudent.getPenRequestBatchStudentID()));
// need to convert to structure MANDATORY otherwise jackson will break.
event.setEventPayload(JsonUtil.getJsonStringFromObject(prbStudentMapper.toStructure(entity)));
event.setEventOutcome(EventOutcome.PEN_REQUEST_BATCH_STUDENT_UPDATED);
} catch (final EntityNotFoundException ex) {
log.error("PenRequestBatchStudent not found while trying to update it", ex);
event.setEventOutcome(EventOutcome.PEN_REQUEST_BATCH_STUDENT_NOT_FOUND);
}
penRequestBatchEvent = this.createPenRequestBatchEventRecord(event);
} else {
log.info(RECORD_FOUND_FOR_SAGA_ID_EVENT_TYPE);
log.trace(EVENT_PAYLOAD, event);
penRequestBatchEvent = penRequestBatchEventOptional.get();
penRequestBatchEvent.setEventStatus(MESSAGE_PUBLISHED.toString());
}
this.getPenRequestBatchEventRepository().save(penRequestBatchEvent);
return penRequestBatchEvent;
}
Aggregations