@Test public void evaluate() throws Exception { Program hivProgram = MetadataUtils.existing(Program.class, HivMetadata._Program.HIV); TestUtils.enrollInProgram(TestUtils.getPatient(2), hivProgram, TestUtils.date(2013, 1, 1)); TestUtils.enrollInProgram(TestUtils.getPatient(6), hivProgram, TestUtils.date(2013, 1, 1)); TestUtils.enrollInProgram(TestUtils.getPatient(7), hivProgram, TestUtils.date(2013, 1, 1)); Concept medOrders = Dictionary.getConcept(Dictionary.MEDICATION_ORDERS); Concept ctx = Dictionary.getConcept(Dictionary.SULFAMETHOXAZOLE_TRIMETHOPRIM); TestUtils.saveObs(TestUtils.getPatient(6), medOrders, ctx, TestUtils.date(2013, 1, 1)); Concept medDispensed = Dictionary.getConcept(Dictionary.COTRIMOXAZOLE_DISPENSED); Concept yes = Dictionary.getConcept(Dictionary.YES); TestUtils.saveObs(TestUtils.getPatient(7), medDispensed, yes, TestUtils.date(2013, 1, 1)); Concept tca = Dictionary.getConcept(Dictionary.RETURN_VISIT_DATE); TestUtils.saveObs(TestUtils.getPatient(6), tca, TestUtils.date(2013, 4, 30), TestUtils.date(2013, 1, 1)); TestUtils.saveObs(TestUtils.getPatient(7), tca, TestUtils.date(2013, 4, 30), TestUtils.date(2013, 1, 1)); List<Integer> ptIds = Arrays.asList(2, 6, 7, 8); PatientCalculationContext context = Context.getService(PatientCalculationService.class).createCalculationContext(); context.setNow(TestUtils.date(2013, 1, 31)); CalculationResultMap resultMap = new OnCtxWithinDurationCalculation().evaluate(ptIds, null, context); Assert.assertFalse((Boolean) resultMap.get(2).getValue()); Assert.assertTrue((Boolean) resultMap.get(6).getValue()); Assert.assertTrue((Boolean) resultMap.get(7).getValue()); Assert.assertFalse((Boolean) resultMap.get(8).getValue()); }
@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 medOrdersObss = Calculations.lastObs(Dictionary.getConcept(Dictionary.MEDICATION_ORDERS), cohort, context); CalculationResultMap medDuration = Calculations.lastObs(Dictionary.getConcept(Dictionary.MEDICATION_DURATION), cohort, context); CalculationResultMap medDurationunits = Calculations.lastObs(Dictionary.getConcept(Dictionary.DURATION_UNITS), cohort, context); Set<Integer> ltfu = CalculationUtils.patientsThatPass(calculate(new LostToFollowUpCalculation(), cohort, context)); Set<Integer> hasTCA = CalculationUtils.patientsThatPass(calculate(new NextOfVisitHigherThanContextCalculation(), cohort, context)); CalculationResultMap medicationDispensed = Calculations.lastObs(Dictionary.getConcept(Dictionary.COTRIMOXAZOLE_DISPENSED), cohort, context); Concept ctx = Dictionary.getConcept(Dictionary.SULFAMETHOXAZOLE_TRIMETHOPRIM); Concept dapson = Dictionary.getConcept(Dictionary.DAPSONE); Concept yes = Dictionary.getConcept(Dictionary.YES); Concept days = Dictionary.getConcept(Dictionary.DAYS); Concept months = Dictionary.getConcept(Dictionary.MONTHS); Concept weeks = Dictionary.getConcept(Dictionary.WEEKS); Concept years = Dictionary.getConcept(Dictionary.YEARS); CalculationResultMap ret = new CalculationResultMap(); for (Integer ptId : cohort) { boolean onCtxOnDuration = false; if (inHivProgram.contains(ptId)) { Obs ctxMedsObs = EmrCalculationUtils.obsResultForPatient(medOrdersObss, ptId); Obs ctxDurationObs = EmrCalculationUtils.obsResultForPatient(medDuration, ptId); Obs ctxDurationUnits = EmrCalculationUtils.obsResultForPatient(medDurationunits, ptId); Obs medicationDispensedObs = EmrCalculationUtils.obsResultForPatient(medicationDispensed, ptId); if(ctxMedsObs != null) { if(ctxMedsObs.getValueCoded().equals(ctx) || ctxMedsObs.getValueCoded().equals(dapson)) { Calendar cal = Calendar.getInstance(); cal.setTime(ctxMedsObs.getObsDatetime()); if(ctxDurationObs != null && ctxDurationUnits != null && ctxMedsObs.getObsGroup().equals(ctxDurationObs.getObsGroup()) && ctxMedsObs.getObsGroup().equals(ctxDurationUnits.getObsGroup())) { Integer durationMonthsDays = Integer.parseInt(ctxDurationObs.getValueNumeric().toString().trim().split("\\.")[0]); Date obsDate = ctxMedsObs.getObsDatetime(); Concept durationUnits = ctxDurationUnits.getValueCoded(); cal.setTime(obsDate); if (durationUnits.equals(months)) { cal.add(Calendar.MONTH, durationMonthsDays); } else if (durationUnits.equals(days)) { cal.add(Calendar.DATE, durationMonthsDays); } else if (durationUnits.equals(weeks)) { cal.add(Calendar.DATE, (durationMonthsDays * 7)); } else if (durationUnits.equals(years)) { cal.add(Calendar.YEAR, durationMonthsDays); } } Date nextRefilldate = cal.getTime(); if(nextRefilldate.after(DateUtil.getStartOfMonth(context.getNow()))) { onCtxOnDuration = true; } } } if(medicationDispensedObs != null && medicationDispensedObs.getValueCoded().equals(yes) && hasTCA.contains(ptId)){ onCtxOnDuration = true; } if(ctxMedsObs != null && ctxMedsObs.getValueCoded().equals(ctx) && hasTCA.contains(ptId)){ onCtxOnDuration = true; } if(ctxMedsObs != null && ctxMedsObs.getValueCoded().equals(dapson) && hasTCA.contains(ptId)){ onCtxOnDuration = true; } if(ltfu.contains(ptId)){ onCtxOnDuration = false; } } ret.put(ptId, new BooleanResult(onCtxOnDuration, this)); } return ret; }
OnCtxWithinDurationCalculation extends BaseEmrCalculation { @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 medOrdersObss = Calculations.lastObs(Dictionary.getConcept(Dictionary.MEDICATION_ORDERS), cohort, context); CalculationResultMap medDuration = Calculations.lastObs(Dictionary.getConcept(Dictionary.MEDICATION_DURATION), cohort, context); CalculationResultMap medDurationunits = Calculations.lastObs(Dictionary.getConcept(Dictionary.DURATION_UNITS), cohort, context); Set<Integer> ltfu = CalculationUtils.patientsThatPass(calculate(new LostToFollowUpCalculation(), cohort, context)); Set<Integer> hasTCA = CalculationUtils.patientsThatPass(calculate(new NextOfVisitHigherThanContextCalculation(), cohort, context)); CalculationResultMap medicationDispensed = Calculations.lastObs(Dictionary.getConcept(Dictionary.COTRIMOXAZOLE_DISPENSED), cohort, context); Concept ctx = Dictionary.getConcept(Dictionary.SULFAMETHOXAZOLE_TRIMETHOPRIM); Concept dapson = Dictionary.getConcept(Dictionary.DAPSONE); Concept yes = Dictionary.getConcept(Dictionary.YES); Concept days = Dictionary.getConcept(Dictionary.DAYS); Concept months = Dictionary.getConcept(Dictionary.MONTHS); Concept weeks = Dictionary.getConcept(Dictionary.WEEKS); Concept years = Dictionary.getConcept(Dictionary.YEARS); CalculationResultMap ret = new CalculationResultMap(); for (Integer ptId : cohort) { boolean onCtxOnDuration = false; if (inHivProgram.contains(ptId)) { Obs ctxMedsObs = EmrCalculationUtils.obsResultForPatient(medOrdersObss, ptId); Obs ctxDurationObs = EmrCalculationUtils.obsResultForPatient(medDuration, ptId); Obs ctxDurationUnits = EmrCalculationUtils.obsResultForPatient(medDurationunits, ptId); Obs medicationDispensedObs = EmrCalculationUtils.obsResultForPatient(medicationDispensed, ptId); if(ctxMedsObs != null) { if(ctxMedsObs.getValueCoded().equals(ctx) || ctxMedsObs.getValueCoded().equals(dapson)) { Calendar cal = Calendar.getInstance(); cal.setTime(ctxMedsObs.getObsDatetime()); if(ctxDurationObs != null && ctxDurationUnits != null && ctxMedsObs.getObsGroup().equals(ctxDurationObs.getObsGroup()) && ctxMedsObs.getObsGroup().equals(ctxDurationUnits.getObsGroup())) { Integer durationMonthsDays = Integer.parseInt(ctxDurationObs.getValueNumeric().toString().trim().split("\\.")[0]); Date obsDate = ctxMedsObs.getObsDatetime(); Concept durationUnits = ctxDurationUnits.getValueCoded(); cal.setTime(obsDate); if (durationUnits.equals(months)) { cal.add(Calendar.MONTH, durationMonthsDays); } else if (durationUnits.equals(days)) { cal.add(Calendar.DATE, durationMonthsDays); } else if (durationUnits.equals(weeks)) { cal.add(Calendar.DATE, (durationMonthsDays * 7)); } else if (durationUnits.equals(years)) { cal.add(Calendar.YEAR, durationMonthsDays); } } Date nextRefilldate = cal.getTime(); if(nextRefilldate.after(DateUtil.getStartOfMonth(context.getNow()))) { onCtxOnDuration = true; } } } if(medicationDispensedObs != null && medicationDispensedObs.getValueCoded().equals(yes) && hasTCA.contains(ptId)){ onCtxOnDuration = true; } if(ctxMedsObs != null && ctxMedsObs.getValueCoded().equals(ctx) && hasTCA.contains(ptId)){ onCtxOnDuration = true; } if(ctxMedsObs != null && ctxMedsObs.getValueCoded().equals(dapson) && hasTCA.contains(ptId)){ onCtxOnDuration = true; } if(ltfu.contains(ptId)){ onCtxOnDuration = false; } } ret.put(ptId, new BooleanResult(onCtxOnDuration, this)); } return ret; } }
OnCtxWithinDurationCalculation extends BaseEmrCalculation { @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 medOrdersObss = Calculations.lastObs(Dictionary.getConcept(Dictionary.MEDICATION_ORDERS), cohort, context); CalculationResultMap medDuration = Calculations.lastObs(Dictionary.getConcept(Dictionary.MEDICATION_DURATION), cohort, context); CalculationResultMap medDurationunits = Calculations.lastObs(Dictionary.getConcept(Dictionary.DURATION_UNITS), cohort, context); Set<Integer> ltfu = CalculationUtils.patientsThatPass(calculate(new LostToFollowUpCalculation(), cohort, context)); Set<Integer> hasTCA = CalculationUtils.patientsThatPass(calculate(new NextOfVisitHigherThanContextCalculation(), cohort, context)); CalculationResultMap medicationDispensed = Calculations.lastObs(Dictionary.getConcept(Dictionary.COTRIMOXAZOLE_DISPENSED), cohort, context); Concept ctx = Dictionary.getConcept(Dictionary.SULFAMETHOXAZOLE_TRIMETHOPRIM); Concept dapson = Dictionary.getConcept(Dictionary.DAPSONE); Concept yes = Dictionary.getConcept(Dictionary.YES); Concept days = Dictionary.getConcept(Dictionary.DAYS); Concept months = Dictionary.getConcept(Dictionary.MONTHS); Concept weeks = Dictionary.getConcept(Dictionary.WEEKS); Concept years = Dictionary.getConcept(Dictionary.YEARS); CalculationResultMap ret = new CalculationResultMap(); for (Integer ptId : cohort) { boolean onCtxOnDuration = false; if (inHivProgram.contains(ptId)) { Obs ctxMedsObs = EmrCalculationUtils.obsResultForPatient(medOrdersObss, ptId); Obs ctxDurationObs = EmrCalculationUtils.obsResultForPatient(medDuration, ptId); Obs ctxDurationUnits = EmrCalculationUtils.obsResultForPatient(medDurationunits, ptId); Obs medicationDispensedObs = EmrCalculationUtils.obsResultForPatient(medicationDispensed, ptId); if(ctxMedsObs != null) { if(ctxMedsObs.getValueCoded().equals(ctx) || ctxMedsObs.getValueCoded().equals(dapson)) { Calendar cal = Calendar.getInstance(); cal.setTime(ctxMedsObs.getObsDatetime()); if(ctxDurationObs != null && ctxDurationUnits != null && ctxMedsObs.getObsGroup().equals(ctxDurationObs.getObsGroup()) && ctxMedsObs.getObsGroup().equals(ctxDurationUnits.getObsGroup())) { Integer durationMonthsDays = Integer.parseInt(ctxDurationObs.getValueNumeric().toString().trim().split("\\.")[0]); Date obsDate = ctxMedsObs.getObsDatetime(); Concept durationUnits = ctxDurationUnits.getValueCoded(); cal.setTime(obsDate); if (durationUnits.equals(months)) { cal.add(Calendar.MONTH, durationMonthsDays); } else if (durationUnits.equals(days)) { cal.add(Calendar.DATE, durationMonthsDays); } else if (durationUnits.equals(weeks)) { cal.add(Calendar.DATE, (durationMonthsDays * 7)); } else if (durationUnits.equals(years)) { cal.add(Calendar.YEAR, durationMonthsDays); } } Date nextRefilldate = cal.getTime(); if(nextRefilldate.after(DateUtil.getStartOfMonth(context.getNow()))) { onCtxOnDuration = true; } } } if(medicationDispensedObs != null && medicationDispensedObs.getValueCoded().equals(yes) && hasTCA.contains(ptId)){ onCtxOnDuration = true; } if(ctxMedsObs != null && ctxMedsObs.getValueCoded().equals(ctx) && hasTCA.contains(ptId)){ onCtxOnDuration = true; } if(ctxMedsObs != null && ctxMedsObs.getValueCoded().equals(dapson) && hasTCA.contains(ptId)){ onCtxOnDuration = true; } if(ltfu.contains(ptId)){ onCtxOnDuration = false; } } ret.put(ptId, new BooleanResult(onCtxOnDuration, this)); } return ret; }  }
OnCtxWithinDurationCalculation extends BaseEmrCalculation { @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 medOrdersObss = Calculations.lastObs(Dictionary.getConcept(Dictionary.MEDICATION_ORDERS), cohort, context); CalculationResultMap medDuration = Calculations.lastObs(Dictionary.getConcept(Dictionary.MEDICATION_DURATION), cohort, context); CalculationResultMap medDurationunits = Calculations.lastObs(Dictionary.getConcept(Dictionary.DURATION_UNITS), cohort, context); Set<Integer> ltfu = CalculationUtils.patientsThatPass(calculate(new LostToFollowUpCalculation(), cohort, context)); Set<Integer> hasTCA = CalculationUtils.patientsThatPass(calculate(new NextOfVisitHigherThanContextCalculation(), cohort, context)); CalculationResultMap medicationDispensed = Calculations.lastObs(Dictionary.getConcept(Dictionary.COTRIMOXAZOLE_DISPENSED), cohort, context); Concept ctx = Dictionary.getConcept(Dictionary.SULFAMETHOXAZOLE_TRIMETHOPRIM); Concept dapson = Dictionary.getConcept(Dictionary.DAPSONE); Concept yes = Dictionary.getConcept(Dictionary.YES); Concept days = Dictionary.getConcept(Dictionary.DAYS); Concept months = Dictionary.getConcept(Dictionary.MONTHS); Concept weeks = Dictionary.getConcept(Dictionary.WEEKS); Concept years = Dictionary.getConcept(Dictionary.YEARS); CalculationResultMap ret = new CalculationResultMap(); for (Integer ptId : cohort) { boolean onCtxOnDuration = false; if (inHivProgram.contains(ptId)) { Obs ctxMedsObs = EmrCalculationUtils.obsResultForPatient(medOrdersObss, ptId); Obs ctxDurationObs = EmrCalculationUtils.obsResultForPatient(medDuration, ptId); Obs ctxDurationUnits = EmrCalculationUtils.obsResultForPatient(medDurationunits, ptId); Obs medicationDispensedObs = EmrCalculationUtils.obsResultForPatient(medicationDispensed, ptId); if(ctxMedsObs != null) { if(ctxMedsObs.getValueCoded().equals(ctx) || ctxMedsObs.getValueCoded().equals(dapson)) { Calendar cal = Calendar.getInstance(); cal.setTime(ctxMedsObs.getObsDatetime()); if(ctxDurationObs != null && ctxDurationUnits != null && ctxMedsObs.getObsGroup().equals(ctxDurationObs.getObsGroup()) && ctxMedsObs.getObsGroup().equals(ctxDurationUnits.getObsGroup())) { Integer durationMonthsDays = Integer.parseInt(ctxDurationObs.getValueNumeric().toString().trim().split("\\.")[0]); Date obsDate = ctxMedsObs.getObsDatetime(); Concept durationUnits = ctxDurationUnits.getValueCoded(); cal.setTime(obsDate); if (durationUnits.equals(months)) { cal.add(Calendar.MONTH, durationMonthsDays); } else if (durationUnits.equals(days)) { cal.add(Calendar.DATE, durationMonthsDays); } else if (durationUnits.equals(weeks)) { cal.add(Calendar.DATE, (durationMonthsDays * 7)); } else if (durationUnits.equals(years)) { cal.add(Calendar.YEAR, durationMonthsDays); } } Date nextRefilldate = cal.getTime(); if(nextRefilldate.after(DateUtil.getStartOfMonth(context.getNow()))) { onCtxOnDuration = true; } } } if(medicationDispensedObs != null && medicationDispensedObs.getValueCoded().equals(yes) && hasTCA.contains(ptId)){ onCtxOnDuration = true; } if(ctxMedsObs != null && ctxMedsObs.getValueCoded().equals(ctx) && hasTCA.contains(ptId)){ onCtxOnDuration = true; } if(ctxMedsObs != null && ctxMedsObs.getValueCoded().equals(dapson) && hasTCA.contains(ptId)){ onCtxOnDuration = true; } if(ltfu.contains(ptId)){ onCtxOnDuration = false; } } ret.put(ptId, new BooleanResult(onCtxOnDuration, this)); } return ret; }  @Override CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context); }
OnCtxWithinDurationCalculation extends BaseEmrCalculation { @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 medOrdersObss = Calculations.lastObs(Dictionary.getConcept(Dictionary.MEDICATION_ORDERS), cohort, context); CalculationResultMap medDuration = Calculations.lastObs(Dictionary.getConcept(Dictionary.MEDICATION_DURATION), cohort, context); CalculationResultMap medDurationunits = Calculations.lastObs(Dictionary.getConcept(Dictionary.DURATION_UNITS), cohort, context); Set<Integer> ltfu = CalculationUtils.patientsThatPass(calculate(new LostToFollowUpCalculation(), cohort, context)); Set<Integer> hasTCA = CalculationUtils.patientsThatPass(calculate(new NextOfVisitHigherThanContextCalculation(), cohort, context)); CalculationResultMap medicationDispensed = Calculations.lastObs(Dictionary.getConcept(Dictionary.COTRIMOXAZOLE_DISPENSED), cohort, context); Concept ctx = Dictionary.getConcept(Dictionary.SULFAMETHOXAZOLE_TRIMETHOPRIM); Concept dapson = Dictionary.getConcept(Dictionary.DAPSONE); Concept yes = Dictionary.getConcept(Dictionary.YES); Concept days = Dictionary.getConcept(Dictionary.DAYS); Concept months = Dictionary.getConcept(Dictionary.MONTHS); Concept weeks = Dictionary.getConcept(Dictionary.WEEKS); Concept years = Dictionary.getConcept(Dictionary.YEARS); CalculationResultMap ret = new CalculationResultMap(); for (Integer ptId : cohort) { boolean onCtxOnDuration = false; if (inHivProgram.contains(ptId)) { Obs ctxMedsObs = EmrCalculationUtils.obsResultForPatient(medOrdersObss, ptId); Obs ctxDurationObs = EmrCalculationUtils.obsResultForPatient(medDuration, ptId); Obs ctxDurationUnits = EmrCalculationUtils.obsResultForPatient(medDurationunits, ptId); Obs medicationDispensedObs = EmrCalculationUtils.obsResultForPatient(medicationDispensed, ptId); if(ctxMedsObs != null) { if(ctxMedsObs.getValueCoded().equals(ctx) || ctxMedsObs.getValueCoded().equals(dapson)) { Calendar cal = Calendar.getInstance(); cal.setTime(ctxMedsObs.getObsDatetime()); if(ctxDurationObs != null && ctxDurationUnits != null && ctxMedsObs.getObsGroup().equals(ctxDurationObs.getObsGroup()) && ctxMedsObs.getObsGroup().equals(ctxDurationUnits.getObsGroup())) { Integer durationMonthsDays = Integer.parseInt(ctxDurationObs.getValueNumeric().toString().trim().split("\\.")[0]); Date obsDate = ctxMedsObs.getObsDatetime(); Concept durationUnits = ctxDurationUnits.getValueCoded(); cal.setTime(obsDate); if (durationUnits.equals(months)) { cal.add(Calendar.MONTH, durationMonthsDays); } else if (durationUnits.equals(days)) { cal.add(Calendar.DATE, durationMonthsDays); } else if (durationUnits.equals(weeks)) { cal.add(Calendar.DATE, (durationMonthsDays * 7)); } else if (durationUnits.equals(years)) { cal.add(Calendar.YEAR, durationMonthsDays); } } Date nextRefilldate = cal.getTime(); if(nextRefilldate.after(DateUtil.getStartOfMonth(context.getNow()))) { onCtxOnDuration = true; } } } if(medicationDispensedObs != null && medicationDispensedObs.getValueCoded().equals(yes) && hasTCA.contains(ptId)){ onCtxOnDuration = true; } if(ctxMedsObs != null && ctxMedsObs.getValueCoded().equals(ctx) && hasTCA.contains(ptId)){ onCtxOnDuration = true; } if(ctxMedsObs != null && ctxMedsObs.getValueCoded().equals(dapson) && hasTCA.contains(ptId)){ onCtxOnDuration = true; } if(ltfu.contains(ptId)){ onCtxOnDuration = false; } } ret.put(ptId, new BooleanResult(onCtxOnDuration, this)); } return ret; }  @Override CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context);  }