@Test public void evaluate_shouldDetermineWhetherPatientsWereAssessedOnFirstVisit() throws Exception { Program mchmsProgram = MetadataUtils.existing(Program.class, MchMetadata._Program.MCHMS); EncounterType enrollmentEncounterType = MetadataUtils.existing(EncounterType.class, MchMetadata._EncounterType.MCHMS_ENROLLMENT); EncounterType consultationEncounterType = MetadataUtils.existing(EncounterType.class, MchMetadata._EncounterType.MCHMS_CONSULTATION); Form enrollmentForm = MetadataUtils.existing(Form.class, MchMetadata._Form.MCHMS_ENROLLMENT); Form antenatalVisitForm = MetadataUtils.existing(Form.class, MchMetadata._Form.MCHMS_ANTENATAL_VISIT); Date enrollmentDate = TestUtils.date(2012, 1, 1); TestUtils.enrollInProgram(TestUtils.getPatient(2), mchmsProgram, enrollmentDate); for (int i = 6; i <= 8; i++) { TestUtils.enrollInProgram(TestUtils.getPatient(i), mchmsProgram, enrollmentDate); } Concept hivStatusConcept = Dictionary.getConcept(Dictionary.HIV_STATUS); Concept whoStageConcept = Dictionary.getConcept(Dictionary.CURRENT_WHO_STAGE); Concept cd4CountConcept = Dictionary.getConcept(Dictionary.CD4_COUNT); { Patient patient = TestUtils.getPatient(2); Obs[] enrollmentEncounterObs = {TestUtils.saveObs(patient, hivStatusConcept, Dictionary.getConcept(Dictionary.POSITIVE), enrollmentDate)}; TestUtils.saveEncounter(patient, enrollmentEncounterType, enrollmentForm, enrollmentDate, enrollmentEncounterObs); Obs[] whoStagingAndCd4Obss = {TestUtils.saveObs(patient, cd4CountConcept, 500.00, enrollmentDate), TestUtils.saveObs(patient, whoStageConcept, Dictionary.getConcept(Dictionary.WHO_STAGE_1_ADULT), enrollmentDate)}; TestUtils.saveEncounter(patient, consultationEncounterType, antenatalVisitForm, enrollmentDate, whoStagingAndCd4Obss); } { Patient patient = TestUtils.getPatient(6); Obs[] enrollmentEncounterObs = {TestUtils.saveObs(patient, hivStatusConcept, Dictionary.getConcept(Dictionary.POSITIVE), enrollmentDate)}; TestUtils.saveEncounter(patient, enrollmentEncounterType, enrollmentForm, enrollmentDate, enrollmentEncounterObs); Obs[] cd4Obs = {TestUtils.saveObs(patient, cd4CountConcept, 500.00, enrollmentDate)}; TestUtils.saveEncounter(patient, consultationEncounterType, antenatalVisitForm, enrollmentDate, cd4Obs); } { Patient patient = TestUtils.getPatient(7); Obs[] enrollmentEncounterObs = {TestUtils.saveObs(patient, hivStatusConcept, Dictionary.getConcept(Dictionary.POSITIVE), enrollmentDate)}; TestUtils.saveEncounter(patient, enrollmentEncounterType, enrollmentForm, enrollmentDate, enrollmentEncounterObs); Obs[] whoStageObs = {TestUtils.saveObs(patient, whoStageConcept, Dictionary.getConcept(Dictionary.WHO_STAGE_1_ADULT), enrollmentDate)}; TestUtils.saveEncounter(patient, consultationEncounterType, antenatalVisitForm, enrollmentDate, whoStageObs); } { Patient patient = TestUtils.getPatient(8); Obs[] enrollmentEncounterObs = {TestUtils.saveObs(patient, hivStatusConcept, Dictionary.getConcept(Dictionary.POSITIVE), enrollmentDate)}; TestUtils.saveEncounter(patient, enrollmentEncounterType, enrollmentForm, enrollmentDate, enrollmentEncounterObs); } Context.flushSession(); List<Integer> ptIds = Arrays.asList(2, 6, 7, 8); Map<String, Object> parameters = new HashMap<String, Object>(); CalculationResultMap resultMap = new AssessedOnFirstVisitCalculation().evaluate(ptIds, parameters, Context.getService(PatientCalculationService.class).createCalculationContext()); Assert.assertTrue((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> params, PatientCalculationContext context) { ArtAssessmentMethod artAssessmentMethod = (params != null && params.containsKey("artAssessmentMethod")) ? (ArtAssessmentMethod) params.get("artAssessmentMethod") : null; Program mchmsProgram = MetadataUtils.existing(Program.class, MchMetadata._Program.MCHMS); EncounterType mchConsultation = MetadataUtils.existing(EncounterType.class, MchMetadata._EncounterType.MCHMS_CONSULTATION); Concept whoStageConcept = Dictionary.getConcept(Dictionary.CURRENT_WHO_STAGE); Concept cd4CountConcept = Dictionary.getConcept(Dictionary.CD4_COUNT); Set<Integer> alive = Filters.alive(cohort, context); Set<Integer> inMchmsProgram = Filters.inProgram(mchmsProgram, alive, context); CalculationResultMap crm = Calculations.firstEncounter(mchConsultation, inMchmsProgram, context); CalculationResultMap resultMap = new CalculationResultMap(); for (Integer ptId : cohort) { boolean qualified = false; Encounter encounter = EmrCalculationUtils.encounterResultForPatient(crm, ptId); if (encounter != null) { EncounterWrapper wrapper = new EncounterWrapper(encounter); if (artAssessmentMethod == ArtAssessmentMethod.WHO_STAGING) { Obs whoStageObs = wrapper.firstObs(whoStageConcept); qualified = whoStageObs != null && whoStageObs.getValueCoded() != null; } else if (artAssessmentMethod == ArtAssessmentMethod.CD4_COUNT) { Obs cd4CountObs = wrapper.firstObs(cd4CountConcept); qualified = cd4CountObs != null && cd4CountObs.getValueNumeric() != null; } else { Obs whoStageObs = wrapper.firstObs(whoStageConcept); Obs cd4CountObs = wrapper.firstObs(cd4CountConcept); qualified = (whoStageObs != null && whoStageObs.getValueCoded() != null) || (cd4CountObs != null && cd4CountObs.getValueNumeric() != null); } } resultMap.put(ptId, new BooleanResult(qualified, this, context)); } return resultMap; }
AssessedOnFirstVisitCalculation extends AbstractPatientCalculation { @Override public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> params, PatientCalculationContext context) { ArtAssessmentMethod artAssessmentMethod = (params != null && params.containsKey("artAssessmentMethod")) ? (ArtAssessmentMethod) params.get("artAssessmentMethod") : null; Program mchmsProgram = MetadataUtils.existing(Program.class, MchMetadata._Program.MCHMS); EncounterType mchConsultation = MetadataUtils.existing(EncounterType.class, MchMetadata._EncounterType.MCHMS_CONSULTATION); Concept whoStageConcept = Dictionary.getConcept(Dictionary.CURRENT_WHO_STAGE); Concept cd4CountConcept = Dictionary.getConcept(Dictionary.CD4_COUNT); Set<Integer> alive = Filters.alive(cohort, context); Set<Integer> inMchmsProgram = Filters.inProgram(mchmsProgram, alive, context); CalculationResultMap crm = Calculations.firstEncounter(mchConsultation, inMchmsProgram, context); CalculationResultMap resultMap = new CalculationResultMap(); for (Integer ptId : cohort) { boolean qualified = false; Encounter encounter = EmrCalculationUtils.encounterResultForPatient(crm, ptId); if (encounter != null) { EncounterWrapper wrapper = new EncounterWrapper(encounter); if (artAssessmentMethod == ArtAssessmentMethod.WHO_STAGING) { Obs whoStageObs = wrapper.firstObs(whoStageConcept); qualified = whoStageObs != null && whoStageObs.getValueCoded() != null; } else if (artAssessmentMethod == ArtAssessmentMethod.CD4_COUNT) { Obs cd4CountObs = wrapper.firstObs(cd4CountConcept); qualified = cd4CountObs != null && cd4CountObs.getValueNumeric() != null; } else { Obs whoStageObs = wrapper.firstObs(whoStageConcept); Obs cd4CountObs = wrapper.firstObs(cd4CountConcept); qualified = (whoStageObs != null && whoStageObs.getValueCoded() != null) || (cd4CountObs != null && cd4CountObs.getValueNumeric() != null); } } resultMap.put(ptId, new BooleanResult(qualified, this, context)); } return resultMap; } }
AssessedOnFirstVisitCalculation extends AbstractPatientCalculation { @Override public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> params, PatientCalculationContext context) { ArtAssessmentMethod artAssessmentMethod = (params != null && params.containsKey("artAssessmentMethod")) ? (ArtAssessmentMethod) params.get("artAssessmentMethod") : null; Program mchmsProgram = MetadataUtils.existing(Program.class, MchMetadata._Program.MCHMS); EncounterType mchConsultation = MetadataUtils.existing(EncounterType.class, MchMetadata._EncounterType.MCHMS_CONSULTATION); Concept whoStageConcept = Dictionary.getConcept(Dictionary.CURRENT_WHO_STAGE); Concept cd4CountConcept = Dictionary.getConcept(Dictionary.CD4_COUNT); Set<Integer> alive = Filters.alive(cohort, context); Set<Integer> inMchmsProgram = Filters.inProgram(mchmsProgram, alive, context); CalculationResultMap crm = Calculations.firstEncounter(mchConsultation, inMchmsProgram, context); CalculationResultMap resultMap = new CalculationResultMap(); for (Integer ptId : cohort) { boolean qualified = false; Encounter encounter = EmrCalculationUtils.encounterResultForPatient(crm, ptId); if (encounter != null) { EncounterWrapper wrapper = new EncounterWrapper(encounter); if (artAssessmentMethod == ArtAssessmentMethod.WHO_STAGING) { Obs whoStageObs = wrapper.firstObs(whoStageConcept); qualified = whoStageObs != null && whoStageObs.getValueCoded() != null; } else if (artAssessmentMethod == ArtAssessmentMethod.CD4_COUNT) { Obs cd4CountObs = wrapper.firstObs(cd4CountConcept); qualified = cd4CountObs != null && cd4CountObs.getValueNumeric() != null; } else { Obs whoStageObs = wrapper.firstObs(whoStageConcept); Obs cd4CountObs = wrapper.firstObs(cd4CountConcept); qualified = (whoStageObs != null && whoStageObs.getValueCoded() != null) || (cd4CountObs != null && cd4CountObs.getValueNumeric() != null); } } resultMap.put(ptId, new BooleanResult(qualified, this, context)); } return resultMap; }  }
AssessedOnFirstVisitCalculation extends AbstractPatientCalculation { @Override public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> params, PatientCalculationContext context) { ArtAssessmentMethod artAssessmentMethod = (params != null && params.containsKey("artAssessmentMethod")) ? (ArtAssessmentMethod) params.get("artAssessmentMethod") : null; Program mchmsProgram = MetadataUtils.existing(Program.class, MchMetadata._Program.MCHMS); EncounterType mchConsultation = MetadataUtils.existing(EncounterType.class, MchMetadata._EncounterType.MCHMS_CONSULTATION); Concept whoStageConcept = Dictionary.getConcept(Dictionary.CURRENT_WHO_STAGE); Concept cd4CountConcept = Dictionary.getConcept(Dictionary.CD4_COUNT); Set<Integer> alive = Filters.alive(cohort, context); Set<Integer> inMchmsProgram = Filters.inProgram(mchmsProgram, alive, context); CalculationResultMap crm = Calculations.firstEncounter(mchConsultation, inMchmsProgram, context); CalculationResultMap resultMap = new CalculationResultMap(); for (Integer ptId : cohort) { boolean qualified = false; Encounter encounter = EmrCalculationUtils.encounterResultForPatient(crm, ptId); if (encounter != null) { EncounterWrapper wrapper = new EncounterWrapper(encounter); if (artAssessmentMethod == ArtAssessmentMethod.WHO_STAGING) { Obs whoStageObs = wrapper.firstObs(whoStageConcept); qualified = whoStageObs != null && whoStageObs.getValueCoded() != null; } else if (artAssessmentMethod == ArtAssessmentMethod.CD4_COUNT) { Obs cd4CountObs = wrapper.firstObs(cd4CountConcept); qualified = cd4CountObs != null && cd4CountObs.getValueNumeric() != null; } else { Obs whoStageObs = wrapper.firstObs(whoStageConcept); Obs cd4CountObs = wrapper.firstObs(cd4CountConcept); qualified = (whoStageObs != null && whoStageObs.getValueCoded() != null) || (cd4CountObs != null && cd4CountObs.getValueNumeric() != null); } } resultMap.put(ptId, new BooleanResult(qualified, this, context)); } return resultMap; }  @Override CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> params, PatientCalculationContext context); }
AssessedOnFirstVisitCalculation extends AbstractPatientCalculation { @Override public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> params, PatientCalculationContext context) { ArtAssessmentMethod artAssessmentMethod = (params != null && params.containsKey("artAssessmentMethod")) ? (ArtAssessmentMethod) params.get("artAssessmentMethod") : null; Program mchmsProgram = MetadataUtils.existing(Program.class, MchMetadata._Program.MCHMS); EncounterType mchConsultation = MetadataUtils.existing(EncounterType.class, MchMetadata._EncounterType.MCHMS_CONSULTATION); Concept whoStageConcept = Dictionary.getConcept(Dictionary.CURRENT_WHO_STAGE); Concept cd4CountConcept = Dictionary.getConcept(Dictionary.CD4_COUNT); Set<Integer> alive = Filters.alive(cohort, context); Set<Integer> inMchmsProgram = Filters.inProgram(mchmsProgram, alive, context); CalculationResultMap crm = Calculations.firstEncounter(mchConsultation, inMchmsProgram, context); CalculationResultMap resultMap = new CalculationResultMap(); for (Integer ptId : cohort) { boolean qualified = false; Encounter encounter = EmrCalculationUtils.encounterResultForPatient(crm, ptId); if (encounter != null) { EncounterWrapper wrapper = new EncounterWrapper(encounter); if (artAssessmentMethod == ArtAssessmentMethod.WHO_STAGING) { Obs whoStageObs = wrapper.firstObs(whoStageConcept); qualified = whoStageObs != null && whoStageObs.getValueCoded() != null; } else if (artAssessmentMethod == ArtAssessmentMethod.CD4_COUNT) { Obs cd4CountObs = wrapper.firstObs(cd4CountConcept); qualified = cd4CountObs != null && cd4CountObs.getValueNumeric() != null; } else { Obs whoStageObs = wrapper.firstObs(whoStageConcept); Obs cd4CountObs = wrapper.firstObs(cd4CountConcept); qualified = (whoStageObs != null && whoStageObs.getValueCoded() != null) || (cd4CountObs != null && cd4CountObs.getValueNumeric() != null); } } resultMap.put(ptId, new BooleanResult(qualified, this, context)); } return resultMap; }  @Override CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> params, PatientCalculationContext context);  }