@Test public void valuate_shouldCalculateInfantsWhoReceivedDNAPCRTestAtSixWeeks() throws Exception { Form followUpForm = MetadataUtils.existing(Form.class, MchMetadata._Form.MCHCS_FOLLOW_UP); EncounterType mchcsConsultationEncounterType = MetadataUtils.existing(EncounterType.class, MchMetadata._EncounterType.MCHCS_CONSULTATION); { Patient patient = TestUtils.getPatient(7); patient.setBirthdate(TestUtils.date(2014, 8, 10)); Obs[] dnaPcrObs = { TestUtils.saveObs(patient, Dictionary.getConcept(Metadata.Concept.HIV_DNA_POLYMERASE_CHAIN_REACTION), Dictionary.getConcept(Metadata.Concept.DETECTED), TestUtils.date(2014, 9, 20)), TestUtils.saveObs(patient, Dictionary.getConcept(Metadata.Concept.HIV_DNA_POLYMERASE_CHAIN_REACTION), Dictionary.getConcept(Metadata.Concept.NOT_DETECTED) , TestUtils.date(2014, 9, 30)), }; TestUtils.saveEncounter(patient, mchcsConsultationEncounterType, followUpForm, TestUtils.date(2014, 9, 30), dnaPcrObs); } { Patient patient = TestUtils.getPatient(8); patient.setBirthdate(TestUtils.date(2014, 8, 10)); Obs[] dnaPcrObs = { TestUtils.saveObs(patient, Dictionary.getConcept(Metadata.Concept.HIV_DNA_POLYMERASE_CHAIN_REACTION), Dictionary.getConcept(Metadata.Concept.DETECTED), TestUtils.date(2014, 8, 30)), TestUtils.saveObs(patient, Dictionary.getConcept(Metadata.Concept.HIV_DNA_POLYMERASE_CHAIN_REACTION), Dictionary.getConcept(Metadata.Concept.NOT_DETECTED) , TestUtils.date(2014, 9, 10)), }; TestUtils.saveEncounter(patient, mchcsConsultationEncounterType, followUpForm, TestUtils.date(2014, 9, 30), dnaPcrObs); } { Patient patient = TestUtils.getPatient(6); Obs[] dnaPcrObs = { TestUtils.saveObs(patient, Dictionary.getConcept(Metadata.Concept.HIV_DNA_POLYMERASE_CHAIN_REACTION), Dictionary.getConcept(Metadata.Concept.DETECTED), TestUtils.date(2014, 8, 30)), TestUtils.saveObs(patient, Dictionary.getConcept(Metadata.Concept.HIV_DNA_POLYMERASE_CHAIN_REACTION), Dictionary.getConcept(Metadata.Concept.NOT_DETECTED) , TestUtils.date(2014, 9, 10)), }; TestUtils.saveEncounter(patient, mchcsConsultationEncounterType, followUpForm, TestUtils.date(2014, 9, 30)); } List<Integer> ptIds = Arrays.asList(7, 8, 999, 6); Map<String, Object> params = new HashMap<String, Object>(); params.put("durationAfterBirth", 6); CalculationResultMap resultMap = new InfantsDNAPCRCalculation().evaluate(ptIds, params, Context.getService(PatientCalculationService.class).createCalculationContext()); Assert.assertTrue((Boolean) resultMap.get(7).getValue()); Assert.assertFalse((Boolean) resultMap.get(8).getValue()); Assert.assertNull(resultMap.get(999)); Assert.assertFalse((Boolean) resultMap.get(6).getValue()); }
@Override public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context) { CalculationResultMap infantsWithDNAPCR = Calculations.allObs(Dictionary.getConcept(Dictionary.HIV_DNA_POLYMERASE_CHAIN_REACTION),cohort, context); CalculationResultMap ret = new CalculationResultMap(); Integer durationAfterBirth = (Integer)parameterValues.get("durationAfterBirth"); PatientService service = Context.getPatientService(); for (Integer ptId : cohort) { boolean receivedTest = false; ListResult result = (ListResult) infantsWithDNAPCR.get(ptId); List<Obs> obs = CalculationUtils.extractResultValues(result); Date dob = service.getPatient(ptId).getBirthdate(); if (dob != null && !obs.isEmpty()) { Calendar target_date = setCalendarTime(dob); target_date.add(Calendar.DAY_OF_YEAR, (durationAfterBirth * 7)); Calendar lower_boundary = setCalendarTime(target_date.getTime()); lower_boundary.add(Calendar.DAY_OF_YEAR, -2); Calendar upper_boundary = setCalendarTime(target_date.getTime()); upper_boundary.add(Calendar.DAY_OF_YEAR, 6); for (Obs o : obs) { if(o != null) { Calendar obs_date = setCalendarTime(o.getObsDatetime()); if (obs_date.after(lower_boundary) && obs_date.before(upper_boundary)) { receivedTest = true; break; } } } ret.put(ptId, new BooleanResult(receivedTest, this)); } } return ret; }
InfantsDNAPCRCalculation extends AbstractPatientCalculation { @Override public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context) { CalculationResultMap infantsWithDNAPCR = Calculations.allObs(Dictionary.getConcept(Dictionary.HIV_DNA_POLYMERASE_CHAIN_REACTION),cohort, context); CalculationResultMap ret = new CalculationResultMap(); Integer durationAfterBirth = (Integer)parameterValues.get("durationAfterBirth"); PatientService service = Context.getPatientService(); for (Integer ptId : cohort) { boolean receivedTest = false; ListResult result = (ListResult) infantsWithDNAPCR.get(ptId); List<Obs> obs = CalculationUtils.extractResultValues(result); Date dob = service.getPatient(ptId).getBirthdate(); if (dob != null && !obs.isEmpty()) { Calendar target_date = setCalendarTime(dob); target_date.add(Calendar.DAY_OF_YEAR, (durationAfterBirth * 7)); Calendar lower_boundary = setCalendarTime(target_date.getTime()); lower_boundary.add(Calendar.DAY_OF_YEAR, -2); Calendar upper_boundary = setCalendarTime(target_date.getTime()); upper_boundary.add(Calendar.DAY_OF_YEAR, 6); for (Obs o : obs) { if(o != null) { Calendar obs_date = setCalendarTime(o.getObsDatetime()); if (obs_date.after(lower_boundary) && obs_date.before(upper_boundary)) { receivedTest = true; break; } } } ret.put(ptId, new BooleanResult(receivedTest, this)); } } return ret; } }
InfantsDNAPCRCalculation extends AbstractPatientCalculation { @Override public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context) { CalculationResultMap infantsWithDNAPCR = Calculations.allObs(Dictionary.getConcept(Dictionary.HIV_DNA_POLYMERASE_CHAIN_REACTION),cohort, context); CalculationResultMap ret = new CalculationResultMap(); Integer durationAfterBirth = (Integer)parameterValues.get("durationAfterBirth"); PatientService service = Context.getPatientService(); for (Integer ptId : cohort) { boolean receivedTest = false; ListResult result = (ListResult) infantsWithDNAPCR.get(ptId); List<Obs> obs = CalculationUtils.extractResultValues(result); Date dob = service.getPatient(ptId).getBirthdate(); if (dob != null && !obs.isEmpty()) { Calendar target_date = setCalendarTime(dob); target_date.add(Calendar.DAY_OF_YEAR, (durationAfterBirth * 7)); Calendar lower_boundary = setCalendarTime(target_date.getTime()); lower_boundary.add(Calendar.DAY_OF_YEAR, -2); Calendar upper_boundary = setCalendarTime(target_date.getTime()); upper_boundary.add(Calendar.DAY_OF_YEAR, 6); for (Obs o : obs) { if(o != null) { Calendar obs_date = setCalendarTime(o.getObsDatetime()); if (obs_date.after(lower_boundary) && obs_date.before(upper_boundary)) { receivedTest = true; break; } } } ret.put(ptId, new BooleanResult(receivedTest, this)); } } return ret; }  }
InfantsDNAPCRCalculation extends AbstractPatientCalculation { @Override public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context) { CalculationResultMap infantsWithDNAPCR = Calculations.allObs(Dictionary.getConcept(Dictionary.HIV_DNA_POLYMERASE_CHAIN_REACTION),cohort, context); CalculationResultMap ret = new CalculationResultMap(); Integer durationAfterBirth = (Integer)parameterValues.get("durationAfterBirth"); PatientService service = Context.getPatientService(); for (Integer ptId : cohort) { boolean receivedTest = false; ListResult result = (ListResult) infantsWithDNAPCR.get(ptId); List<Obs> obs = CalculationUtils.extractResultValues(result); Date dob = service.getPatient(ptId).getBirthdate(); if (dob != null && !obs.isEmpty()) { Calendar target_date = setCalendarTime(dob); target_date.add(Calendar.DAY_OF_YEAR, (durationAfterBirth * 7)); Calendar lower_boundary = setCalendarTime(target_date.getTime()); lower_boundary.add(Calendar.DAY_OF_YEAR, -2); Calendar upper_boundary = setCalendarTime(target_date.getTime()); upper_boundary.add(Calendar.DAY_OF_YEAR, 6); for (Obs o : obs) { if(o != null) { Calendar obs_date = setCalendarTime(o.getObsDatetime()); if (obs_date.after(lower_boundary) && obs_date.before(upper_boundary)) { receivedTest = true; break; } } } ret.put(ptId, new BooleanResult(receivedTest, this)); } } return ret; }  @Override CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context); }
InfantsDNAPCRCalculation extends AbstractPatientCalculation { @Override public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context) { CalculationResultMap infantsWithDNAPCR = Calculations.allObs(Dictionary.getConcept(Dictionary.HIV_DNA_POLYMERASE_CHAIN_REACTION),cohort, context); CalculationResultMap ret = new CalculationResultMap(); Integer durationAfterBirth = (Integer)parameterValues.get("durationAfterBirth"); PatientService service = Context.getPatientService(); for (Integer ptId : cohort) { boolean receivedTest = false; ListResult result = (ListResult) infantsWithDNAPCR.get(ptId); List<Obs> obs = CalculationUtils.extractResultValues(result); Date dob = service.getPatient(ptId).getBirthdate(); if (dob != null && !obs.isEmpty()) { Calendar target_date = setCalendarTime(dob); target_date.add(Calendar.DAY_OF_YEAR, (durationAfterBirth * 7)); Calendar lower_boundary = setCalendarTime(target_date.getTime()); lower_boundary.add(Calendar.DAY_OF_YEAR, -2); Calendar upper_boundary = setCalendarTime(target_date.getTime()); upper_boundary.add(Calendar.DAY_OF_YEAR, 6); for (Obs o : obs) { if(o != null) { Calendar obs_date = setCalendarTime(o.getObsDatetime()); if (obs_date.after(lower_boundary) && obs_date.before(upper_boundary)) { receivedTest = true; break; } } } ret.put(ptId, new BooleanResult(receivedTest, this)); } } return ret; }  @Override CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context);  }