@Test public void evaluate_shouldCalculateVisitsWithinAPeriod() throws Exception { VisitType visitType = Context.getVisitService().getVisitType(1); { Patient patient = TestUtils.getPatient(7); TestUtils.saveVisit(patient, visitType, computeWithinRangeEncounterDate(6), computeWithinRangeEncounterDate(6) ); TestUtils.saveVisit(patient, visitType, computeWithinRangeEncounterDate(5), computeWithinRangeEncounterDate(5)); TestUtils.saveVisit(patient, visitType, computeWithinRangeEncounterDate(3), computeWithinRangeEncounterDate(3)); } { Patient patient = TestUtils.getPatient(8); TestUtils.saveVisit(patient, visitType, computeWithinRangeEncounterDate(12), computeWithinRangeEncounterDate(12)); TestUtils.saveVisit(patient, visitType, computeWithinRangeEncounterDate(8), computeWithinRangeEncounterDate(8)); TestUtils.saveVisit(patient, visitType, computeWithinRangeEncounterDate(6), computeWithinRangeEncounterDate(6)); } List<Integer> ptIds = Arrays.asList(7, 8, 999); Map<String, Object> params = new HashMap<String, Object>(); params.put("reviewPeriod", 4); CalculationResultMap resultMap = new VisitsWithinAPeriodCalculation().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(0)); Assert.assertThat(((ListResult) resultMap.get(999)).getValues().size(), is(0)); }
@Override public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context) { Integer reviewPeriod = (Integer) parameterValues.get("reviewPeriod"); Date endDate = context.getNow(); Calendar startDate = Calendar.getInstance(); startDate.clear(Calendar.HOUR); startDate.clear(Calendar.MINUTE); startDate.clear(Calendar.MILLISECOND); startDate.setTime(endDate); startDate.add(Calendar.MONTH, -(reviewPeriod)); Date beginningOfReviewPeriod = startDate.getTime(); Set<Patient> patients = getPatientSetFromIds(cohort); List<Visit> visits = Context.getVisitService().getVisits(null, patients, null, null, beginningOfReviewPeriod, null, null, endDate, null, true, false); CalculationResultMap ret = new CalculationResultMap(); for (Visit v : visits) { Integer ptId = v.getPatient().getId(); ListResult holder = (ListResult) ret.get(ptId); if (holder == null) { holder = new ListResult(); ret.put(ptId, holder); } holder.add(new SimpleResult(v, this)); } CalculationUtils.ensureEmptyListResults(ret, cohort); return ret; }
VisitsWithinAPeriodCalculation extends AbstractPatientCalculation { @Override public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context) { Integer reviewPeriod = (Integer) parameterValues.get("reviewPeriod"); Date endDate = context.getNow(); Calendar startDate = Calendar.getInstance(); startDate.clear(Calendar.HOUR); startDate.clear(Calendar.MINUTE); startDate.clear(Calendar.MILLISECOND); startDate.setTime(endDate); startDate.add(Calendar.MONTH, -(reviewPeriod)); Date beginningOfReviewPeriod = startDate.getTime(); Set<Patient> patients = getPatientSetFromIds(cohort); List<Visit> visits = Context.getVisitService().getVisits(null, patients, null, null, beginningOfReviewPeriod, null, null, endDate, null, true, false); CalculationResultMap ret = new CalculationResultMap(); for (Visit v : visits) { Integer ptId = v.getPatient().getId(); ListResult holder = (ListResult) ret.get(ptId); if (holder == null) { holder = new ListResult(); ret.put(ptId, holder); } holder.add(new SimpleResult(v, this)); } CalculationUtils.ensureEmptyListResults(ret, cohort); return ret; } }
VisitsWithinAPeriodCalculation extends AbstractPatientCalculation { @Override public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context) { Integer reviewPeriod = (Integer) parameterValues.get("reviewPeriod"); Date endDate = context.getNow(); Calendar startDate = Calendar.getInstance(); startDate.clear(Calendar.HOUR); startDate.clear(Calendar.MINUTE); startDate.clear(Calendar.MILLISECOND); startDate.setTime(endDate); startDate.add(Calendar.MONTH, -(reviewPeriod)); Date beginningOfReviewPeriod = startDate.getTime(); Set<Patient> patients = getPatientSetFromIds(cohort); List<Visit> visits = Context.getVisitService().getVisits(null, patients, null, null, beginningOfReviewPeriod, null, null, endDate, null, true, false); CalculationResultMap ret = new CalculationResultMap(); for (Visit v : visits) { Integer ptId = v.getPatient().getId(); ListResult holder = (ListResult) ret.get(ptId); if (holder == null) { holder = new ListResult(); ret.put(ptId, holder); } holder.add(new SimpleResult(v, this)); } CalculationUtils.ensureEmptyListResults(ret, cohort); return ret; }  }
VisitsWithinAPeriodCalculation extends AbstractPatientCalculation { @Override public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context) { Integer reviewPeriod = (Integer) parameterValues.get("reviewPeriod"); Date endDate = context.getNow(); Calendar startDate = Calendar.getInstance(); startDate.clear(Calendar.HOUR); startDate.clear(Calendar.MINUTE); startDate.clear(Calendar.MILLISECOND); startDate.setTime(endDate); startDate.add(Calendar.MONTH, -(reviewPeriod)); Date beginningOfReviewPeriod = startDate.getTime(); Set<Patient> patients = getPatientSetFromIds(cohort); List<Visit> visits = Context.getVisitService().getVisits(null, patients, null, null, beginningOfReviewPeriod, null, null, endDate, null, true, false); CalculationResultMap ret = new CalculationResultMap(); for (Visit v : visits) { Integer ptId = v.getPatient().getId(); ListResult holder = (ListResult) ret.get(ptId); if (holder == null) { holder = new ListResult(); ret.put(ptId, holder); } holder.add(new SimpleResult(v, this)); } CalculationUtils.ensureEmptyListResults(ret, cohort); return ret; }  @Override CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context); }
VisitsWithinAPeriodCalculation extends AbstractPatientCalculation { @Override public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context) { Integer reviewPeriod = (Integer) parameterValues.get("reviewPeriod"); Date endDate = context.getNow(); Calendar startDate = Calendar.getInstance(); startDate.clear(Calendar.HOUR); startDate.clear(Calendar.MINUTE); startDate.clear(Calendar.MILLISECOND); startDate.setTime(endDate); startDate.add(Calendar.MONTH, -(reviewPeriod)); Date beginningOfReviewPeriod = startDate.getTime(); Set<Patient> patients = getPatientSetFromIds(cohort); List<Visit> visits = Context.getVisitService().getVisits(null, patients, null, null, beginningOfReviewPeriod, null, null, endDate, null, true, false); CalculationResultMap ret = new CalculationResultMap(); for (Visit v : visits) { Integer ptId = v.getPatient().getId(); ListResult holder = (ListResult) ret.get(ptId); if (holder == null) { holder = new ListResult(); ret.put(ptId, holder); } holder.add(new SimpleResult(v, this)); } CalculationUtils.ensureEmptyListResults(ret, cohort); return ret; }  @Override CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context);  }