1 | package gov.hhs.fha.nhinc.gateway.aggregator.persistence;
|
---|
2 |
|
---|
3 | import java.io.Serializable;
|
---|
4 | import org.apache.commons.logging.Log;
|
---|
5 | import org.apache.commons.logging.LogFactory;
|
---|
6 |
|
---|
7 | import org.hibernate.SessionFactory;
|
---|
8 | import org.hibernate.cfg.Configuration;
|
---|
9 | import org.hibernate.Session;
|
---|
10 | import org.hibernate.Transaction;
|
---|
11 |
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * Utility to obtain hibernate connections.
|
---|
15 | *
|
---|
16 | * @author Neil Webb, Les Westberg
|
---|
17 | */
|
---|
18 | public class HibernateUtil
|
---|
19 | {
|
---|
20 | private static Log log = LogFactory.getLog(HibernateUtil.class);
|
---|
21 | private static final SessionFactory sessionFactory;
|
---|
22 | private static final String HIBERNATE_CONFIG = "aggregator.cfg.xml";
|
---|
23 |
|
---|
24 | static
|
---|
25 | {
|
---|
26 | try
|
---|
27 | {
|
---|
28 | // Create the SessionFactory from hibernate.cfg.xml
|
---|
29 | sessionFactory = new Configuration().configure(HIBERNATE_CONFIG).buildSessionFactory();
|
---|
30 | } catch (Throwable ex)
|
---|
31 | {
|
---|
32 | // Make sure you log the exception, as it might be swallowed
|
---|
33 | System.err.println("Initial SessionFactory creation failed." + ex);
|
---|
34 | throw new ExceptionInInitializerError(ex);
|
---|
35 | }
|
---|
36 | }
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Return the session factory singleton object.
|
---|
40 | *
|
---|
41 | * @return The session factory.
|
---|
42 | */
|
---|
43 | public static SessionFactory getSessionFactory()
|
---|
44 | {
|
---|
45 | return sessionFactory;
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * This method saves the specified data into the tables associatd with
|
---|
50 | * the Hibernate objects
|
---|
51 | *
|
---|
52 | * @param oObject An instance of the Hibernate object that is to be saved.
|
---|
53 | * @param sObjectId The ID of the hibernate object. If this is a new record, then
|
---|
54 | * leave this field blank. This field is used for log entry
|
---|
55 | * purposes.
|
---|
56 | * @param sObjectIdFieldName The name of the field that represents the Object's ID. This
|
---|
57 | * is used for log entry purposes.
|
---|
58 | */
|
---|
59 | public static void save(Object oObject, String sObjectId, String sObjectIdFieldName)
|
---|
60 | {
|
---|
61 | String sLocalObjId = "";
|
---|
62 |
|
---|
63 | if ((sObjectId == null) ||
|
---|
64 | (sObjectId.length() <= 0))
|
---|
65 | {
|
---|
66 | sLocalObjId = "New record";
|
---|
67 | }
|
---|
68 | else
|
---|
69 | {
|
---|
70 | sLocalObjId = sObjectId;
|
---|
71 | }
|
---|
72 |
|
---|
73 | log.debug("Performing save for " + sObjectIdFieldName + ": " + sLocalObjId);
|
---|
74 | Session oSession = null;
|
---|
75 | Transaction oTransaction = null;
|
---|
76 | try
|
---|
77 | {
|
---|
78 | SessionFactory oSessionFactory = HibernateUtil.getSessionFactory();
|
---|
79 | if (oSessionFactory != null)
|
---|
80 | {
|
---|
81 | oSession = oSessionFactory.openSession();
|
---|
82 | if (oSession != null)
|
---|
83 | {
|
---|
84 | oTransaction = oSession.beginTransaction();
|
---|
85 | oSession.saveOrUpdate(oObject);
|
---|
86 | }
|
---|
87 | else
|
---|
88 | {
|
---|
89 | log.error("Failed to obtain a session from the sessionFactory while saving " +
|
---|
90 | sObjectIdFieldName + ": " + sLocalObjId);
|
---|
91 | }
|
---|
92 | }
|
---|
93 | else
|
---|
94 | {
|
---|
95 | log.error("Session factory was null while saving " + sObjectIdFieldName +
|
---|
96 | ": " + sLocalObjId);
|
---|
97 | }
|
---|
98 | }
|
---|
99 | finally
|
---|
100 | {
|
---|
101 | if (oTransaction != null)
|
---|
102 | {
|
---|
103 | try
|
---|
104 | {
|
---|
105 | oTransaction.commit();
|
---|
106 | }
|
---|
107 | catch (Throwable t)
|
---|
108 | {
|
---|
109 | log.error("Failed to commit transaction for " + sObjectIdFieldName + ": " +
|
---|
110 | sLocalObjId + ". Message: " + t.getMessage(), t);
|
---|
111 | }
|
---|
112 | }
|
---|
113 | if (oSession != null)
|
---|
114 | {
|
---|
115 | try
|
---|
116 | {
|
---|
117 | oSession.close();
|
---|
118 | }
|
---|
119 | catch (Throwable t)
|
---|
120 | {
|
---|
121 | log.error("Failed to close session for " + sObjectIdFieldName + ": " +
|
---|
122 | sLocalObjId + ". Message: " + t.getMessage(), t);
|
---|
123 | }
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | log.debug("Completed document save for " + sObjectIdFieldName + ": " + sLocalObjId);
|
---|
128 | }
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Delete a row in table associated with the specified Hibernate class.
|
---|
132 | *
|
---|
133 | * @param object An instance of the specific Hibernate object that is to be deleted.
|
---|
134 | * @param sObjectId The ID of the hibernate object. This field is used for log entry
|
---|
135 | * purposes.
|
---|
136 | * @param sObjectIdFieldName The name of the field that represents the Object's ID. This
|
---|
137 | * is used for log entry purposes.
|
---|
138 | */
|
---|
139 | public static void delete(Object oObject, String sObjectId, String sObjectIdFieldName)
|
---|
140 | {
|
---|
141 | log.debug("Performing delete for " + sObjectIdFieldName + ": " + sObjectId);
|
---|
142 |
|
---|
143 | Session oSession = null;
|
---|
144 | Transaction oTransaction = null;
|
---|
145 |
|
---|
146 | try
|
---|
147 | {
|
---|
148 | SessionFactory oSessionFactory = HibernateUtil.getSessionFactory();
|
---|
149 | if (oSessionFactory != null)
|
---|
150 | {
|
---|
151 | oSession = oSessionFactory.openSession();
|
---|
152 | if (oSession != null)
|
---|
153 | {
|
---|
154 | oTransaction = oSession.beginTransaction();
|
---|
155 | oSession.delete(oObject);
|
---|
156 | }
|
---|
157 | else
|
---|
158 | {
|
---|
159 | log.error("Failed to obtain a session from the sessionFactory " +
|
---|
160 | "while deleting " + sObjectIdFieldName + ": " + sObjectId);
|
---|
161 | }
|
---|
162 | }
|
---|
163 | else
|
---|
164 | {
|
---|
165 | log.error("Session factory was null while attempting to delete " +
|
---|
166 | sObjectIdFieldName + ": " + sObjectId);
|
---|
167 | }
|
---|
168 | }
|
---|
169 | finally
|
---|
170 | {
|
---|
171 | if (oTransaction != null)
|
---|
172 | {
|
---|
173 | try
|
---|
174 | {
|
---|
175 | oTransaction.commit();
|
---|
176 |
|
---|
177 | }
|
---|
178 | catch (Throwable t)
|
---|
179 | {
|
---|
180 | log.error("Failed to commit transaction for " + sObjectIdFieldName + ": " +
|
---|
181 | sObjectId + ". Message: " + t.getMessage(), t);
|
---|
182 | }
|
---|
183 | }
|
---|
184 | if (oSession != null)
|
---|
185 | {
|
---|
186 | try
|
---|
187 | {
|
---|
188 | oSession.close();
|
---|
189 | }
|
---|
190 | catch (Throwable t)
|
---|
191 | {
|
---|
192 | log.error("Failed to close session for " + sObjectIdFieldName + ": " +
|
---|
193 | sObjectId + ". Message: " + t.getMessage(), t);
|
---|
194 | }
|
---|
195 | }
|
---|
196 | }
|
---|
197 |
|
---|
198 | log.debug("Completed delete for " + sObjectIdFieldName +
|
---|
199 | ": " + sObjectId);
|
---|
200 | }
|
---|
201 |
|
---|
202 | /**
|
---|
203 | * Retrieve a record by identifier (TransactionId)
|
---|
204 | *
|
---|
205 | * @param oClass The class object for the Hibernate data object.
|
---|
206 | * @param oObjectId The object ID to be used in the call to find the object.
|
---|
207 | * @param sObjectId The ID of the hibernate object. This field is used for log entry
|
---|
208 | * purposes.
|
---|
209 | * @param sObjectIdFieldName The name of the field that represents the Object's ID. This
|
---|
210 | * is used for log entry purposes.
|
---|
211 | * @return The retrieved data. The caller is responsible to cast this to the
|
---|
212 | * correct type..
|
---|
213 | */
|
---|
214 | public static Object findById(Class oClass, Serializable oObjectId, String sObjectId, String sObjectIdFieldName)
|
---|
215 | {
|
---|
216 | log.debug("Performing findById(" + sObjectId + ").");
|
---|
217 | Object oObject = null;
|
---|
218 | Session oSession = null;
|
---|
219 | try
|
---|
220 | {
|
---|
221 | SessionFactory oSessionFactory = HibernateUtil.getSessionFactory();
|
---|
222 | if (oSessionFactory != null)
|
---|
223 | {
|
---|
224 | oSession = oSessionFactory.openSession();
|
---|
225 | if (oSession != null)
|
---|
226 | {
|
---|
227 | oObject = oSession.get(oClass, oObjectId);
|
---|
228 | }
|
---|
229 | else
|
---|
230 | {
|
---|
231 | log.error("Failed to obtain a session from the sessionFactory " +
|
---|
232 | "while retrieving " + sObjectIdFieldName + ":" + sObjectId);
|
---|
233 | }
|
---|
234 | }
|
---|
235 | else
|
---|
236 | {
|
---|
237 | log.error("Session factory was null while retrieving " + sObjectIdFieldName +
|
---|
238 | ": " + sObjectId);
|
---|
239 | }
|
---|
240 |
|
---|
241 | if (log.isDebugEnabled())
|
---|
242 | {
|
---|
243 | log.debug("Completed findById(" + sObjectId + ")" +
|
---|
244 | ". Result was: " + ((oObject == null) ? "not " : "") + "found");
|
---|
245 | }
|
---|
246 | }
|
---|
247 | finally
|
---|
248 | {
|
---|
249 | if (oSession != null)
|
---|
250 | {
|
---|
251 | try
|
---|
252 | {
|
---|
253 | oSession.close();
|
---|
254 | }
|
---|
255 | catch (Throwable t)
|
---|
256 | {
|
---|
257 | log.error("Failed to close session for " + sObjectIdFieldName + ":" +
|
---|
258 | sObjectId + ". Message: " + t.getMessage(), t);
|
---|
259 | }
|
---|
260 | }
|
---|
261 | }
|
---|
262 | return oObject;
|
---|
263 | }
|
---|
264 |
|
---|
265 |
|
---|
266 | }
|
---|