HivCareVisitsIndicatorEvaluator implements IndicatorEvaluator { @Override public SimpleIndicatorResult evaluate(Indicator indicator, EvaluationContext context) throws EvaluationException { HivCareVisitsIndicator visitIndicator = (HivCareVisitsIndicator) indicator; List<Form> hivCareForms = Arrays.asList( MetadataUtils.existing(Form.class, HivMetadata._Form.CLINICAL_ENCOUNTER_HIV_ADDENDUM), MetadataUtils.existing(Form.class, HivMetadata._Form.MOH_257_VISIT_SUMMARY) ); Date fromDate = visitIndicator.getStartDate(); Date toDate = DateUtil.getEndOfDayIfTimeExcluded(visitIndicator.getEndDate()); Location defaultLocation = kenyaEmrService.getDefaultLocation(); List<Encounter> hivCareEncounters = Context.getEncounterService().getEncounters(null, defaultLocation, fromDate, toDate, hivCareForms, null, null, null, null, false); List<Encounter> filtered = new ArrayList<Encounter>(); if (HivCareVisitsIndicator.Filter.FEMALES_18_AND_OVER.equals(visitIndicator.getFilter())) { EvaluatedCohort females18AndOver = Context.getService(CohortDefinitionService.class).evaluate( ReportUtils.map(cohortLibrary.femalesAgedAtLeast18(), "effectiveDate", "${endDate}"), context ); for (Encounter enc : hivCareEncounters) { if (females18AndOver.contains(enc.getPatient().getPatientId())) { filtered.add(enc); } } } else if (HivCareVisitsIndicator.Filter.SCHEDULED.equals(visitIndicator.getFilter())) { for (Encounter enc : hivCareEncounters) { if (wasScheduledVisit(enc)) { filtered.add(enc); } } } else if (HivCareVisitsIndicator.Filter.UNSCHEDULED.equals(visitIndicator.getFilter())) { for (Encounter enc : hivCareEncounters) { if (!wasScheduledVisit(enc)) { filtered.add(enc); } } } else { filtered = hivCareEncounters; } SimpleIndicatorResult result = new SimpleIndicatorResult(); result.setIndicator(indicator); result.setContext(context); result.setNumeratorResult(filtered.size()); return result; }  @Override SimpleIndicatorResult evaluate(Indicator indicator, EvaluationContext context);  }
@Test public void evaluate_shouldCalculateValueBasedOnDatesAndFilter() throws EvaluationException { IndicatorResult result = indicatorService.evaluate(indicator, evaluationContext); Assert.assertEquals(5, result.getValue().intValue()); indicator.setFilter(HivCareVisitsIndicator.Filter.FEMALES_18_AND_OVER); result = indicatorService.evaluate(indicator, evaluationContext); Assert.assertEquals(2, result.getValue().intValue()); indicator.setFilter(HivCareVisitsIndicator.Filter.SCHEDULED); result = indicatorService.evaluate(indicator, evaluationContext); Assert.assertEquals(1, result.getValue().intValue()); indicator.setFilter(HivCareVisitsIndicator.Filter.UNSCHEDULED); result = indicatorService.evaluate(indicator, evaluationContext); Assert.assertEquals(4, result.getValue().intValue()); }