TbPatientAtArtStartCalculation extends AbstractPatientCalculation { @Override public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context) { Program tbProgram = MetadataUtils.existing(Program.class, TbMetadata._Program.TB); CalculationResultMap artStartDates = calculate(new InitialArtStartDateCalculation(), cohort, context); CalculationResultMap tbEnrollments = Calculations.allEnrollments(tbProgram, cohort, context); CalculationResultMap ret = new CalculationResultMap(); for (Integer ptId : cohort) { boolean result = false; Date artStartDate = EmrCalculationUtils.datetimeResultForPatient(artStartDates, ptId); ListResult enrollmentsResult = (ListResult) tbEnrollments.get(ptId); if (artStartDate != null && enrollmentsResult != null && !enrollmentsResult.isEmpty()) { List<PatientProgram> enrollments = CalculationUtils.extractResultValues(enrollmentsResult); for (PatientProgram enrollment : enrollments) { if (enrollment.getActive(artStartDate)) { result = true; break; } } } ret.put(ptId, new BooleanResult(result, this)); } return ret; }  @Override CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context);  }
@Test public void evaluate_shouldCalculateTbEnrollmentStatusAtArtStart() throws Exception { Program tbProgram = MetadataUtils.existing(Program.class, TbMetadata._Program.TB); Concept stavudine = Dictionary.getConcept(Dictionary.STAVUDINE); TestUtils.enrollInProgram(TestUtils.getPatient(2), tbProgram, TestUtils.date(2007, 12, 25)); TestUtils.enrollInProgram(TestUtils.getPatient(7), tbProgram, TestUtils.date(2012, 1, 1), TestUtils.date(2012, 1, 3)); TestUtils.saveDrugOrder(TestUtils.getPatient(7), stavudine, TestUtils.date(2012, 1, 8), null); TestUtils.enrollInProgram(TestUtils.getPatient(7), tbProgram, TestUtils.date(2012, 1, 1)); TestUtils.saveDrugOrder(TestUtils.getPatient(7), stavudine, TestUtils.date(2012, 1, 8), null); List<Integer> ptIds = Arrays.asList(2, 6, 7); CalculationResultMap resultMap = new TbPatientAtArtStartCalculation().evaluate(ptIds, null, Context.getService(PatientCalculationService.class).createCalculationContext()); Assert.assertTrue((Boolean) resultMap.get(2).getValue()); Assert.assertFalse((Boolean) resultMap.get(6).getValue()); Assert.assertTrue((Boolean) resultMap.get(7).getValue()); }