source: ccr/trunk/nhin-vista/projects/NHINC/Current/Product/Production/Gateway/AggregatorLib/src/gov/hhs/fha/nhinc/gateway/aggregator/dao/AggMessageResultDao.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: 7.7 KB
Line 
1package gov.hhs.fha.nhinc.gateway.aggregator.dao;
2
3import java.util.List;
4
5import org.apache.commons.logging.Log;
6import org.apache.commons.logging.LogFactory;
7
8import gov.hhs.fha.nhinc.gateway.aggregator.model.AggMessageResult;
9
10import gov.hhs.fha.nhinc.gateway.aggregator.persistence.HibernateUtil;
11
12import gov.hhs.fha.nhinc.gateway.aggregator.AggregatorException;
13
14import java.util.ArrayList;
15import org.hibernate.Session;
16import org.hibernate.SessionFactory;
17import org.hibernate.Criteria;
18import org.hibernate.criterion.Expression;
19
20/**
21 * This class is responsible for the persistence of data into the
22 * AGGREGATOR.AGG_MESSAGE_RESULTS table. This is primarily used
23 * if you want to update information in this table independent
24 * of changed to the AGGREGATOR.AGG_TRANSACTION table.
25 *
26 * @author Les Westberg
27 */
28public class AggMessageResultDao
29{
30 private static Log log = LogFactory.getLog(AggMessageResultDao.class);
31
32 /**
33 * Default constructor.
34 */
35 public AggMessageResultDao()
36 {
37 }
38
39 /**
40 * This method saves the specified data into the
41 * AGGREGATOR_AGG_MESSAGE_RESULTS tables.
42 *
43 * @param AggMessageResult The data to be written to the table.
44 */
45 public void save(AggMessageResult oAggMessageResult)
46 {
47
48 String sMessageId = "";
49 if ((oAggMessageResult != null) &&
50 (oAggMessageResult.getMessageId() != null) &&
51 (oAggMessageResult.getMessageId().length() > 0))
52 {
53 sMessageId = oAggMessageResult.getMessageId();
54 }
55
56 log.debug("Performing AggMessageResult save for TransactionId: " +
57 ((sMessageId.length() == 0) ? "New Record" : sMessageId));
58
59 HibernateUtil.save(oAggMessageResult, sMessageId, "MessageId");
60
61 }
62
63 /**
64 * Delete a row in the AGGREGATOR.AGG_MESSAGE_RESULTS table.
65 *
66 * @param document Document to delete
67 */
68 public void delete(AggMessageResult oAggMessageResult)
69 {
70 String sMessageId = null;
71 if (oAggMessageResult != null)
72 {
73 sMessageId = oAggMessageResult.getMessageId();
74 }
75 else
76 {
77 log.warn("Attempt to delete AggMessageResults but the value was null.");
78 return; // there is nothing to delete.
79 }
80
81 log.debug("Performing AggMessageResults delete for MessageId: " + sMessageId);
82
83 HibernateUtil.delete(oAggMessageResult, sMessageId, "MessageId");
84
85 }
86
87 /**
88 * Retrieve a record by identifier (MessageId)
89 *
90 * @param sMessageId Message ID for the message result being returned.
91 * @return Retrieved message result.
92 */
93 public AggMessageResult findById(String sMessageId)
94 {
95 log.debug("Performing AggMessageResults findById for MessageId: " + sMessageId);
96
97 AggMessageResult oAggMessageResult =
98 (AggMessageResult) HibernateUtil.findById(AggMessageResult.class,
99 sMessageId, sMessageId,
100 "TransactionId");
101 return oAggMessageResult;
102
103 }
104
105 /**
106 * Retrieve the record based on the specified message key.
107 *
108 * @param sTransactionId The transaction Id of the set of messages.
109 * @param sMessageKey The message key that uniquely identifies this record.
110 * @return The row from the table that has this message key.
111 * @throws AggregatorException This is thrown if there is an issue with the
112 * passed in parameter.
113 */
114 @SuppressWarnings("unchecked") // Occurs because of olAggTransaction = oCriteria.list(); - but it is safe so suppress the warning
115 public AggMessageResult findByMessageKey(String sTransactionId, String sMessageKey)
116 throws AggregatorException
117 {
118 List<AggMessageResult> olAggMessageResult = new ArrayList<AggMessageResult>();
119 if ((sTransactionId == null) ||
120 (sTransactionId.length() <= 0))
121 {
122 String sErrorMessage = "AggMessageResultDao.findByMessagekey(sTransactionId, sMessageKey) must be called with a valid transaction Id but it was null or empty.";
123 log.error(sErrorMessage);
124 throw new AggregatorException(sErrorMessage);
125 }
126
127 if ((sMessageKey == null) ||
128 (sMessageKey.length() <= 0))
129 {
130 String sErrorMessage = "AggMessageResultDao.findByMessagekey(sTransactionId, sMessageKey) must be called with a valid message key but it was null or empty.";
131 log.error(sErrorMessage);
132 throw new AggregatorException(sErrorMessage);
133 }
134
135 if (log.isDebugEnabled())
136 {
137 log.debug("Performing AggMessageResultsDao.findByMessageKey(" + sTransactionId + ", " + sMessageKey + ").");
138 }
139
140 Session oSession = null;
141 try
142 {
143 SessionFactory oSessionFactory = HibernateUtil.getSessionFactory();
144 if (oSessionFactory != null)
145 {
146 oSession = oSessionFactory.openSession();
147 if (oSession != null)
148 {
149 Criteria oCriteria = oSession.createCriteria(AggMessageResult.class);
150 oCriteria.add(Expression.eq("aggTransaction.transactionId", sTransactionId));
151 oCriteria.add(Expression.eq("messageKey", sMessageKey));
152 olAggMessageResult = oCriteria.list();
153 }
154 else
155 {
156 log.error("Failed to obtain a session from the sessionFactory " +
157 "while calling findByMessageKey(" + sTransactionId + ", " + sMessageKey + "). ");
158 }
159 }
160 else
161 {
162 log.error("Session factory was null while calling findByKey(" + sTransactionId + ", " +
163 sMessageKey + ").");
164 }
165 }
166 finally
167 {
168 if (oSession != null)
169 {
170 try
171 {
172 oSession.close();
173 }
174 catch (Throwable t)
175 {
176 log.error("Failed to close session" +
177 "while calling findByMessageKey(" + sTransactionId + ", " + sMessageKey + "). " +
178 "Message: " + t.getMessage(), t);
179 }
180 }
181 }
182
183 if (log.isDebugEnabled())
184 {
185 log.debug("Completed AggMessageResultDao.findByMessageKey(" + sTransactionId + ", " + sMessageKey + "). " +
186 "Result was: " + (((olAggMessageResult == null) && (olAggMessageResult.size() > 0)) ? "not " : "") + "found");
187 }
188
189 // Note we should get either 0 or 1 record. There should never be a case when
190 // we get more than one record for this key. If we do, the database is corrupted.
191 //--------------------------------------------------------------------------------
192 if ((olAggMessageResult != null) &&
193 (olAggMessageResult.size() == 1))
194 {
195 return olAggMessageResult.toArray(new AggMessageResult[0])[0];
196 }
197 else if ((olAggMessageResult != null) &&
198 (olAggMessageResult.size() > 1))
199 {
200 String sErrorMessage = "AggMessageResult.findByMessgeKey(" + sTransactionId + ", " + sMessageKey +
201 ") returned " + olAggMessageResult.size() +
202 "results. It should have only returned 0 or 1 results.";
203 log.error(sErrorMessage);
204 throw new AggregatorException(sErrorMessage);
205 }
206 else
207 {
208 return null;
209 }
210 }
211
212
213}
Note: See TracBrowser for help on using the repository browser.