LateEnrollmentCalculation extends AbstractPatientCalculation { @Override public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context) { Program mchmsProgram = MetadataUtils.existing(Program.class, MchMetadata._Program.MCHMS); Set<Integer> alive = Filters.alive(cohort, context); Set<Integer> inMchmsProgram = Filters.inProgram(mchmsProgram, alive, context); CalculationResultMap ret = new CalculationResultMap(); CalculationResultMap crm = Calculations.lastEncounter(MetadataUtils.existing(EncounterType.class, MchMetadata._EncounterType.MCHMS_ENROLLMENT), cohort, context); for (Integer ptId : cohort) { boolean lateEnrollment = false; if (inMchmsProgram.contains(ptId)) { lateEnrollment = gestationAtEnrollmentWasGreaterThan28Weeks(ptId, crm); } ret.put(ptId, new BooleanResult(lateEnrollment, this, context)); } return ret; }  @Override CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context);  }
@Test public void evaluate_shouldDetermineWhetherPatientsEnrolledLate() throws Exception { Program mchmsProgram = MetadataUtils.existing(Program.class, MchMetadata._Program.MCHMS); EncounterType enrollmentEncounterType = MetadataUtils.existing(EncounterType.class, MchMetadata._EncounterType.MCHMS_ENROLLMENT); Form enrollmentForm = MetadataUtils.existing(Form.class, MchMetadata._Form.MCHMS_ENROLLMENT); TestUtils.enrollInProgram(TestUtils.getPatient(6), mchmsProgram, new Date()); TestUtils.enrollInProgram(TestUtils.getPatient(7), mchmsProgram, new Date()); Concept lmp = Dictionary.getConcept(Dictionary.LAST_MONTHLY_PERIOD); Calendar tenWeeksAgo = Calendar.getInstance(); tenWeeksAgo.add(Calendar.DATE, -70); Calendar twentyWeeksAgo = Calendar.getInstance(); twentyWeeksAgo.add(Calendar.DATE, -210); Obs[] encounterObss6 = {TestUtils.saveObs(TestUtils.getPatient(6), lmp, tenWeeksAgo.getTime(), new Date())}; TestUtils.saveEncounter(TestUtils.getPatient(6), enrollmentEncounterType, enrollmentForm, new Date(), encounterObss6); Obs[] encounterObss7 = {TestUtils.saveObs(TestUtils.getPatient(7), lmp, twentyWeeksAgo.getTime(), new Date())}; TestUtils.saveEncounter(TestUtils.getPatient(7), enrollmentEncounterType, enrollmentForm, new Date(), encounterObss7); Context.flushSession(); List<Integer> ptIds = Arrays.asList(6, 7, 2, 999); CalculationResultMap resultMap = Context.getService(PatientCalculationService.class).evaluate(ptIds, new LateEnrollmentCalculation()); Assert.assertFalse((Boolean) resultMap.get(6).getValue()); Assert.assertTrue((Boolean) resultMap.get(7).getValue()); Assert.assertFalse((Boolean) resultMap.get(2).getValue()); Assert.assertFalse((Boolean) resultMap.get(999).getValue()); }