From 1614257df13d729c25443d6a50079d91c4fa381e Mon Sep 17 00:00:00 2001 From: Hiren Thacker Date: Tue, 18 May 2021 18:27:44 +0530 Subject: [PATCH] #59 | Fix date related issues --- .../util/FormatAndParseUtil.java | 8 +++++ .../contract/avni/Enrolment.java | 9 ++++-- .../contract/avni/ProgramEncounter.java | 6 ++-- .../contract/avni/Subject.java | 12 +++++++ .../contract/bahmni/OpenMRSSavePerson.java | 12 +++++++ .../contract/bahmni/OpenMRSVisit.java | 31 +++++++++++++++++++ .../openmrs/OpenMRSVisitRepository.java | 11 ++++--- .../mapper/avni/EnrolmentMapper.java | 20 +++++++++--- .../mapper/avni/ProgramEncounterMapper.java | 15 +++++++-- .../mapper/avni/SubjectMapper.java | 15 ++++++++- .../service/EnrolmentService.java | 13 ++++---- .../service/PatientService.java | 6 ++-- .../service/ProgramEncounterService.java | 7 ++--- .../service/VisitService.java | 8 ++--- .../worker/avni/SubjectWorker.java | 9 +++--- .../avni/EnrolmentMapperExternalTest.java | 13 +++++++- .../avni/ObservationMapperExternalTest.java | 6 ++-- .../avni/SubjectMapperExternalTest.java | 12 +++++-- .../avni/EnrolmentWorkerExternalTest.java | 2 +- .../ProgramEncounterWorkerExternalTest.java | 2 +- .../avni/SubjectWorkerExternalTest.java | 2 +- 21 files changed, 171 insertions(+), 48 deletions(-) create mode 100644 integrator/src/main/java/org/bahmni_avni_integration/contract/bahmni/OpenMRSVisit.java diff --git a/integration-data/src/main/java/org/bahmni_avni_integration/integration_data/util/FormatAndParseUtil.java b/integration-data/src/main/java/org/bahmni_avni_integration/integration_data/util/FormatAndParseUtil.java index 79d4409f..9cea5e52 100644 --- a/integration-data/src/main/java/org/bahmni_avni_integration/integration_data/util/FormatAndParseUtil.java +++ b/integration-data/src/main/java/org/bahmni_avni_integration/integration_data/util/FormatAndParseUtil.java @@ -3,6 +3,7 @@ import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.Calendar; import java.util.Date; import java.util.Objects; @@ -52,6 +53,13 @@ public static Date fromIsoDateString(String date) { } } + public static Date addSeconds(Date date, int seconds) { + Calendar c = Calendar.getInstance(); + c.setTime(date); + c.add(Calendar.SECOND, seconds); + return c.getTime(); + } + public static String fromAvniToOpenMRSDate(String dateString) { return FormatAndParseUtil.toISODateString(FormatAndParseUtil.fromAvniDate(dateString)); } diff --git a/integrator/src/main/java/org/bahmni_avni_integration/contract/avni/Enrolment.java b/integrator/src/main/java/org/bahmni_avni_integration/contract/avni/Enrolment.java index 7e697dbc..0226b2c2 100644 --- a/integrator/src/main/java/org/bahmni_avni_integration/contract/avni/Enrolment.java +++ b/integrator/src/main/java/org/bahmni_avni_integration/contract/avni/Enrolment.java @@ -21,6 +21,9 @@ public void setSubjectId(String uuid) { map.put("Subject ID", uuid); } + //TODO: Use separate contracts for read and write. + // Why? When using for read, enrolmentDateTime is a string but for write we are adding a date object. + // This makes the code confusing to others who read it later @JsonIgnore public void setEnrolmentDateTime(Date enrolmentDateTime) { map.put("Enrolment datetime", enrolmentDateTime); @@ -37,14 +40,14 @@ public void setEmptyExitObservations() { @JsonIgnore public Date getEnrolmentDateTime() { - return FormatAndParseUtil.fromAvniDateTime((String) map.get("Enrolment datetime")); + var enrolmentDateTime = (String) map.get("Enrolment datetime"); + return enrolmentDateTime == null ? null : FormatAndParseUtil.fromAvniDateTime(enrolmentDateTime); } @JsonIgnore public Date getExitDateTime() { var exitDateTime = (String) map.get("Exit datetime"); - if (exitDateTime == null) return null; - return FormatAndParseUtil.fromAvniDateTime(exitDateTime); + return exitDateTime == null ? null : FormatAndParseUtil.fromAvniDateTime(exitDateTime); } @JsonIgnore diff --git a/integrator/src/main/java/org/bahmni_avni_integration/contract/avni/ProgramEncounter.java b/integrator/src/main/java/org/bahmni_avni_integration/contract/avni/ProgramEncounter.java index 0602a2ad..ba19be39 100644 --- a/integrator/src/main/java/org/bahmni_avni_integration/contract/avni/ProgramEncounter.java +++ b/integrator/src/main/java/org/bahmni_avni_integration/contract/avni/ProgramEncounter.java @@ -1,6 +1,7 @@ package org.bahmni_avni_integration.contract.avni; import com.fasterxml.jackson.annotation.JsonIgnore; +import org.bahmni_avni_integration.integration_data.util.FormatAndParseUtil; import org.bahmni_avni_integration.util.Empty; import java.util.Date; @@ -28,8 +29,9 @@ public String getEncounterType() { } @JsonIgnore - public String getEncounterDateTime() { - return (String) get("Encounter date time"); + public Date getEncounterDateTime() { + var encounterDateTime = (String) map.get("Encounter date time"); + return encounterDateTime == null ? null : FormatAndParseUtil.fromAvniDateTime(encounterDateTime); } @JsonIgnore diff --git a/integrator/src/main/java/org/bahmni_avni_integration/contract/avni/Subject.java b/integrator/src/main/java/org/bahmni_avni_integration/contract/avni/Subject.java index 6047dd50..a58c4742 100644 --- a/integrator/src/main/java/org/bahmni_avni_integration/contract/avni/Subject.java +++ b/integrator/src/main/java/org/bahmni_avni_integration/contract/avni/Subject.java @@ -1,6 +1,9 @@ package org.bahmni_avni_integration.contract.avni; import org.bahmni_avni_integration.integration_data.internal.SubjectToPatientMetaData; +import org.bahmni_avni_integration.integration_data.util.FormatAndParseUtil; + +import java.util.Date; public class Subject extends AvniBaseContract { @@ -15,4 +18,13 @@ public String getFirstName() { public String getLastName() { return (String) getObservation("Last name"); } + + public String getDateOfBirth() { + return (String) getObservation("Date of birth"); + } + + public Date getRegistrationDate() { + var registrationDate = (String) map.get("Registration date"); + return registrationDate == null ? null : FormatAndParseUtil.fromAvniDate(registrationDate); + } } \ No newline at end of file diff --git a/integrator/src/main/java/org/bahmni_avni_integration/contract/bahmni/OpenMRSSavePerson.java b/integrator/src/main/java/org/bahmni_avni_integration/contract/bahmni/OpenMRSSavePerson.java index 0896e6c3..95f48381 100644 --- a/integrator/src/main/java/org/bahmni_avni_integration/contract/bahmni/OpenMRSSavePerson.java +++ b/integrator/src/main/java/org/bahmni_avni_integration/contract/bahmni/OpenMRSSavePerson.java @@ -2,6 +2,7 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; import java.util.List; @@ -11,6 +12,9 @@ public class OpenMRSSavePerson { private List names; private String gender; + @JsonProperty("birthdate") + private String birthDate; + public List getNames() { return names; } @@ -26,4 +30,12 @@ public String getGender() { public void setGender(String gender) { this.gender = gender; } + + public String getBirthDate() { + return birthDate; + } + + public void setBirthDate(String birthDate) { + this.birthDate = birthDate; + } } \ No newline at end of file diff --git a/integrator/src/main/java/org/bahmni_avni_integration/contract/bahmni/OpenMRSVisit.java b/integrator/src/main/java/org/bahmni_avni_integration/contract/bahmni/OpenMRSVisit.java new file mode 100644 index 00000000..53cd7a84 --- /dev/null +++ b/integrator/src/main/java/org/bahmni_avni_integration/contract/bahmni/OpenMRSVisit.java @@ -0,0 +1,31 @@ +package org.bahmni_avni_integration.contract.bahmni; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import org.bahmni_avni_integration.integration_data.util.FormatAndParseUtil; + +import java.util.Date; + +@JsonIgnoreProperties(ignoreUnknown = true) +public class OpenMRSVisit { + private String uuid; + + @JsonProperty("startDatetime") + private String startDatetime; + + public String getUuid() { + return uuid; + } + + public void setUuid(String uuid) { + this.uuid = uuid; + } + + public Date getStartDatetime() { + return FormatAndParseUtil.fromIsoDateString(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/OpenMRSVisitRepository.java b/integrator/src/main/java/org/bahmni_avni_integration/integration_data/repository/openmrs/OpenMRSVisitRepository.java index 43db9442..7d139284 100644 --- a/integrator/src/main/java/org/bahmni_avni_integration/integration_data/repository/openmrs/OpenMRSVisitRepository.java +++ b/integrator/src/main/java/org/bahmni_avni_integration/integration_data/repository/openmrs/OpenMRSVisitRepository.java @@ -4,6 +4,7 @@ import org.bahmni_avni_integration.client.OpenMRSWebClient; import org.bahmni_avni_integration.contract.bahmni.OpenMRSSaveVisit; import org.bahmni_avni_integration.contract.bahmni.OpenMRSUuidHolder; +import org.bahmni_avni_integration.contract.bahmni.OpenMRSVisit; import org.bahmni_avni_integration.contract.bahmni.SearchResults; import org.bahmni_avni_integration.util.ObjectJsonMapper; import org.springframework.beans.factory.annotation.Autowired; @@ -19,17 +20,17 @@ public OpenMRSVisitRepository(OpenMRSWebClient openMRSWebClient) { super(openMRSWebClient); } - public OpenMRSUuidHolder getVisit(String patientUuid, String locationUuid) { - String json = openMRSWebClient.get("%s?patient=%s&location=%s".formatted(resourcePath(), patientUuid, locationUuid)); - SearchResults searchResults = ObjectJsonMapper.readValue(json, new TypeReference>() { + public OpenMRSVisit getVisit(String patientUuid, String locationUuid) { + String json = openMRSWebClient.get("%s?patient=%s&location=%s&v=full".formatted(resourcePath(), patientUuid, locationUuid)); + SearchResults searchResults = ObjectJsonMapper.readValue(json, new TypeReference>() { }); return pickAndExpectOne(searchResults, String.format("%s-%s", patientUuid, locationUuid)); } - public OpenMRSUuidHolder createVisit(OpenMRSSaveVisit openMRSSaveVisit) { + public OpenMRSVisit createVisit(OpenMRSSaveVisit openMRSSaveVisit) { String json = ObjectJsonMapper.writeValueAsString(openMRSSaveVisit); String outputJson = openMRSWebClient.post(resourcePath(), json); - return ObjectJsonMapper.readValue(outputJson, OpenMRSUuidHolder.class); + return ObjectJsonMapper.readValue(outputJson, OpenMRSVisit.class); } public void deleteVisit(String visitUuid) { diff --git a/integrator/src/main/java/org/bahmni_avni_integration/mapper/avni/EnrolmentMapper.java b/integrator/src/main/java/org/bahmni_avni_integration/mapper/avni/EnrolmentMapper.java index 1ab1b3bb..0a34e47f 100644 --- a/integrator/src/main/java/org/bahmni_avni_integration/mapper/avni/EnrolmentMapper.java +++ b/integrator/src/main/java/org/bahmni_avni_integration/mapper/avni/EnrolmentMapper.java @@ -7,6 +7,7 @@ import org.bahmni_avni_integration.integration_data.util.FormatAndParseUtil; import org.springframework.stereotype.Component; +import java.time.LocalDateTime; import java.util.*; @Component @@ -20,7 +21,7 @@ public EnrolmentMapper(ObservationMapper observationMapper, MappingMetaDataRepos this.mappingMetaDataRepository = mappingMetaDataRepository; } - public OpenMRSEncounter mapEnrolmentToEnrolmentEncounter(Enrolment enrolment, String patientUuid, Constants constants) { + public OpenMRSEncounter mapEnrolmentToEnrolmentEncounter(Enrolment enrolment, String patientUuid, OpenMRSVisit visit, Constants constants) { var encounterTypes = mappingMetaDataRepository.findAll(MappingGroup.ProgramEnrolment, MappingType.CommunityEnrolment_EncounterType); var encounterTypeUuid = encounterTypes.getBahmniValueForAvniValue(enrolment.getProgram()); var formGroupObservation = formGroupObservation(enrolment, MappingType.CommunityEnrolment_BahmniForm); @@ -30,10 +31,11 @@ public OpenMRSEncounter mapEnrolmentToEnrolmentEncounter(Enrolment enrolment, St patientUuid, encounterTypeUuid, constants, + visit, false); } - public OpenMRSEncounter mapEnrolmentToExitEncounter(Enrolment enrolment, String patientUuid, Constants constants) { + public OpenMRSEncounter mapEnrolmentToExitEncounter(Enrolment enrolment, String patientUuid, OpenMRSVisit visit, Constants constants) { var encounterTypes = mappingMetaDataRepository.findAll(MappingGroup.ProgramEnrolment, MappingType.CommunityEnrolmentExit_EncounterType); var encounterTypeUuid = encounterTypes.getBahmniValueForAvniValue(enrolment.getProgram()); var formGroupObservation = formGroupObservation(enrolment, MappingType.CommunityEnrolmentExit_BahmniForm); @@ -43,6 +45,7 @@ public OpenMRSEncounter mapEnrolmentToExitEncounter(Enrolment enrolment, String patientUuid, encounterTypeUuid, constants, + visit, true); } @@ -52,12 +55,12 @@ private OpenMRSEncounter mapEnrolmentToEncounter(Enrolment enrolment, String patientUuid, String encounterTypeUuid, Constants constants, + OpenMRSVisit visit, boolean isExit) { - var encounterDateTime = isExit ? enrolment.getExitDateTime() : enrolment.getEnrolmentDateTime(); OpenMRSEncounter openMRSEncounter = new OpenMRSEncounter(); openMRSEncounter.setPatient(patientUuid); openMRSEncounter.setEncounterType(encounterTypeUuid); - openMRSEncounter.setEncounterDatetime(FormatAndParseUtil.toISODateStringWithTimezone(encounterDateTime)); + openMRSEncounter.setEncounterDatetime(getEncounterDateTime(enrolment, visit, isExit)); openMRSEncounter.setLocation(constants.getValue(ConstantKey.IntegrationBahmniLocation)); openMRSEncounter.addEncounterProvider(new OpenMRSEncounterProvider(constants.getValue(ConstantKey.IntegrationBahmniProvider), constants.getValue(ConstantKey.IntegrationBahmniEncounterRole))); List observations = observationMapper.mapObservations(avniObservations); @@ -67,6 +70,15 @@ private OpenMRSEncounter mapEnrolmentToEncounter(Enrolment enrolment, return openMRSEncounter; } + private String getEncounterDateTime(Enrolment enrolment, OpenMRSVisit visit, boolean isExit) { + var encounterDateTime = isExit ? enrolment.getExitDateTime() : enrolment.getEnrolmentDateTime(); + var visitStartDateTime = visit.getStartDatetime(); + if (encounterDateTime.before(visitStartDateTime)) { + encounterDateTime = FormatAndParseUtil.addSeconds(visitStartDateTime, 1); + } + return FormatAndParseUtil.toISODateStringWithTimezone(encounterDateTime); + } + private OpenMRSSaveObservation formGroupObservation(Enrolment enrolment, MappingType mappingType) { var formConcept = mappingMetaDataRepository.getBahmniValue(MappingGroup.ProgramEnrolment, mappingType, enrolment.getProgram()); diff --git a/integrator/src/main/java/org/bahmni_avni_integration/mapper/avni/ProgramEncounterMapper.java b/integrator/src/main/java/org/bahmni_avni_integration/mapper/avni/ProgramEncounterMapper.java index 28ea6e3e..c86a7a2e 100644 --- a/integrator/src/main/java/org/bahmni_avni_integration/mapper/avni/ProgramEncounterMapper.java +++ b/integrator/src/main/java/org/bahmni_avni_integration/mapper/avni/ProgramEncounterMapper.java @@ -19,15 +19,14 @@ public ProgramEncounterMapper(MappingMetaDataRepository mappingMetaDataRepositor this.observationMapper = observationMapper; } - public OpenMRSEncounter mapEncounter(ProgramEncounter programEncounter, String patientUuid, Constants constants) { + public OpenMRSEncounter mapEncounter(ProgramEncounter programEncounter, String patientUuid, Constants constants, OpenMRSVisit visit) { var encounterTypeUuid = mappingMetaDataRepository.getBahmniValue(MappingGroup.ProgramEncounter, MappingType.CommunityProgramEncounter_EncounterType, programEncounter.getEncounterType()); var openMRSEncounter = new OpenMRSEncounter(); openMRSEncounter.setPatient(patientUuid); openMRSEncounter.setEncounterType(encounterTypeUuid); - String encounterDateTime = programEncounter.getEncounterDateTime(); - openMRSEncounter.setEncounterDatetime(FormatAndParseUtil.toISODateStringWithTimezone(FormatAndParseUtil.fromAvniDateTime(encounterDateTime))); + openMRSEncounter.setEncounterDatetime(getEncounterDateTime(programEncounter, visit)); openMRSEncounter.setLocation(constants.getValue(ConstantKey.IntegrationBahmniLocation)); openMRSEncounter.addEncounterProvider(new OpenMRSEncounterProvider(constants.getValue(ConstantKey.IntegrationBahmniProvider), constants.getValue(ConstantKey.IntegrationBahmniEncounterRole))); @@ -37,9 +36,19 @@ public OpenMRSEncounter mapEncounter(ProgramEncounter programEncounter, String p OpenMRSSaveObservation formGroupObservation = formGroupObservation(programEncounter); formGroupObservation.setGroupMembers(observations); openMRSEncounter.setObservations(List.of(formGroupObservation)); + openMRSEncounter.setVisit(visit.getUuid()); return openMRSEncounter; } + private String getEncounterDateTime(ProgramEncounter programEncounter, OpenMRSVisit visit) { + var encounterDateTime = programEncounter.getEncounterDateTime(); + var visitStartDateTime = visit.getStartDatetime(); + if (encounterDateTime.before(visitStartDateTime)) { + encounterDateTime = FormatAndParseUtil.addSeconds(visitStartDateTime, 1); + } + return FormatAndParseUtil.toISODateStringWithTimezone(encounterDateTime); + } + private OpenMRSSaveObservation formGroupObservation(ProgramEncounter programEncounter) { var groupObservation = new OpenMRSSaveObservation(); groupObservation.setConcept(formConcept(programEncounter)); diff --git a/integrator/src/main/java/org/bahmni_avni_integration/mapper/avni/SubjectMapper.java b/integrator/src/main/java/org/bahmni_avni_integration/mapper/avni/SubjectMapper.java index 7a77ac79..a58ed3e6 100644 --- a/integrator/src/main/java/org/bahmni_avni_integration/mapper/avni/SubjectMapper.java +++ b/integrator/src/main/java/org/bahmni_avni_integration/mapper/avni/SubjectMapper.java @@ -1,9 +1,11 @@ package org.bahmni_avni_integration.mapper.avni; +import org.bahmni_avni_integration.contract.avni.ProgramEncounter; import org.bahmni_avni_integration.contract.avni.Subject; import org.bahmni_avni_integration.contract.bahmni.*; import org.bahmni_avni_integration.integration_data.domain.*; import org.bahmni_avni_integration.integration_data.repository.MappingMetaDataRepository; +import org.bahmni_avni_integration.integration_data.util.FormatAndParseUtil; import org.springframework.stereotype.Component; import java.util.*; @@ -18,7 +20,7 @@ public SubjectMapper(MappingMetaDataRepository mappingMetaDataRepository, Observ this.observationMapper = observationMapper; } - public OpenMRSEncounter mapSubjectToEncounter(Subject subject, String patientUuid, String encounterTypeUuid, Constants constants) { + public OpenMRSEncounter mapSubjectToEncounter(Subject subject, String patientUuid, String encounterTypeUuid, Constants constants, OpenMRSVisit visit) { var openMRSEncounter = new OpenMRSEncounter(); openMRSEncounter.setPatient(patientUuid); openMRSEncounter.setEncounterType(encounterTypeUuid); @@ -31,11 +33,22 @@ public OpenMRSEncounter mapSubjectToEncounter(Subject subject, String patientUui var observations = observationMapper.mapObservations((LinkedHashMap) subject.get("observations")); observations.add(avniUuidObs(subject.getUuid())); openMRSEncounter.setObservations(groupObs(observations)); + openMRSEncounter.setEncounterDatetime(getRegistrationDate(subject, visit)); + openMRSEncounter.setVisit(visit.getUuid()); // story-todo - map audit observations var avniAuditObservations = (LinkedHashMap) subject.get("audit"); return openMRSEncounter; } + private String getRegistrationDate(Subject subject, OpenMRSVisit visit) { + var registrationDate = subject.getRegistrationDate(); + var visitStartDateTime = visit.getStartDatetime(); + if (registrationDate.before(visitStartDateTime)) { + registrationDate = FormatAndParseUtil.addSeconds(visitStartDateTime, 1); + } + return FormatAndParseUtil.toISODateStringWithTimezone(registrationDate); + } + public OpenMRSEncounter mapSubjectToExistingEncounter(OpenMRSFullEncounter existingEncounter, Subject subject, String patientUuid, String encounterTypeUuid, Constants constants) { OpenMRSEncounter openMRSEncounter = new OpenMRSEncounter(); openMRSEncounter.setUuid(existingEncounter.getUuid()); 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 94b739d5..8dc9a4e2 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 @@ -49,20 +49,19 @@ public void processPatientNotFound(Enrolment enrolment) { 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); + var visit = visitService.getOrCreateVisit(openMRSPatient); + var encounter = enrolmentMapper.mapEnrolmentToEnrolmentEncounter(enrolment, openMRSPatient.getUuid(), visit, constants); encounter.setVisit(visit.getUuid()); - OpenMRSFullEncounter savedEncounter = openMRSEncounterRepository.createEncounter(encounter); + var savedEncounter = openMRSEncounterRepository.createEncounter(encounter); return savedEncounter; } public OpenMRSFullEncounter createCommunityExitEnrolment(Enrolment enrolment, OpenMRSPatient openMRSPatient, Constants constants) { if (enrolment.getVoided()) return null; - - OpenMRSEncounter encounter = enrolmentMapper.mapEnrolmentToExitEncounter(enrolment, openMRSPatient.getUuid(), constants); - OpenMRSUuidHolder visit = visitService.getOrCreateVisit(openMRSPatient); + var visit = visitService.getOrCreateVisit(openMRSPatient); + var encounter = enrolmentMapper.mapEnrolmentToExitEncounter(enrolment, openMRSPatient.getUuid(), visit, constants); encounter.setVisit(visit.getUuid()); - OpenMRSFullEncounter savedEncounter = openMRSEncounterRepository.createEncounter(encounter); + var savedEncounter = openMRSEncounterRepository.createEncounter(encounter); return savedEncounter; } 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 187f7157..39686d05 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 @@ -52,9 +52,8 @@ public OpenMRSFullEncounter createSubject(Subject subject, OpenMRSPatient patien if (subject.getVoided()) return null; - OpenMRSUuidHolder visit = visitService.getOrCreateVisit(patient); - OpenMRSEncounter encounter = subjectMapper.mapSubjectToEncounter(subject, patient.getUuid(), subjectToPatientMetaData.encounterTypeUuid(), constants); - encounter.setVisit(visit.getUuid()); + var visit = visitService.getOrCreateVisit(patient); + OpenMRSEncounter encounter = subjectMapper.mapSubjectToEncounter(subject, patient.getUuid(), subjectToPatientMetaData.encounterTypeUuid(), constants, visit); OpenMRSFullEncounter savedEncounter = openMRSEncounterRepository.createEncounter(encounter); errorService.successfullyProcessed(subject); @@ -99,6 +98,7 @@ public OpenMRSPatient createPatient(Subject subject, SubjectToPatientMetaData me subject.getLastName(), true ))); + person.setBirthDate(subject.getDateOfBirth()); person.setGender(FormatAndParseUtil.fromAvniToOpenMRSGender((String) subject.getObservation("Gender"))); OpenMRSUuidHolder uuidHolder = openMRSPersonRepository.createPerson(person); OpenMRSSavePatient patient = new OpenMRSSavePatient(); 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 563b5b6e..638ad6a0 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 @@ -48,10 +48,9 @@ public Pair findCommunityEncounter(Program public OpenMRSFullEncounter createCommunityEncounter(ProgramEncounter programEncounter, OpenMRSPatient patient, Constants constants) { if (programEncounter.getVoided()) return null; - OpenMRSUuidHolder visit = visitService.getOrCreateVisit(patient); - OpenMRSEncounter encounter = programEncounterMapper.mapEncounter(programEncounter, patient.getUuid(), constants); - encounter.setVisit(visit.getUuid()); - OpenMRSFullEncounter savedEncounter = openMRSEncounterRepository.createEncounter(encounter); + var visit = visitService.getOrCreateVisit(patient); + var encounter = programEncounterMapper.mapEncounter(programEncounter, patient.getUuid(), constants, visit); + var savedEncounter = openMRSEncounterRepository.createEncounter(encounter); errorService.successfullyProcessed(programEncounter); return savedEncounter; 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 5a71e176..1db6617f 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,12 +18,12 @@ public VisitService(ConstantsRepository constantsRepository, OpenMRSVisitReposit this.openMRSVisitRepository = openMRSVisitRepository; } - private OpenMRSUuidHolder getVisit(String patientUuid) { + private OpenMRSVisit getVisit(String patientUuid) { String locationUuid = constantsRepository.findAllConstants().getValue(ConstantKey.IntegrationBahmniLocation); return openMRSVisitRepository.getVisit(patientUuid, locationUuid); } - private OpenMRSUuidHolder createVisit(OpenMRSPatient patient) { + private OpenMRSVisit createVisit(OpenMRSPatient patient) { String location = constantsRepository.findAllConstants().getValue(ConstantKey.IntegrationBahmniLocation); String visitType = constantsRepository.findAllConstants().getValue(ConstantKey.IntegrationBahmniVisitType); OpenMRSSaveVisit openMRSSaveVisit = new OpenMRSSaveVisit(); @@ -31,12 +31,12 @@ private OpenMRSUuidHolder createVisit(OpenMRSPatient patient) { openMRSSaveVisit.setVisitType(visitType); openMRSSaveVisit.setPatient(patient.getUuid()); openMRSSaveVisit.setStartDatetime(patient.getAuditInfo().getDateCreated()); - OpenMRSUuidHolder visit = openMRSVisitRepository.createVisit(openMRSSaveVisit); + OpenMRSVisit visit = openMRSVisitRepository.createVisit(openMRSSaveVisit); logger.debug("Created new visit with uuid %s".formatted(visit.getUuid())); return visit; } - public OpenMRSUuidHolder getOrCreateVisit(OpenMRSPatient patient) { + public OpenMRSVisit getOrCreateVisit(OpenMRSPatient patient) { var visit = getVisit(patient.getUuid()); if (visit == null) { return createVisit(patient); 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 0a23a01d..d1dc4d18 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 @@ -79,8 +79,8 @@ protected void processSubject(Subject subject) { return; } - OpenMRSPatient patient = patientEncounter.getValue0(); - OpenMRSFullEncounter encounter = patientEncounter.getValue1(); + var patient = patientEncounter.getValue0(); + var encounter = patientEncounter.getValue1(); if (encounter != null && patient != null) { logger.debug(String.format("Updating existing encounter %s for subject %s", encounter.getUuid(), subject.getUuid())); @@ -93,9 +93,10 @@ protected void processSubject(Subject subject) { patientService.createSubject(subject, patient, metaData, constants); } else if (encounter == null && patient == null) { logger.debug(String.format("Creating new patient for subject %s", subject.getUuid())); - patient = patientService.createPatient(subject, metaData, constants); + var newPatient = patientService.createPatient(subject, metaData, constants); + var fullPatientObject = patientService.getPatient(newPatient.getUuid()); logger.debug(String.format("Creating new encounter for subject %s", subject.getUuid())); - patientService.createSubject(subject, patient, metaData, constants); + patientService.createSubject(subject, fullPatientObject, metaData, constants); } entityStatusService.saveEntityStatus(subject); } diff --git a/integrator/src/test/java/org/bahmni_avni_integration/mapper/avni/EnrolmentMapperExternalTest.java b/integrator/src/test/java/org/bahmni_avni_integration/mapper/avni/EnrolmentMapperExternalTest.java index 2e28ef7a..14326e33 100644 --- a/integrator/src/test/java/org/bahmni_avni_integration/mapper/avni/EnrolmentMapperExternalTest.java +++ b/integrator/src/test/java/org/bahmni_avni_integration/mapper/avni/EnrolmentMapperExternalTest.java @@ -4,14 +4,17 @@ import org.bahmni_avni_integration.contract.bahmni.OpenMRSFullEncounter; import org.bahmni_avni_integration.contract.bahmni.OpenMRSSaveObservation; import org.bahmni_avni_integration.contract.bahmni.OpenMRSUuidHolder; +import org.bahmni_avni_integration.contract.bahmni.OpenMRSVisit; import org.bahmni_avni_integration.integration_data.domain.MappingGroup; import org.bahmni_avni_integration.integration_data.domain.MappingType; import org.bahmni_avni_integration.integration_data.repository.ConstantsRepository; import org.bahmni_avni_integration.integration_data.repository.MappingMetaDataRepository; +import org.bahmni_avni_integration.integration_data.util.FormatAndParseUtil; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import java.util.Date; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -49,8 +52,14 @@ public void mapEnrolmentToEncounter() { enrolment.set("observations", avniObservations); var formConcept = mappingMetaDataRepository.getBahmniValue(MappingGroup.ProgramEnrolment, MappingType.CommunityEnrolment_BahmniForm, program); + enrolment.set("Enrolment datetime", FormatAndParseUtil.toISODateString(new Date())); + + OpenMRSVisit openMRSVisit = new OpenMRSVisit(); + openMRSVisit.setStartDatetime(FormatAndParseUtil.toISODateString(new Date())); + var openMRSEncounter = enrolmentMapper.mapEnrolmentToEnrolmentEncounter(enrolment, patientUuid, + openMRSVisit, constantsRepository.findAllConstants()); var groupObs = openMRSEncounter.getObservations().get(0); @@ -106,6 +115,7 @@ private OpenMRSFullEncounter getExistingEncounter() { encounter.setAny("obs", List.of( Map.of("uuid", existingGroupUuid, "concept", Map.of("uuid", formConceptUuid), + "voided", false, "groupMembers", List.of(createPrimitiveObservation(avniIdObsUuid, avniIdConcept, enrolmentUuid))))); return encounter; } @@ -119,7 +129,8 @@ private Map createPrimitiveObservation(String uuid, String conce Map observation = Map.of( "uuid", uuid, "concept", Map.of("uuid", conceptUuid), - "value", value + "value", value, + "voided", false ); return observation; } diff --git a/integrator/src/test/java/org/bahmni_avni_integration/mapper/avni/ObservationMapperExternalTest.java b/integrator/src/test/java/org/bahmni_avni_integration/mapper/avni/ObservationMapperExternalTest.java index 8085d566..854484f2 100644 --- a/integrator/src/test/java/org/bahmni_avni_integration/mapper/avni/ObservationMapperExternalTest.java +++ b/integrator/src/test/java/org/bahmni_avni_integration/mapper/avni/ObservationMapperExternalTest.java @@ -254,7 +254,8 @@ private Map createCodedObservation(UUID uuid, String conceptUuid Map observation = Map.of( "uuid", uuid.toString(), "concept", Map.of("uuid", conceptUuid), - "value", Map.of("uuid", answerUuid) + "value", Map.of("uuid", answerUuid), + "voided", false ); return observation; } @@ -263,7 +264,8 @@ private Map createPrimitiveObservation(UUID uuid, String concept Map observation = Map.of( "uuid", uuid.toString(), "concept", Map.of("uuid", conceptUuid), - "value", value + "value", value, + "voided", false ); return observation; } diff --git a/integrator/src/test/java/org/bahmni_avni_integration/mapper/avni/SubjectMapperExternalTest.java b/integrator/src/test/java/org/bahmni_avni_integration/mapper/avni/SubjectMapperExternalTest.java index 2cd1749e..cd0abb63 100644 --- a/integrator/src/test/java/org/bahmni_avni_integration/mapper/avni/SubjectMapperExternalTest.java +++ b/integrator/src/test/java/org/bahmni_avni_integration/mapper/avni/SubjectMapperExternalTest.java @@ -4,15 +4,18 @@ import org.bahmni_avni_integration.contract.bahmni.OpenMRSFullEncounter; import org.bahmni_avni_integration.contract.bahmni.OpenMRSSaveObservation; import org.bahmni_avni_integration.contract.bahmni.OpenMRSUuidHolder; +import org.bahmni_avni_integration.contract.bahmni.OpenMRSVisit; import org.bahmni_avni_integration.integration_data.domain.MappingGroup; import org.bahmni_avni_integration.integration_data.domain.MappingType; import org.bahmni_avni_integration.integration_data.repository.ConstantsRepository; import org.bahmni_avni_integration.integration_data.repository.MappingMetaDataRepository; +import org.bahmni_avni_integration.integration_data.util.FormatAndParseUtil; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import javax.sql.DataSource; +import java.util.Date; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -45,12 +48,16 @@ public void mapSubjectToEncounter() { int numberOfBabies = 4; avniObservations.put("Number of babies", numberOfBabies); subject.set("observations", avniObservations); + subject.set("Registration date", FormatAndParseUtil.toISODateString(new Date())); var formConcept = mappingMetaDataRepository.getBahmniValue(MappingGroup.PatientSubject, MappingType.CommunityRegistration_BahmniForm); var entityUuidConcept = mappingMetaDataRepository.getBahmniValueForAvniIdConcept(); + OpenMRSVisit openMRSVisit = new OpenMRSVisit(); + openMRSVisit.setStartDatetime(FormatAndParseUtil.toISODateString(new Date())); var openMRSEncounter = subjectMapper.mapSubjectToEncounter(subject, "cc0369c8-748c-42cc-a534-5ab40855c3f8", "f39ce690-d1c4-4bb3-aa4b-893bdd73e5a1", - constantsRepository.findAllConstants()); + constantsRepository.findAllConstants(), + openMRSVisit); var groupObs = openMRSEncounter.getObservations().get(0); assertEquals(formConcept, groupObs.getConcept()); var entityUuidObs = getGroupMember(entityUuidConcept, groupObs); @@ -93,7 +100,8 @@ private OpenMRSFullEncounter getExistingEncounter() { encounter.setAny("obs", List.of( Map.of("uuid", existingGroupUuid, "concept", Map.of("uuid", formConceptUuid), - "groupMembers", List.of()))); + "groupMembers", List.of(), + "voided", false))); return encounter; } diff --git a/integrator/src/test/java/org/bahmni_avni_integration/worker/avni/EnrolmentWorkerExternalTest.java b/integrator/src/test/java/org/bahmni_avni_integration/worker/avni/EnrolmentWorkerExternalTest.java index cbe718fd..89ae25f2 100644 --- a/integrator/src/test/java/org/bahmni_avni_integration/worker/avni/EnrolmentWorkerExternalTest.java +++ b/integrator/src/test/java/org/bahmni_avni_integration/worker/avni/EnrolmentWorkerExternalTest.java @@ -38,7 +38,7 @@ void processAllEnrolments() { void processSpecificEnrolment() { Constants constants = constantsRepository.findAllConstants(); enrolmentWorker.cacheRunImmutables(constants); - Enrolment enrolment = avniEnrolmentRepository.getEnrolment("cec8b907-31d2-4eba-b725-b1b607c10c9e"); + Enrolment enrolment = avniEnrolmentRepository.getEnrolment("83bede87-68db-2edc-771b-d214f64d08f0"); enrolmentWorker.processEnrolment(enrolment); } } \ No newline at end of file diff --git a/integrator/src/test/java/org/bahmni_avni_integration/worker/avni/ProgramEncounterWorkerExternalTest.java b/integrator/src/test/java/org/bahmni_avni_integration/worker/avni/ProgramEncounterWorkerExternalTest.java index ffd97f18..c2460470 100644 --- a/integrator/src/test/java/org/bahmni_avni_integration/worker/avni/ProgramEncounterWorkerExternalTest.java +++ b/integrator/src/test/java/org/bahmni_avni_integration/worker/avni/ProgramEncounterWorkerExternalTest.java @@ -30,7 +30,7 @@ class ProgramEncounterWorkerExternalTest { public void processProgramEncountersOfAnEnrolment() { Constants constants = constantsRepository.findAllConstants(); programEncounterWorker.cacheRunImmutables(constants); - var enrolment = avniEnrolmentRepository.getEnrolment("cec8b907-31d2-4eba-b725-b1b607c10c9e"); + var enrolment = avniEnrolmentRepository.getEnrolment("83bede87-68db-2edc-771b-d214f64d08f0"); var encounters = (List) enrolment.get("encounters"); for (String encounterUuid : encounters) { var programEncounter = programEncounterRepository.getProgramEncounter(encounterUuid); diff --git a/integrator/src/test/java/org/bahmni_avni_integration/worker/avni/SubjectWorkerExternalTest.java b/integrator/src/test/java/org/bahmni_avni_integration/worker/avni/SubjectWorkerExternalTest.java index 9d09c6b5..26d43ab0 100644 --- a/integrator/src/test/java/org/bahmni_avni_integration/worker/avni/SubjectWorkerExternalTest.java +++ b/integrator/src/test/java/org/bahmni_avni_integration/worker/avni/SubjectWorkerExternalTest.java @@ -43,7 +43,7 @@ public void processSubjects() { public void processSpecificSubject() { Constants constants = constantsRepository.findAllConstants(); subjectWorker.cacheRunImmutables(constants); - var subject = avniSubjectRepository.getSubject("fb73250c-6669-a6bb-326a-a49a806b1d9a"); + var subject = avniSubjectRepository.getSubject("9d6ddacc-a2ea-2e2c-1112-c90c1ca55cdb"); subjectWorker.processSubject(subject); } } \ No newline at end of file