diff --git a/api-tests/src/test/java/org/openmrs/module/htmlformentry/VelocityFunctionsTest.java b/api-tests/src/test/java/org/openmrs/module/htmlformentry/VelocityFunctionsTest.java
index 2088b226f..38a3c2dac 100644
--- a/api-tests/src/test/java/org/openmrs/module/htmlformentry/VelocityFunctionsTest.java
+++ b/api-tests/src/test/java/org/openmrs/module/htmlformentry/VelocityFunctionsTest.java
@@ -16,6 +16,8 @@
import org.openmrs.Obs;
import org.openmrs.Patient;
import org.openmrs.PatientState;
+import org.openmrs.Visit;
+import org.openmrs.VisitType;
import org.openmrs.api.context.Context;
import java.text.DateFormat;
@@ -401,7 +403,6 @@ public void aMomentBefore_shouldReturnDateFromAMomentBefore() throws Exception {
Date date = new Date();
Date aMomentBefore = new DateTime(date).minusMillis(1).toDate();
Assertions.assertEquals(aMomentBefore, functions.aMomentBefore(date));
- ;
}
@Test
@@ -411,6 +412,98 @@ public void aMomentAfter_shouldReturnDateFromAMomentAfter() throws Exception {
Date date = new Date();
Date aMomentAfter = new DateTime(date).plusMillis(1).toDate();
Assertions.assertEquals(aMomentAfter, functions.aMomentAfter(date));
- ;
+ }
+
+ @Test
+ public void latestObsBeforeCurrentEncounter_shouldReturnTheMostRecentObsGivenThePassedConceptUuid() throws Exception {
+ 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");
+
+ HtmlForm htmlform = new HtmlForm();
+ Form form = new Form();
+ form.setEncounterType(encounterType);
+ htmlform.setForm(form);
+ htmlform.setDateChanged(new Date());
+ htmlform.setXmlData("");
+
+ 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(weightConcept);
+ o.setValueNumeric(30.1);
+ 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(weightConcept);
+ o.setValueNumeric(30.2);
+ 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(weightConcept);
+ o.setValueNumeric(30.3);
+ 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 get the most recent result not in this encounter, or null if limited to visit
+ session.getContext().setupExistingData(e1);
+ Obs o = functions.latestObsBeforeCurrentEncounter(weightConcept.getUuid(), false);
+ Assert.assertEquals(61, o.getValueNumeric().intValue());
+ o = functions.latestObsBeforeCurrentEncounter(weightConcept.getUuid(), true);
+ Assert.assertNull(o);
+
+ // Encounter 2: should get the result from encounter 1, or null if limited to visit
+ session.getContext().setupExistingData(e2);
+ o = functions.latestObsBeforeCurrentEncounter(weightConcept.getUuid(), false);
+ Assert.assertEquals(Double.valueOf(30.1), o.getValueNumeric());
+ o = functions.latestObsBeforeCurrentEncounter(weightConcept.getUuid(), true);
+ Assert.assertNull(o);
+
+ // Encounter 3: should get the result from encounter 2, or encounter 1 if limited to visit
+ session.getContext().setupExistingData(e3);
+ o = functions.latestObsBeforeCurrentEncounter(weightConcept.getUuid(), false);
+ Assert.assertEquals(Double.valueOf(30.2), o.getValueNumeric());
+ o = functions.latestObsBeforeCurrentEncounter(weightConcept.getUuid(), true);
+ Assert.assertEquals(Double.valueOf(30.1), o.getValueNumeric());
}
}
diff --git a/api/src/main/java/org/openmrs/module/htmlformentry/VelocityFunctions.java b/api/src/main/java/org/openmrs/module/htmlformentry/VelocityFunctions.java
index f2b7cdd23..253581cc0 100644
--- a/api/src/main/java/org/openmrs/module/htmlformentry/VelocityFunctions.java
+++ b/api/src/main/java/org/openmrs/module/htmlformentry/VelocityFunctions.java
@@ -17,6 +17,7 @@
import org.openmrs.Person;
import org.openmrs.Program;
import org.openmrs.ProgramWorkflow;
+import org.openmrs.Visit;
import org.openmrs.api.AdministrationService;
import org.openmrs.api.LocationService;
import org.openmrs.api.MissingRequiredPropertyException;
@@ -25,6 +26,7 @@
import org.openmrs.api.context.Context;
import org.openmrs.parameter.EncounterSearchCriteriaBuilder;
import org.openmrs.util.LocaleUtility;
+import org.openmrs.util.OpenmrsUtil;
import java.text.DateFormat;
import java.text.ParseException;
@@ -159,6 +161,28 @@ public Obs latestObs(String conceptId, Date latestDate) {
}
}
+ public Obs latestObsBeforeCurrentEncounter(String conceptId, boolean onlyInCurrentVisit) throws ParseException {
+ Encounter currentEncounter = session.getContext().getExistingEncounter();
+ Visit currentVisit = (Visit) session.getContext().getVisit();
+ Date maxDate = null;
+ if (currentEncounter != null) {
+ currentVisit = currentEncounter.getVisit();
+ maxDate = currentEncounter.getEncounterDatetime();
+ }
+ List 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 (!onlyInCurrentVisit || (currentVisit != null && currentVisit.equals(obsVisit))) {
+ return o;
+ }
+ }
+ }
+ }
+ return null;
+ }
+
public Obs latestObs(Integer conceptId) {
return latestObs(conceptId.toString(), null);
}