Source:
public List<RuleResult<Patient>> singlePatientWithDuplicateTBNumberAcrossMultipleTreatmentPrograms(String identifierConceptUuid, String identifierTitle) { log.info("Executing rule to find Patients with similar TB identifiers: duplicated for a single patient"); String queryString = "SELECT o FROM Obs o WHERE o.voided = false AND o.encounter.patient.dead = 0 AND o.encounter.voided = 0 AND o.concept.uuid = :identifierConceptUuid AND o.obsId IN " + " (SELECT min(ob.obsId) FROM Obs ob WHERE ob.concept.uuid = :identifierConceptUuid AND ob.voided = 0 GROUP BY ob.concept.id, ob.valueText HAVING COUNT(ob.valueText) > 1)"; Query query = getSession().createQuery(queryString); query.setParameter("identifierConceptUuid", identifierConceptUuid); List<Obs> obsList = query.list(); Set<Patient> uniquePatientList = new HashSet<Patient>(); List<RuleResult<Patient>> ruleResults = new ArrayList<>(); for (Obs obs : obsList) { Patient patient = obs.getEncounter().getPatient(); RuleResult<Patient> ruleResult = new RuleResult<>(); ruleResult.setActionUrl("coreapps/patientdashboard/patientDashboard.page?patientId=" + patient.getUuid()); ruleResult.setNotes("Patient #" + getTbNumber(patient, obs.getEncounter()) + " with similar " + identifierTitle + " identifiers(" + obs.getValueText() + "): duplicated for a single patient,"); ruleResult.setEntity(patient); ruleResults.add(ruleResult); uniquePatientList.add(patient); } log.info("There are " + uniquePatientList.size() + " Patients with similar " + identifierTitle + " identifiers: duplicated for a single patient,"); return ruleResults; }

Target:
public List<RuleResult<Patient>> singlePatientWithDuplicateTBNumberAcrossMultipleTreatmentPrograms(String identifierConceptUuid, String identifierTitle) { log.info("Executing rule to find Patients with similar TB identifiers: duplicated for a single patient"); String queryString = "SELECT o FROM Obs o WHERE o.voided = false AND o.encounter.patient.dead = 0 AND o.encounter.voided = 0 AND o.concept.uuid = :identifierConceptUuid AND o.obsId IN " + " (SELECT min(ob.obsId) FROM Obs ob WHERE ob.concept.uuid = :identifierConceptUuid AND ob.voided = 0 GROUP BY ob.person.personId, ob.concept.id, ob.valueText HAVING COUNT(ob.valueText) > 1)"; Query query = getSession().createQuery(queryString); query.setParameter("identifierConceptUuid", identifierConceptUuid); List<Obs> obsList = query.list(); Set<Patient> uniquePatientList = new HashSet<Patient>(); List<RuleResult<Patient>> ruleResults = new ArrayList<>(); for (Obs obs : obsList) { Patient patient = obs.getEncounter().getPatient(); RuleResult<Patient> ruleResult = new RuleResult<>(); if (uniquePatientList.contains(patient) == false) { ruleResult.setActionUrl("htmlformentryui/htmlform/editHtmlFormWithStandardUi.page?patientId=" + patient.getUuid() + "&encounterId=" + obs.getEncounter().getId()); ruleResult.setNotes("The " + identifierTitle + " " + getTbNumber(patient, obs.getEncounter(), identifierConceptUuid) + " is used by the same patient across multiple treatment programs"); ruleResult.setEntity(patient); ruleResults.add(ruleResult); } uniquePatientList.add(patient); } log.info("There are " + uniquePatientList.size() + " Sharing the same " + identifierTitle); return ruleResults; }