use of ca.bc.gov.educ.penreg.api.model.v1.PenRequestBatchEntity 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.PenRequestBatchEntity in project EDUC-PEN-REG-BATCH-API by bcgov.
the class EventTaskSchedulerAsyncService method checkAndUpdateStatusToActiveOrArchived.
/**
* Check and update status to active.
*
* @param penReqBatchEntities the pen req batch entities
* @param penRequestBatchEntity the pen request batch entity
* @param redisKey the redis key
*/
private void checkAndUpdateStatusToActiveOrArchived(final List<PenRequestBatchEntity> penReqBatchEntities, final PenRequestBatchEntity penRequestBatchEntity, final String redisKey) {
long loadedCount = this.penRequestBatchStudentRepository.countAllByPenRequestBatchEntityAndPenRequestBatchStudentStatusCodeIn(penRequestBatchEntity, List.of(LOADED.getCode()));
if (loadedCount > 0) {
// all the records are not processed yet.
return;
}
val studentSagaRecordsCount = this.getSagaRepository().countAllByPenRequestBatchIDAndSagaName(penRequestBatchEntity.getPenRequestBatchID(), PEN_REQUEST_BATCH_STUDENT_PROCESSING_SAGA.toString());
long dupCount = this.penRequestBatchStudentRepository.countAllByPenRequestBatchEntityAndPenRequestBatchStudentStatusCodeIn(penRequestBatchEntity, List.of(DUPLICATE.getCode()));
long rptCount = this.penRequestBatchStudentRepository.countAllByPenRequestBatchEntityAndPenRequestBatchStudentStatusCodeIn(penRequestBatchEntity, List.of(REPEAT.getCode()));
long newPenAndMatchCount = this.penRequestBatchStudentRepository.countAllByPenRequestBatchEntityAndPenRequestBatchStudentStatusCodeIn(penRequestBatchEntity, List.of(PenRequestBatchStudentStatusCodes.SYS_NEW_PEN.getCode(), PenRequestBatchStudentStatusCodes.SYS_MATCHED.getCode()));
final List<PenRequestBatchMultiplePen> recordWithMultiples = this.penRequestBatchStudentRepository.findBatchFilesWithMultipleAssignedPens(List.of(penRequestBatchEntity.getPenRequestBatchID()));
boolean isSamePenAssignedToMultiplePRForSameBatch = !recordWithMultiples.isEmpty();
if (penRequestBatchEntity.getStudentCount() == (rptCount + dupCount)) {
// all records are either repeat or
this.handleAllDuplicateOrRepeat(penReqBatchEntities, penRequestBatchEntity, redisKey);
} else if (studentSagaRecordsCount > 0) {
this.updateBasedOnCompletedSagas(penReqBatchEntities, penRequestBatchEntity, redisKey, studentSagaRecordsCount, newPenAndMatchCount, isSamePenAssignedToMultiplePRForSameBatch);
}
}
use of ca.bc.gov.educ.penreg.api.model.v1.PenRequestBatchEntity 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.model.v1.PenRequestBatchEntity in project EDUC-PEN-REG-BATCH-API by bcgov.
the class PenRequestBatchEventService method archivePenRequestBatch.
@Transactional(propagation = Propagation.REQUIRES_NEW)
public PenRequestBatchEvent archivePenRequestBatch(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 penRequestBatchArchive = JsonUtil.getJsonObjectFromString(PenRequestBatchArchive.class, event.getEventPayload());
var penRequestBatchEntityOptional = getPenRequestBatchService().findById(penRequestBatchArchive.getPenRequestBatchID());
if (penRequestBatchEntityOptional.isPresent()) {
PenRequestBatchEntity penRequestBatch = penRequestBatchEntityOptional.get();
if (StringUtils.equals(penRequestBatch.getPenRequestBatchStatusCode(), PenRequestBatchStatusCodes.UNARCHIVED.getCode()) || StringUtils.equals(penRequestBatch.getPenRequestBatchStatusCode(), PenRequestBatchStatusCodes.UNARCHIVED_CHANGED.getCode())) {
penRequestBatch.setPenRequestBatchStatusCode(REARCHIVED.getCode());
} else {
penRequestBatch.setPenRequestBatchStatusCode(ARCHIVED.getCode());
}
if (penRequestBatchArchive.getUpdateUser() == null) {
penRequestBatch.setUpdateUser(PEN_REQUEST_BATCH_API);
} else {
penRequestBatch.setUpdateUser(penRequestBatchArchive.getUpdateUser());
}
penRequestBatch.setProcessDate(LocalDateTime.now());
try {
getPenRequestBatchService().updatePenRequestBatch(penRequestBatch, penRequestBatch.getPenRequestBatchID());
event.setEventPayload(JsonUtil.getJsonStringFromObject(batchMapper.toStructure(penRequestBatch)));
event.setEventOutcome(EventOutcome.PEN_REQUEST_BATCH_UPDATED);
} catch (EntityNotFoundException ex) {
log.error("PenRequestBatch not found while trying to update it. This should not happen :: ", ex);
event.setEventOutcome(EventOutcome.PEN_REQUEST_BATCH_NOT_FOUND);
}
} else {
log.error("PenRequestBatch not found while trying to update it. This should not happen :: " + penRequestBatchArchive.getPenRequestBatchID());
event.setEventOutcome(EventOutcome.PEN_REQUEST_BATCH_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;
}
use of ca.bc.gov.educ.penreg.api.model.v1.PenRequestBatchEntity in project EDUC-PEN-REG-BATCH-API by bcgov.
the class PenRequestBatchService method savePenRequestBatch.
/**
* Create pen request batch pen request batch entity.
*
* @param penRequestBatchEntity the pen request batch entity
* @return the pen request batch entity
*/
@Transactional(propagation = Propagation.MANDATORY)
public PenRequestBatchEntity savePenRequestBatch(final PenRequestBatchEntity penRequestBatchEntity) {
final PenRequestBatchHistoryEntity penRequestBatchHistory = historyMapper.toModelFromBatch(penRequestBatchEntity, PenRequestBatchEventCodes.STATUS_CHANGED.getCode());
penRequestBatchEntity.getPenRequestBatchHistoryEntities().add(penRequestBatchHistory);
return this.getRepository().save(penRequestBatchEntity);
}
Aggregations