Enrollment extends AbstractObjectWrapper<PatientProgram> { public Encounter lastEncounter(EncounterType type) { List<Encounter> encounters = Context.getEncounterService().getEncounters(target.getPatient(), null, target.getDateEnrolled(), target.getDateCompleted(), null, Collections.singleton(type), null, null, null, false); return encounters.size() > 0 ? encounters.get(encounters.size() - 1) : null; } Enrollment(PatientProgram target); Obs firstObs(Concept concept); Encounter lastEncounter(EncounterType type); Encounter encounterByForm(EncounterType encounterType, Form form);  }
@Test public void lastEncounter_shouldFindLastEncounterWithType() { Patient patient = TestUtils.getPatient(6); Program tbProgram = MetadataUtils.existing(Program.class, TbMetadata._Program.TB); EncounterType tbScreenEncType = MetadataUtils.existing(EncounterType.class, TbMetadata._EncounterType.TB_SCREENING); Enrollment enrollment = new Enrollment(TestUtils.enrollInProgram(patient, tbProgram, TestUtils.date(2012, 1, 1), TestUtils.date(2012, 4, 1))); Assert.assertNull(enrollment.lastEncounter(tbScreenEncType)); Encounter enc0 = TestUtils.saveEncounter(patient, tbScreenEncType, TestUtils.date(2011, 12, 1)); Encounter enc1 = TestUtils.saveEncounter(patient, tbScreenEncType, TestUtils.date(2012, 2, 1)); Encounter enc2 = TestUtils.saveEncounter(patient, tbScreenEncType, TestUtils.date(2012, 3, 1)); Encounter enc3 = TestUtils.saveEncounter(patient, tbScreenEncType, TestUtils.date(2012, 5, 1)); Assert.assertEquals(enc2, enrollment.lastEncounter(tbScreenEncType)); enrollment = new Enrollment(TestUtils.enrollInProgram(patient, tbProgram, TestUtils.date(2012, 1, 1))); Assert.assertEquals(enc3, enrollment.lastEncounter(tbScreenEncType)); }