DecliningCd4Calculation extends AbstractPatientCalculation implements PatientFlagCalculation { @Override public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context) { Program hivProgram = MetadataUtils.existing(Program.class, HivMetadata._Program.HIV); Set<Integer> alive = Filters.alive(cohort, context); Set<Integer> inHivProgram = Filters.inProgram(hivProgram, alive, context); CalculationResultMap lastCD4Obss = Calculations.lastObs(Dictionary.getConcept(Dictionary.CD4_COUNT), inHivProgram, context); CalculationResultMap oldCD4Obss = Calculations.lastObsAtLeastDaysAgo(Dictionary.getConcept(Dictionary.CD4_COUNT), HivConstants.DECLINING_CD4_COUNT_ACROSS_DAYS, inHivProgram, context); Set<Integer> ltfu = CalculationUtils.patientsThatPass(calculate(new LostToFollowUpCalculation(), cohort, context)); CalculationResultMap ret = new CalculationResultMap(); for (Integer ptId : cohort) { boolean declining = false; if (inHivProgram.contains(ptId)) { Double lastCD4Count = EmrCalculationUtils.numericObsResultForPatient(lastCD4Obss, ptId); Double oldCD4Count = EmrCalculationUtils.numericObsResultForPatient(oldCD4Obss, ptId); if (lastCD4Count != null && oldCD4Count != null) { declining = lastCD4Count < oldCD4Count; } if(ltfu.contains(ptId)){ declining = false; } } ret.put(ptId, new BooleanResult(declining, this, context)); } return ret; }  @Override String getFlagMessage(); @Override CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context);  }
@Test public void evaluate_shouldDetermineWhetherPatientsHasDeclinedCD4() throws Exception { Program hivProgram = MetadataUtils.existing(Program.class, HivMetadata._Program.HIV); TestUtils.enrollInProgram(TestUtils.getPatient(6), hivProgram, new Date()); TestUtils.enrollInProgram(TestUtils.getPatient(7), hivProgram, new Date()); TestUtils.enrollInProgram(TestUtils.getPatient(8), hivProgram, new Date()); Concept cd4 = Dictionary.getConcept(Dictionary.CD4_COUNT); Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DATE, -180); TestUtils.saveObs(TestUtils.getPatient(7), cd4, 123d, calendar.getTime()); TestUtils.saveObs(TestUtils.getPatient(8), cd4, 123d, calendar.getTime()); TestUtils.saveObs(TestUtils.getPatient(7), cd4, 120d, new Date()); TestUtils.saveObs(TestUtils.getPatient(8), cd4, 126d, new Date()); List<Integer> ptIds = Arrays.asList(6, 7, 8, 999); CalculationResultMap resultMap = new DecliningCd4Calculation().evaluate(ptIds, null, Context.getService(PatientCalculationService.class).createCalculationContext()); Assert.assertFalse((Boolean) resultMap.get(6).getValue()); Assert.assertTrue((Boolean) resultMap.get(7).getValue()); Assert.assertFalse((Boolean) resultMap.get(8).getValue()); Assert.assertFalse((Boolean) resultMap.get(999).getValue()); }