MotherNewBornPairReviewedCalculation extends AbstractPatientCalculation { @Override public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> params, PatientCalculationContext context) { Set<Integer> female = Filters.female(cohort, context); CalculationResultMap deliveryDateMap = Calculations.lastObs(Dictionary.getConcept(Dictionary.DATE_OF_CONFINEMENT),female, context); CalculationResultMap postNatalEncounterType = Calculations.lastEncounter(MetadataUtils.existing(EncounterType.class, MchMetadata._EncounterType.MCHMS_CONSULTATION), female, context); CalculationResultMap ret = new CalculationResultMap(); for(Integer ptId : cohort){ boolean hasNewBornAndReviewed = false; Date deliveryDate = EmrCalculationUtils.datetimeObsResultForPatient(deliveryDateMap, ptId); if(deliveryDate != null) { Encounter postNatalEncounter = EmrCalculationUtils.encounterResultForPatient(postNatalEncounterType, ptId); if(postNatalEncounter != null && postNatalEncounter.getForm().equals(MetadataUtils.existing(Form.class, MchMetadata._Form.MCHMS_POSTNATAL_VISIT))) { Date encounterDate = postNatalEncounter.getEncounterDatetime(); Date dod7DaysAfter = CoreUtils.dateAddDays(deliveryDate, 7); Date dod14DaysAfter = CoreUtils.dateAddDays(deliveryDate, 14); if((encounterDate.after(dod7DaysAfter) || encounterDate.equals(dod7DaysAfter)) && (encounterDate.before(dod14DaysAfter) || encounterDate.equals(dod14DaysAfter))) { hasNewBornAndReviewed = true; } } } ret.put(ptId, new BooleanResult(hasNewBornAndReviewed, this)); } return ret; }  @Override CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> params, PatientCalculationContext context);  }
@Test public void evaluate_shouldCheckMotherNewBornPairReviewed() { EncounterType consultationEncounterType = MetadataUtils.existing(EncounterType.class, MchMetadata._EncounterType.MCHMS_CONSULTATION); Form postNatalForm = MetadataUtils.existing(Form.class, MchMetadata._Form.MCHMS_POSTNATAL_VISIT); Concept dateOfConfinement = Dictionary.getConcept(Dictionary.DATE_OF_CONFINEMENT); Patient p7 = TestUtils.getPatient(7); TestUtils.saveObs(p7, dateOfConfinement, TestUtils.date(2014, 7, 10), TestUtils.date(2014, 7, 15)); TestUtils.saveEncounter(p7, postNatalForm, TestUtils.date(2014, 7, 20)); List<Integer> cohort = Arrays.asList(2, 5, 7); CalculationResultMap resultMap = new MotherNewBornPairReviewedCalculation().evaluate(cohort, null, Context.getService(PatientCalculationService.class).createCalculationContext()); Assert.assertTrue((Boolean) resultMap.get(7).getValue()); Assert.assertFalse((Boolean) resultMap.get(5).getValue()); }