Skip to content

Commit

Permalink
HTML-840 Add velocity function to get the latest encounter prior to t…
Browse files Browse the repository at this point in the history
…he current encounter, optionally limited to the current visit
  • Loading branch information
mseaton committed Mar 27, 2024
1 parent b0e42a7 commit 6c6b902
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
import org.openmrs.PatientState;
import org.openmrs.Visit;
import org.openmrs.VisitType;
import org.openmrs.api.ConceptService;
import org.openmrs.api.context.Context;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
Expand All @@ -32,6 +34,9 @@

public class VelocityFunctionsTest extends BaseHtmlFormEntryTest {

@Autowired
ConceptService conceptService;

private Integer ageInMonths;

private Integer ageInDays;
Expand Down Expand Up @@ -419,7 +424,7 @@ public void latestObsBeforeCurrentEncounter_shouldReturnTheMostRecentObsGivenThe
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
VisitType visitType = Context.getVisitService().getVisitType(1);
EncounterType encounterType = Context.getEncounterService().getEncounterType(1);
Concept weightConcept = Context.getConceptService().getConceptByUuid("c607c80f-1ea9-4da3-bb88-6276ce8868dd");
Concept weightConcept = conceptService.getConceptByUuid("c607c80f-1ea9-4da3-bb88-6276ce8868dd");

HtmlForm htmlform = new HtmlForm();
Form form = new Form();
Expand Down Expand Up @@ -506,4 +511,107 @@ public void latestObsBeforeCurrentEncounter_shouldReturnTheMostRecentObsGivenThe
o = functions.latestObsBeforeCurrentEncounter(weightConcept.getUuid(), true);
Assert.assertEquals(Double.valueOf(30.1), o.getValueNumeric());
}

@Test
public void latestObsInVisitPriorToEncounter_shouldReturnTheMostRecentObsGivenThePassedConceptUuid() throws Exception {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
VisitType visitType = Context.getVisitService().getVisitType(1);
EncounterType encounterType = Context.getEncounterService().getEncounterType(1);
Concept civilStatus = conceptService.getConcept(4);
Concept married = conceptService.getConcept(5);
Concept single = conceptService.getConcept(6);

HtmlForm htmlform = new HtmlForm();
Form form = new Form();
form.setEncounterType(encounterType);
htmlform.setForm(form);
htmlform.setDateChanged(new Date());
htmlform.setXmlData("<htmlform></htmlform>");

Date visitDate = df.parse("2023-03-04");
Date encounter1Date = df.parse("2023-03-04");
Date encounter2Date = df.parse("2023-03-05");
Date encounter3Date = df.parse("2023-03-06");

Patient patient = Context.getPatientService().getPatient(7);
Visit v1 = new Visit();
v1.setPatient(patient);
v1.setVisitType(visitType);
v1.setStartDatetime(visitDate);
Context.getVisitService().saveVisit(v1);

Encounter e1;
Encounter e2;
Encounter e3;

{
Encounter e = new Encounter();
e.setPatient(patient);
e.setEncounterType(encounterType);
e.setEncounterDatetime(encounter1Date);
Obs o = new Obs();
o.setConcept(civilStatus);
o.setValueCoded(single);
e.addObs(o);
e.setVisit(v1);
e1 = Context.getEncounterService().saveEncounter(e);
}
{
Encounter e = new Encounter();
e.setPatient(patient);
e.setEncounterType(encounterType);
e.setEncounterDatetime(encounter2Date);
Obs o = new Obs();
o.setConcept(civilStatus);
o.setValueCoded(married);
e.addObs(o);
e2 = Context.getEncounterService().saveEncounter(e);
}
{
Encounter e = new Encounter();
e.setPatient(patient);
e.setEncounterType(encounterType);
e.setEncounterDatetime(encounter3Date);
Obs o = new Obs();
o.setConcept(civilStatus);
o.setValueCoded(married);
e.addObs(o);
e.setVisit(v1);
e3 = Context.getEncounterService().saveEncounter(e);
}

Patient p = new Patient(7);
FormEntrySession session = new FormEntrySession(p, htmlform, null);
VelocityFunctions functions = new VelocityFunctions(session);
session.getContext().setVisit(v1);

// Encounter 1: should return null for all answers, no prior data in this visit
session.getContext().setupExistingData(e1);
Obs o = functions.latestObsInVisitPriorToEncounter(civilStatus.getUuid(), null);
Assert.assertNull(o);
o = functions.latestObsInVisitPriorToEncounter(civilStatus.getUuid(), married.getUuid());
Assert.assertNull(o);
o = functions.latestObsInVisitPriorToEncounter(civilStatus.getUuid(), single.getUuid());
Assert.assertNull(o);

// Encounter 2: should return null for all answers, no visit
session.getContext().setupExistingData(e2);
o = functions.latestObsInVisitPriorToEncounter(civilStatus.getUuid(), null);
Assert.assertNull(o);
o = functions.latestObsInVisitPriorToEncounter(civilStatus.getUuid(), married.getUuid());
Assert.assertNull(o);
o = functions.latestObsInVisitPriorToEncounter(civilStatus.getUuid(), single.getUuid());
Assert.assertNull(o);

// Encounter 3: should return data from encounter 1
session.getContext().setupExistingData(e3);
o = functions.latestObsInVisitPriorToEncounter(civilStatus.getUuid(), null);
Assert.assertEquals(single, o.getValueCoded());
Assert.assertEquals(encounter1Date, o.getObsDatetime());
o = functions.latestObsInVisitPriorToEncounter(civilStatus.getUuid(), married.getUuid());
Assert.assertNull(o);
o = functions.latestObsInVisitPriorToEncounter(civilStatus.getUuid(), single.getUuid());
Assert.assertEquals(single, o.getValueCoded());
Assert.assertEquals(encounter1Date, o.getObsDatetime());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public Obs latestObs(String conceptId, Date latestDate) {
}
}

public Obs latestObsBeforeCurrentEncounter(String conceptId, boolean onlyInCurrentVisit) throws ParseException {
public Obs latestObsBeforeCurrentEncounter(String conceptId, boolean onlyInCurrentVisit) {
Encounter currentEncounter = session.getContext().getExistingEncounter();
Visit currentVisit = (Visit) session.getContext().getVisit();
Date maxDate = null;
Expand All @@ -183,6 +183,33 @@ public Obs latestObsBeforeCurrentEncounter(String conceptId, boolean onlyInCurre
return null;
}

public Obs latestObsInVisitPriorToEncounter(String conceptId, String valueCodedConceptId) {
Encounter currentEncounter = session.getContext().getExistingEncounter();
Visit currentVisit = (Visit) session.getContext().getVisit();
Date maxDate = null;
if (currentEncounter != null) {
currentVisit = currentEncounter.getVisit();
maxDate = currentEncounter.getEncounterDatetime();
}
if (currentVisit != null) {
Concept valCoded = (valueCodedConceptId == null ? null : HtmlFormEntryUtil.getConcept(valueCodedConceptId));
List<Obs> obs = allObs(conceptId, maxDate);
if (obs != null) {
for (Obs o : obs) {
if (!OpenmrsUtil.nullSafeEquals(currentEncounter, o.getEncounter())) {
Visit obsVisit = (o.getEncounter() != null ? o.getEncounter().getVisit() : null);
if (currentVisit.equals(obsVisit)) {
if (valCoded == null || valCoded.equals(o.getValueCoded())) {
return o;
}
}
}
}
}
}
return null;
}

public Obs latestObs(Integer conceptId) {
return latestObs(conceptId.toString(), null);
}
Expand Down

0 comments on commit 6c6b902

Please sign in to comment.