InfantMotherGuardianRelationsCalculation extends AbstractPatientCalculation { @Override public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context) { PersonService service = Context.getPersonService(); RelationshipType guardian_rel = service.getRelationshipTypeByUuid(CommonMetadata._RelationshipType.GUARDIAN_DEPENDANT); CalculationResultMap resultMap = new CalculationResultMap(); for (Integer ptId : cohort) { Person person = service.getPerson(ptId); Set<Integer> ids = new HashSet<Integer>(); List<Relationship> parentRelationship = service.getRelationships(null, person, null); if (parentRelationship != null){ for (Relationship r : parentRelationship) { if (r.getRelationshipType().getaIsToB().equals(PARENT_RELATIONSHIP)) { Integer parent_id = r.getPersonA().getId(); Person parent = service.getPerson(parent_id); if (parent.getGender().equals("F")) { ids.add(r.getPersonA().getId()); } } else if (r.getRelationshipType().equals(guardian_rel)) { ids.add(r.getPersonA().getId()); } } } resultMap.put(ptId, new SimpleResult(ids, this)); } return resultMap; }  @Override CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context); static String PARENT_RELATIONSHIP; }
@Test public void evaluate_shouldCalculateVisitsWithinAPeriod() throws Exception { { 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); } { Patient infant = TestUtils.getPatient(6); Patient mother = TestUtils.getPatient(7); TestUtils.saveRelationship(mother, mother_relation, infant); } List<Integer> ptIds = Arrays.asList(2, 6, 999); Map<String, Object> params = new HashMap<String, Object>(); CalculationResultMap resultMap = new InfantMotherGuardianRelationsCalculation().evaluate(ptIds, params, Context.getService(PatientCalculationService.class).createCalculationContext()); Set<Integer> patient2 = (Set<Integer>)resultMap.get(2).getValue(); Set<Integer> patient6 = (Set<Integer>)resultMap.get(6).getValue(); Set<Integer> patient999 = (Set<Integer>)resultMap.get(999).getValue(); Assert.assertThat(patient2.size(), is(2)); Assert.assertThat(patient6.size(), is(1)); Assert.assertThat(patient999.size(), is(0)); }