Skip to content

Commit

Permalink
#59 | Fix date related issues
Browse files Browse the repository at this point in the history
  • Loading branch information
hithacker committed May 18, 2021
1 parent ee86f8a commit 1614257
Show file tree
Hide file tree
Showing 21 changed files with 171 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -11,6 +12,9 @@ public class OpenMRSSavePerson {
private List<OpenMRSSaveName> names;
private String gender;

@JsonProperty("birthdate")
private String birthDate;

public List<OpenMRSSaveName> getNames() {
return names;
}
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<OpenMRSUuidHolder> searchResults = ObjectJsonMapper.readValue(json, new TypeReference<SearchResults<OpenMRSUuidHolder>>() {
public OpenMRSVisit getVisit(String patientUuid, String locationUuid) {
String json = openMRSWebClient.get("%s?patient=%s&location=%s&v=full".formatted(resourcePath(), patientUuid, locationUuid));
SearchResults<OpenMRSVisit> searchResults = ObjectJsonMapper.readValue(json, new TypeReference<SearchResults<OpenMRSVisit>>() {
});
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -43,6 +45,7 @@ public OpenMRSEncounter mapEnrolmentToExitEncounter(Enrolment enrolment, String
patientUuid,
encounterTypeUuid,
constants,
visit,
true);
}

Expand All @@ -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<OpenMRSSaveObservation> observations = observationMapper.mapObservations(avniObservations);
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand All @@ -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));
Expand Down
Original file line number Diff line number Diff line change
@@ -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.*;
Expand All @@ -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);
Expand All @@ -31,11 +33,22 @@ public OpenMRSEncounter mapSubjectToEncounter(Subject subject, String patientUui
var observations = observationMapper.mapObservations((LinkedHashMap<String, Object>) 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<String, Object>) 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
Loading

0 comments on commit 1614257

Please sign in to comment.