MotherGuardianVisitsCalculation extends AbstractPatientCalculation { @Override public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context) { CalculationResultMap infantRelationsResultMap = calculate(new InfantMotherGuardianRelationsCalculation(), cohort, context); Set<Integer> all_parent_guardian_ids = new HashSet<Integer>(); for (Integer id : cohort) { all_parent_guardian_ids.addAll((Set<Integer>)infantRelationsResultMap.get(id).getValue()); } Map<String, Object> params = new HashMap<String, Object>(); Integer reviewPeriod = (Integer)parameterValues.get("reviewPeriod"); params.put("reviewPeriod", reviewPeriod); CalculationResultMap motherGuardianVisits = Context.getService(PatientCalculationService.class).evaluate(all_parent_guardian_ids, new VisitsWithinAPeriodCalculation(), params, context ); CalculationUtils.ensureEmptyListResults(motherGuardianVisits, all_parent_guardian_ids); return motherGuardianVisits; }  @Override CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context);  }
@Test public void evaluate_shouldCalculateVisitsWithinAPeriod() throws Exception { VisitType visitType = Context.getVisitService().getVisitType(1); { Patient infant = TestUtils.getPatient(2); Patient mother = TestUtils.getPatient(7); Patient guardian = TestUtils.getPatient(8); TestUtils.saveRelationship(mother, mother_relation, infant); TestUtils.saveRelationship(guardian, guardian_relation, infant); TestUtils.saveVisit(mother, visitType, computeWithinRangeEncounterDate(6), computeWithinRangeEncounterDate(6) ); TestUtils.saveVisit(mother, visitType, computeWithinRangeEncounterDate(5), computeWithinRangeEncounterDate(5)); TestUtils.saveVisit(guardian, visitType, computeWithinRangeEncounterDate(3), computeWithinRangeEncounterDate(3)); } { Patient infant = TestUtils.getPatient(6); Patient mother = TestUtils.getPatient(7); TestUtils.saveRelationship(mother, mother_relation, infant); TestUtils.saveVisit(mother, visitType, computeWithinRangeEncounterDate(2), computeWithinRangeEncounterDate(2)); TestUtils.saveVisit(mother, visitType, computeWithinRangeEncounterDate(10), computeWithinRangeEncounterDate(10)); TestUtils.saveVisit(mother, visitType, computeWithinRangeEncounterDate(12), computeWithinRangeEncounterDate(12)); } List<Integer> ptIds = Arrays.asList(2, 6, 999); Map<String, Object> params = new HashMap<String, Object>(); params.put("reviewPeriod", 4); CalculationResultMap resultMap = new MotherGuardianVisitsCalculation().evaluate(ptIds, params, Context.getService(PatientCalculationService.class).createCalculationContext()); Assert.assertThat(((ListResult) resultMap.get(7)).getValues().size(), is(1)); Assert.assertThat(((ListResult) resultMap.get(8)).getValues().size(), is(1)); Assert.assertNull(resultMap.get(999)); }