1 | package gov.hhs.fha.nhinc.repository.dao;
|
---|
2 |
|
---|
3 | import gov.hhs.fha.nhinc.repository.model.Document;
|
---|
4 | import gov.hhs.fha.nhinc.repository.model.DocumentQueryParams;
|
---|
5 | import gov.hhs.fha.nhinc.repository.persistence.HibernateUtil;
|
---|
6 | import java.text.SimpleDateFormat;
|
---|
7 | import java.util.Date;
|
---|
8 | import java.util.List;
|
---|
9 | import org.apache.commons.logging.Log;
|
---|
10 | import org.apache.commons.logging.LogFactory;
|
---|
11 | import org.hibernate.Criteria;
|
---|
12 | import org.hibernate.SessionFactory;
|
---|
13 | import org.hibernate.Session;
|
---|
14 | import org.hibernate.Transaction;
|
---|
15 | import org.hibernate.criterion.Expression;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * Data access object class for Document data
|
---|
19 | *
|
---|
20 | * @author Neil Webb
|
---|
21 | */
|
---|
22 | public class DocumentDao
|
---|
23 | {
|
---|
24 | Log log = LogFactory.getLog(DocumentDao.class);
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Save a document record to the database.
|
---|
28 | * Insert if document id is null. Update otherwise.
|
---|
29 | *
|
---|
30 | * @param document Document object to save.
|
---|
31 | */
|
---|
32 | public void save(Document document)
|
---|
33 | {
|
---|
34 | log.debug("Performing document save");
|
---|
35 | Session sess = null;
|
---|
36 | Transaction trans = null;
|
---|
37 | try
|
---|
38 | {
|
---|
39 | SessionFactory fact = HibernateUtil.getSessionFactory();
|
---|
40 | if (fact != null)
|
---|
41 | {
|
---|
42 | sess = fact.openSession();
|
---|
43 | if (sess != null)
|
---|
44 | {
|
---|
45 | trans = sess.beginTransaction();
|
---|
46 | sess.saveOrUpdate(document);
|
---|
47 | }
|
---|
48 | else
|
---|
49 | {
|
---|
50 | log.error("Failed to obtain a session from the sessionFactory");
|
---|
51 | }
|
---|
52 | }
|
---|
53 | else
|
---|
54 | {
|
---|
55 | log.error("Session factory was null");
|
---|
56 | }
|
---|
57 | }
|
---|
58 | finally
|
---|
59 | {
|
---|
60 | if (trans != null)
|
---|
61 | {
|
---|
62 | try
|
---|
63 | {
|
---|
64 | trans.commit();
|
---|
65 | }
|
---|
66 | catch (Throwable t)
|
---|
67 | {
|
---|
68 | log.error("Failed to commit transaction: " + t.getMessage(), t);
|
---|
69 | }
|
---|
70 | }
|
---|
71 | if (sess != null)
|
---|
72 | {
|
---|
73 | try
|
---|
74 | {
|
---|
75 | sess.close();
|
---|
76 | }
|
---|
77 | catch (Throwable t)
|
---|
78 | {
|
---|
79 | log.error("Failed to close session: " + t.getMessage(), t);
|
---|
80 | }
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | log.debug("Completed document save");
|
---|
85 | }
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Delete a document
|
---|
89 | *
|
---|
90 | * @param document Document to delete
|
---|
91 | */
|
---|
92 | public void delete(Document document)
|
---|
93 | {
|
---|
94 | log.debug("Performing document delete");
|
---|
95 |
|
---|
96 | Session sess = null;
|
---|
97 | Transaction trans = null;
|
---|
98 | try
|
---|
99 | {
|
---|
100 | SessionFactory fact = HibernateUtil.getSessionFactory();
|
---|
101 | if (fact != null)
|
---|
102 | {
|
---|
103 | sess = fact.openSession();
|
---|
104 | if (sess != null)
|
---|
105 | {
|
---|
106 | trans = sess.beginTransaction();
|
---|
107 | sess.delete(document);
|
---|
108 | }
|
---|
109 | else
|
---|
110 | {
|
---|
111 | log.error("Failed to obtain a session from the sessionFactory");
|
---|
112 | }
|
---|
113 | }
|
---|
114 | else
|
---|
115 | {
|
---|
116 | log.error("Session factory was null");
|
---|
117 | }
|
---|
118 | }
|
---|
119 | finally
|
---|
120 | {
|
---|
121 | if (trans != null)
|
---|
122 | {
|
---|
123 | try
|
---|
124 | {
|
---|
125 | trans.commit();
|
---|
126 | }
|
---|
127 | catch (Throwable t)
|
---|
128 | {
|
---|
129 | log.error("Failed to commit transaction: " + t.getMessage(), t);
|
---|
130 | }
|
---|
131 | }
|
---|
132 | if (sess != null)
|
---|
133 | {
|
---|
134 | try
|
---|
135 | {
|
---|
136 | sess.close();
|
---|
137 | }
|
---|
138 | catch (Throwable t)
|
---|
139 | {
|
---|
140 | log.error("Failed to close session: " + t.getMessage(), t);
|
---|
141 | }
|
---|
142 | }
|
---|
143 | }
|
---|
144 | log.debug("Completed document delete");
|
---|
145 | }
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Retrieve a document by identifier
|
---|
149 | *
|
---|
150 | * @param documentId Document identifier
|
---|
151 | * @return Retrieved document
|
---|
152 | */
|
---|
153 | public Document findById(Long documentId)
|
---|
154 | {
|
---|
155 | log.debug("Performing document retrieve using id: " + documentId);
|
---|
156 | Document document = null;
|
---|
157 | Session sess = null;
|
---|
158 | try
|
---|
159 | {
|
---|
160 | SessionFactory fact = HibernateUtil.getSessionFactory();
|
---|
161 | if (fact != null)
|
---|
162 | {
|
---|
163 | sess = fact.openSession();
|
---|
164 | if (sess != null)
|
---|
165 | {
|
---|
166 | document = (Document) sess.get(Document.class, documentId);
|
---|
167 | }
|
---|
168 | else
|
---|
169 | {
|
---|
170 | log.error("Failed to obtain a session from the sessionFactory");
|
---|
171 | }
|
---|
172 | }
|
---|
173 | else
|
---|
174 | {
|
---|
175 | log.error("Session factory was null");
|
---|
176 | }
|
---|
177 | if (log.isDebugEnabled())
|
---|
178 | {
|
---|
179 | log.debug("Completed document retrieve by id. Result was " + ((document == null) ? "not " : "") + "found");
|
---|
180 | }
|
---|
181 | }
|
---|
182 | finally
|
---|
183 | {
|
---|
184 | if (sess != null)
|
---|
185 | {
|
---|
186 | try
|
---|
187 | {
|
---|
188 | sess.close();
|
---|
189 | }
|
---|
190 | catch (Throwable t)
|
---|
191 | {
|
---|
192 | log.error("Failed to close session: " + t.getMessage(), t);
|
---|
193 | }
|
---|
194 | }
|
---|
195 | }
|
---|
196 | return document;
|
---|
197 | }
|
---|
198 |
|
---|
199 | /**
|
---|
200 | * Retrieves all documents
|
---|
201 | *
|
---|
202 | * @return All document records
|
---|
203 | */
|
---|
204 | @SuppressWarnings("unchecked")
|
---|
205 | public List<Document> findAll()
|
---|
206 | {
|
---|
207 | log.debug("Performing retrieve of all documents");
|
---|
208 | List<Document> documents = null;
|
---|
209 | Session sess = null;
|
---|
210 | try
|
---|
211 | {
|
---|
212 | SessionFactory fact = HibernateUtil.getSessionFactory();
|
---|
213 | if (fact != null)
|
---|
214 | {
|
---|
215 | sess = fact.openSession();
|
---|
216 | if (sess != null)
|
---|
217 | {
|
---|
218 | Criteria criteria = sess.createCriteria(Document.class);
|
---|
219 | documents = criteria.list();
|
---|
220 | }
|
---|
221 | else
|
---|
222 | {
|
---|
223 | log.error("Failed to obtain a session from the sessionFactory");
|
---|
224 | }
|
---|
225 | }
|
---|
226 | else
|
---|
227 | {
|
---|
228 | log.error("Session factory was null");
|
---|
229 | }
|
---|
230 | if (log.isDebugEnabled())
|
---|
231 | {
|
---|
232 | log.debug("Completed retrieve of all documents. " + ((documents == null) ? "0" : Integer.toString(documents.size())) + " results returned.");
|
---|
233 | }
|
---|
234 | }
|
---|
235 | finally
|
---|
236 | {
|
---|
237 | if (sess != null)
|
---|
238 | {
|
---|
239 | try
|
---|
240 | {
|
---|
241 | sess.close();
|
---|
242 | }
|
---|
243 | catch (Throwable t)
|
---|
244 | {
|
---|
245 | log.error("Failed to close session: " + t.getMessage(), t);
|
---|
246 | }
|
---|
247 | }
|
---|
248 | }
|
---|
249 | return documents;
|
---|
250 | }
|
---|
251 |
|
---|
252 | /**
|
---|
253 | * Perform a query for documents
|
---|
254 | *
|
---|
255 | * @param params Query parameters
|
---|
256 | * @return Query results
|
---|
257 | */
|
---|
258 | @SuppressWarnings("unchecked")
|
---|
259 | public List<Document> findDocuments(DocumentQueryParams params)
|
---|
260 | {
|
---|
261 | log.debug("Beginning document query");
|
---|
262 |
|
---|
263 | String patientId = null;
|
---|
264 | List<String> classCodes = null;
|
---|
265 | String classCodeScheme = null;
|
---|
266 | Date creationTimeFrom = null;
|
---|
267 | Date creationTimeTo = null;
|
---|
268 | Date serviceStartTimeFrom = null;
|
---|
269 | Date serviceStartTimeTo = null;
|
---|
270 | Date serviceStopTimeFrom = null;
|
---|
271 | Date serviceStopTimeTo = null;
|
---|
272 | List<String> statuses = null;
|
---|
273 | List<String> documentUniqueIds = null;
|
---|
274 | if (params != null)
|
---|
275 | {
|
---|
276 | patientId = params.getPatientId();
|
---|
277 | classCodes = params.getClassCodes();
|
---|
278 | classCodeScheme = params.getClassCodeScheme();
|
---|
279 | creationTimeFrom = params.getCreationTimeFrom();
|
---|
280 | creationTimeTo = params.getCreationTimeTo();
|
---|
281 | serviceStartTimeFrom = params.getServiceStartTimeFrom();
|
---|
282 | serviceStartTimeTo = params.getServiceStartTimeTo();
|
---|
283 | serviceStopTimeFrom = params.getServiceStopTimeFrom();
|
---|
284 | serviceStopTimeTo = params.getServiceStopTimeTo();
|
---|
285 | statuses = params.getStatuses();
|
---|
286 | documentUniqueIds = params.getDocumentUniqueIds();
|
---|
287 |
|
---|
288 | }
|
---|
289 | List<Document> documents = null;
|
---|
290 | Session sess = null;
|
---|
291 | try
|
---|
292 | {
|
---|
293 | SessionFactory fact = HibernateUtil.getSessionFactory();
|
---|
294 | if (fact != null)
|
---|
295 | {
|
---|
296 | sess = fact.openSession();
|
---|
297 | if (sess != null)
|
---|
298 | {
|
---|
299 | SimpleDateFormat logDateFormatter = new SimpleDateFormat("yyyyMMdd hh:mm:ss a");
|
---|
300 | Criteria criteria = sess.createCriteria(Document.class);
|
---|
301 |
|
---|
302 | if (patientId != null)
|
---|
303 | {
|
---|
304 | if (log.isDebugEnabled())
|
---|
305 | {
|
---|
306 | log.debug("Document query - patient id: " + patientId);
|
---|
307 | }
|
---|
308 | criteria.add(Expression.eq("patientId", patientId));
|
---|
309 | }
|
---|
310 |
|
---|
311 | if ((classCodes != null) && (!classCodes.isEmpty()))
|
---|
312 | {
|
---|
313 | if (log.isDebugEnabled())
|
---|
314 | {
|
---|
315 | for (String classCode : classCodes)
|
---|
316 | {
|
---|
317 | log.debug("Document query - class code: " + classCode);
|
---|
318 | }
|
---|
319 | }
|
---|
320 | criteria.add(Expression.in("classCode", classCodes));
|
---|
321 | }
|
---|
322 |
|
---|
323 | if (classCodeScheme != null)
|
---|
324 | {
|
---|
325 | if (log.isDebugEnabled())
|
---|
326 | {
|
---|
327 | log.debug("Document query - class code scheme: " + classCodeScheme);
|
---|
328 | }
|
---|
329 | criteria.add(Expression.eq("classCodeScheme", classCodeScheme));
|
---|
330 | }
|
---|
331 |
|
---|
332 | if (creationTimeFrom != null)
|
---|
333 | {
|
---|
334 | if (log.isDebugEnabled())
|
---|
335 | {
|
---|
336 | log.debug("Document query - creation time from: " + logDateFormatter.format(creationTimeFrom));
|
---|
337 | }
|
---|
338 | criteria.add(Expression.ge("creationTime", creationTimeFrom));
|
---|
339 | }
|
---|
340 |
|
---|
341 | if (creationTimeTo != null)
|
---|
342 | {
|
---|
343 | if (log.isDebugEnabled())
|
---|
344 | {
|
---|
345 | log.debug("Document query - creation time to: " + logDateFormatter.format(creationTimeTo));
|
---|
346 | }
|
---|
347 | criteria.add(Expression.le("creationTime", creationTimeTo));
|
---|
348 | }
|
---|
349 |
|
---|
350 | if (serviceStartTimeFrom != null)
|
---|
351 | {
|
---|
352 | if (log.isDebugEnabled())
|
---|
353 | {
|
---|
354 | log.debug("Document query - service start time from: " + logDateFormatter.format(serviceStartTimeFrom));
|
---|
355 | }
|
---|
356 | criteria.add(Expression.ge("serviceStartTime", serviceStartTimeFrom));
|
---|
357 | }
|
---|
358 |
|
---|
359 | if (serviceStartTimeTo != null)
|
---|
360 | {
|
---|
361 | if (log.isDebugEnabled())
|
---|
362 | {
|
---|
363 | log.debug("Document query - service start time to: " + logDateFormatter.format(serviceStartTimeTo));
|
---|
364 | }
|
---|
365 | criteria.add(Expression.le("serviceStartTime", serviceStartTimeTo));
|
---|
366 | }
|
---|
367 |
|
---|
368 | if (serviceStopTimeFrom != null)
|
---|
369 | {
|
---|
370 | if (log.isDebugEnabled())
|
---|
371 | {
|
---|
372 | log.debug("Document query - service stop time from: " + logDateFormatter.format(serviceStopTimeFrom));
|
---|
373 | }
|
---|
374 | criteria.add(Expression.ge("serviceStopTime", serviceStopTimeFrom));
|
---|
375 | }
|
---|
376 |
|
---|
377 | if (serviceStopTimeTo != null)
|
---|
378 | {
|
---|
379 | if (log.isDebugEnabled())
|
---|
380 | {
|
---|
381 | log.debug("Document query - service stop time to: " + logDateFormatter.format(serviceStopTimeTo));
|
---|
382 | }
|
---|
383 | criteria.add(Expression.le("serviceStopTime", serviceStopTimeTo));
|
---|
384 | }
|
---|
385 |
|
---|
386 | if ((statuses != null) && (!statuses.isEmpty()))
|
---|
387 | {
|
---|
388 | if (log.isDebugEnabled())
|
---|
389 | {
|
---|
390 | for (String status : statuses)
|
---|
391 | {
|
---|
392 | log.debug("Document query - status: " + status);
|
---|
393 | }
|
---|
394 | }
|
---|
395 | criteria.add(Expression.in("status", statuses));
|
---|
396 | }
|
---|
397 |
|
---|
398 | if ((documentUniqueIds != null) && (!documentUniqueIds.isEmpty()))
|
---|
399 | {
|
---|
400 | if (log.isDebugEnabled())
|
---|
401 | {
|
---|
402 | for (String documentUniqueId : documentUniqueIds)
|
---|
403 | {
|
---|
404 | log.debug("Document query - document unique id: " + documentUniqueId);
|
---|
405 | }
|
---|
406 | }
|
---|
407 | criteria.add(Expression.in("documentUniqueId", documentUniqueIds));
|
---|
408 | }
|
---|
409 |
|
---|
410 | documents = criteria.list();
|
---|
411 | }
|
---|
412 | else
|
---|
413 | {
|
---|
414 | log.error("Failed to obtain a session from the sessionFactory");
|
---|
415 | }
|
---|
416 | }
|
---|
417 | else
|
---|
418 | {
|
---|
419 | log.error("Session factory was null");
|
---|
420 | }
|
---|
421 | if (log.isDebugEnabled())
|
---|
422 | {
|
---|
423 | log.debug("Completed retrieve of document query. " + ((documents == null) ? "0" : Integer.toString(documents.size())) + " results returned.");
|
---|
424 | }
|
---|
425 | }
|
---|
426 | finally
|
---|
427 | {
|
---|
428 | if (sess != null)
|
---|
429 | {
|
---|
430 | try
|
---|
431 | {
|
---|
432 | sess.close();
|
---|
433 | }
|
---|
434 | catch (Throwable t)
|
---|
435 | {
|
---|
436 | log.error("Failed to close session: " + t.getMessage(), t);
|
---|
437 | }
|
---|
438 | }
|
---|
439 | }
|
---|
440 | return documents;
|
---|
441 | }
|
---|
442 | }
|
---|