Search in sources :

Example 1 with PenRegAPIRuntimeException

use of ca.bc.gov.educ.penreg.api.exception.PenRegAPIRuntimeException in project EDUC-PEN-REG-BATCH-API by bcgov.

the class BaseReturnFilesOrchestrator method saveReports.

protected void saveReports(final Event event, final Saga saga, final BasePenRequestBatchReturnFilesSagaData penRequestBatchReturnFilesSagaData) throws IOException, InterruptedException, TimeoutException {
    final SagaEvent eventStates = this.createEventState(saga, event.getEventType(), event.getEventOutcome(), event.getEventPayload());
    saga.setSagaState(SAVE_REPORTS.toString());
    this.getSagaService().updateAttachedSagaWithEvents(saga, eventStates);
    if (penRequestBatchReturnFilesSagaData.getStudents() == null) {
        log.info("students in saga data is null or empty for batch id :: {} and saga id :: {}, setting it from event states table", penRequestBatchReturnFilesSagaData.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<>() {
            });
            penRequestBatchReturnFilesSagaData.setStudents(event, students);
        } else {
            throw new PenRegAPIRuntimeException("students not found in event states table for saga id :: " + saga.getSagaId());
        }
    }
    this.getResponseFileGeneratorService().saveReports(event.getEventPayload(), mapper.toModel(penRequestBatchReturnFilesSagaData.getPenRequestBatch()), penRequestBatchReturnFilesSagaData.getPenRequestBatchStudents(), penRequestBatchReturnFilesSagaData.getStudents(), reportMapper.toReportData(penRequestBatchReturnFilesSagaData));
    val nextEvent = Event.builder().sagaId(saga.getSagaId()).eventType(SAVE_REPORTS).eventOutcome(REPORTS_SAVED).build();
    this.handleEvent(nextEvent);
}
Also used : lombok.val(lombok.val) PenRegAPIRuntimeException(ca.bc.gov.educ.penreg.api.exception.PenRegAPIRuntimeException) Student(ca.bc.gov.educ.penreg.api.struct.Student) SagaEvent(ca.bc.gov.educ.penreg.api.model.v1.SagaEvent)

Example 2 with PenRegAPIRuntimeException

use of ca.bc.gov.educ.penreg.api.exception.PenRegAPIRuntimeException in project EDUC-PEN-REG-BATCH-API by bcgov.

the class PenReqBatchNewPenOrchestrator method updatePenRequestBatchStudent.

/**
 * Update PEN Request Batch record and PRB Student record.
 *
 * @param event the event
 * @param saga  the saga
 * @param penRequestBatchUserActionsSagaData     the pen request batch student saga data
 * @throws JsonProcessingException the json processing exception
 */
@Override
protected void updatePenRequestBatchStudent(final Event event, final Saga saga, final PenRequestBatchUserActionsSagaData penRequestBatchUserActionsSagaData) throws JsonProcessingException {
    this.updateSagaData(event, penRequestBatchUserActionsSagaData);
    saga.setSagaState(UPDATE_PEN_REQUEST_BATCH_STUDENT.toString());
    saga.setPayload(JsonUtil.getJsonStringFromObject(penRequestBatchUserActionsSagaData));
    final SagaEvent eventStates = this.createEventState(saga, event.getEventType(), event.getEventOutcome(), event.getEventPayload());
    this.getSagaService().updateAttachedSagaWithEvents(saga, eventStates);
    if (penRequestBatchUserActionsSagaData.getStudentID() == null) {
        log.info("studentID in saga data is null for batch student id :: {} and saga id :: {}, setting it from event states table", penRequestBatchUserActionsSagaData.getPenRequestBatchStudentID(), saga.getSagaId());
        val sagaEventOptional = this.getSagaService().findSagaEvent(saga, CREATE_STUDENT.toString(), 3);
        if (sagaEventOptional.isPresent()) {
            val sagaEvent = sagaEventOptional.get();
            if (STUDENT_CREATED.toString().equals(sagaEvent.getSagaEventOutcome())) {
                final Student student = JsonUtil.getJsonObjectFromString(Student.class, sagaEvent.getSagaEventResponse());
                penRequestBatchUserActionsSagaData.setStudentID(student.getStudentID());
            } else if (STUDENT_ALREADY_EXIST.toString().equals(sagaEvent.getSagaEventOutcome())) {
                penRequestBatchUserActionsSagaData.setStudentID(sagaEvent.getSagaEventResponse());
            } else {
                throw new PenRegAPIRuntimeException("CREATE_STUDENT event with outcome not found in event states table for saga id :: " + saga.getSagaId());
            }
        } else {
            throw new PenRegAPIRuntimeException("CREATE_STUDENT event not found in event states table for saga id :: " + saga.getSagaId());
        }
    }
    final PenRequestBatchStudent prbStudent = this.createPRBStudent(event, penRequestBatchUserActionsSagaData);
    final Event nextEvent = Event.builder().sagaId(saga.getSagaId()).eventType(UPDATE_PEN_REQUEST_BATCH_STUDENT).replyTo(this.getTopicToSubscribe()).eventPayload(JsonUtil.getJsonStringFromObject(prbStudent)).build();
    this.postMessageToTopic(PEN_REQUEST_BATCH_API_TOPIC.toString(), nextEvent);
    log.info("message sent to PEN_REQUEST_BATCH_API_TOPIC for UPDATE_PEN_REQUEST_BATCH_STUDENT Event. :: {}", saga.getSagaId());
}
Also used : lombok.val(lombok.val) PenRegAPIRuntimeException(ca.bc.gov.educ.penreg.api.exception.PenRegAPIRuntimeException) SagaEvent(ca.bc.gov.educ.penreg.api.model.v1.SagaEvent) Event(ca.bc.gov.educ.penreg.api.struct.Event) 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) SagaEvent(ca.bc.gov.educ.penreg.api.model.v1.SagaEvent)

Example 3 with PenRegAPIRuntimeException

use of ca.bc.gov.educ.penreg.api.exception.PenRegAPIRuntimeException in project EDUC-PEN-REG-BATCH-API by bcgov.

the class BaseBatchStudentPenMatchResultProcessingService method handleSystemMatchedStatus.

/**
 * case AA:
 * case B1:
 * case C1:
 * case D1:
 *
 * @param saga                           the saga
 * @param penMatchResult                 the pen match result
 * @param penRequestBatchStudent         the pen request batch student
 * @param penRequestBatch                the pen request batch
 * @param penRequestBatchStudentSagaData th saga data which contains scrubbed payload.
 * @return the event
 */
protected Event handleSystemMatchedStatus(final Saga saga, final PenMatchResult penMatchResult, final PenRequestBatchStudentEntity penRequestBatchStudent, final PenRequestBatchEntity penRequestBatch, final PenRequestBatchStudentSagaData penRequestBatchStudentSagaData) {
    final var penMatchRecordOptional = penMatchResult.getMatchingRecords().stream().findFirst();
    if (penMatchRecordOptional.isPresent()) {
        final var penMatchRecord = penMatchRecordOptional.get();
        val pen = penMatchRecord.getMatchingPEN();
        val penAlreadyAssigned = this.penRequestBatchStudentService.isPenAlreadyAssigned(penRequestBatch, pen);
        if (penAlreadyAssigned) {
            penRequestBatchStudent.setPenRequestBatchStudentStatusCode(FIXABLE.getCode());
            this.getPenRequestBatchStudentService().saveAttachedEntity(penRequestBatchStudent);
        } else {
            final var studentID = penMatchRecord.getStudentID();
            penRequestBatchStudent.setPenRequestBatchStudentStatusCode(SYS_MATCHED.getCode());
            penRequestBatchStudent.setStudentID(UUID.fromString(studentID));
            penRequestBatchStudent.setAssignedPEN(penMatchRecord.getMatchingPEN());
            penRequestBatchStudent.setBestMatchPEN(penMatchRecord.getMatchingPEN());
            this.getPenRequestBatchStudentService().saveAttachedEntity(penRequestBatchStudent);
            this.updateStudent(studentID, penRequestBatchStudentSagaData, penRequestBatch, penRequestBatchStudent);
        }
        return Event.builder().sagaId(saga.getSagaId()).eventType(PROCESS_PEN_MATCH_RESULTS).eventOutcome(PEN_MATCH_RESULTS_PROCESSED).eventPayload(penMatchResult.getPenStatus()).build();
    } else {
        log.error("PenMatchRecord in priority queue is empty for matched status, this should not have happened.");
        throw new PenRegAPIRuntimeException("PenMatchRecord in priority queue is empty for matched status, this should not have happened.");
    }
}
Also used : lombok.val(lombok.val) PenRegAPIRuntimeException(ca.bc.gov.educ.penreg.api.exception.PenRegAPIRuntimeException)

Example 4 with PenRegAPIRuntimeException

use of ca.bc.gov.educ.penreg.api.exception.PenRegAPIRuntimeException 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);
}
Also used : lombok.val(lombok.val) PenRequestBatch(ca.bc.gov.educ.penreg.api.struct.v1.PenRequestBatch) PenRegAPIRuntimeException(ca.bc.gov.educ.penreg.api.exception.PenRegAPIRuntimeException) Student(ca.bc.gov.educ.penreg.api.struct.Student) SagaEvent(ca.bc.gov.educ.penreg.api.model.v1.SagaEvent)

Example 5 with PenRegAPIRuntimeException

use of ca.bc.gov.educ.penreg.api.exception.PenRegAPIRuntimeException in project EDUC-PEN-REG-BATCH-API by bcgov.

the class PenRequestBatchStudentService method isPenAlreadyAssigned.

@Transactional(propagation = Propagation.REQUIRES_NEW)
public boolean isPenAlreadyAssigned(final PenRequestBatchEntity penRequestBatch, final String assignedPen) {
    boolean penAlreadyAssigned = false;
    val redisKey = "multiple-assigned-pen-check::".concat(penRequestBatch.getPenRequestBatchID().toString()).concat("::").concat(assignedPen);
    log.debug("checking for multiples in batch:: {}", penRequestBatch.getPenRequestBatchID());
    final RPermitExpirableSemaphore semaphore = this.getRedissonClient().getPermitExpirableSemaphore("checkForMultiple::" + penRequestBatch.getPenRequestBatchID());
    semaphore.trySetPermits(1);
    semaphore.expire(120, TimeUnit.SECONDS);
    try {
        final String id = semaphore.tryAcquire(120, 40, TimeUnit.SECONDS);
        final String assignedPEN = this.getStringRedisTemplate().opsForValue().get(redisKey);
        if (StringUtils.isNotBlank(assignedPEN)) {
            penAlreadyAssigned = true;
        } else {
            this.getStringRedisTemplate().opsForValue().set(redisKey, "true", Duration.ofDays(1));
        }
        semaphore.tryRelease(id);
    } catch (final Exception e) {
        log.error("PenMatchRecord in priority queue is empty for matched status, this should not have happened.");
        throw new PenRegAPIRuntimeException("PenMatchRecord in priority queue is empty for matched status, this should not have happened.");
    }
    return penAlreadyAssigned;
}
Also used : lombok.val(lombok.val) PenRegAPIRuntimeException(ca.bc.gov.educ.penreg.api.exception.PenRegAPIRuntimeException) RPermitExpirableSemaphore(org.redisson.api.RPermitExpirableSemaphore) InvalidParameterException(ca.bc.gov.educ.penreg.api.exception.InvalidParameterException) EntityNotFoundException(ca.bc.gov.educ.penreg.api.exception.EntityNotFoundException) CompletionException(java.util.concurrent.CompletionException) PenRegAPIRuntimeException(ca.bc.gov.educ.penreg.api.exception.PenRegAPIRuntimeException) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

PenRegAPIRuntimeException (ca.bc.gov.educ.penreg.api.exception.PenRegAPIRuntimeException)8 lombok.val (lombok.val)7 SagaEvent (ca.bc.gov.educ.penreg.api.model.v1.SagaEvent)5 Student (ca.bc.gov.educ.penreg.api.struct.Student)4 Event (ca.bc.gov.educ.penreg.api.struct.Event)2 PenRequestBatch (ca.bc.gov.educ.penreg.api.struct.v1.PenRequestBatch)2 EntityNotFoundException (ca.bc.gov.educ.penreg.api.exception.EntityNotFoundException)1 InvalidParameterException (ca.bc.gov.educ.penreg.api.exception.InvalidParameterException)1 PenRequestBatchStudent (ca.bc.gov.educ.penreg.api.struct.v1.PenRequestBatchStudent)1 CompletionException (java.util.concurrent.CompletionException)1 RPermitExpirableSemaphore (org.redisson.api.RPermitExpirableSemaphore)1 Transactional (org.springframework.transaction.annotation.Transactional)1