Search in sources :

Example 1 with PenRequestBatchEvent

use of ca.bc.gov.educ.penreg.api.model.v1.PenRequestBatchEvent in project EDUC-PEN-REG-BATCH-API by bcgov.

the class EventHandlerService method handleArchivePenRequestBatchEvent.

/**
 * Handle archive pen request batch event
 *
 * @param event the archive pen request batch event
 * @throws JsonProcessingException the json processing exception
 */
private void handleArchivePenRequestBatchEvent(final Event event) throws JsonProcessingException {
    final PenRequestBatchEvent penRequestBatchEvent = this.getPenRequestBatchEventService().archivePenRequestBatch(event);
    this.getEventPublisherService().send(penRequestBatchEvent);
}
Also used : PenRequestBatchEvent(ca.bc.gov.educ.penreg.api.model.v1.PenRequestBatchEvent)

Example 2 with PenRequestBatchEvent

use of ca.bc.gov.educ.penreg.api.model.v1.PenRequestBatchEvent 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 3 with PenRequestBatchEvent

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

Example 4 with PenRequestBatchEvent

use of ca.bc.gov.educ.penreg.api.model.v1.PenRequestBatchEvent in project EDUC-PEN-REG-BATCH-API by bcgov.

the class EventHandlerService method handleUpdatePrbStudentEvent.

/**
 * Send event immediately after update PrbStudent. The scheduler will resend it if failed.
 * Make sure that the PrbStudent update and event sending are in different transactions,
 * so the failure to send event would not affect PrbStudent update.
 *
 * @param event the update PrbStudent event
 * @throws JsonProcessingException the json processing exception
 */
private void handleUpdatePrbStudentEvent(final Event event) throws JsonProcessingException {
    final PenRequestBatchEvent penRequestBatchEvent = this.getPenRequestBatchEventService().updatePenRequestBatchStudent(event);
    this.getEventPublisherService().send(penRequestBatchEvent);
}
Also used : PenRequestBatchEvent(ca.bc.gov.educ.penreg.api.model.v1.PenRequestBatchEvent)

Aggregations

PenRequestBatchEvent (ca.bc.gov.educ.penreg.api.model.v1.PenRequestBatchEvent)4 EntityNotFoundException (ca.bc.gov.educ.penreg.api.exception.EntityNotFoundException)2 lombok.val (lombok.val)2 Transactional (org.springframework.transaction.annotation.Transactional)2 PenRequestBatchEntity (ca.bc.gov.educ.penreg.api.model.v1.PenRequestBatchEntity)1 PenRequestBatchStudentEntity (ca.bc.gov.educ.penreg.api.model.v1.PenRequestBatchStudentEntity)1