HivCohortLibrary { public CohortDefinition referredFrom(Concept... entryPoints) { EncounterType hivEnrollEncType = MetadataUtils.existing(EncounterType.class, HivMetadata._EncounterType.HIV_ENROLLMENT); Concept methodOfEnrollment = Dictionary.getConcept(Dictionary.METHOD_OF_ENROLLMENT); CodedObsCohortDefinition cd = new CodedObsCohortDefinition(); cd.setName("referred from"); cd.addParameter(new Parameter("onOrAfter", "After Date", Date.class)); cd.addParameter(new Parameter("onOrBefore", "Before Date", Date.class)); cd.setTimeModifier(PatientSetService.TimeModifier.ANY); cd.setQuestion(methodOfEnrollment); cd.setValueList(Arrays.asList(entryPoints)); cd.setOperator(SetComparator.IN); cd.setEncounterTypeList(Collections.singletonList(hivEnrollEncType)); return cd; }  CohortDefinition referredFrom(Concept... entryPoints); CohortDefinition referredNotFrom(Concept... entryPoints); CohortDefinition enrolled(); CohortDefinition enrolledExcludingTransfers(); CohortDefinition enrolledExcludingTransfersAndReferredFrom(Concept... entryPoints); CohortDefinition enrolledExcludingTransfersAndNotReferredFrom(Concept... entryPoints); CohortDefinition hasCd4Result(); CohortDefinition hasHivVisit(); CohortDefinition onCtxProphylaxis(); CohortDefinition transferredOutDeadAndLtf(); CohortDefinition inHivProgramAndOnCtxProphylaxis(); CohortDefinition inHivProgramAndOnFluconazoleProphylaxis(); CohortDefinition inHivProgramAndOnAnyProphylaxis(); CohortDefinition testedForHiv(); CohortDefinition testedHivStatus(Concept status); CohortDefinition startedArtFromTransferringFacilityOnDate(); CohortDefinition firstProgramEnrollment(Integer outcomePeriod);  }
@Test public void referredFrom_shouldReturnPatientsWithSpecifiedEntryPoint() throws Exception { EncounterType hivEnrollEncType = MetadataUtils.existing(EncounterType.class, HivMetadata._EncounterType.HIV_ENROLLMENT); Concept method = Dictionary.getConcept(Dictionary.METHOD_OF_ENROLLMENT); Concept pmtct = Dictionary.getConcept(Dictionary.PMTCT_PROGRAM); Concept other = Dictionary.getConcept(Dictionary.OTHER_NON_CODED); Encounter enrollEnc = TestUtils.saveEncounter(Context.getPatientService().getPatient(6), hivEnrollEncType, TestUtils.date(2012, 6, 15)); Obs entryObs = TestUtils.saveObs(Context.getPatientService().getPatient(6), method, pmtct, TestUtils.date(2012, 6, 15)); enrollEnc.addObs(entryObs); Context.getEncounterService().saveEncounter(enrollEnc); enrollEnc = TestUtils.saveEncounter(Context.getPatientService().getPatient(7), hivEnrollEncType, TestUtils.date(2012, 6, 15)); entryObs = TestUtils.saveObs(Context.getPatientService().getPatient(6), method, other, TestUtils.date(2012, 6, 15)); enrollEnc.addObs(entryObs); Context.getEncounterService().saveEncounter(enrollEnc); enrollEnc = TestUtils.saveEncounter(Context.getPatientService().getPatient(8), hivEnrollEncType, TestUtils.date(2012, 7, 1)); entryObs = TestUtils.saveObs(Context.getPatientService().getPatient(8), method, pmtct, TestUtils.date(2012, 7, 1)); enrollEnc.addObs(entryObs); Context.getEncounterService().saveEncounter(enrollEnc); Context.flushSession(); CohortDefinition cd = hivCohortLibrary.referredFrom(pmtct); context.addParameterValue("onOrAfter", TestUtils.date(2012, 6, 1)); context.addParameterValue("onOrBefore", TestUtils.date(2012, 6, 30)); EvaluatedCohort evaluated = Context.getService(CohortDefinitionService.class).evaluate(cd, context); ReportingTestUtils.assertCohortEquals(Arrays.asList(6), evaluated); }