use of ca.bc.gov.educ.penreg.api.exception.EntityNotFoundException 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.exception.EntityNotFoundException 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