Source:
public List<RuleResult<Patient>> patientsWithMissingTBNumbers(String tbIdentifierConceptUuid, String identifierTitle) { log.info("Executing rule to find patients with missing " + identifierTitle + " TB identifiers"); String queryString = "SELECT pp.patient FROM PatientProgram pp WHERE pp.voided = 0 AND pp.program.uuid = :tbProgramUuid GROUP BY pp.patient.patientId"; Query query = getSession().createQuery(queryString); query.setParameter("tbProgramUuid", Programs.TB_PROGRAM.uuid()); List<Patient> patientList = query.list(); log.info("There are " + patientList.size() + " Patients Enrolled in TB program"); List<RuleResult<Patient>> ruleResults = new ArrayList<>(); Program tbProgram = Context.getProgramWorkflowService().getProgramByUuid(Programs.TB_PROGRAM.uuid()); for (Patient patient : patientList) { String encounterQueryString = "FROM Encounter e WHERE e.patient.id = :patientId AND e.encounterType.uuid = :encounterType AND e.id NOT IN (SELECT o.encounter.id FROM Obs o WHERE o.concept.uuid = :conceptUuid)"; Query encounterQuery = getSession().createQuery(encounterQueryString); encounterQuery.setParameter("patientId", patient.getId()); encounterQuery.setParameter("encounterType", EncounterTypes.TB_SUMMARY_ENCOUNTER.uuid()); encounterQuery.setParameter("conceptUuid", tbIdentifierConceptUuid); List<Encounter> patientEncounters = encounterQuery.list(); for (Encounter encounter : patientEncounters) { RuleResult<Patient> ruleResult = new RuleResult<>(); ruleResult.setActionUrl("htmlformentryui/htmlform/editHtmlFormWithStandardUi.page?patientId=" + patient.getUuid() + "&encounterId=" + encounter.getId()); ruleResult.setNotes("Client# " + getTbNumber(patient, encounter) + ", has missing " + identifierTitle + " identifier"); ruleResult.setEntity(patient); ruleResults.add(ruleResult); } } return ruleResults; }

Target:
public List<RuleResult<Patient>> patientsWithMissingTBNumbers(String tbIdentifierConceptUuid, String identifierTitle) { log.info("Executing rule to find patients with missing " + identifierTitle + " TB identifiers"); String queryString = "SELECT pp.patient FROM PatientProgram pp WHERE pp.voided = 0 AND pp.program.uuid = :tbProgramUuid GROUP BY pp.patient.patientId"; Query query = getSession().createQuery(queryString); query.setParameter("tbProgramUuid", Programs.TB_PROGRAM.uuid()); List<Patient> patientList = query.list(); log.info("There are " + patientList.size() + " Patients Enrolled in TB program"); List<RuleResult<Patient>> ruleResults = new ArrayList<>(); Program tbProgram = Context.getProgramWorkflowService().getProgramByUuid(Programs.TB_PROGRAM.uuid()); for (Patient patient : patientList) { String encounterQueryString = "FROM Encounter e WHERE e.patient.id = :patientId AND e.encounterType.uuid = :encounterType AND e.id NOT IN (SELECT o.encounter.id FROM Obs o WHERE o.concept.uuid = :conceptUuid)"; Query encounterQuery = getSession().createQuery(encounterQueryString); encounterQuery.setParameter("patientId", patient.getId()); encounterQuery.setParameter("encounterType", EncounterTypes.TB_SUMMARY_ENCOUNTER.uuid()); encounterQuery.setParameter("conceptUuid", tbIdentifierConceptUuid); List<Encounter> patientEncounters = encounterQuery.list(); for (Encounter encounter : patientEncounters) { RuleResult<Patient> ruleResult = new RuleResult<>(); ruleResult.setActionUrl("htmlformentryui/htmlform/editHtmlFormWithStandardUi.page?patientId=" + patient.getUuid() + "&encounterId=" + encounter.getId()); ruleResult.setNotes("Patient #" + getTbNumber(patient, encounter, null) + " has a missing " + identifierTitle ); ruleResult.setEntity(patient); ruleResults.add(ruleResult); } } return ruleResults; }