source: ccr/trunk/nhin-vista/projects/NHINC/Current/Product/Production/Gateway/AuditRepositoryDAO/src/gov/hhs/fha/nhinc/hibernate/AuditRepositoryDAO.java@ 507

Last change on this file since 507 was 507, checked in by George Lilly, 15 years ago

NHIN gateway and adaptor for use on linux with VistA EHR and RPMS

File size: 6.0 KB
Line 
1/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5
6package gov.hhs.fha.nhinc.hibernate;
7
8import gov.hhs.fha.nhinc.hibernate.util.*;
9import java.util.List;
10import org.apache.commons.logging.Log;
11import org.apache.commons.logging.LogFactory;
12import org.hibernate.Session;
13import org.hibernate.SessionFactory;
14import org.hibernate.Transaction;
15import java.util.Date;
16import org.hibernate.Criteria;
17import org.hibernate.criterion.Expression;
18
19/**
20 * AuditRepositoryDAO Class provides methods to query and update Audit Data to/from MySQL Database using Hibernate
21 * @author svalluripalli
22 */
23public class AuditRepositoryDAO {
24 //Log4j logging initiated
25 private static Log log = LogFactory.getLog(AuditRepositoryDAO.class);
26 private static AuditRepositoryDAO auditDAO = new AuditRepositoryDAO();
27 public static String JAVA_IO_TMPDIR = "java.io.tmpdir";
28
29 /**
30 * Constructor
31 */
32 private AuditRepositoryDAO() {
33 log.info("AuditRepositoryDAO - Initialized");
34 }
35
36 /**
37 * Singleton instance returned...
38 * @return AuditRepositoryDAO
39 */
40 public static AuditRepositoryDAO getAuditRepositoryDAOInstance() {
41 log.debug("getAuditRepositoryDAOInstance()..");
42 return auditDAO;
43 }
44
45 /**
46 *
47 * @param query
48 * @param whereClause
49 * @return List<AuditRepositoryRecord>
50 */
51 public List queryAuditRepository(String query) {
52 log.debug("AuditRepositoryDAO.getData() Begin");
53
54 Session session = null;
55
56 List<AuditRepositoryRecord> queryList = null;
57 try {
58 SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
59 session = sessionFactory.openSession();
60 log.info("Getting Record");
61 queryList = session.createSQLQuery(query).addEntity("auditrepository", AuditRepositoryRecord.class).list();
62 } catch (Exception e) {
63 e.printStackTrace();
64 log.error("Exception in AuditLog.get() occured due to :" + e.getMessage());
65 } finally {
66 // Actual contact insertion will happen at this step
67 if (session != null) {
68 session.flush();
69 session.close();
70 }
71 }
72
73 log.debug("AuditRepositoryDAO.getData() end");
74 return queryList;
75 }
76
77 /**
78 *
79 * @param auditList
80 * @return boolean
81 */
82 public boolean insertAuditRepository(List<AuditRepositoryRecord> auditList) {
83 log.debug("AuditRepositoryDAO.createAuditRepository() - Begin");
84 Session session = null;
85 Transaction tx = null;
86 boolean result = true;
87 if (auditList != null && auditList.size() > 0) {
88 int size = auditList.size();
89 AuditRepositoryRecord auditRecord = null;
90
91 try {
92 SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
93 session = sessionFactory.openSession();
94 tx = session.beginTransaction();
95 log.info("Inserting Record...");
96 for (int i = 0; i < size; i++) {
97 auditRecord = (AuditRepositoryRecord) auditList.get(i);
98 session.persist(auditRecord);
99 }
100 log.info("AuditRepository List Inserted seccussfully...");
101 tx.commit();
102 } catch (Exception e) {
103 result = false;
104 if (tx != null) {
105 tx.rollback();
106 }
107 log.error("Error during insertion caused by :" + e.getMessage());
108 } finally {
109 // Actual event_log insertion will happen at this step
110 if (session != null) {
111 session.close();
112 }
113 }
114 }
115 log.debug("AuditRepositoryDAO.createAuditRepository() - End");
116 return result;
117 }
118
119 /**
120 * This method does a query to database to get the Audit Log Messages based on user id and/or patient id and/or
121 * community id and/or timeframe
122 * @param eUserId
123 * @param ePatientId
124 * @param startDate
125 * @param endDate
126 * @return List
127 */
128 public List queryAuditRepositoryOnCriteria(String eUserId, String ePatientId, Date startDate, Date endDate) {
129 log.debug("AuditRepositoryDAO.getAuditRepositoryOnCriteria() Begin");
130
131 if (eUserId == null && ePatientId == null && startDate == null) {
132 log.info("-- No - Input Parameters found for Audit Query --");
133 log.debug("AuditRepositoryDAO.getAuditRepositoryOnCriteria() End");
134 return null;
135 }
136
137 Session session = null;
138 List<AuditRepositoryRecord> queryList = null;
139 try {
140 SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
141 session = sessionFactory.openSession();
142 log.info("Getting Record");
143
144 // Build the criteria
145 Criteria aCriteria = session.createCriteria(AuditRepositoryRecord.class);
146 if (eUserId != null && !eUserId.equals("")) {
147 aCriteria.add(Expression.eq("userId", eUserId));
148 }
149 if (ePatientId != null && !ePatientId.equals("")) {
150 aCriteria.add(Expression.eq("receiverPatientId", ePatientId));
151 }
152
153 if (startDate != null && endDate != null) {
154 aCriteria.add(Expression.between("timeStamp", new Date(startDate.getTime()), new Date(endDate.getTime())));
155 } else if (startDate != null && endDate == null) {
156 aCriteria.add(Expression.ge("timeStamp", new Date(startDate.getTime())));
157 }
158 queryList = aCriteria.list();
159 } catch (Exception e) {
160 log.error("Exception in AuditLog.get() occured due to :" + e.getMessage());
161 } finally {
162 // Actual contact insertion will happen at this step
163 if (session != null) {
164 session.flush();
165 session.close();
166 }
167 }
168 log.debug("AuditRepositoryDAO.getAuditRepositoryOnCriteria() End");
169 return queryList;
170 }
171}
Note: See TracBrowser for help on using the repository browser.