diff --git a/integrator/src/main/java/org/bahmni_avni_integration/contract/bahmni/OpenMRSAuditInfo.java b/integrator/src/main/java/org/bahmni_avni_integration/contract/bahmni/OpenMRSAuditInfo.java new file mode 100644 index 00000000..6fc098ee --- /dev/null +++ b/integrator/src/main/java/org/bahmni_avni_integration/contract/bahmni/OpenMRSAuditInfo.java @@ -0,0 +1,16 @@ +package org.bahmni_avni_integration.contract.bahmni; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +@JsonIgnoreProperties(ignoreUnknown = true) +public class OpenMRSAuditInfo { + private String dateCreated; + + public String getDateCreated() { + return dateCreated; + } + + public void setDateCreated(String dateCreated) { + this.dateCreated = dateCreated; + } +} \ No newline at end of file diff --git a/integrator/src/main/java/org/bahmni_avni_integration/contract/bahmni/OpenMRSPatient.java b/integrator/src/main/java/org/bahmni_avni_integration/contract/bahmni/OpenMRSPatient.java index 6fda14aa..f0c2c3aa 100644 --- a/integrator/src/main/java/org/bahmni_avni_integration/contract/bahmni/OpenMRSPatient.java +++ b/integrator/src/main/java/org/bahmni_avni_integration/contract/bahmni/OpenMRSPatient.java @@ -12,6 +12,7 @@ public class OpenMRSPatient { private OpenMRSPerson person; private List identifiers; private boolean voided; + private OpenMRSAuditInfo auditInfo; public OpenMRSPatient(OpenMRSPerson person) { this.person = person; @@ -81,4 +82,12 @@ public boolean isVoided() { public void setVoided(boolean voided) { this.voided = voided; } + + public OpenMRSAuditInfo getAuditInfo() { + return auditInfo; + } + + public void setAuditInfo(OpenMRSAuditInfo auditInfo) { + this.auditInfo = auditInfo; + } } \ No newline at end of file diff --git a/integrator/src/main/java/org/bahmni_avni_integration/contract/bahmni/OpenMRSSaveVisit.java b/integrator/src/main/java/org/bahmni_avni_integration/contract/bahmni/OpenMRSSaveVisit.java index 109a99c5..d519ea02 100644 --- a/integrator/src/main/java/org/bahmni_avni_integration/contract/bahmni/OpenMRSSaveVisit.java +++ b/integrator/src/main/java/org/bahmni_avni_integration/contract/bahmni/OpenMRSSaveVisit.java @@ -9,6 +9,7 @@ public class OpenMRSSaveVisit { private String patient; private String visitType; private String location; + private String startDatetime; public String getPatient() { return patient; @@ -33,4 +34,12 @@ public String getLocation() { public void setLocation(String location) { this.location = location; } + + public String getStartDatetime() { + return startDatetime; + } + + public void setStartDatetime(String startDatetime) { + this.startDatetime = startDatetime; + } } \ No newline at end of file diff --git a/integrator/src/main/java/org/bahmni_avni_integration/integration_data/repository/openmrs/OpenMRSPatientRepository.java b/integrator/src/main/java/org/bahmni_avni_integration/integration_data/repository/openmrs/OpenMRSPatientRepository.java index 3d0178ff..5b3c92b2 100644 --- a/integrator/src/main/java/org/bahmni_avni_integration/integration_data/repository/openmrs/OpenMRSPatientRepository.java +++ b/integrator/src/main/java/org/bahmni_avni_integration/integration_data/repository/openmrs/OpenMRSPatientRepository.java @@ -22,18 +22,18 @@ public OpenMRSPatient getPatient(Event event) { return ObjectJsonMapper.readValue(patientJSON, OpenMRSPatient.class); } - public OpenMRSUuidHolder getPatientByIdentifier(String identifier) { - String patientJSON = openMRSWebClient.get(URI.create(String.format("%s?identifier=%s", getResourcePath("patient"), identifier))); - SearchResults searchResults = ObjectJsonMapper.readValue(patientJSON, new TypeReference>() { + public OpenMRSPatient getPatientByIdentifier(String identifier) { + String patientJSON = openMRSWebClient.get(URI.create(String.format("%s?identifier=%s&v=full", getResourcePath("patient"), identifier))); + SearchResults searchResults = ObjectJsonMapper.readValue(patientJSON, new TypeReference>() { }); // story-todo do full run after changing it return pickAndExpectOne(searchResults.removeDuplicates(), identifier); } - public OpenMRSUuidHolder createPatient(OpenMRSSavePatient openMRSSavePatient) { + public OpenMRSPatient createPatient(OpenMRSSavePatient openMRSSavePatient) { String json = ObjectJsonMapper.writeValueAsString(openMRSSavePatient); String outputJson = openMRSWebClient.post(getResourcePath("patient"), json); - return ObjectJsonMapper.readValue(outputJson, OpenMRSUuidHolder.class); + return ObjectJsonMapper.readValue(outputJson, OpenMRSPatient.class); } public OpenMRSPatient getPatient(String patientUuid) { diff --git a/integrator/src/main/java/org/bahmni_avni_integration/service/EnrolmentService.java b/integrator/src/main/java/org/bahmni_avni_integration/service/EnrolmentService.java index 6f5e67a1..94b739d5 100644 --- a/integrator/src/main/java/org/bahmni_avni_integration/service/EnrolmentService.java +++ b/integrator/src/main/java/org/bahmni_avni_integration/service/EnrolmentService.java @@ -3,6 +3,7 @@ import org.bahmni_avni_integration.contract.avni.Enrolment; import org.bahmni_avni_integration.contract.bahmni.OpenMRSEncounter; import org.bahmni_avni_integration.contract.bahmni.OpenMRSFullEncounter; +import org.bahmni_avni_integration.contract.bahmni.OpenMRSPatient; import org.bahmni_avni_integration.contract.bahmni.OpenMRSUuidHolder; import org.bahmni_avni_integration.integration_data.domain.*; import org.bahmni_avni_integration.mapper.avni.EnrolmentMapper; @@ -26,15 +27,15 @@ public EnrolmentService(MappingMetaDataRepository mappingMetaDataRepository, Ope this.visitService = visitService; } - public OpenMRSFullEncounter findCommunityEnrolment(Enrolment enrolment, OpenMRSUuidHolder patient) { + public OpenMRSFullEncounter findCommunityEnrolment(Enrolment enrolment, OpenMRSPatient patient) { return findCommunityEnrolment(enrolment, patient, MappingType.CommunityEnrolment_EncounterType); } - public OpenMRSFullEncounter findCommunityExitEnrolment(Enrolment enrolment, OpenMRSUuidHolder patient) { + public OpenMRSFullEncounter findCommunityExitEnrolment(Enrolment enrolment, OpenMRSPatient patient) { return findCommunityEnrolment(enrolment, patient, MappingType.CommunityEnrolmentExit_EncounterType); } - private OpenMRSFullEncounter findCommunityEnrolment(Enrolment enrolment, OpenMRSUuidHolder patient, MappingType mappingType) { + private OpenMRSFullEncounter findCommunityEnrolment(Enrolment enrolment, OpenMRSPatient patient, MappingType mappingType) { String bahmniValueForAvniUuidConcept = mappingMetaDataRepository.getBahmniValueForAvniIdConcept(); var encounterTypeUuid = mappingMetaDataRepository.getBahmniValue(MappingGroup.ProgramEnrolment, mappingType, enrolment.getProgram()); OpenMRSFullEncounter encounter = openMRSEncounterRepository @@ -46,7 +47,7 @@ public void processPatientNotFound(Enrolment enrolment) { errorService.errorOccurred(enrolment, ErrorType.NoPatientWithId); } - public OpenMRSFullEncounter createCommunityEnrolment(Enrolment enrolment, OpenMRSUuidHolder openMRSPatient, Constants constants) { + public OpenMRSFullEncounter createCommunityEnrolment(Enrolment enrolment, OpenMRSPatient openMRSPatient, Constants constants) { if (enrolment.getVoided()) return null; OpenMRSEncounter encounter = enrolmentMapper.mapEnrolmentToEnrolmentEncounter(enrolment, openMRSPatient.getUuid(), constants); OpenMRSUuidHolder visit = visitService.getOrCreateVisit(openMRSPatient); @@ -55,7 +56,7 @@ public OpenMRSFullEncounter createCommunityEnrolment(Enrolment enrolment, OpenMR return savedEncounter; } - public OpenMRSFullEncounter createCommunityExitEnrolment(Enrolment enrolment, OpenMRSUuidHolder openMRSPatient, Constants constants) { + public OpenMRSFullEncounter createCommunityExitEnrolment(Enrolment enrolment, OpenMRSPatient openMRSPatient, Constants constants) { if (enrolment.getVoided()) return null; OpenMRSEncounter encounter = enrolmentMapper.mapEnrolmentToExitEncounter(enrolment, openMRSPatient.getUuid(), constants); diff --git a/integrator/src/main/java/org/bahmni_avni_integration/service/PatientService.java b/integrator/src/main/java/org/bahmni_avni_integration/service/PatientService.java index 89967884..187f7157 100644 --- a/integrator/src/main/java/org/bahmni_avni_integration/service/PatientService.java +++ b/integrator/src/main/java/org/bahmni_avni_integration/service/PatientService.java @@ -38,7 +38,7 @@ public PatientService(SubjectMapper subjectMapper, OpenMRSEncounterRepository op this.visitService = visitService; } - public void updateSubject(OpenMRSFullEncounter existingEncounter, OpenMRSUuidHolder patient, Subject subject, SubjectToPatientMetaData subjectToPatientMetaData, Constants constants) { + public void updateSubject(OpenMRSFullEncounter existingEncounter, OpenMRSPatient patient, Subject subject, SubjectToPatientMetaData subjectToPatientMetaData, Constants constants) { if (subject.getVoided()) { openMRSEncounterRepository.voidEncounter(existingEncounter); } else { @@ -48,7 +48,7 @@ public void updateSubject(OpenMRSFullEncounter existingEncounter, OpenMRSUuidHol } } - public OpenMRSFullEncounter createSubject(Subject subject, OpenMRSUuidHolder patient, SubjectToPatientMetaData subjectToPatientMetaData, Constants constants) { + public OpenMRSFullEncounter createSubject(Subject subject, OpenMRSPatient patient, SubjectToPatientMetaData subjectToPatientMetaData, Constants constants) { if (subject.getVoided()) return null; @@ -61,9 +61,9 @@ public OpenMRSFullEncounter createSubject(Subject subject, OpenMRSUuidHolder pat return savedEncounter; } - public Pair findSubject(Subject subject, Constants constants, SubjectToPatientMetaData subjectToPatientMetaData) { + public Pair findSubject(Subject subject, Constants constants, SubjectToPatientMetaData subjectToPatientMetaData) { String subjectId = subject.getUuid(); - OpenMRSUuidHolder patient = findPatient(subject, constants, subjectToPatientMetaData); + OpenMRSPatient patient = findPatient(subject, constants, subjectToPatientMetaData); if (patient == null) { return new Pair<>(null, null); } @@ -71,9 +71,9 @@ public Pair findSubject(Subject subject return new Pair<>(patient, encounter); } - public OpenMRSUuidHolder findPatient(Subject subject, Constants constants, SubjectToPatientMetaData subjectToPatientMetaData) { + public OpenMRSPatient findPatient(Subject subject, Constants constants, SubjectToPatientMetaData subjectToPatientMetaData) { String patientIdentifier = constants.getValue(ConstantKey.BahmniIdentifierPrefix) + subject.getId(subjectToPatientMetaData); - OpenMRSUuidHolder patient = openMRSPatientRepository.getPatientByIdentifier(patientIdentifier); + OpenMRSPatient patient = openMRSPatientRepository.getPatientByIdentifier(patientIdentifier); return patient; } @@ -89,7 +89,7 @@ public void processPatientNotFound(Subject subject, SubjectToPatientMetaData met errorService.errorOccurred(subject, ErrorType.NoPatientWithId); } - public OpenMRSUuidHolder createPatient(Subject subject, SubjectToPatientMetaData metaData, Constants constants) { + public OpenMRSPatient createPatient(Subject subject, SubjectToPatientMetaData metaData, Constants constants) { if (subject.getVoided()) return null; diff --git a/integrator/src/main/java/org/bahmni_avni_integration/service/ProgramEncounterService.java b/integrator/src/main/java/org/bahmni_avni_integration/service/ProgramEncounterService.java index deb78cea..563b5b6e 100644 --- a/integrator/src/main/java/org/bahmni_avni_integration/service/ProgramEncounterService.java +++ b/integrator/src/main/java/org/bahmni_avni_integration/service/ProgramEncounterService.java @@ -4,6 +4,7 @@ import org.bahmni_avni_integration.contract.avni.Subject; import org.bahmni_avni_integration.contract.bahmni.OpenMRSEncounter; import org.bahmni_avni_integration.contract.bahmni.OpenMRSFullEncounter; +import org.bahmni_avni_integration.contract.bahmni.OpenMRSPatient; import org.bahmni_avni_integration.contract.bahmni.OpenMRSUuidHolder; import org.bahmni_avni_integration.integration_data.domain.Constants; import org.bahmni_avni_integration.integration_data.domain.ErrorType; @@ -33,8 +34,8 @@ public ProgramEncounterService(PatientService patientService, MappingMetaDataRep this.errorService = errorService; } - public Pair findCommunityEncounter(ProgramEncounter programEncounter, Subject subject, Constants constants, SubjectToPatientMetaData subjectToPatientMetaData) { - OpenMRSUuidHolder patient = patientService.findPatient(subject, constants, subjectToPatientMetaData); + public Pair findCommunityEncounter(ProgramEncounter programEncounter, Subject subject, Constants constants, SubjectToPatientMetaData subjectToPatientMetaData) { + OpenMRSPatient patient = patientService.findPatient(subject, constants, subjectToPatientMetaData); if (patient == null) { return new Pair<>(null, null); } @@ -44,7 +45,7 @@ public Pair findCommunityEncounter(Prog return new Pair<>(patient, encounter); } - public OpenMRSFullEncounter createCommunityEncounter(ProgramEncounter programEncounter, OpenMRSUuidHolder patient, Constants constants) { + public OpenMRSFullEncounter createCommunityEncounter(ProgramEncounter programEncounter, OpenMRSPatient patient, Constants constants) { if (programEncounter.getVoided()) return null; OpenMRSUuidHolder visit = visitService.getOrCreateVisit(patient); diff --git a/integrator/src/main/java/org/bahmni_avni_integration/service/VisitService.java b/integrator/src/main/java/org/bahmni_avni_integration/service/VisitService.java index 3ff41fe0..5a71e176 100644 --- a/integrator/src/main/java/org/bahmni_avni_integration/service/VisitService.java +++ b/integrator/src/main/java/org/bahmni_avni_integration/service/VisitService.java @@ -18,27 +18,28 @@ public VisitService(ConstantsRepository constantsRepository, OpenMRSVisitReposit this.openMRSVisitRepository = openMRSVisitRepository; } - public OpenMRSUuidHolder getVisit(String patientUuid) { + private OpenMRSUuidHolder getVisit(String patientUuid) { String locationUuid = constantsRepository.findAllConstants().getValue(ConstantKey.IntegrationBahmniLocation); return openMRSVisitRepository.getVisit(patientUuid, locationUuid); } - public OpenMRSUuidHolder createVisit(String patientUuid) { + private OpenMRSUuidHolder createVisit(OpenMRSPatient patient) { String location = constantsRepository.findAllConstants().getValue(ConstantKey.IntegrationBahmniLocation); String visitType = constantsRepository.findAllConstants().getValue(ConstantKey.IntegrationBahmniVisitType); OpenMRSSaveVisit openMRSSaveVisit = new OpenMRSSaveVisit(); openMRSSaveVisit.setLocation(location); openMRSSaveVisit.setVisitType(visitType); - openMRSSaveVisit.setPatient(patientUuid); + openMRSSaveVisit.setPatient(patient.getUuid()); + openMRSSaveVisit.setStartDatetime(patient.getAuditInfo().getDateCreated()); OpenMRSUuidHolder visit = openMRSVisitRepository.createVisit(openMRSSaveVisit); logger.debug("Created new visit with uuid %s".formatted(visit.getUuid())); return visit; } - public OpenMRSUuidHolder getOrCreateVisit(OpenMRSUuidHolder patient) { + public OpenMRSUuidHolder getOrCreateVisit(OpenMRSPatient patient) { var visit = getVisit(patient.getUuid()); if (visit == null) { - return createVisit(patient.getUuid()); + return createVisit(patient); } logger.debug("Retrieved existing visit with uuid %s".formatted(visit.getUuid())); return visit; diff --git a/integrator/src/main/java/org/bahmni_avni_integration/worker/avni/EnrolmentWorker.java b/integrator/src/main/java/org/bahmni_avni_integration/worker/avni/EnrolmentWorker.java index d1e88928..f2e48ba3 100644 --- a/integrator/src/main/java/org/bahmni_avni_integration/worker/avni/EnrolmentWorker.java +++ b/integrator/src/main/java/org/bahmni_avni_integration/worker/avni/EnrolmentWorker.java @@ -4,6 +4,7 @@ import org.bahmni_avni_integration.contract.avni.Enrolment; import org.bahmni_avni_integration.contract.avni.EnrolmentsResponse; import org.bahmni_avni_integration.contract.avni.Subject; +import org.bahmni_avni_integration.contract.bahmni.OpenMRSPatient; import org.bahmni_avni_integration.contract.bahmni.OpenMRSUuidHolder; import org.bahmni_avni_integration.integration_data.domain.AvniEntityStatus; import org.bahmni_avni_integration.integration_data.domain.AvniEntityType; @@ -126,7 +127,7 @@ protected void processEnrolment(Enrolment enrolment) { entityStatusService.saveEntityStatus(enrolment); } - private void processExitedEnrolment(Constants constants, Enrolment enrolment, OpenMRSUuidHolder patient) { + private void processExitedEnrolment(Constants constants, Enrolment enrolment, OpenMRSPatient patient) { var exitEncounter = enrolmentService.findCommunityExitEnrolment(enrolment, patient); if (exitEncounter == null) { logger.debug(String.format("Creating new Bahmni exit enrolment for Avni enrolment %s", enrolment.getUuid())); diff --git a/integrator/src/main/java/org/bahmni_avni_integration/worker/avni/ProgramEncounterWorker.java b/integrator/src/main/java/org/bahmni_avni_integration/worker/avni/ProgramEncounterWorker.java index e112216c..5db48b00 100644 --- a/integrator/src/main/java/org/bahmni_avni_integration/worker/avni/ProgramEncounterWorker.java +++ b/integrator/src/main/java/org/bahmni_avni_integration/worker/avni/ProgramEncounterWorker.java @@ -4,6 +4,7 @@ import org.bahmni_avni_integration.contract.avni.ProgramEncounter; import org.bahmni_avni_integration.contract.avni.ProgramEncountersResponse; import org.bahmni_avni_integration.contract.bahmni.OpenMRSFullEncounter; +import org.bahmni_avni_integration.contract.bahmni.OpenMRSPatient; import org.bahmni_avni_integration.contract.bahmni.OpenMRSUuidHolder; import org.bahmni_avni_integration.integration_data.domain.AvniEntityStatus; import org.bahmni_avni_integration.integration_data.domain.AvniEntityType; @@ -92,7 +93,7 @@ protected void processProgramEncounter(ProgramEncounter programEncounter) { var subject = avniSubjectRepository.getSubject(programEncounter.getSubjectId()); logger.debug(String.format("Found avni subject %s", subject.getUuid())); - Pair patientEncounter = programEncounterService.findCommunityEncounter(programEncounter, subject, constants, metaData); + Pair patientEncounter = programEncounterService.findCommunityEncounter(programEncounter, subject, constants, metaData); var patient = patientEncounter.getValue0(); var encounter = patientEncounter.getValue1(); diff --git a/integrator/src/main/java/org/bahmni_avni_integration/worker/avni/SubjectWorker.java b/integrator/src/main/java/org/bahmni_avni_integration/worker/avni/SubjectWorker.java index 2e133e5b..0a23a01d 100644 --- a/integrator/src/main/java/org/bahmni_avni_integration/worker/avni/SubjectWorker.java +++ b/integrator/src/main/java/org/bahmni_avni_integration/worker/avni/SubjectWorker.java @@ -4,6 +4,7 @@ import org.bahmni_avni_integration.contract.avni.Subject; import org.bahmni_avni_integration.contract.avni.SubjectsResponse; import org.bahmni_avni_integration.contract.bahmni.OpenMRSFullEncounter; +import org.bahmni_avni_integration.contract.bahmni.OpenMRSPatient; import org.bahmni_avni_integration.contract.bahmni.OpenMRSUuidHolder; import org.bahmni_avni_integration.integration_data.domain.*; import org.bahmni_avni_integration.integration_data.internal.SubjectToPatientMetaData; @@ -70,7 +71,7 @@ private void removeIgnoredObservations(Subject subject) { @Transactional(propagation = Propagation.REQUIRES_NEW) protected void processSubject(Subject subject) { removeIgnoredObservations(subject); - Pair patientEncounter; + Pair patientEncounter; try { patientEncounter = patientService.findSubject(subject, constants, metaData); } catch (MultipleResultsFoundException e) { @@ -78,7 +79,7 @@ protected void processSubject(Subject subject) { return; } - OpenMRSUuidHolder patient = patientEncounter.getValue0(); + OpenMRSPatient patient = patientEncounter.getValue0(); OpenMRSFullEncounter encounter = patientEncounter.getValue1(); if (encounter != null && patient != null) { diff --git a/integrator/src/test/java/org/bahmni_avni_integration/integration_data/repository/openmrs/OpenMRSPatientRepositoryExternalTest.java b/integrator/src/test/java/org/bahmni_avni_integration/integration_data/repository/openmrs/OpenMRSPatientRepositoryExternalTest.java index 7dab11b0..75283d85 100644 --- a/integrator/src/test/java/org/bahmni_avni_integration/integration_data/repository/openmrs/OpenMRSPatientRepositoryExternalTest.java +++ b/integrator/src/test/java/org/bahmni_avni_integration/integration_data/repository/openmrs/OpenMRSPatientRepositoryExternalTest.java @@ -1,6 +1,7 @@ package org.bahmni_avni_integration.integration_data.repository.openmrs; import com.fasterxml.jackson.core.JsonProcessingException; +import org.bahmni_avni_integration.contract.bahmni.OpenMRSPatient; import org.bahmni_avni_integration.contract.bahmni.OpenMRSUuidHolder; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -15,7 +16,7 @@ class OpenMRSPatientRepositoryExternalTest { @Test public void findPatientByIdentifier() throws JsonProcessingException { - OpenMRSUuidHolder patient = openMRSPatientRepository.getPatientByIdentifier("TRI08121601"); + OpenMRSPatient patient = openMRSPatientRepository.getPatientByIdentifier("TRI08121601"); assertNotNull(patient); assertNotNull(patient.getUuid()); }