PatientsWhoMeetCriteriaForNutritionalSupport extends AbstractPatientCalculation { @Override public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> params, PatientCalculationContext context) { Concept weight = Dictionary.getConcept(Dictionary.WEIGHT_KG); Concept height = Dictionary.getConcept(Dictionary.HEIGHT_CM); Concept muac = Dictionary.getConcept(Dictionary.MUAC); Calendar calendar = Calendar.getInstance(); calendar.setTime(context.getNow()); calendar.add(Calendar.MONTH, -6); CalculationResultMap weightMap = Calculations.lastObs(weight, cohort, context); CalculationResultMap heightMap = Calculations.lastObs(height, cohort, context); CalculationResultMap muacMap = Calculations.lastObs(muac, cohort, context); CalculationResultMap ret = new CalculationResultMap(); for(Integer ptId:cohort){ boolean meetCriteria = false; Double weightMapValue = 0.0; Double heightMapValue = 0.0; Double muacMapValue = 0.0; Date weightDate = EmrCalculationUtils.datetimeObsResultForPatient(weightMap, ptId); Date heightDate = EmrCalculationUtils.datetimeObsResultForPatient(heightMap, ptId); Date muacDate = EmrCalculationUtils.datetimeObsResultForPatient(muacMap, ptId); if(weightDate != null && weightDate.after(calendar.getTime()) && weightDate.before(context.getNow())) { weightMapValue = EmrCalculationUtils.numericObsResultForPatient(weightMap, ptId); } if(heightDate != null && heightDate.after(calendar.getTime()) && heightDate.before(context.getNow())) { heightMapValue = EmrCalculationUtils.numericObsResultForPatient(heightMap, ptId); } if(muacDate != null && muacDate.after(calendar.getTime()) && muacDate.before(context.getNow())) { muacMapValue = EmrCalculationUtils.numericObsResultForPatient(muacMap, ptId); } double bmi; if(weightMapValue != null && heightMapValue != null) { bmi = bmi(weightMapValue, heightMapValue); if(bmi < 18.5){ meetCriteria = true; } } if(muacMapValue != null && muacMapValue < 23){ meetCriteria = true; } ret.put(ptId, new BooleanResult(meetCriteria, this)); } return ret; }  @Override CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> params, PatientCalculationContext context);  }
@Test public void evaluate_shouldFindPatientsWhoMeetCriteriaForNutritionalSupport() throws Exception { Concept weight = Dictionary.getConcept(Dictionary.WEIGHT_KG); Concept height = Dictionary.getConcept(Dictionary.HEIGHT_CM); Concept muac = Dictionary.getConcept(Dictionary.MUAC); Date reportingDate = Context.getService(PatientCalculationService.class).createCalculationContext().getNow(); Calendar calendar = Calendar.getInstance(); calendar.setTime(reportingDate); calendar.add(Calendar.MONTH, -6); saveObs(getPatient(7), weight, 50d, date(2014, 7, 1)); saveObs(getPatient(7), height, 180d, date(2014, 7, 1)); saveObs(getPatient(2), weight, 60d, date(2014, 7, 1)); saveObs(getPatient(2), height, 120d, date(2014, 7, 1)); saveObs(getPatient(6), muac, 22d, date(2014, 7, 1)); saveObs(getPatient(8), muac, 30d, date(2014, 7, 1)); List<Integer> ptIds = Arrays.asList(2, 6, 7, 8, 999); CalculationResultMap resultMap = new PatientsWhoMeetCriteriaForNutritionalSupport().evaluate(ptIds, null, Context.getService(PatientCalculationService.class).createCalculationContext()); }