[507] | 1 | /*
|
---|
| 2 | * To change this template, choose Tools | Templates
|
---|
| 3 | * and open the template in the editor.
|
---|
| 4 | */
|
---|
| 5 | package gov.hhs.fha.nhinc.patientcorrelation.dao;
|
---|
| 6 |
|
---|
| 7 | import gov.hhs.fha.nhinc.nhinclib.NullChecker;
|
---|
| 8 | import gov.hhs.fha.nhinc.patientcorrelation.model.CorrelatedIdentifiers;
|
---|
| 9 | import gov.hhs.fha.nhinc.patientcorrelation.model.QualifiedPatientIdentifier;
|
---|
| 10 | import gov.hhs.fha.nhinc.patientcorrelation.persistence.HibernateUtil;
|
---|
| 11 | import java.util.ArrayList;
|
---|
| 12 | import java.util.List;
|
---|
| 13 | import org.apache.commons.logging.Log;
|
---|
| 14 | import org.apache.commons.logging.LogFactory;
|
---|
| 15 | import org.hibernate.Criteria;
|
---|
| 16 | import org.hibernate.Session;
|
---|
| 17 | import org.hibernate.SessionFactory;
|
---|
| 18 | import org.hibernate.criterion.Expression;
|
---|
| 19 |
|
---|
| 20 | /**
|
---|
| 21 | *
|
---|
| 22 | * @author rayj
|
---|
| 23 | */
|
---|
| 24 | public class Retriever {
|
---|
| 25 |
|
---|
| 26 | static Log log = LogFactory.getLog(Retriever.class);
|
---|
| 27 |
|
---|
| 28 | public static List<QualifiedPatientIdentifier> retrievePatientCorrelation(QualifiedPatientIdentifier qualifiedPatientIdentifier, List<String> includeOnlyAssigningAuthorities) {
|
---|
| 29 | List<QualifiedPatientIdentifier> qualifiedPatientIdentifiers = retrievePatientCorrelation(qualifiedPatientIdentifier);
|
---|
| 30 | log.info("unfiltered list = " + qualifiedPatientIdentifiers.size() + " record(s)");
|
---|
| 31 | qualifiedPatientIdentifiers = filterByIncludeList(qualifiedPatientIdentifiers, includeOnlyAssigningAuthorities);
|
---|
| 32 | log.info("filtered list = " + qualifiedPatientIdentifiers.size() + " record(s)");
|
---|
| 33 | return qualifiedPatientIdentifiers;
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | private static List<QualifiedPatientIdentifier> filterByIncludeList(List<QualifiedPatientIdentifier> qualifiedPatientIdentifiers, List<String> includeOnlyAssigningAuthorities) {
|
---|
| 37 | List<QualifiedPatientIdentifier> filteredQualifiedPatientIdentifiers;
|
---|
| 38 |
|
---|
| 39 | if (NullChecker.isNotNullish(includeOnlyAssigningAuthorities)) {
|
---|
| 40 | filteredQualifiedPatientIdentifiers = new ArrayList<QualifiedPatientIdentifier>();
|
---|
| 41 | for (QualifiedPatientIdentifier qualifiedPatientIdentifier : qualifiedPatientIdentifiers) {
|
---|
| 42 | if (isAssigningAuthorityInList(qualifiedPatientIdentifier, includeOnlyAssigningAuthorities)) {
|
---|
| 43 | filteredQualifiedPatientIdentifiers.add(qualifiedPatientIdentifier);
|
---|
| 44 | }
|
---|
| 45 | }
|
---|
| 46 | } else {
|
---|
| 47 | filteredQualifiedPatientIdentifiers = qualifiedPatientIdentifiers;
|
---|
| 48 | }
|
---|
| 49 | return filteredQualifiedPatientIdentifiers;
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | private static boolean isAssigningAuthorityInList(QualifiedPatientIdentifier qualifiedPatientIdentifier, List<String> assigningAuthorities) {
|
---|
| 53 | boolean found = false;
|
---|
| 54 | for (String assigningAuthority : assigningAuthorities) {
|
---|
| 55 | if (qualifiedPatientIdentifier.getAssigningAuthorityId().contentEquals(assigningAuthority)) {
|
---|
| 56 | found = true;
|
---|
| 57 | break;
|
---|
| 58 | }
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | return found;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | public static List<QualifiedPatientIdentifier> retrievePatientCorrelation(QualifiedPatientIdentifier qualifiedPatientIdentifier) {
|
---|
| 65 | log.debug("-- Begin CorrelatedIdentifiersDao.retrieveAllPatientCorrelation() ---");
|
---|
| 66 |
|
---|
| 67 | if (qualifiedPatientIdentifier == null) {
|
---|
| 68 | throw new IllegalArgumentException("Missing required parameter: qualifiedPatientIdentifier");
|
---|
| 69 | } else if (NullChecker.isNullish(qualifiedPatientIdentifier.getAssigningAuthorityId())) {
|
---|
| 70 | throw new IllegalArgumentException("Missing required parameter: qualifiedPatientIdentifier.getAssigningAuthorityId");
|
---|
| 71 | } else if (NullChecker.isNullish(qualifiedPatientIdentifier.getPatientId())) {
|
---|
| 72 | throw new IllegalArgumentException("Missing required parameter: qualifiedPatientIdentifier.getPatientId");
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | CorrelatedIdentifiers criteria;
|
---|
| 76 |
|
---|
| 77 | criteria =
|
---|
| 78 | new CorrelatedIdentifiers();
|
---|
| 79 | criteria.setPatientId(qualifiedPatientIdentifier.getPatientId());
|
---|
| 80 | criteria.setPatientAssigningAuthorityId(qualifiedPatientIdentifier.getAssigningAuthorityId());
|
---|
| 81 | List<CorrelatedIdentifiers> result1 = retrievePatientCorrelation(criteria);
|
---|
| 82 |
|
---|
| 83 | criteria =
|
---|
| 84 | new CorrelatedIdentifiers();
|
---|
| 85 | criteria.setCorrelatedPatientId(qualifiedPatientIdentifier.getPatientId());
|
---|
| 86 | criteria.setCorrelatedPatientAssigningAuthorityId(qualifiedPatientIdentifier.getAssigningAuthorityId());
|
---|
| 87 | List<CorrelatedIdentifiers> result2 = retrievePatientCorrelation(criteria);
|
---|
| 88 |
|
---|
| 89 | List<CorrelatedIdentifiers> existingCorrelatedIdentifiers = unionList(result1, result2);
|
---|
| 90 |
|
---|
| 91 | List<QualifiedPatientIdentifier> resultQualifiedPatientIdentifiers = new ArrayList<QualifiedPatientIdentifier>();
|
---|
| 92 | for (CorrelatedIdentifiers correlatedIdentifiers : existingCorrelatedIdentifiers) {
|
---|
| 93 | QualifiedPatientIdentifier resultQualifiedPatientIdentifier;
|
---|
| 94 |
|
---|
| 95 | resultQualifiedPatientIdentifier =
|
---|
| 96 | new QualifiedPatientIdentifier();
|
---|
| 97 | resultQualifiedPatientIdentifier.setAssigningAuthority(correlatedIdentifiers.getPatientAssigningAuthorityId());
|
---|
| 98 | resultQualifiedPatientIdentifier.setPatientId(correlatedIdentifiers.getPatientId());
|
---|
| 99 | if (!AreSame(qualifiedPatientIdentifier, resultQualifiedPatientIdentifier)) {
|
---|
| 100 | resultQualifiedPatientIdentifiers.add(resultQualifiedPatientIdentifier);
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | resultQualifiedPatientIdentifier = new QualifiedPatientIdentifier();
|
---|
| 104 | resultQualifiedPatientIdentifier.setAssigningAuthority(correlatedIdentifiers.getCorrelatedPatientAssigningAuthorityId());
|
---|
| 105 | resultQualifiedPatientIdentifier.setPatientId(correlatedIdentifiers.getCorrelatedPatientId());
|
---|
| 106 | if (!AreSame(qualifiedPatientIdentifier, resultQualifiedPatientIdentifier)) {
|
---|
| 107 | resultQualifiedPatientIdentifiers.add(resultQualifiedPatientIdentifier);
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | log.info("resultQualifiedPatientIdentifiers=" + resultQualifiedPatientIdentifiers.size() + " record(s)");
|
---|
| 113 | log.debug("-- End CorrelatedIdentifiersDao.retrieveAllPatientCorrelation() ---");
|
---|
| 114 | return resultQualifiedPatientIdentifiers;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | private static boolean AreSame(QualifiedPatientIdentifier a, QualifiedPatientIdentifier b) {
|
---|
| 118 | return ((a.getAssigningAuthorityId().contentEquals(b.getAssigningAuthorityId())) && (a.getPatientId().contentEquals(b.getPatientId())));
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | private static List<CorrelatedIdentifiers> unionList(List<CorrelatedIdentifiers> list1, List<CorrelatedIdentifiers> list2) {
|
---|
| 122 | if (list1 == null) {
|
---|
| 123 | list1 = new ArrayList<CorrelatedIdentifiers>();
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | for (CorrelatedIdentifiers correlatedIdentifiers : list2) {
|
---|
| 127 | list1.add(correlatedIdentifiers);
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | return list1;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | public static boolean doesCorrelationExist(CorrelatedIdentifiers correlatedIdentifers) {
|
---|
| 134 | boolean exists = false;
|
---|
| 135 |
|
---|
| 136 | CorrelatedIdentifiers criteria;
|
---|
| 137 |
|
---|
| 138 | List<CorrelatedIdentifiers> existingCorrelations;
|
---|
| 139 | criteria =
|
---|
| 140 | correlatedIdentifers;
|
---|
| 141 | existingCorrelations =
|
---|
| 142 | retrievePatientCorrelation(criteria);
|
---|
| 143 | exists =
|
---|
| 144 | NullChecker.isNotNullish(existingCorrelations);
|
---|
| 145 |
|
---|
| 146 | if (!exists) {
|
---|
| 147 | criteria = new CorrelatedIdentifiers();
|
---|
| 148 | criteria.setPatientId(correlatedIdentifers.getCorrelatedPatientId());
|
---|
| 149 | criteria.setPatientAssigningAuthorityId(correlatedIdentifers.getCorrelatedPatientAssigningAuthorityId());
|
---|
| 150 | criteria.setCorrelatedPatientId(correlatedIdentifers.getPatientId());
|
---|
| 151 | criteria.setCorrelatedPatientAssigningAuthorityId(correlatedIdentifers.getPatientAssigningAuthorityId());
|
---|
| 152 |
|
---|
| 153 | existingCorrelations =
|
---|
| 154 | retrievePatientCorrelation(criteria);
|
---|
| 155 | exists =
|
---|
| 156 | NullChecker.isNotNullish(existingCorrelations);
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | log.debug("correlation exists? = " + exists);
|
---|
| 160 | return exists;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | private static List<CorrelatedIdentifiers> retrievePatientCorrelation(CorrelatedIdentifiers correlatedIdentifers) {
|
---|
| 164 | SessionFactory fact = null;
|
---|
| 165 | Session sess = null;
|
---|
| 166 | List<CorrelatedIdentifiers> result = null;
|
---|
| 167 |
|
---|
| 168 | try {
|
---|
| 169 | fact = HibernateUtil.getSessionFactory();
|
---|
| 170 | sess =
|
---|
| 171 | fact.openSession();
|
---|
| 172 |
|
---|
| 173 | Criteria criteria;
|
---|
| 174 |
|
---|
| 175 | criteria =
|
---|
| 176 | sess.createCriteria(CorrelatedIdentifiers.class);
|
---|
| 177 |
|
---|
| 178 |
|
---|
| 179 |
|
---|
| 180 |
|
---|
| 181 | if (NullChecker.isNotNullish(correlatedIdentifers.getPatientAssigningAuthorityId())) {
|
---|
| 182 | log.debug("Retrieving by patientAssigningAuthorityId=" + correlatedIdentifers.getPatientAssigningAuthorityId());
|
---|
| 183 | criteria.add(Expression.eq("patientAssigningAuthorityId", correlatedIdentifers.getPatientAssigningAuthorityId()));
|
---|
| 184 | }
|
---|
| 185 | if (NullChecker.isNotNullish(correlatedIdentifers.getPatientId())) {
|
---|
| 186 | log.debug("Retrieving by patientId=" + correlatedIdentifers.getPatientId());
|
---|
| 187 | criteria.add(Expression.eq("patientId", correlatedIdentifers.getPatientId()));
|
---|
| 188 | }
|
---|
| 189 | if (NullChecker.isNotNullish(correlatedIdentifers.getCorrelatedPatientAssigningAuthorityId())) {
|
---|
| 190 | log.debug("Retrieving by correlatedPatientAssigningAuthorityId=" + correlatedIdentifers.getCorrelatedPatientAssigningAuthorityId());
|
---|
| 191 | criteria.add(Expression.eq("correlatedPatientAssigningAuthorityId", correlatedIdentifers.getCorrelatedPatientAssigningAuthorityId()));
|
---|
| 192 | }
|
---|
| 193 | if (NullChecker.isNotNullish(correlatedIdentifers.getCorrelatedPatientId())) {
|
---|
| 194 | log.debug("Retrieving by correlatedPatientId=" + correlatedIdentifers.getCorrelatedPatientId());
|
---|
| 195 | criteria.add(Expression.eq("correlatedPatientId", correlatedIdentifers.getCorrelatedPatientId()));
|
---|
| 196 | }
|
---|
| 197 | result = criteria.list();
|
---|
| 198 |
|
---|
| 199 | log.debug("Found " + result.size() + " record(s)");
|
---|
| 200 | } finally {
|
---|
| 201 | if (sess != null) {
|
---|
| 202 | try {
|
---|
| 203 | sess.close();
|
---|
| 204 | } catch (Throwable t) {
|
---|
| 205 | log.error("Failed to close session: " + t.getMessage(), t);
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | }
|
---|
| 209 | }
|
---|
| 210 | return result;
|
---|
| 211 | }
|
---|
| 212 | }
|
---|