IsPregnantCalculation extends AbstractPatientCalculation implements PatientFlagCalculation { @Override public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context) { Set<Integer> aliveAndFemale = Filters.female(Filters.alive(cohort, context), context); EncounterType mchEnrollment = MetadataUtils.existing(EncounterType.class, MchMetadata._EncounterType.MCHMS_ENROLLMENT); EncounterType mchPostNatal = MetadataUtils.existing(EncounterType.class, MchMetadata._EncounterType.MCHMS_CONSULTATION); EncounterType mchDiscontinuation = MetadataUtils.existing(EncounterType.class, MchMetadata._EncounterType.MCHMS_DISCONTINUATION); Concept yes = Dictionary.getConcept(Dictionary.YES); CalculationResultMap pregStatusObss = Calculations.lastObs(Dictionary.getConcept(Dictionary.PREGNANCY_STATUS), aliveAndFemale, context); CalculationResultMap ret = new CalculationResultMap(); CalculationResultMap enrollmentMap = Calculations.lastEncounter(mchEnrollment, aliveAndFemale, context); CalculationResultMap postNatal = Calculations.lastEncounter(mchPostNatal, aliveAndFemale, context); CalculationResultMap discontinuation = Calculations.lastEncounter(mchDiscontinuation, aliveAndFemale, context); CalculationResultMap confinementDateMap = Calculations.lastObs(Dictionary.getConcept(Dictionary.DATE_OF_CONFINEMENT), cohort, context); for (Integer ptId : cohort) { boolean result = false; Obs pregStatusObs = EmrCalculationUtils.obsResultForPatient(pregStatusObss, ptId); Encounter encounter = EmrCalculationUtils.encounterResultForPatient(enrollmentMap, ptId); Encounter encounterPostNatal = EmrCalculationUtils.encounterResultForPatient(postNatal, ptId); Encounter encounterDiscontinuation = EmrCalculationUtils.encounterResultForPatient(discontinuation, ptId); Obs confinement = EmrCalculationUtils.obsResultForPatient(confinementDateMap, ptId); if (pregStatusObs != null && pregStatusObs.getValueCoded().equals(yes)) { result = true; } if(encounter != null) { result = true; } if(encounter != null && encounterPostNatal != null && encounterPostNatal.getEncounterDatetime().after(encounter.getEncounterDatetime())){ result = false; } if(encounter != null && encounterDiscontinuation != null && encounterDiscontinuation.getEncounterDatetime().after(encounter.getEncounterDatetime())){ result = false; } if(pregStatusObs !=null && confinement != null && confinement.getValueDatetime().after(pregStatusObs.getObsDatetime())) { result = false; } if(encounter !=null && confinement != null && confinement.getValueDatetime().after(encounter.getEncounterDatetime())) { result = false; } ret.put(ptId, new BooleanResult(result, this)); } return ret; }  @Override String getFlagMessage(); @Override CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context);  }
@Test public void evaluate_shouldCalculatePregnancyStatus() throws Exception { Concept pregnancyStatus = Dictionary.getConcept(Dictionary.PREGNANCY_STATUS); Concept yes = Dictionary.getConcept(Dictionary.YES); Concept no = Dictionary.getConcept(Dictionary.NO); TestUtils.saveObs(TestUtils.getPatient(6), pregnancyStatus, yes, TestUtils.date(2012, 12, 1)); TestUtils.saveObs(TestUtils.getPatient(7), pregnancyStatus, yes, TestUtils.date(2010, 11, 1)); TestUtils.saveObs(TestUtils.getPatient(7), pregnancyStatus, no, TestUtils.date(2012, 12, 1)); TestUtils.saveObs(TestUtils.getPatient(8), pregnancyStatus, yes, TestUtils.date(2012, 12, 1)); List<Integer> ptIds = Arrays.asList(6, 7, 8); CalculationResultMap resultMap = new IsPregnantCalculation().evaluate(ptIds, null, Context.getService(PatientCalculationService.class).createCalculationContext()); Assert.assertFalse((Boolean) resultMap.get(6).getValue()); Assert.assertFalse((Boolean) resultMap.get(7).getValue()); Assert.assertTrue((Boolean) resultMap.get(8).getValue()); }