@Test public void evaluate_shouldDetermineWhenAPatientsFirstAncVisitDateWas() 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); Date enrollmentDate = TestUtils.date(2012, 1, 1); Concept whoStageConcept = Dictionary.getConcept(Dictionary.CURRENT_WHO_STAGE); Concept cd4CountConcept = Dictionary.getConcept(Dictionary.CD4_COUNT); for (int i = 6; i <= 8; i++) { TestUtils.enrollInProgram(TestUtils.getPatient(i), mchmsProgram, enrollmentDate); Patient patient = TestUtils.getPatient(i); EncounterType encounterType = null; if (i ==7) { encounterType = enrollmentEncounterType; } else if (i == 8) { encounterType = consultationEncounterType; } Obs[] whoStagingAndCd4Obss = {TestUtils.saveObs(patient, cd4CountConcept, 500.00, enrollmentDate), TestUtils.saveObs(patient, whoStageConcept, Dictionary.getConcept(Dictionary.WHO_STAGE_1_ADULT), enrollmentDate)}; TestUtils.saveEncounter(patient, encounterType, enrollmentDate, whoStagingAndCd4Obss); } Context.flushSession(); List<Integer> ptIds = Arrays.asList(2, 6, 7, 8, 999); CalculationResultMap resultMap = new MchmsFirstVisitDateCalculation().evaluate(ptIds, null, Context.getService(PatientCalculationService.class).createCalculationContext()); Assert.assertNull(resultMap.get(2)); Assert.assertNull(resultMap.get(6)); Assert.assertNull(resultMap.get(7)); Assert.assertEquals(enrollmentDate, resultMap.get(8).getValue()); Assert.assertNull(resultMap.get(999)); }
@Override public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> params, PatientCalculationContext context) { Program mchmsProgram = MetadataUtils.existing(Program.class, MchMetadata._Program.MCHMS); EncounterType mchConsultation = MetadataUtils.existing(EncounterType.class, MchMetadata._EncounterType.MCHMS_CONSULTATION); 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) { if (inMchmsProgram.contains(ptId)) { Encounter encounter = EmrCalculationUtils.encounterResultForPatient(crm, ptId); if (encounter != null) { resultMap.put(ptId, new SimpleResult(encounter.getEncounterDatetime(), null)); } else { resultMap.put(ptId, null); } } else { resultMap.put(ptId, null); } } return resultMap; }
MchmsFirstVisitDateCalculation extends AbstractPatientCalculation { @Override public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> params, PatientCalculationContext context) { Program mchmsProgram = MetadataUtils.existing(Program.class, MchMetadata._Program.MCHMS); EncounterType mchConsultation = MetadataUtils.existing(EncounterType.class, MchMetadata._EncounterType.MCHMS_CONSULTATION); 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) { if (inMchmsProgram.contains(ptId)) { Encounter encounter = EmrCalculationUtils.encounterResultForPatient(crm, ptId); if (encounter != null) { resultMap.put(ptId, new SimpleResult(encounter.getEncounterDatetime(), null)); } else { resultMap.put(ptId, null); } } else { resultMap.put(ptId, null); } } return resultMap; } }
MchmsFirstVisitDateCalculation extends AbstractPatientCalculation { @Override public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> params, PatientCalculationContext context) { Program mchmsProgram = MetadataUtils.existing(Program.class, MchMetadata._Program.MCHMS); EncounterType mchConsultation = MetadataUtils.existing(EncounterType.class, MchMetadata._EncounterType.MCHMS_CONSULTATION); 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) { if (inMchmsProgram.contains(ptId)) { Encounter encounter = EmrCalculationUtils.encounterResultForPatient(crm, ptId); if (encounter != null) { resultMap.put(ptId, new SimpleResult(encounter.getEncounterDatetime(), null)); } else { resultMap.put(ptId, null); } } else { resultMap.put(ptId, null); } } return resultMap; }  }
MchmsFirstVisitDateCalculation extends AbstractPatientCalculation { @Override public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> params, PatientCalculationContext context) { Program mchmsProgram = MetadataUtils.existing(Program.class, MchMetadata._Program.MCHMS); EncounterType mchConsultation = MetadataUtils.existing(EncounterType.class, MchMetadata._EncounterType.MCHMS_CONSULTATION); 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) { if (inMchmsProgram.contains(ptId)) { Encounter encounter = EmrCalculationUtils.encounterResultForPatient(crm, ptId); if (encounter != null) { resultMap.put(ptId, new SimpleResult(encounter.getEncounterDatetime(), null)); } else { resultMap.put(ptId, null); } } else { resultMap.put(ptId, null); } } return resultMap; }  @Override CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> params,
										 PatientCalculationContext context); }
MchmsFirstVisitDateCalculation extends AbstractPatientCalculation { @Override public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> params, PatientCalculationContext context) { Program mchmsProgram = MetadataUtils.existing(Program.class, MchMetadata._Program.MCHMS); EncounterType mchConsultation = MetadataUtils.existing(EncounterType.class, MchMetadata._EncounterType.MCHMS_CONSULTATION); 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) { if (inMchmsProgram.contains(ptId)) { Encounter encounter = EmrCalculationUtils.encounterResultForPatient(crm, ptId); if (encounter != null) { resultMap.put(ptId, new SimpleResult(encounter.getEncounterDatetime(), null)); } else { resultMap.put(ptId, null); } } else { resultMap.put(ptId, null); } } return resultMap; }  @Override CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> params,
										 PatientCalculationContext context);  }