Skip to content

Commit

Permalink
Merge pull request #72 from bcgov/feature/iosas
Browse files Browse the repository at this point in the history
Feature/iosas
  • Loading branch information
Harry0589 authored Apr 4, 2024
2 parents 659dd0a + b49cc8a commit 4f871dc
Show file tree
Hide file tree
Showing 9 changed files with 379 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,6 @@ public void handleEvent(@NonNull final Event event) {
final EventService<List<StudentMerge>> deleteMergeService = (EventService<List<StudentMerge>>) this.eventServiceMap.get(DELETE_MERGE.toString());
deleteMergeService.processEvent(deleteStudentMergeList, event);
break;
case "UPDATE_SCHOOL":
log.info("Processing UPDATE_SCHOOL event record :: {} ", event);
val school = JsonUtil.getJsonObjectFromString(School.class, event.getEventPayload());
final EventService<School> schoolEventService = (EventService<School>) this.eventServiceMap.get(UPDATE_SCHOOL.toString());
schoolEventService.processEvent(school, event);
break;
case "UPDATE_AUTHORITY":
log.info("Processing UPDATE_AUTHORITY event record :: {} ", event);
val updateAuthority = JsonUtil.getJsonObjectFromString(IndependentAuthority.class, event.getEventPayload());
final EventService<IndependentAuthority> authorityEventServiceUpdate = (EventService<IndependentAuthority>) this.eventServiceMap.get(UPDATE_AUTHORITY.toString());
authorityEventServiceUpdate.processEvent(updateAuthority, event);
break;
case "UPDATE_DISTRICT":
log.info("Processing UPDATE_DISTRICT event record :: {} ", event);
val updateDistrict = JsonUtil.getJsonObjectFromString(District.class, event.getEventPayload());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,20 @@ public enum EventOutcome {

SCHOOL_CREATED_IN_SPM,

SCHOOL_UPDATED_IN_SPM,


SCHOOL_WRITE_SKIPPED_IN_SPM_FOR_DATES,

SCHOOL_CREATED_IN_IOSAS,

SCHOOL_UPDATED_IN_IOSAS,

SCHOOL_CREATED_IN_ISFS,

SCHOOL_UPDATED_IN_ISFS,


DISTRICT_UPDATED,

DISTRICT_CREATED,
Expand All @@ -71,12 +79,18 @@ public enum EventOutcome {

AUTHORITY_CREATED_IN_SPM,

AUTHORITY_UPDATED_IN_SPM,

AUTHORITY_WRITE_SKIPPED_IN_SPM_FOR_DATES,

AUTHORITY_CREATED_IN_IOSAS,

AUTHORITY_UPDATED_IN_IOSAS,

AUTHORITY_CREATED_IN_ISFS,

AUTHORITY_UPDATED_IN_ISFS,

AUTHORITY_FOUND,

AUTHORITY_NOT_FOUND
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,17 @@ public enum EventType {

CREATE_SCHOOL_IN_SPM,

UPDATE_SCHOOL_IN_SPM,


CREATE_SCHOOL_IN_IOSAS,

UPDATE_SCHOOL_IN_IOSAS,

CREATE_SCHOOL_IN_ISFS,

UPDATE_SCHOOL_IN_ISFS,

UPDATE_DISTRICT,

CREATE_DISTRICT,
Expand All @@ -57,10 +65,13 @@ public enum EventType {

CREATE_AUTHORITY_IN_SPM,

UPDATE_AUTHORITY_IN_SPM,
CREATE_AUTHORITY_IN_IOSAS,

UPDATE_AUTHORITY_IN_IOSAS,
CREATE_AUTHORITY_IN_ISFS,

UPDATE_AUTHORITY_IN_ISFS,

GET_AUTHORITY,

GET_PAGINATED_SCHOOLS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,17 @@ public enum SagaEnum {
* Pen replication authority create saga enum.
*/
PEN_REPLICATION_AUTHORITY_CREATE_SAGA("PEN_REPLICATION_AUTHORITY_CREATE_SAGA"),

PEN_REPLICATION_AUTHORITY_UPDATE_SAGA("PEN_REPLICATION_AUTHORITY_UPDATE_SAGA"),
/**
* Pen replication school create saga enum.
*/
PEN_REPLICATION_SCHOOL_CREATE_SAGA("PEN_REPLICATION_SCHOOL_CREATE_SAGA"),
/**
* Pen replication school update saga enum.
*/
PEN_REPLICATION_SCHOOL_UPDATE_SAGA("PEN_REPLICATION_SCHOOL_UPDATE_SAGA"),

/**
* Pen replication district create saga enum.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package ca.bc.gov.educ.api.pen.replication.orchestrator;

import ca.bc.gov.educ.api.pen.replication.constants.IndependentSchoolSystem;
import ca.bc.gov.educ.api.pen.replication.constants.SagaEnum;
import ca.bc.gov.educ.api.pen.replication.constants.SagaTopicsEnum;
import ca.bc.gov.educ.api.pen.replication.mappers.AuthorityMapper;
import ca.bc.gov.educ.api.pen.replication.mappers.AuthorityMapperHelper;
import ca.bc.gov.educ.api.pen.replication.messaging.MessagePublisher;
import ca.bc.gov.educ.api.pen.replication.model.AuthorityMasterEntity;
import ca.bc.gov.educ.api.pen.replication.model.Saga;
import ca.bc.gov.educ.api.pen.replication.model.SagaEvent;
import ca.bc.gov.educ.api.pen.replication.orchestrator.base.BaseOrchestrator;
import ca.bc.gov.educ.api.pen.replication.repository.AuthorityMasterRepository;
import ca.bc.gov.educ.api.pen.replication.rest.RestUtils;
import ca.bc.gov.educ.api.pen.replication.service.AuthorityCreateService;
import ca.bc.gov.educ.api.pen.replication.service.SagaService;
import ca.bc.gov.educ.api.pen.replication.struct.Event;
import ca.bc.gov.educ.api.pen.replication.struct.saga.AuthorityCreateSagaData;
import ca.bc.gov.educ.api.pen.replication.struct.saga.AuthorityUpdateSagaData;
import ca.bc.gov.educ.api.pen.replication.util.JsonUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import jakarta.persistence.EntityManagerFactory;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.stereotype.Component;

import static ca.bc.gov.educ.api.pen.replication.constants.EventOutcome.*;
import static ca.bc.gov.educ.api.pen.replication.constants.EventType.*;


@Component
@Slf4j
public class AuthorityUpdateOrchestrator extends BaseOrchestrator<AuthorityUpdateSagaData> {

public static final String RESPONDED_VIA_NATS_TO_FOR_EVENT = "responded via NATS to {} for {} Event. :: {}";
private final RestUtils restUtils;

private AuthorityMasterRepository authorityMasterRepository = null;
private AuthorityMapperHelper authorityMapperHelper = null;
private static final AuthorityMapper authorityMapper = AuthorityMapper.mapper;


protected AuthorityUpdateOrchestrator(final SagaService sagaService, final MessagePublisher messagePublisher, final EntityManagerFactory entityManagerFactory, final RestUtils restUtils, AuthorityCreateService authorityCreateService) {
super(entityManagerFactory, sagaService, messagePublisher, AuthorityUpdateSagaData.class, SagaEnum.PEN_REPLICATION_AUTHORITY_UPDATE_SAGA, SagaTopicsEnum.PEN_REPLICATION_AUTHORITY_CREATE_SAGA_TOPIC);
this.restUtils = restUtils;
this.authorityMasterRepository = authorityMasterRepository;
this.authorityMapperHelper = authorityMapperHelper;
}

@Override
public void populateStepsToExecuteMap() {
this.stepBuilder()
.begin(UPDATE_AUTHORITY_IN_SPM, this::updateAuthorityInSPM)
.step(UPDATE_AUTHORITY_IN_SPM, AUTHORITY_UPDATED_IN_SPM, UPDATE_AUTHORITY_IN_IOSAS, this::updateAuthorityInIOSAS)
.step(UPDATE_AUTHORITY_IN_SPM, AUTHORITY_WRITE_SKIPPED_IN_SPM_FOR_DATES, UPDATE_AUTHORITY_IN_IOSAS, this::updateAuthorityInIOSAS)
.step(UPDATE_AUTHORITY_IN_IOSAS, AUTHORITY_UPDATED_IN_IOSAS, UPDATE_AUTHORITY_IN_ISFS, this::updateAuthorityInISFS)
.end(UPDATE_AUTHORITY_IN_ISFS, AUTHORITY_UPDATED_IN_ISFS);
}

private void updateAuthorityInSPM(final Event event, final Saga saga, final AuthorityUpdateSagaData authorityUpdateSagaData) throws JsonProcessingException {
saga.setSagaState(UPDATE_AUTHORITY_IN_SPM.toString());
final SagaEvent eventStates = this.createEventState(saga, event.getEventType(), event.getEventOutcome(), event.getEventPayload());
this.getSagaService().updateAttachedSagaWithEvents(saga, eventStates);
var authority = authorityUpdateSagaData.getIndependentAuthority();
AuthorityMasterEntity newAuthorityMaster = null;
Event nextEvent = null;

val existingSchoolMasterRecord = this.authorityMasterRepository.findById(authority.getAuthorityNumber());
if (existingSchoolMasterRecord.isPresent()) {
val existingAuthorityMaster = existingSchoolMasterRecord.get();
newAuthorityMaster = authorityMapperHelper.toAuthorityMaster(authority, false);
authorityMapper.updateAuthorityMaster(newAuthorityMaster, existingAuthorityMaster);
log.info("Processing choreography update event with payload : {}", newAuthorityMaster);
authorityMasterRepository.save(existingAuthorityMaster);
}
if(newAuthorityMaster != null) {
nextEvent = Event.builder().sagaId(saga.getSagaId())
.eventType(UPDATE_AUTHORITY_IN_SPM)
.eventOutcome(AUTHORITY_UPDATED_IN_SPM)
.eventPayload(JsonUtil.getJsonStringFromObject(newAuthorityMaster))
.build();
}else{
nextEvent = Event.builder().sagaId(saga.getSagaId())
.eventType(UPDATE_AUTHORITY_IN_SPM)
.eventOutcome(AUTHORITY_WRITE_SKIPPED_IN_SPM_FOR_DATES)
.eventPayload("Authority not written to SPM due to date")
.build();
}
this.postMessageToTopic(this.getTopicToSubscribe().getCode(), nextEvent); // this will make it async and use pub/sub flow even though it is sending message to itself
log.info(RESPONDED_VIA_NATS_TO_FOR_EVENT, this.getTopicToSubscribe(), UPDATE_AUTHORITY_IN_SPM, saga.getSagaId());
}

private void updateAuthorityInIOSAS(final Event event, final Saga saga, final AuthorityUpdateSagaData authorityUpdateSagaData) throws JsonProcessingException {
saga.setSagaState(UPDATE_AUTHORITY_IN_IOSAS.toString());
final SagaEvent eventStates = this.createEventState(saga, event.getEventType(), event.getEventOutcome(), event.getEventPayload());
this.getSagaService().updateAttachedSagaWithEvents(saga, eventStates);
restUtils.createOrUpdateAuthorityInIndependentSchoolSystem(authorityUpdateSagaData.getIndependentAuthority(), IndependentSchoolSystem.IOSAS);

val nextEvent = Event.builder().sagaId(saga.getSagaId())
.eventType(UPDATE_AUTHORITY_IN_IOSAS)
.eventOutcome(AUTHORITY_UPDATED_IN_IOSAS)
.eventPayload(JsonUtil.getJsonStringFromObject(authorityUpdateSagaData.getIndependentAuthority()))
.build();
this.postMessageToTopic(this.getTopicToSubscribe().getCode(), nextEvent); // this will make it async and use pub/sub flow even though it is sending message to itself
log.info(RESPONDED_VIA_NATS_TO_FOR_EVENT, this.getTopicToSubscribe(), UPDATE_AUTHORITY_IN_IOSAS, saga.getSagaId());
}

private void updateAuthorityInISFS(final Event event, final Saga saga, final AuthorityUpdateSagaData authorityUpdateSagaData) throws JsonProcessingException {
saga.setSagaState(UPDATE_AUTHORITY_IN_ISFS.toString());
final SagaEvent eventStates = this.createEventState(saga, event.getEventType(), event.getEventOutcome(), event.getEventPayload());
this.getSagaService().updateAttachedSagaWithEvents(saga, eventStates);

restUtils.createOrUpdateAuthorityInIndependentSchoolSystem(authorityUpdateSagaData.getIndependentAuthority(), IndependentSchoolSystem.ISFS);

val nextEvent = Event.builder().sagaId(saga.getSagaId())
.eventType(UPDATE_AUTHORITY_IN_ISFS)
.eventOutcome(AUTHORITY_UPDATED_IN_ISFS)
.eventPayload(JsonUtil.getJsonStringFromObject(authorityUpdateSagaData.getIndependentAuthority()))
.build();
this.postMessageToTopic(this.getTopicToSubscribe().getCode(), nextEvent); // this will make it async and use pub/sub flow even though it is sending message to itself
log.info(RESPONDED_VIA_NATS_TO_FOR_EVENT, this.getTopicToSubscribe(), UPDATE_AUTHORITY_IN_ISFS, saga.getSagaId());
}

}
Loading

0 comments on commit 4f871dc

Please sign in to comment.