PregnantWithANCVisitsCalculation extends AbstractPatientCalculation { @Override public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context) { Set<Integer> female = Filters.female(cohort, context); Form ancForm = MetadataUtils.existing(Form.class,MchMetadata._Form.MCHMS_ANTENATAL_VISIT); CalculationResultMap allEncountersForMCHConsultation = Calculations.allEncounters(MetadataUtils.existing(EncounterType.class, MchMetadata._EncounterType.MCHMS_CONSULTATION), female, context); Integer minVisits = (Integer) parameterValues.get("visits"); Program mchmsProgram = MetadataUtils.existing(Program.class, MchMetadata._Program.MCHMS); Set<Integer> patientActiveOnMchms = Filters.inProgram(mchmsProgram, female, context); Set<Integer> pregnantCohort = CalculationUtils.patientsThatPass(calculate(new IsPregnantCalculation(),female,context)); CalculationResultMap ret = new CalculationResultMap(); for (Integer ptId : cohort) { boolean result = false; if (pregnantCohort.contains(ptId) || patientActiveOnMchms.contains(ptId)){ ListResult mchcsEncountersResult = (ListResult) allEncountersForMCHConsultation.get(ptId); List<Encounter> encounters = CalculationUtils.extractResultValues(mchcsEncountersResult); int counter = 0; for(Encounter e: encounters){ if(ancForm.getUuid().equals(e.getForm().getUuid())){ counter++; if(counter >= minVisits){ 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_shouldCalculateWomenWithANCVisits() throws Exception { Concept pregnancyStatus = Dictionary.getConcept(Dictionary.PREGNANCY_STATUS); Concept yes = Dictionary.getConcept(Dictionary.YES); Concept no = Dictionary.getConcept(Dictionary.NO); Form ancForm = MetadataUtils.existing(Form.class, MchMetadata._Form.MCHMS_ANTENATAL_VISIT); EncounterType ancEncounterType = MetadataUtils.existing(EncounterType.class, MchMetadata._EncounterType.MCHMS_CONSULTATION); Program program = MetadataUtils.existing(Program.class, MchMetadata._Program.MCHMS); { Patient patient = TestUtils.getPatient(7); TestUtils.enrollInProgram(patient, program, TestUtils.date(2014, 1, 1)); TestUtils.saveEncounter(patient, ancEncounterType, ancForm, TestUtils.date(2014, 1, 20)); TestUtils.saveEncounter(patient, ancEncounterType, ancForm, TestUtils.date(2014, 1, 20)); TestUtils.saveEncounter(patient, ancEncounterType, ancForm, TestUtils.date(2014, 2, 20)); TestUtils.saveEncounter(patient, ancEncounterType, ancForm, TestUtils.date(2014, 3, 20)); TestUtils.saveEncounter(patient, ancEncounterType, ancForm, TestUtils.date(2014, 4, 20)); } { Patient patient = TestUtils.getPatient(8); TestUtils.saveEncounter(patient, ancEncounterType, ancForm, TestUtils.date(2014, 1, 20)); TestUtils.saveEncounter(patient, ancEncounterType, ancForm, TestUtils.date(2014, 1, 20)); TestUtils.saveEncounter(patient, ancEncounterType, ancForm, TestUtils.date(2014, 2, 20)); TestUtils.saveObs(patient, pregnancyStatus, yes, TestUtils.date(2014, 9, 10)); TestUtils.saveObs(patient, pregnancyStatus, yes, TestUtils.date(2014, 8, 10)); TestUtils.saveObs(patient, pregnancyStatus, yes, TestUtils.date(2014, 7, 10)); } List<Integer> ptIds = Arrays.asList(7, 8, 999); Map<String, Object> params = new HashMap<String, Object>(); params.put("visits", 4); CalculationResultMap resultMap = new PregnantWithANCVisitsCalculation().evaluate(ptIds, params, Context.getService(PatientCalculationService.class).createCalculationContext()); Assert.assertTrue((Boolean) resultMap.get(7).getValue()); Assert.assertFalse((Boolean) resultMap.get(8).getValue()); Assert.assertFalse((Boolean) resultMap.get(999).getValue()); }