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.patientcorrelation.model.CorrelatedIdentifiers;
|
---|
8 | import gov.hhs.fha.nhinc.patientcorrelation.model.QualifiedPatientIdentifier;
|
---|
9 | import gov.hhs.fha.nhinc.patientcorrelation.persistence.HibernateUtil;
|
---|
10 | import java.util.List;
|
---|
11 | import org.apache.commons.logging.Log;
|
---|
12 | import org.apache.commons.logging.LogFactory;
|
---|
13 | import org.hibernate.Criteria;
|
---|
14 | import org.hibernate.HibernateException;
|
---|
15 | import org.hibernate.Session;
|
---|
16 | import org.hibernate.SessionFactory;
|
---|
17 | import org.hibernate.Transaction;
|
---|
18 | import org.hibernate.criterion.Expression;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | *
|
---|
22 | * @author rayj
|
---|
23 | */
|
---|
24 | public class Storer {
|
---|
25 |
|
---|
26 | static Log log = LogFactory.getLog(Storer.class);
|
---|
27 |
|
---|
28 | public static void addPatientCorrelation(CorrelatedIdentifiers correlatedIdentifers) {
|
---|
29 | log.info("patient correlation add requested");
|
---|
30 | if (!Retriever.doesCorrelationExist(correlatedIdentifers)) {
|
---|
31 | localAddPatientCorrelation(correlatedIdentifers);
|
---|
32 | } else {
|
---|
33 | log.info("Correlation already exists, no store needed");
|
---|
34 | }
|
---|
35 | }
|
---|
36 |
|
---|
37 | private static void localAddPatientCorrelation(CorrelatedIdentifiers correlatedIdentifers) {
|
---|
38 | log.debug("-- Begin CorrelatedIdentifiersDao.addPatientCorrelation() ---");
|
---|
39 | Session sess = null;
|
---|
40 | Transaction trans = null;
|
---|
41 |
|
---|
42 | try {
|
---|
43 | SessionFactory fact = HibernateUtil.getSessionFactory();
|
---|
44 | if (fact != null) {
|
---|
45 | sess = fact.openSession();
|
---|
46 | trans = sess.beginTransaction();
|
---|
47 | sess.saveOrUpdate(correlatedIdentifers);
|
---|
48 | } else {
|
---|
49 | log.error("Session factory was null");
|
---|
50 | }
|
---|
51 | } finally {
|
---|
52 | if (trans != null) {
|
---|
53 | try {
|
---|
54 | trans.commit();
|
---|
55 | } catch (Throwable t) {
|
---|
56 | log.error("Failed to commit transaction: " + t.getMessage(), t);
|
---|
57 | }
|
---|
58 | }
|
---|
59 | if (sess != null) {
|
---|
60 | try {
|
---|
61 | sess.close();
|
---|
62 | } catch (Throwable t) {
|
---|
63 | log.error("Failed to close session: " + t.getMessage(), t);
|
---|
64 | }
|
---|
65 | }
|
---|
66 | }
|
---|
67 | log.debug("-- End CorrelatedIdentifiersDao.addPatientCorrelation() ---");
|
---|
68 | }
|
---|
69 |
|
---|
70 | public static void removePatientCorrelation(CorrelatedIdentifiers correlatedIdentifers) {
|
---|
71 | log.debug("-- Begin CorrelatedIdentifiersDao.removePatientCorrelation() ---");
|
---|
72 | Session sess = null;
|
---|
73 | Transaction trans = null;
|
---|
74 | boolean result = false;
|
---|
75 | String param1 = correlatedIdentifers.getPatientAssigningAuthorityId();
|
---|
76 | String param2 = correlatedIdentifers.getPatientId();
|
---|
77 | String param3 = correlatedIdentifers.getCorrelatedPatientAssigningAuthorityId();
|
---|
78 | String param4 = correlatedIdentifers.getCorrelatedPatientId();
|
---|
79 | String sql = "delete from correlatedidentifiers where ((PatientAssigningAuthorityId='" + param1 + "' and PatientId='" + param2 + "' and CorrelatedPatientAssigningAuthorityId='" +
|
---|
80 | param3 + "' and CorrelatedPatientId='" + param4 + "') or (PatientAssigningAuthorityId='" + param3 + "' and PatientId='" + param4 + "' and CorrelatedPatientAssigningAuthorityId='" +
|
---|
81 | param1 + "' and CorrelatedPatientId='" + param2 + "'))";
|
---|
82 | try {
|
---|
83 | SessionFactory fact = HibernateUtil.getSessionFactory();
|
---|
84 | if (fact != null) {
|
---|
85 | System.out.println("Factory Created...");
|
---|
86 | sess = fact.openSession();
|
---|
87 | if (sess != null) {
|
---|
88 | trans = sess.beginTransaction();
|
---|
89 | int rowsDeleted = sess.createSQLQuery(sql).executeUpdate();
|
---|
90 | trans.commit();
|
---|
91 | if (rowsDeleted != 0) {
|
---|
92 | result = true;
|
---|
93 | }
|
---|
94 | } else {
|
---|
95 | log.error("Unable to create session...");
|
---|
96 | }
|
---|
97 | } else {
|
---|
98 | log.error("Unable to create Factory...");
|
---|
99 | }
|
---|
100 | } catch (HibernateException exp) {
|
---|
101 | exp.printStackTrace();
|
---|
102 | } finally {
|
---|
103 | if (trans != null && trans.isActive()) {
|
---|
104 | try {
|
---|
105 | trans.rollback();
|
---|
106 | } catch (Throwable t) {
|
---|
107 | log.error("Failed to commit transaction: " + t.getMessage(), t);
|
---|
108 | }
|
---|
109 | }
|
---|
110 | if (sess != null) {
|
---|
111 | try {
|
---|
112 | sess.close();
|
---|
113 | } catch (Throwable t) {
|
---|
114 | log.error("Failed to close session: " + t.getMessage(), t);
|
---|
115 | }
|
---|
116 | }
|
---|
117 | }
|
---|
118 | log.debug("-- End CorrelatedIdentifiersDao.removePatientCorrelation() ---");
|
---|
119 | }
|
---|
120 | }
|
---|