EddEstimateFromMchmsProgramCalculation extends AbstractPatientCalculation { @Override public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> params, PatientCalculationContext context) { Program mchmsProgram = MetadataUtils.existing(Program.class, MchMetadata._Program.MCHMS); Set<Integer> alive = Filters.alive(cohort, context); Set<Integer> female = Filters.female(alive, context); Set<Integer> activeMchmsEnrollment = Filters.inProgram(mchmsProgram, female, context); CalculationResultMap lmp = Calculations.lastObs(Dictionary.getConcept(Dictionary.LAST_MONTHLY_PERIOD), female, context); CalculationResultMap ret = new CalculationResultMap(); for(Integer ptId:cohort) { Date edd = new Date(); if(activeMchmsEnrollment.contains(ptId)) { Obs lmpObs = EmrCalculationUtils.obsResultForPatient(lmp, ptId); if(lmpObs != null) { edd = CoreUtils.dateAddDays(lmpObs.getValueDate(), 280); } ret.put(ptId, new SimpleResult(edd, this)); } } return ret; }  @Override CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> params, PatientCalculationContext context);  }
@Test public void evaluate_shouldEstimateEddFromLmp() { Program mchmsProgram = MetadataUtils.existing(Program.class, MchMetadata._Program.MCHMS); Patient p7 = TestUtils.getPatient(7); Patient p8 = TestUtils.getPatient(8); TestUtils.enrollInProgram(p7, mchmsProgram, TestUtils.date(2014, 6, 1)); TestUtils.enrollInProgram(p8, mchmsProgram, TestUtils.date(2014, 6, 1)); Date lmp = TestUtils.date(2014, 5, 1); Date edd = new Date(); TestUtils.saveObs(p7, Dictionary.getConcept(Dictionary.LAST_MONTHLY_PERIOD), lmp , TestUtils.date(2014, 6, 1)); Calendar calendar = Calendar.getInstance(); calendar.setTime(lmp); calendar.add(Calendar.DATE, 280); edd = calendar.getTime(); List<Integer> cohort = Arrays.asList(6, 7, 8, 9); CalculationResultMap resultMap = new EddEstimateFromMchmsProgramCalculation().evaluate(cohort, null, Context.getService(PatientCalculationService.class).createCalculationContext()); Assert.assertEquals(edd, resultMap.get(7).getValue()); Assert.assertThat(resultMap.get(8).isEmpty(), is(false)); }