Source:
public List<RuleResult<Patient>> multiplePatientsWithTheSameTBIdentifiers(String identifierConceptUuid, String identifierTitle) { log.info("Executing rule to find Patients with similar TB identifiers: duplicated across multiple patients"); 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.valueText IN " + " (SELECT ob.valueText FROM Obs ob WHERE ob.concept.uuid = :identifierConceptUuid AND ob.voided = 0 GROUP BY ob.concept.conceptId, 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>> multiplePatientsWithTheSameTBIdentifiers(String identifierConceptUuid, String identifierTitle) { log.info("Executing rule to find Patients with similar TB identifiers: duplicated across multiple patients"); String queryString = "SELECT obs FROM Obs obs WHERE obs.obsId IN (" + "SELECT min(o.obsId) 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.valueText IN " + " (SELECT ob.valueText FROM Obs ob WHERE ob.concept.uuid = :identifierConceptUuid AND ob.voided = 0 GROUP BY ob.concept.conceptId, ob.valueText HAVING COUNT(ob.valueText) > 1)" + " GROUP BY o.person.personId, o.concept.id, o.valueText HAVING count(*) = 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 being used by another patient"); ruleResult.setEntity(patient); ruleResults.add(ruleResult); } uniquePatientList.add(patient); } log.info("There are " + uniquePatientList.size() + " Patients with similar " + identifierTitle); return ruleResults; }