@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, TestUtils.date(2014, 5, 10, 10, 30, 00), TestUtils.date(2014, 5, 10, 12, 30, 00) ); TestUtils.saveVisit(mother, visitType, TestUtils.date(2014, 7, 10, 10, 30, 00), TestUtils.date(2014, 7, 10, 12, 30, 00)); TestUtils.saveVisit(guardian, visitType, TestUtils.date(2014, 8, 10, 10, 30, 00), TestUtils.date(2014, 8, 10, 12, 30, 00)); TestUtils.saveVisit(infant, visitType, TestUtils.date(2014, 8, 10, 10, 30, 00), TestUtils.date(2014, 8, 10, 12, 30, 00)); } { Patient infant = TestUtils.getPatient(6); Patient mother = TestUtils.getPatient(7); TestUtils.saveRelationship(mother, mother_relation, infant); TestUtils.saveVisit(mother, visitType, TestUtils.date(2014, 9, 10, 10, 30, 00), TestUtils.date(2014, 9, 10, 12, 30, 00)); TestUtils.saveVisit(mother, visitType, TestUtils.date(2014, 1, 10, 10, 30, 00), TestUtils.date(2014, 1, 10, 12, 30, 00)); TestUtils.saveVisit(mother, visitType, TestUtils.date(2014, 2, 10, 10, 30, 00), TestUtils.date(2014, 2, 10, 12, 30, 00)); } List<Integer> ptIds = Arrays.asList(2, 6, 999); Map<String, Object> params = new HashMap<String, Object>(); params.put("reviewPeriod", 4); Date endDate = TestUtils.date(2014, 10, 13); PatientCalculationContext p = Context.getService(PatientCalculationService.class).createCalculationContext(); p.setNow(endDate); CalculationResultMap resultMap = new InfantMotherOrGuardianPairVisitsCalculation().evaluate(ptIds, params, p); Assert.assertTrue((Boolean) resultMap.get(2).getValue()); Assert.assertFalse((Boolean) resultMap.get(6).getValue()); Assert.assertFalse((Boolean) resultMap.get(999).getValue()); }
@Override public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context) { 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(cohort, new MotherGuardianVisitsCalculation(), params, context ); CalculationResultMap infantVisits = Context.getService(PatientCalculationService.class).evaluate(cohort, new VisitsWithinAPeriodCalculation(), params, context ); CalculationResultMap infantRelations = calculate(new InfantMotherGuardianRelationsCalculation(), cohort, context); Set<Integer> infantIds = infantVisits.keySet(); CalculationResultMap ret = new CalculationResultMap(); for (Integer ptid : cohort){ Set<Date> infantVisitDates = new HashSet<Date>(); Set<Date> motherVisitDates = new HashSet<Date>(); boolean result = false; if (infantIds.contains(ptid)){ ListResult infantData = (ListResult)infantVisits.get(ptid); List<Visit> ivisits = CalculationUtils.extractResultValues(infantData); for (Visit iv: ivisits){ infantVisitDates.add(extractDateFromDateTime(iv.getStartDatetime())); } Set<Integer> mother_guardian_ids = (Set<Integer>)infantRelations.get(ptid).getValue(); for(Integer id : mother_guardian_ids) { ListResult mother_visits_data = (ListResult)motherGuardianVisits.get(id); List<Visit> mother_visits = CalculationUtils.extractResultValues(mother_visits_data); for (Visit mv : mother_visits){ motherVisitDates.add(extractDateFromDateTime(mv.getStartDatetime())); } } if (!Collections.disjoint(infantVisitDates, motherVisitDates)){ result = true; } ret.put(ptid, new BooleanResult(result, this)); } } CalculationUtils.ensureEmptyListResults(ret, cohort); return ret; }
InfantMotherOrGuardianPairVisitsCalculation extends AbstractPatientCalculation { @Override public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context) { 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(cohort, new MotherGuardianVisitsCalculation(), params, context ); CalculationResultMap infantVisits = Context.getService(PatientCalculationService.class).evaluate(cohort, new VisitsWithinAPeriodCalculation(), params, context ); CalculationResultMap infantRelations = calculate(new InfantMotherGuardianRelationsCalculation(), cohort, context); Set<Integer> infantIds = infantVisits.keySet(); CalculationResultMap ret = new CalculationResultMap(); for (Integer ptid : cohort){ Set<Date> infantVisitDates = new HashSet<Date>(); Set<Date> motherVisitDates = new HashSet<Date>(); boolean result = false; if (infantIds.contains(ptid)){ ListResult infantData = (ListResult)infantVisits.get(ptid); List<Visit> ivisits = CalculationUtils.extractResultValues(infantData); for (Visit iv: ivisits){ infantVisitDates.add(extractDateFromDateTime(iv.getStartDatetime())); } Set<Integer> mother_guardian_ids = (Set<Integer>)infantRelations.get(ptid).getValue(); for(Integer id : mother_guardian_ids) { ListResult mother_visits_data = (ListResult)motherGuardianVisits.get(id); List<Visit> mother_visits = CalculationUtils.extractResultValues(mother_visits_data); for (Visit mv : mother_visits){ motherVisitDates.add(extractDateFromDateTime(mv.getStartDatetime())); } } if (!Collections.disjoint(infantVisitDates, motherVisitDates)){ result = true; } ret.put(ptid, new BooleanResult(result, this)); } } CalculationUtils.ensureEmptyListResults(ret, cohort); return ret; } }
InfantMotherOrGuardianPairVisitsCalculation extends AbstractPatientCalculation { @Override public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context) { 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(cohort, new MotherGuardianVisitsCalculation(), params, context ); CalculationResultMap infantVisits = Context.getService(PatientCalculationService.class).evaluate(cohort, new VisitsWithinAPeriodCalculation(), params, context ); CalculationResultMap infantRelations = calculate(new InfantMotherGuardianRelationsCalculation(), cohort, context); Set<Integer> infantIds = infantVisits.keySet(); CalculationResultMap ret = new CalculationResultMap(); for (Integer ptid : cohort){ Set<Date> infantVisitDates = new HashSet<Date>(); Set<Date> motherVisitDates = new HashSet<Date>(); boolean result = false; if (infantIds.contains(ptid)){ ListResult infantData = (ListResult)infantVisits.get(ptid); List<Visit> ivisits = CalculationUtils.extractResultValues(infantData); for (Visit iv: ivisits){ infantVisitDates.add(extractDateFromDateTime(iv.getStartDatetime())); } Set<Integer> mother_guardian_ids = (Set<Integer>)infantRelations.get(ptid).getValue(); for(Integer id : mother_guardian_ids) { ListResult mother_visits_data = (ListResult)motherGuardianVisits.get(id); List<Visit> mother_visits = CalculationUtils.extractResultValues(mother_visits_data); for (Visit mv : mother_visits){ motherVisitDates.add(extractDateFromDateTime(mv.getStartDatetime())); } } if (!Collections.disjoint(infantVisitDates, motherVisitDates)){ result = true; } ret.put(ptid, new BooleanResult(result, this)); } } CalculationUtils.ensureEmptyListResults(ret, cohort); return ret; }  }
InfantMotherOrGuardianPairVisitsCalculation extends AbstractPatientCalculation { @Override public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context) { 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(cohort, new MotherGuardianVisitsCalculation(), params, context ); CalculationResultMap infantVisits = Context.getService(PatientCalculationService.class).evaluate(cohort, new VisitsWithinAPeriodCalculation(), params, context ); CalculationResultMap infantRelations = calculate(new InfantMotherGuardianRelationsCalculation(), cohort, context); Set<Integer> infantIds = infantVisits.keySet(); CalculationResultMap ret = new CalculationResultMap(); for (Integer ptid : cohort){ Set<Date> infantVisitDates = new HashSet<Date>(); Set<Date> motherVisitDates = new HashSet<Date>(); boolean result = false; if (infantIds.contains(ptid)){ ListResult infantData = (ListResult)infantVisits.get(ptid); List<Visit> ivisits = CalculationUtils.extractResultValues(infantData); for (Visit iv: ivisits){ infantVisitDates.add(extractDateFromDateTime(iv.getStartDatetime())); } Set<Integer> mother_guardian_ids = (Set<Integer>)infantRelations.get(ptid).getValue(); for(Integer id : mother_guardian_ids) { ListResult mother_visits_data = (ListResult)motherGuardianVisits.get(id); List<Visit> mother_visits = CalculationUtils.extractResultValues(mother_visits_data); for (Visit mv : mother_visits){ motherVisitDates.add(extractDateFromDateTime(mv.getStartDatetime())); } } if (!Collections.disjoint(infantVisitDates, motherVisitDates)){ result = true; } ret.put(ptid, new BooleanResult(result, this)); } } CalculationUtils.ensureEmptyListResults(ret, cohort); return ret; }  @Override CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context); }
InfantMotherOrGuardianPairVisitsCalculation extends AbstractPatientCalculation { @Override public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context) { 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(cohort, new MotherGuardianVisitsCalculation(), params, context ); CalculationResultMap infantVisits = Context.getService(PatientCalculationService.class).evaluate(cohort, new VisitsWithinAPeriodCalculation(), params, context ); CalculationResultMap infantRelations = calculate(new InfantMotherGuardianRelationsCalculation(), cohort, context); Set<Integer> infantIds = infantVisits.keySet(); CalculationResultMap ret = new CalculationResultMap(); for (Integer ptid : cohort){ Set<Date> infantVisitDates = new HashSet<Date>(); Set<Date> motherVisitDates = new HashSet<Date>(); boolean result = false; if (infantIds.contains(ptid)){ ListResult infantData = (ListResult)infantVisits.get(ptid); List<Visit> ivisits = CalculationUtils.extractResultValues(infantData); for (Visit iv: ivisits){ infantVisitDates.add(extractDateFromDateTime(iv.getStartDatetime())); } Set<Integer> mother_guardian_ids = (Set<Integer>)infantRelations.get(ptid).getValue(); for(Integer id : mother_guardian_ids) { ListResult mother_visits_data = (ListResult)motherGuardianVisits.get(id); List<Visit> mother_visits = CalculationUtils.extractResultValues(mother_visits_data); for (Visit mv : mother_visits){ motherVisitDates.add(extractDateFromDateTime(mv.getStartDatetime())); } } if (!Collections.disjoint(infantVisitDates, motherVisitDates)){ result = true; } ret.put(ptid, new BooleanResult(result, this)); } } CalculationUtils.ensureEmptyListResults(ret, cohort); return ret; }  @Override CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context);  }