|
| 1 | +package ca.bc.gov.educ.api.pen.replication.service; |
| 2 | + |
| 3 | +import ca.bc.gov.educ.api.pen.replication.model.Event; |
| 4 | +import ca.bc.gov.educ.api.pen.replication.repository.EventRepository; |
| 5 | +import ca.bc.gov.educ.api.pen.replication.rest.RestUtils; |
| 6 | +import ca.bc.gov.educ.api.pen.replication.struct.GraduationStudentRecord; |
| 7 | +import ca.bc.gov.educ.api.pen.replication.struct.StudentSchoolOfRecordUpdate; |
| 8 | +import jakarta.persistence.EntityManager; |
| 9 | +import jakarta.persistence.EntityManagerFactory; |
| 10 | +import lombok.extern.slf4j.Slf4j; |
| 11 | +import lombok.val; |
| 12 | +import org.apache.commons.lang3.StringUtils; |
| 13 | +import org.springframework.stereotype.Service; |
| 14 | + |
| 15 | +import static ca.bc.gov.educ.api.pen.replication.constants.EventType.UPDATE_GRAD_STUDENT_CITIZENSHIP; |
| 16 | +import static ca.bc.gov.educ.api.pen.replication.constants.EventType.UPDATE_SCHOOL_OF_RECORD; |
| 17 | + |
| 18 | +@Service |
| 19 | +@Slf4j |
| 20 | +public class SchoolOfRecordUpdateService extends BaseService<StudentSchoolOfRecordUpdate> { |
| 21 | + |
| 22 | + private final TraxStudentService traxStudentService; |
| 23 | + /** |
| 24 | + * The Rest utils. |
| 25 | + */ |
| 26 | + private final RestUtils restUtils; |
| 27 | + |
| 28 | + public SchoolOfRecordUpdateService(final EntityManagerFactory emf, final EventRepository eventRepository, TraxStudentService traxStudentService, final RestUtils restUtils) { |
| 29 | + super(eventRepository, emf); |
| 30 | + this.traxStudentService = traxStudentService; |
| 31 | + this.restUtils = restUtils; |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Process event. |
| 36 | + * |
| 37 | + * @param event the event |
| 38 | + */ |
| 39 | + @Override |
| 40 | + public void processEvent(final StudentSchoolOfRecordUpdate schoolOfRecordUpdate, final Event event) { |
| 41 | + var studentPEN = restUtils.getStudentPen(schoolOfRecordUpdate.getStudentID()); |
| 42 | + val existingTraxStudentRecord = this.traxStudentService.findTraxStudentByPen(StringUtils.rightPad(studentPEN, 10)); |
| 43 | + if (existingTraxStudentRecord.isPresent()) { |
| 44 | + val existingTraxStudent = existingTraxStudentRecord.get(); |
| 45 | + var school = restUtils.getSchoolBySchoolID(schoolOfRecordUpdate.getSchoolOfRecordID()); |
| 46 | + existingTraxStudent.setMincode(school.get().getMincode()); |
| 47 | + |
| 48 | + log.info("Processing choreography update event with ID {} :: payload is: {}", event.getEventId(), existingTraxStudent); |
| 49 | + this.traxStudentService.saveTraxStudent(existingTraxStudent); |
| 50 | + } |
| 51 | + this.updateEvent(event); |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | + /** |
| 56 | + * Gets event type. |
| 57 | + * |
| 58 | + * @return the event type |
| 59 | + */ |
| 60 | + @Override |
| 61 | + public String getEventType() { |
| 62 | + return UPDATE_SCHOOL_OF_RECORD.toString(); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + protected void buildAndExecutePreparedStatements(final EntityManager em, final StudentSchoolOfRecordUpdate schoolOfRecordUpdate) { |
| 67 | + // Not required this child class use repository pattern of spring. |
| 68 | + } |
| 69 | +} |
0 commit comments