DeliveriesWithFullPartographsCalculation extends AbstractPatientCalculation { @Override public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context) { List<String> conceptList = Arrays.asList( Dictionary.getConcept(Metadata.Concept.PREGNANCY_DURATION_AMOUNT).getUuid(), Dictionary.getConcept(Metadata.Concept.METHOD_OF_DELIVERY).getUuid(), Dictionary.getConcept(Metadata.Concept.DATE_OF_CONFINEMENT).getUuid(), Dictionary.getConcept(Metadata.Concept.ESTIMATED_MATERNAL_BLOOD_LOSS_QUALITATIVE).getUuid(), Dictionary.getConcept(Metadata.Concept.MATERNAL_CONDITION_DURING_PUERPERIUM).getUuid(), Dictionary.getConcept(Metadata.Concept.APGAR_SCORE_AT_1_MINUTE).getUuid(), Dictionary.getConcept(Metadata.Concept.APGAR_SCORE_AT_5_MINUTES).getUuid(), Dictionary.getConcept(Metadata.Concept.APGAR_SCORE_AT_10_MINUTES).getUuid(), Dictionary.getConcept(Metadata.Concept.NEONTAL_RESUSCITATION).getUuid(), Dictionary.getConcept(Metadata.Concept.LOCATION_OF_BIRTH).getUuid(), Dictionary.getConcept(Metadata.Concept.DELIVERY_ASSISTANT).getUuid() ); Integer onOrAfter = (Integer)parameterValues.get("onOrAfter"); Calendar cal = Calendar.getInstance(); cal.setTime(context.getNow()); cal.add(Calendar.MONTH, -(onOrAfter)); Date effectiveDate = cal.getTime(); Set<Integer> female = Filters.female(cohort, context); Form deliveryForm = MetadataUtils.existing(Form.class,MchMetadata._Form.MCHMS_DELIVERY); CalculationResultMap allEncountersForMCHConsultation = Calculations.allEncountersOnOrAfter(MetadataUtils.existing(EncounterType.class, MchMetadata._EncounterType.MCHMS_CONSULTATION), effectiveDate, female, context); Set<Integer> deliveries = allEncountersForMCHConsultation.keySet(); CalculationResultMap ret = new CalculationResultMap(); ObsService obsService = Context.getObsService(); PersonService personService = Context.getPersonService(); for (Integer ptId : cohort) { boolean result = false; if (deliveries != null && deliveries.contains(ptId)){ ListResult mchcsEncountersResult = (ListResult) allEncountersForMCHConsultation.get(ptId); List<Encounter> encounters = CalculationUtils.extractResultValues(mchcsEncountersResult); for(Encounter e: encounters){ if(deliveryForm.getUuid().equals(e.getForm().getUuid())){ List<Obs> deliveryObs = obsService.getObservations(Arrays.asList(personService.getPerson(ptId)), Arrays.asList(e), null, null, null, null, null, null, null, null, null, false); result = isCompletePartograph(conceptList, deliveryObs); break; } } } ret.put(ptId, new BooleanResult(result, this)); } return ret; }  @Override CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context);  }
@Test public void valuate_shouldCalculateDeliveriesWithFullPartographs() throws Exception { Form deliveryForm = MetadataUtils.existing(Form.class, MchMetadata._Form.MCHMS_DELIVERY); EncounterType ancEncounterType = MetadataUtils.existing(EncounterType.class, MchMetadata._EncounterType.MCHMS_CONSULTATION); { Patient patient = TestUtils.getPatient(7); Date encounterDate = TestUtils.date(2014, 7, 20); Obs[] deliveryObs = { TestUtils.saveObs(patient, Dictionary.getConcept(Metadata.Concept.PREGNANCY_DURATION_AMOUNT), 6, encounterDate), TestUtils.saveObs(patient, Dictionary.getConcept(Metadata.Concept.METHOD_OF_DELIVERY), Dictionary.getConcept(Metadata.Concept.OTHER_NON_CODED) , encounterDate), TestUtils.saveObs(patient, Dictionary.getConcept(Metadata.Concept.DATE_OF_CONFINEMENT), encounterDate , encounterDate), TestUtils.saveObs(patient, Dictionary.getConcept(Metadata.Concept.ESTIMATED_MATERNAL_BLOOD_LOSS_QUALITATIVE), Dictionary.getConcept(Metadata.Concept.NONE), encounterDate), saveObs(patient, Dictionary.getConcept(Metadata.Concept.MATERNAL_CONDITION_DURING_PUERPERIUM), "OK", encounterDate), TestUtils.saveObs(patient, Dictionary.getConcept(Metadata.Concept.APGAR_SCORE_AT_1_MINUTE), 5, encounterDate), TestUtils.saveObs(patient, Dictionary.getConcept(Metadata.Concept.APGAR_SCORE_AT_5_MINUTES), 5, encounterDate), TestUtils.saveObs(patient, Dictionary.getConcept(Metadata.Concept.APGAR_SCORE_AT_10_MINUTES), 5, encounterDate) }; TestUtils.saveEncounter(patient, ancEncounterType, deliveryForm, TestUtils.date(2014, 7, 20), deliveryObs); } { Patient patient = TestUtils.getPatient(8); Date encounterDate = TestUtils.date(2014, 7, 20); Obs[] deliveryObs = { TestUtils.saveObs(patient, Dictionary.getConcept(Metadata.Concept.PREGNANCY_DURATION_AMOUNT), 6, encounterDate), TestUtils.saveObs(patient, Dictionary.getConcept(Metadata.Concept.METHOD_OF_DELIVERY), Dictionary.getConcept(Metadata.Concept.OTHER_NON_CODED) , encounterDate), TestUtils.saveObs(patient, Dictionary.getConcept(Metadata.Concept.DATE_OF_CONFINEMENT), encounterDate , encounterDate), TestUtils.saveObs(patient, Dictionary.getConcept(Metadata.Concept.ESTIMATED_MATERNAL_BLOOD_LOSS_QUALITATIVE), Dictionary.getConcept(Metadata.Concept.NONE), encounterDate), saveObs(patient, Dictionary.getConcept(Metadata.Concept.MATERNAL_CONDITION_DURING_PUERPERIUM), "OK", encounterDate), TestUtils.saveObs(patient, Dictionary.getConcept(Metadata.Concept.APGAR_SCORE_AT_1_MINUTE), 5, encounterDate), TestUtils.saveObs(patient, Dictionary.getConcept(Metadata.Concept.APGAR_SCORE_AT_5_MINUTES), 5, encounterDate), TestUtils.saveObs(patient, Dictionary.getConcept(Metadata.Concept.APGAR_SCORE_AT_10_MINUTES), 5, encounterDate), TestUtils.saveObs(patient, Dictionary.getConcept(Metadata.Concept.NEONTAL_RESUSCITATION), Dictionary.getConcept(Metadata.Concept.YES), encounterDate), TestUtils.saveObs(patient, Dictionary.getConcept(Metadata.Concept.LOCATION_OF_BIRTH), Dictionary.getConcept(Metadata.Concept.UNKNOWN), encounterDate), TestUtils.saveObs(patient, Dictionary.getConcept(Metadata.Concept.DELIVERY_ASSISTANT), Dictionary.getConcept(Metadata.Concept.FAMILY_MEMBER), encounterDate) }; TestUtils.saveEncounter(patient, ancEncounterType, deliveryForm, TestUtils.date(2014, 7, 20), deliveryObs); } List<Integer> ptIds = Arrays.asList(7, 8, 999); Map<String, Object> params = new HashMap<String, Object>(); params.put("onOrAfter", 4); CalculationResultMap resultMap = new DeliveriesWithFullPartographsCalculation().evaluate(ptIds, params, Context.getService(PatientCalculationService.class).createCalculationContext()); Assert.assertFalse((Boolean) resultMap.get(7).getValue()); Assert.assertFalse((Boolean) resultMap.get(999).getValue()); }