/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package gov.hhs.fha.nhinc.patientcorrelation.dao; import gov.hhs.fha.nhinc.nhinclib.NullChecker; import gov.hhs.fha.nhinc.patientcorrelation.model.CorrelatedIdentifiers; import gov.hhs.fha.nhinc.patientcorrelation.model.QualifiedPatientIdentifier; import gov.hhs.fha.nhinc.patientcorrelation.persistence.HibernateUtil; import java.util.ArrayList; import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.hibernate.Criteria; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.criterion.Expression; /** * * @author rayj */ public class Retriever { static Log log = LogFactory.getLog(Retriever.class); public static List retrievePatientCorrelation(QualifiedPatientIdentifier qualifiedPatientIdentifier, List includeOnlyAssigningAuthorities) { List qualifiedPatientIdentifiers = retrievePatientCorrelation(qualifiedPatientIdentifier); log.info("unfiltered list = " + qualifiedPatientIdentifiers.size() + " record(s)"); qualifiedPatientIdentifiers = filterByIncludeList(qualifiedPatientIdentifiers, includeOnlyAssigningAuthorities); log.info("filtered list = " + qualifiedPatientIdentifiers.size() + " record(s)"); return qualifiedPatientIdentifiers; } private static List filterByIncludeList(List qualifiedPatientIdentifiers, List includeOnlyAssigningAuthorities) { List filteredQualifiedPatientIdentifiers; if (NullChecker.isNotNullish(includeOnlyAssigningAuthorities)) { filteredQualifiedPatientIdentifiers = new ArrayList(); for (QualifiedPatientIdentifier qualifiedPatientIdentifier : qualifiedPatientIdentifiers) { if (isAssigningAuthorityInList(qualifiedPatientIdentifier, includeOnlyAssigningAuthorities)) { filteredQualifiedPatientIdentifiers.add(qualifiedPatientIdentifier); } } } else { filteredQualifiedPatientIdentifiers = qualifiedPatientIdentifiers; } return filteredQualifiedPatientIdentifiers; } private static boolean isAssigningAuthorityInList(QualifiedPatientIdentifier qualifiedPatientIdentifier, List assigningAuthorities) { boolean found = false; for (String assigningAuthority : assigningAuthorities) { if (qualifiedPatientIdentifier.getAssigningAuthorityId().contentEquals(assigningAuthority)) { found = true; break; } } return found; } public static List retrievePatientCorrelation(QualifiedPatientIdentifier qualifiedPatientIdentifier) { log.debug("-- Begin CorrelatedIdentifiersDao.retrieveAllPatientCorrelation() ---"); if (qualifiedPatientIdentifier == null) { throw new IllegalArgumentException("Missing required parameter: qualifiedPatientIdentifier"); } else if (NullChecker.isNullish(qualifiedPatientIdentifier.getAssigningAuthorityId())) { throw new IllegalArgumentException("Missing required parameter: qualifiedPatientIdentifier.getAssigningAuthorityId"); } else if (NullChecker.isNullish(qualifiedPatientIdentifier.getPatientId())) { throw new IllegalArgumentException("Missing required parameter: qualifiedPatientIdentifier.getPatientId"); } CorrelatedIdentifiers criteria; criteria = new CorrelatedIdentifiers(); criteria.setPatientId(qualifiedPatientIdentifier.getPatientId()); criteria.setPatientAssigningAuthorityId(qualifiedPatientIdentifier.getAssigningAuthorityId()); List result1 = retrievePatientCorrelation(criteria); criteria = new CorrelatedIdentifiers(); criteria.setCorrelatedPatientId(qualifiedPatientIdentifier.getPatientId()); criteria.setCorrelatedPatientAssigningAuthorityId(qualifiedPatientIdentifier.getAssigningAuthorityId()); List result2 = retrievePatientCorrelation(criteria); List existingCorrelatedIdentifiers = unionList(result1, result2); List resultQualifiedPatientIdentifiers = new ArrayList(); for (CorrelatedIdentifiers correlatedIdentifiers : existingCorrelatedIdentifiers) { QualifiedPatientIdentifier resultQualifiedPatientIdentifier; resultQualifiedPatientIdentifier = new QualifiedPatientIdentifier(); resultQualifiedPatientIdentifier.setAssigningAuthority(correlatedIdentifiers.getPatientAssigningAuthorityId()); resultQualifiedPatientIdentifier.setPatientId(correlatedIdentifiers.getPatientId()); if (!AreSame(qualifiedPatientIdentifier, resultQualifiedPatientIdentifier)) { resultQualifiedPatientIdentifiers.add(resultQualifiedPatientIdentifier); } resultQualifiedPatientIdentifier = new QualifiedPatientIdentifier(); resultQualifiedPatientIdentifier.setAssigningAuthority(correlatedIdentifiers.getCorrelatedPatientAssigningAuthorityId()); resultQualifiedPatientIdentifier.setPatientId(correlatedIdentifiers.getCorrelatedPatientId()); if (!AreSame(qualifiedPatientIdentifier, resultQualifiedPatientIdentifier)) { resultQualifiedPatientIdentifiers.add(resultQualifiedPatientIdentifier); } } log.info("resultQualifiedPatientIdentifiers=" + resultQualifiedPatientIdentifiers.size() + " record(s)"); log.debug("-- End CorrelatedIdentifiersDao.retrieveAllPatientCorrelation() ---"); return resultQualifiedPatientIdentifiers; } private static boolean AreSame(QualifiedPatientIdentifier a, QualifiedPatientIdentifier b) { return ((a.getAssigningAuthorityId().contentEquals(b.getAssigningAuthorityId())) && (a.getPatientId().contentEquals(b.getPatientId()))); } private static List unionList(List list1, List list2) { if (list1 == null) { list1 = new ArrayList(); } for (CorrelatedIdentifiers correlatedIdentifiers : list2) { list1.add(correlatedIdentifiers); } return list1; } public static boolean doesCorrelationExist(CorrelatedIdentifiers correlatedIdentifers) { boolean exists = false; CorrelatedIdentifiers criteria; List existingCorrelations; criteria = correlatedIdentifers; existingCorrelations = retrievePatientCorrelation(criteria); exists = NullChecker.isNotNullish(existingCorrelations); if (!exists) { criteria = new CorrelatedIdentifiers(); criteria.setPatientId(correlatedIdentifers.getCorrelatedPatientId()); criteria.setPatientAssigningAuthorityId(correlatedIdentifers.getCorrelatedPatientAssigningAuthorityId()); criteria.setCorrelatedPatientId(correlatedIdentifers.getPatientId()); criteria.setCorrelatedPatientAssigningAuthorityId(correlatedIdentifers.getPatientAssigningAuthorityId()); existingCorrelations = retrievePatientCorrelation(criteria); exists = NullChecker.isNotNullish(existingCorrelations); } log.debug("correlation exists? = " + exists); return exists; } private static List retrievePatientCorrelation(CorrelatedIdentifiers correlatedIdentifers) { SessionFactory fact = null; Session sess = null; List result = null; try { fact = HibernateUtil.getSessionFactory(); sess = fact.openSession(); Criteria criteria; criteria = sess.createCriteria(CorrelatedIdentifiers.class); if (NullChecker.isNotNullish(correlatedIdentifers.getPatientAssigningAuthorityId())) { log.debug("Retrieving by patientAssigningAuthorityId=" + correlatedIdentifers.getPatientAssigningAuthorityId()); criteria.add(Expression.eq("patientAssigningAuthorityId", correlatedIdentifers.getPatientAssigningAuthorityId())); } if (NullChecker.isNotNullish(correlatedIdentifers.getPatientId())) { log.debug("Retrieving by patientId=" + correlatedIdentifers.getPatientId()); criteria.add(Expression.eq("patientId", correlatedIdentifers.getPatientId())); } if (NullChecker.isNotNullish(correlatedIdentifers.getCorrelatedPatientAssigningAuthorityId())) { log.debug("Retrieving by correlatedPatientAssigningAuthorityId=" + correlatedIdentifers.getCorrelatedPatientAssigningAuthorityId()); criteria.add(Expression.eq("correlatedPatientAssigningAuthorityId", correlatedIdentifers.getCorrelatedPatientAssigningAuthorityId())); } if (NullChecker.isNotNullish(correlatedIdentifers.getCorrelatedPatientId())) { log.debug("Retrieving by correlatedPatientId=" + correlatedIdentifers.getCorrelatedPatientId()); criteria.add(Expression.eq("correlatedPatientId", correlatedIdentifers.getCorrelatedPatientId())); } result = criteria.list(); log.debug("Found " + result.size() + " record(s)"); } finally { if (sess != null) { try { sess.close(); } catch (Throwable t) { log.error("Failed to close session: " + t.getMessage(), t); } } } return result; } }