1 | package gov.hhs.fha.nhinc.gateway.aggregator.dao;
|
---|
2 |
|
---|
3 | import java.util.Date;
|
---|
4 | import java.util.List;
|
---|
5 |
|
---|
6 | import java.text.SimpleDateFormat;
|
---|
7 |
|
---|
8 | import org.apache.commons.logging.Log;
|
---|
9 | import org.apache.commons.logging.LogFactory;
|
---|
10 |
|
---|
11 | import gov.hhs.fha.nhinc.gateway.aggregator.model.AggTransaction;
|
---|
12 |
|
---|
13 | import gov.hhs.fha.nhinc.gateway.aggregator.persistence.HibernateUtil;
|
---|
14 |
|
---|
15 | import gov.hhs.fha.nhinc.gateway.aggregator.AggregatorException;
|
---|
16 |
|
---|
17 | import java.util.ArrayList;
|
---|
18 | import org.hibernate.Session;
|
---|
19 | import org.hibernate.SessionFactory;
|
---|
20 | import org.hibernate.Criteria;
|
---|
21 | import org.hibernate.criterion.Expression;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * This class is responsible for the persistence of data into the
|
---|
25 | * AGGREGATOR.AGG_TRANSACTION table and also for associated rows
|
---|
26 | * in the AGGREGATOR.AGG_MESSAGE_RESULTS table if they are stored
|
---|
27 | * at the same time.
|
---|
28 | *
|
---|
29 | * @author Les Westberg
|
---|
30 | */
|
---|
31 | public class AggTransactionDao
|
---|
32 | {
|
---|
33 | private static Log log = LogFactory.getLog(AggTransactionDao.class);
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Default constructor
|
---|
37 | */
|
---|
38 | public AggTransactionDao()
|
---|
39 | {
|
---|
40 | }
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * This method saves the specified data into the AGGREGATOR.AGG_TRANSACTION
|
---|
44 | * and AGGREGATOR_AGG_MESSAGE_RESULTS tables and if this is a new
|
---|
45 | * transaction, it will assign a new transaction ID..
|
---|
46 | *
|
---|
47 | * @param aggTransaction The data to be written to the table.
|
---|
48 | */
|
---|
49 | public void save(AggTransaction oAggTransaction)
|
---|
50 | {
|
---|
51 |
|
---|
52 | String sTransactionId = "";
|
---|
53 | if ((oAggTransaction != null) &&
|
---|
54 | (oAggTransaction.getTransactionId() != null) &&
|
---|
55 | (oAggTransaction.getTransactionId().length() > 0))
|
---|
56 | {
|
---|
57 | sTransactionId = oAggTransaction.getTransactionId();
|
---|
58 | }
|
---|
59 |
|
---|
60 | log.debug("Performing AggTransaction save for TransactionId: " +
|
---|
61 | ((sTransactionId.length() == 0) ? "New Record" : sTransactionId));
|
---|
62 |
|
---|
63 | HibernateUtil.save(oAggTransaction, sTransactionId, "TransactionId");
|
---|
64 |
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Delete a row in the AGGREGATOR.AGG_TRANSACTION table along
|
---|
69 | * with the corresponding entries in the AGGREGATOR.AGG_MESSAGE_RESULTS table.
|
---|
70 | *
|
---|
71 | * @param document Document to delete
|
---|
72 | */
|
---|
73 | public void delete(AggTransaction oAggTransaction)
|
---|
74 | {
|
---|
75 | String sTransactionId = null;
|
---|
76 | if (oAggTransaction != null)
|
---|
77 | {
|
---|
78 | sTransactionId = oAggTransaction.getTransactionId();
|
---|
79 | }
|
---|
80 | else
|
---|
81 | {
|
---|
82 | log.warn("Attempt to delete AggTransaction but the value was null.");
|
---|
83 | return; // there is nothing to delete.
|
---|
84 | }
|
---|
85 |
|
---|
86 | log.debug("Performing AggTransaction delete for TransactionId: " + sTransactionId);
|
---|
87 |
|
---|
88 | HibernateUtil.delete(oAggTransaction, sTransactionId, "TransactionId");
|
---|
89 |
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Retrieve a record by identifier (TransactionId)
|
---|
94 | *
|
---|
95 | * @param sTransactionId Transaction ID for the transaction being returned.
|
---|
96 | * @return Retrieved transaction.
|
---|
97 | */
|
---|
98 | public AggTransaction findById(String sTransactionId)
|
---|
99 | {
|
---|
100 | log.debug("Performing AggTransaction findById for TransactionId: " + sTransactionId);
|
---|
101 |
|
---|
102 | AggTransaction oAggTransaction =
|
---|
103 | (AggTransaction) HibernateUtil.findById(AggTransaction.class,
|
---|
104 | sTransactionId, sTransactionId,
|
---|
105 | "TransactionId");
|
---|
106 | return oAggTransaction;
|
---|
107 |
|
---|
108 | }
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Retrieve records that are older than the specified date and time.
|
---|
112 | *
|
---|
113 | * @param dtDateTime The date in time in which we want to retrieve older records.
|
---|
114 | * @return The set of transactions that were retrieved.
|
---|
115 | * @throws AggregatorException This exception is thrown if there is an error message.
|
---|
116 | */
|
---|
117 | @SuppressWarnings("unchecked") // Occurs because of olAggTransaction = oCriteria.list(); - but it is safe so suppress the warning
|
---|
118 | public AggTransaction[] findOlderThan(Date dtDateTime)
|
---|
119 | throws AggregatorException
|
---|
120 | {
|
---|
121 | List<AggTransaction> olAggTransaction = new ArrayList<AggTransaction>();
|
---|
122 | SimpleDateFormat oFormat = new SimpleDateFormat("MM/dd/yyyy.HH:mm:ss");
|
---|
123 |
|
---|
124 | String sDateTime = "";
|
---|
125 | if (sDateTime != null)
|
---|
126 | {
|
---|
127 | sDateTime = oFormat.format(dtDateTime);
|
---|
128 | }
|
---|
129 | else
|
---|
130 | {
|
---|
131 | String sErrorMessage = "AggTransactionDao.findOlderThan(dtDateTime) must be called with a valid date/time but dtDateTime was null.";
|
---|
132 | log.error(sErrorMessage);
|
---|
133 | throw new AggregatorException(sErrorMessage);
|
---|
134 | }
|
---|
135 |
|
---|
136 | if (log.isDebugEnabled())
|
---|
137 | {
|
---|
138 | log.debug("Performing AggTransactionDao.findOlderThan(" + sDateTime + ").");
|
---|
139 | }
|
---|
140 |
|
---|
141 | Session oSession = null;
|
---|
142 | try
|
---|
143 | {
|
---|
144 | SessionFactory oSessionFactory = HibernateUtil.getSessionFactory();
|
---|
145 | if (oSessionFactory != null)
|
---|
146 | {
|
---|
147 | oSession = oSessionFactory.openSession();
|
---|
148 | if (oSession != null)
|
---|
149 | {
|
---|
150 | Criteria oCriteria = oSession.createCriteria(AggTransaction.class);
|
---|
151 | oCriteria.add(Expression.le("transactionStartTime", dtDateTime));
|
---|
152 | olAggTransaction = oCriteria.list();
|
---|
153 | }
|
---|
154 | else
|
---|
155 | {
|
---|
156 | log.error("Failed to obtain a session from the sessionFactory " +
|
---|
157 | "while calling findOlderThan(" + sDateTime + "). ");
|
---|
158 | }
|
---|
159 | }
|
---|
160 | else
|
---|
161 | {
|
---|
162 | log.error("Session factory was null while calling findOlderThan(" +
|
---|
163 | sDateTime + ").");
|
---|
164 | }
|
---|
165 |
|
---|
166 | if (log.isDebugEnabled())
|
---|
167 | {
|
---|
168 | log.debug("Completed AggTransactionDao.findOlderThan(" + sDateTime + "). " +
|
---|
169 | "Result was: " + (((olAggTransaction == null) && (olAggTransaction.size() > 0)) ? "not " : "") + "found");
|
---|
170 | }
|
---|
171 | }
|
---|
172 | finally
|
---|
173 | {
|
---|
174 | if (oSession != null)
|
---|
175 | {
|
---|
176 | try
|
---|
177 | {
|
---|
178 | oSession.close();
|
---|
179 | }
|
---|
180 | catch (Throwable t)
|
---|
181 | {
|
---|
182 | log.error("Failed to close session" +
|
---|
183 | "while calling findOlderThan(" + sDateTime + "). " +
|
---|
184 | "Message: " + t.getMessage(), t);
|
---|
185 | }
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | if ((olAggTransaction != null) &&
|
---|
190 | (olAggTransaction.size() > 0))
|
---|
191 | {
|
---|
192 | return olAggTransaction.toArray(new AggTransaction[0]);
|
---|
193 | }
|
---|
194 | else
|
---|
195 | {
|
---|
196 | return new AggTransaction[0];
|
---|
197 | }
|
---|
198 | }
|
---|
199 |
|
---|
200 | }
|
---|