PartnerHivStatusUnknownCalculation 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 partnerHivStatusObs = Calculations.lastObs(Dictionary.getConcept(Dictionary.PARTNER_HIV_STATUS), inMchmsProgram, context); Concept unknownConcept = Dictionary.getConcept(Dictionary.UNKNOWN); CalculationResultMap ret = new CalculationResultMap(); for (Integer ptId : cohort) { boolean partnerHivStatusUnknown = false; if (inMchmsProgram.contains(ptId)) { Concept partnerHivStatus = EmrCalculationUtils.codedObsResultForPatient(partnerHivStatusObs, ptId); if (partnerHivStatus != null) { partnerHivStatusUnknown = partnerHivStatus.equals(unknownConcept); } } ret.put(ptId, new BooleanResult(partnerHivStatusUnknown, this, context)); } return ret; }  @Override CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context);  }
@Test public void evaluate_shouldDetermineWhetherPatientsPartnersHiVStatusesAreUnknown() 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); for (int i = 6; i <= 8; i++) { TestUtils.enrollInProgram(TestUtils.getPatient(i), mchmsProgram, new Date()); } Concept partnerHivStatus = Dictionary.getConcept(Dictionary.PARTNER_HIV_STATUS); Obs[] encounterObss6 = {TestUtils.saveObs(TestUtils.getPatient(6), partnerHivStatus, Dictionary.getConcept(Dictionary.UNKNOWN), new Date())}; TestUtils.saveEncounter(TestUtils.getPatient(6), enrollmentEncounterType, enrollmentForm, new Date(), encounterObss6); Obs[] encounterObss7 = {TestUtils.saveObs(TestUtils.getPatient(7), partnerHivStatus, Dictionary.getConcept(Dictionary.NEGATIVE), new Date())}; TestUtils.saveEncounter(TestUtils.getPatient(7), enrollmentEncounterType, enrollmentForm, new Date(), encounterObss7); Obs[] encounterObss8 = {TestUtils.saveObs(TestUtils.getPatient(8), partnerHivStatus, Dictionary.getConcept(Dictionary.POSITIVE), new Date())}; TestUtils.saveEncounter(TestUtils.getPatient(8), enrollmentEncounterType, enrollmentForm, new Date(), encounterObss8); Context.flushSession(); List<Integer> ptIds = Arrays.asList(6, 7, 8, 2, 999); CalculationResultMap resultMap = Context.getService(PatientCalculationService.class).evaluate(ptIds, new PartnerHivStatusUnknownCalculation()); Assert.assertTrue((Boolean) resultMap.get(6).getValue()); Assert.assertFalse((Boolean) resultMap.get(7).getValue()); Assert.assertFalse((Boolean) resultMap.get(8).getValue()); Assert.assertFalse((Boolean) resultMap.get(2).getValue()); Assert.assertFalse((Boolean) resultMap.get(999).getValue()); }