Skip to content

Commit

Permalink
Avni To Bahmni - Set community visit date correctly
Browse files Browse the repository at this point in the history
Setting community visit date to be OpenMRS patient created date
  • Loading branch information
hithacker committed May 15, 2021
1 parent 4ae7266 commit 742159c
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class OpenMRSPatient {
private OpenMRSPerson person;
private List<OpenMRSPatientIdentifier> identifiers;
private boolean voided;
private OpenMRSAuditInfo auditInfo;

public OpenMRSPatient(OpenMRSPerson person) {
this.person = person;
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class OpenMRSSaveVisit {
private String patient;
private String visitType;
private String location;
private String startDatetime;

public String getPatient() {
return patient;
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<OpenMRSUuidHolder> searchResults = ObjectJsonMapper.readValue(patientJSON, new TypeReference<SearchResults<OpenMRSUuidHolder>>() {
public OpenMRSPatient getPatientByIdentifier(String identifier) {
String patientJSON = openMRSWebClient.get(URI.create(String.format("%s?identifier=%s&v=full", getResourcePath("patient"), identifier)));
SearchResults<OpenMRSPatient> searchResults = ObjectJsonMapper.readValue(patientJSON, new TypeReference<SearchResults<OpenMRSPatient>>() {
});
// 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;

Expand All @@ -61,19 +61,19 @@ public OpenMRSFullEncounter createSubject(Subject subject, OpenMRSUuidHolder pat
return savedEncounter;
}

public Pair<OpenMRSUuidHolder, OpenMRSFullEncounter> findSubject(Subject subject, Constants constants, SubjectToPatientMetaData subjectToPatientMetaData) {
public Pair<OpenMRSPatient, OpenMRSFullEncounter> 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);
}
OpenMRSFullEncounter encounter = openMRSEncounterRepository.getEncounterByPatientAndObservation(patient.getUuid(), subjectToPatientMetaData.subjectUuidConceptUuid(), subjectId);
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;
}

Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -33,8 +34,8 @@ public ProgramEncounterService(PatientService patientService, MappingMetaDataRep
this.errorService = errorService;
}

public Pair<OpenMRSUuidHolder, OpenMRSFullEncounter> findCommunityEncounter(ProgramEncounter programEncounter, Subject subject, Constants constants, SubjectToPatientMetaData subjectToPatientMetaData) {
OpenMRSUuidHolder patient = patientService.findPatient(subject, constants, subjectToPatientMetaData);
public Pair<OpenMRSPatient, OpenMRSFullEncounter> findCommunityEncounter(ProgramEncounter programEncounter, Subject subject, Constants constants, SubjectToPatientMetaData subjectToPatientMetaData) {
OpenMRSPatient patient = patientService.findPatient(subject, constants, subjectToPatientMetaData);
if (patient == null) {
return new Pair<>(null, null);
}
Expand All @@ -44,7 +45,7 @@ public Pair<OpenMRSUuidHolder, OpenMRSFullEncounter> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<OpenMRSUuidHolder, OpenMRSFullEncounter> patientEncounter = programEncounterService.findCommunityEncounter(programEncounter, subject, constants, metaData);
Pair<OpenMRSPatient, OpenMRSFullEncounter> patientEncounter = programEncounterService.findCommunityEncounter(programEncounter, subject, constants, metaData);
var patient = patientEncounter.getValue0();
var encounter = patientEncounter.getValue1();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -70,15 +71,15 @@ private void removeIgnoredObservations(Subject subject) {
@Transactional(propagation = Propagation.REQUIRES_NEW)
protected void processSubject(Subject subject) {
removeIgnoredObservations(subject);
Pair<OpenMRSUuidHolder, OpenMRSFullEncounter> patientEncounter;
Pair<OpenMRSPatient, OpenMRSFullEncounter> patientEncounter;
try {
patientEncounter = patientService.findSubject(subject, constants, metaData);
} catch (MultipleResultsFoundException e) {
patientService.processMultipleSubjectsFound(subject);
return;
}

OpenMRSUuidHolder patient = patientEncounter.getValue0();
OpenMRSPatient patient = patientEncounter.getValue0();
OpenMRSFullEncounter encounter = patientEncounter.getValue1();

if (encounter != null && patient != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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());
}
Expand Down

0 comments on commit 742159c

Please sign in to comment.