OnHaartCalculation extends AbstractPatientCalculation { @Override public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context) { Program mchmsProgram = MetadataUtils.existing(Program.class, MchMetadata._Program.MCHMS); Set<Integer> alive = Filters.alive(cohort, context); Set<Integer> inMchmsProgram = Filters.inProgram(mchmsProgram, alive, context); CalculationResultMap lastHivStatusObss = Calculations.lastObs(Dictionary.getConcept(Dictionary.HIV_STATUS), inMchmsProgram, context); CalculationResultMap artStatusObss = Calculations.lastObs(Dictionary.getConcept(Dictionary.ANTIRETROVIRAL_USE_IN_PREGNANCY), inMchmsProgram, context); Concept hivPositiveConcept = Dictionary.getConcept(Dictionary.POSITIVE); Concept onHaartConcept = Dictionary.getConcept(Dictionary.MOTHER_ON_HAART); CalculationResultMap ret = new CalculationResultMap(); for (Integer ptId : cohort) { boolean onHaart = false; if (inMchmsProgram.contains(ptId)) { Concept lastHivStatus = EmrCalculationUtils.codedObsResultForPatient(lastHivStatusObss, ptId); Concept lastArtStatus = EmrCalculationUtils.codedObsResultForPatient(artStatusObss, ptId); boolean hivPositive = false; if (lastHivStatus != null) { hivPositive = lastHivStatus.equals(hivPositiveConcept); if (lastArtStatus != null) { onHaart = lastArtStatus.equals(onHaartConcept); } } onHaart = hivPositive && onHaart; } ret.put(ptId, new BooleanResult(onHaart, this, context)); } return ret; }  @Override CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context);  }
@Test public void evaluate_shouldDetermineWhetherPatientsAreOnHaart() throws Exception { Program mchmsProgram = MetadataUtils.existing(Program.class, MchMetadata._Program.MCHMS); EncounterType enrollmentEncounterType = MetadataUtils.existing(EncounterType.class, MchMetadata._EncounterType.MCHMS_ENROLLMENT); Form enrollmentForm = MetadataUtils.existing(Form.class, MchMetadata._Form.MCHMS_ENROLLMENT); TestUtils.enrollInProgram(TestUtils.getPatient(2), mchmsProgram, new Date()); for (int i = 6; i <= 8; i++) { TestUtils.enrollInProgram(TestUtils.getPatient(i), mchmsProgram, new Date()); } Concept hivStatus = Dictionary.getConcept(Dictionary.HIV_STATUS); Concept artUse = Dictionary.getConcept(Dictionary.ANTIRETROVIRAL_USE_IN_PREGNANCY); Obs[] encounterObss2 = {TestUtils.saveObs(TestUtils.getPatient(2), hivStatus, Dictionary.getConcept(Dictionary.NEGATIVE), new Date())}; TestUtils.saveEncounter(TestUtils.getPatient(2), enrollmentEncounterType, enrollmentForm, new Date(), encounterObss2); TestUtils.saveObs(TestUtils.getPatient(2), artUse, Dictionary.getConcept(Dictionary.NOT_APPLICABLE), new Date()); Obs[] encounterObss6 = {TestUtils.saveObs(TestUtils.getPatient(6), hivStatus, Dictionary.getConcept(Dictionary.POSITIVE), new Date())}; TestUtils.saveEncounter(TestUtils.getPatient(6), enrollmentEncounterType, enrollmentForm, new Date(), encounterObss6); TestUtils.saveObs(TestUtils.getPatient(6), artUse, Dictionary.getConcept(Dictionary.NOT_APPLICABLE), new Date()); Obs[] encounterObss7 = {TestUtils.saveObs(TestUtils.getPatient(7), hivStatus, Dictionary.getConcept(Dictionary.POSITIVE), new Date())}; TestUtils.saveEncounter(TestUtils.getPatient(7), enrollmentEncounterType, enrollmentForm, new Date(), encounterObss7); TestUtils.saveObs(TestUtils.getPatient(7), artUse, Dictionary.getConcept(Dictionary.MOTHER_ON_HAART), new Date()); Obs[] encounterObss8 = {TestUtils.saveObs(TestUtils.getPatient(8), hivStatus, Dictionary.getConcept(Dictionary.POSITIVE), new Date())}; TestUtils.saveEncounter(TestUtils.getPatient(8), enrollmentEncounterType, enrollmentForm, new Date(), encounterObss8); TestUtils.saveObs(TestUtils.getPatient(8), artUse, Dictionary.getConcept(Dictionary.MOTHER_ON_PROPHYLAXIS), new Date()); Context.flushSession(); List<Integer> ptIds = Arrays.asList(2, 6, 7, 8); CalculationResultMap resultMap = Context.getService(PatientCalculationService.class).evaluate(ptIds, new OnHaartCalculation()); Assert.assertFalse((Boolean) resultMap.get(2).getValue()); Assert.assertFalse((Boolean) resultMap.get(6).getValue()); Assert.assertTrue((Boolean) resultMap.get(7).getValue()); Assert.assertFalse((Boolean) resultMap.get(8).getValue()); }
@Test public void evaluate2_shouldDetermineWhetherPatientsAreOnHaart() throws Exception { List<Integer> ptIds = Arrays.asList(2, 999); CalculationResultMap resultMap = Context.getService(PatientCalculationService.class).evaluate(ptIds, new OnHaartCalculation()); Assert.assertFalse((Boolean) resultMap.get(2).getValue()); Assert.assertFalse((Boolean) resultMap.get(999).getValue()); }