LastWhoStageCalculation extends AbstractPatientCalculation { @Override public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context) { Concept whoQuestion = Dictionary.getConcept(Dictionary.Initial_World_Health_Organization_HIV_stage); CalculationResultMap ret = new CalculationResultMap(); CalculationResultMap map1 = Calculations.lastObs(Dictionary.getConcept(Dictionary.CURRENT_WHO_STAGE), cohort, context); CalculationResultMap map2 = Calculations.lastObs(whoQuestion, cohort, context); if(map1 != null) { ret.putAll(map1); } else if(map2 != null) { ret.putAll(map2); } return ret; }  @Override CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context);  }
@Test public void evaluate_shouldCalculateLatestWHOStage() throws Exception { Concept whoStage = Dictionary.getConcept(Dictionary.CURRENT_WHO_STAGE); Concept whoAdult1 = Dictionary.getConcept(Dictionary.WHO_STAGE_1_ADULT); TestUtils.saveObs(TestUtils.getPatient(7), whoStage, whoAdult1, TestUtils.date(2012, 12, 1)); Concept whoAdult2 = Dictionary.getConcept(Dictionary.WHO_STAGE_2_ADULT); TestUtils.saveObs(TestUtils.getPatient(7), whoStage, whoAdult2, TestUtils.date(2010, 11, 1)); List<Integer> ptIds = Arrays.asList(6, 7); CalculationResultMap resultMap = new LastWhoStageCalculation().evaluate(ptIds, null, Context.getService(PatientCalculationService.class).createCalculationContext()); Assert.assertNull(resultMap.get(6)); Assert.assertEquals(whoAdult1, ((Obs) resultMap.get(7).getValue()).getValueCoded()); }