Search in sources :

Example 1 with PenRequestBatchEntity

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);
}
Also used : PenRequestBatchStudentEntity(ca.bc.gov.educ.penreg.api.model.v1.PenRequestBatchStudentEntity)

Example 2 with PenRequestBatchEntity

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);
    }
}
Also used : lombok.val(lombok.val) PenRequestBatchMultiplePen(ca.bc.gov.educ.penreg.api.model.v1.PenRequestBatchMultiplePen)

Example 3 with PenRequestBatchEntity

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();
}
Also used : PenRequestBatchReportData(ca.bc.gov.educ.penreg.api.struct.v1.reportstructs.PenRequestBatchReportData) SpringTemplateEngine(org.thymeleaf.spring5.SpringTemplateEngine) java.util(java.util) Getter(lombok.Getter) LocalDateTime(java.time.LocalDateTime) Autowired(org.springframework.beans.factory.annotation.Autowired) PenRequestBatchStudentRepository(ca.bc.gov.educ.penreg.api.repository.PenRequestBatchStudentRepository) PenRequestBatchStudent(ca.bc.gov.educ.penreg.api.struct.v1.PenRequestBatchStudent) StringUtils(org.apache.commons.lang3.StringUtils) PenRequestBatchEntity(ca.bc.gov.educ.penreg.api.model.v1.PenRequestBatchEntity) PENWebBlobEntity(ca.bc.gov.educ.penreg.api.model.v1.PENWebBlobEntity) Propagation(org.springframework.transaction.annotation.Propagation) Service(org.springframework.stereotype.Service) SchoolGroupCodes(ca.bc.gov.educ.penreg.api.constants.SchoolGroupCodes) RestUtils(ca.bc.gov.educ.penreg.api.rest.RestUtils) PenCoordinator(ca.bc.gov.educ.penreg.api.struct.v1.PenCoordinator) PenRequestBatchStudentStatusCodes(ca.bc.gov.educ.penreg.api.constants.PenRequestBatchStudentStatusCodes) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Slf4j(lombok.extern.slf4j.Slf4j) Student(ca.bc.gov.educ.penreg.api.struct.Student) DateTimeFormatter(java.time.format.DateTimeFormatter) PenWebBlobRepository(ca.bc.gov.educ.penreg.api.repository.PenWebBlobRepository) PenRegAPIRuntimeException(ca.bc.gov.educ.penreg.api.exception.PenRegAPIRuntimeException) Context(org.thymeleaf.context.Context) Transactional(org.springframework.transaction.annotation.Transactional) PRIVATE(lombok.AccessLevel.PRIVATE) PenRequestBatchStudent(ca.bc.gov.educ.penreg.api.struct.v1.PenRequestBatchStudent) Student(ca.bc.gov.educ.penreg.api.struct.Student) PenRequestBatchStudent(ca.bc.gov.educ.penreg.api.struct.v1.PenRequestBatchStudent)

Example 4 with PenRequestBatchEntity

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;
}
Also used : lombok.val(lombok.val) PenRequestBatchEvent(ca.bc.gov.educ.penreg.api.model.v1.PenRequestBatchEvent) PenRequestBatchEntity(ca.bc.gov.educ.penreg.api.model.v1.PenRequestBatchEntity) EntityNotFoundException(ca.bc.gov.educ.penreg.api.exception.EntityNotFoundException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with PenRequestBatchEntity

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);
}
Also used : PenRequestBatchHistoryEntity(ca.bc.gov.educ.penreg.api.model.v1.PenRequestBatchHistoryEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

PenRequestBatchEntity (ca.bc.gov.educ.penreg.api.model.v1.PenRequestBatchEntity)31 Test (org.junit.Test)22 PenRequestBatchStudentEntity (ca.bc.gov.educ.penreg.api.model.v1.PenRequestBatchStudentEntity)16 PenMatchResult (ca.bc.gov.educ.penreg.api.struct.PenMatchResult)11 PenRequestBatchArchiveAndReturnSagaData (ca.bc.gov.educ.penreg.api.struct.v1.PenRequestBatchArchiveAndReturnSagaData)11 PenMatchRecord (ca.bc.gov.educ.penreg.api.struct.PenMatchRecord)10 lombok.val (lombok.val)10 BaseOrchestratorTest (ca.bc.gov.educ.penreg.api.orchestrator.BaseOrchestratorTest)9 Student (ca.bc.gov.educ.penreg.api.struct.Student)9 PenRequestBatchStudentStatusCodes (ca.bc.gov.educ.penreg.api.constants.PenRequestBatchStudentStatusCodes)7 LocalDateTime (java.time.LocalDateTime)7 java.util (java.util)7 Collectors (java.util.stream.Collectors)7 Autowired (org.springframework.beans.factory.annotation.Autowired)7 Transactional (org.springframework.transaction.annotation.Transactional)7 PENWebBlobEntity (ca.bc.gov.educ.penreg.api.model.v1.PENWebBlobEntity)5 RestUtils (ca.bc.gov.educ.penreg.api.rest.RestUtils)5 JsonUtil (ca.bc.gov.educ.penreg.api.util.JsonUtil)5 Slf4j (lombok.extern.slf4j.Slf4j)5 SchoolGroupCodes (ca.bc.gov.educ.penreg.api.constants.SchoolGroupCodes)4