InfantFeedingOptionsCalculation extends AbstractPatientCalculation { @Override public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context) { Program mchcsProgram = MetadataUtils.existing(Program.class, MchMetadata._Program.MCHCS); Set<Integer> alive = Filters.alive(cohort, context); Set<Integer> inMchcsProgram = Filters.inProgram(mchcsProgram, alive, context); CalculationResultMap lastChildFeedingOption = Calculations.lastObs(Dictionary.getConcept(Dictionary.INFANT_FEEDING_METHOD), inMchcsProgram, context); CalculationResultMap ret = new CalculationResultMap(); for (Integer ptId : cohort) { boolean hasFeedingOption = false; Obs feedingOptions = EmrCalculationUtils.obsResultForPatient(lastChildFeedingOption, ptId); if (inMchcsProgram.contains(ptId) && feedingOptions != null) { hasFeedingOption = true; } ret.put(ptId, new BooleanResult(hasFeedingOption, this)); } return ret; }  @Override CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context);  }
@Test public void evaluate_shouldCalculateInfantFeedingOptions() throws Exception { Program mchcsProgram = MetadataUtils.existing(Program.class, MchMetadata._Program.MCHCS); TestUtils.enrollInProgram(TestUtils.getPatient(6), mchcsProgram, new Date()); TestUtils.enrollInProgram(TestUtils.getPatient(7), mchcsProgram, new Date()); TestUtils.enrollInProgram(TestUtils.getPatient(8), mchcsProgram, new Date()); Concept feedingOptions = Dictionary.getConcept(Dictionary.INFANT_FEEDING_METHOD); TestUtils.saveObs(TestUtils.getPatient(6), feedingOptions, Dictionary.getConcept(Dictionary.BREASTFED_EXCLUSIVELY), new Date()); TestUtils.saveObs(TestUtils.getPatient(7), feedingOptions, Dictionary.getConcept(Dictionary.REPLACEMENT_FEEDING), new Date()); Context.flushSession(); List<Integer> ptIds = Arrays.asList(6, 7, 8); CalculationResultMap resultMap = Context.getService(PatientCalculationService.class).evaluate(ptIds, new InfantFeedingOptionsCalculation()); Assert.assertTrue((Boolean) resultMap.get(6).getValue()); Assert.assertTrue((Boolean) resultMap.get(7).getValue()); Assert.assertFalse((Boolean) resultMap.get(8).getValue()); }