[507] | 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.EventCode;
|
---|
| 5 | import gov.hhs.fha.nhinc.repository.persistence.HibernateUtil;
|
---|
| 6 | import java.util.List;
|
---|
| 7 | import org.apache.commons.logging.Log;
|
---|
| 8 | import org.apache.commons.logging.LogFactory;
|
---|
| 9 | import org.hibernate.Criteria;
|
---|
| 10 | import org.hibernate.Session;
|
---|
| 11 | import org.hibernate.SessionFactory;
|
---|
| 12 | import org.hibernate.Transaction;
|
---|
| 13 | import org.hibernate.criterion.Restrictions;
|
---|
| 14 |
|
---|
| 15 | /**
|
---|
| 16 | * Data access object class for EventCode data
|
---|
| 17 | *
|
---|
| 18 | * @author Neil Webb
|
---|
| 19 | */
|
---|
| 20 | public class EventCodeDao
|
---|
| 21 | {
|
---|
| 22 | Log log = LogFactory.getLog(EventCodeDao.class);
|
---|
| 23 |
|
---|
| 24 | /**
|
---|
| 25 | * Save an event code record to the database.
|
---|
| 26 | * Insert if event code id is null. Update otherwise.
|
---|
| 27 | *
|
---|
| 28 | * @param eventCode EventCode record to save.
|
---|
| 29 | */
|
---|
| 30 | public void save(EventCode eventCode)
|
---|
| 31 | {
|
---|
| 32 | SessionFactory fact = null;
|
---|
| 33 | Session sess = null;
|
---|
| 34 | Transaction trans = null;
|
---|
| 35 | try
|
---|
| 36 | {
|
---|
| 37 | fact = HibernateUtil.getSessionFactory();
|
---|
| 38 | if (fact != null)
|
---|
| 39 | {
|
---|
| 40 | sess = fact.openSession();
|
---|
| 41 | if (sess != null)
|
---|
| 42 | {
|
---|
| 43 | trans = sess.beginTransaction();
|
---|
| 44 | sess.saveOrUpdate(eventCode);
|
---|
| 45 | }
|
---|
| 46 | else
|
---|
| 47 | {
|
---|
| 48 | log.error("Failed to obtain a session from the sessionFactory");
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
| 51 | else
|
---|
| 52 | {
|
---|
| 53 | log.error("Session factory was null");
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
| 56 | finally
|
---|
| 57 | {
|
---|
| 58 | if (trans != null)
|
---|
| 59 | {
|
---|
| 60 | try
|
---|
| 61 | {
|
---|
| 62 | trans.commit();
|
---|
| 63 | }
|
---|
| 64 | catch (Throwable t)
|
---|
| 65 | {
|
---|
| 66 | log.error("Failed to commit transaction: " + t.getMessage(), t);
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
| 69 | if (sess != null)
|
---|
| 70 | {
|
---|
| 71 | try
|
---|
| 72 | {
|
---|
| 73 | sess.close();
|
---|
| 74 | }
|
---|
| 75 | catch (Throwable t)
|
---|
| 76 | {
|
---|
| 77 | log.error("Failed to close session: " + t.getMessage(), t);
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | /**
|
---|
| 84 | * Delete an event code record.
|
---|
| 85 | *
|
---|
| 86 | * @param eventCode EventCode record to delete.
|
---|
| 87 | */
|
---|
| 88 | public void delete(EventCode eventCode)
|
---|
| 89 | {
|
---|
| 90 | Session sess = null;
|
---|
| 91 | Transaction trans = null;
|
---|
| 92 | try
|
---|
| 93 | {
|
---|
| 94 | SessionFactory fact = HibernateUtil.getSessionFactory();
|
---|
| 95 | if (fact != null)
|
---|
| 96 | {
|
---|
| 97 | sess = fact.openSession();
|
---|
| 98 | if (sess != null)
|
---|
| 99 | {
|
---|
| 100 | trans = sess.beginTransaction();
|
---|
| 101 | sess.delete(eventCode);
|
---|
| 102 |
|
---|
| 103 | }
|
---|
| 104 | else
|
---|
| 105 | {
|
---|
| 106 | log.error("Failed to obtain a session from the sessionFactory");
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 | else
|
---|
| 110 | {
|
---|
| 111 | log.error("Session factory was null");
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 | finally
|
---|
| 115 | {
|
---|
| 116 | if (trans != null)
|
---|
| 117 | {
|
---|
| 118 | try
|
---|
| 119 | {
|
---|
| 120 | trans.commit();
|
---|
| 121 | }
|
---|
| 122 | catch (Throwable t)
|
---|
| 123 | {
|
---|
| 124 | log.error("Failed to commit transaction: " + t.getMessage(), t);
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 | if (sess != null)
|
---|
| 128 | {
|
---|
| 129 | try
|
---|
| 130 | {
|
---|
| 131 | sess.close();
|
---|
| 132 | }
|
---|
| 133 | catch (Throwable t)
|
---|
| 134 | {
|
---|
| 135 | log.error("Failed to close session: " + t.getMessage(), t);
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | /**
|
---|
| 142 | * Retrieve an event code record by identifier.
|
---|
| 143 | *
|
---|
| 144 | * @param eventCodeId EventCode record identifier.
|
---|
| 145 | * @return EventCode record retrieved from the database.
|
---|
| 146 | */
|
---|
| 147 | public EventCode findById(Long eventCodeId)
|
---|
| 148 | {
|
---|
| 149 | EventCode eventCode = null;
|
---|
| 150 |
|
---|
| 151 | Session sess = null;
|
---|
| 152 | try
|
---|
| 153 | {
|
---|
| 154 | SessionFactory fact = HibernateUtil.getSessionFactory();
|
---|
| 155 | if (fact != null)
|
---|
| 156 | {
|
---|
| 157 | sess = fact.openSession();
|
---|
| 158 | if (sess != null)
|
---|
| 159 | {
|
---|
| 160 | eventCode = (EventCode) sess.get(EventCode.class, eventCodeId);
|
---|
| 161 | }
|
---|
| 162 | else
|
---|
| 163 | {
|
---|
| 164 | log.error("Failed to obtain a session from the sessionFactory");
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 167 | else
|
---|
| 168 | {
|
---|
| 169 | log.error("Session factory was null");
|
---|
| 170 | }
|
---|
| 171 | }
|
---|
| 172 | finally
|
---|
| 173 | {
|
---|
| 174 | if (sess != null)
|
---|
| 175 | {
|
---|
| 176 | try
|
---|
| 177 | {
|
---|
| 178 | sess.close();
|
---|
| 179 | }
|
---|
| 180 | catch (Throwable t)
|
---|
| 181 | {
|
---|
| 182 | log.error("Failed to close session: " + t.getMessage(), t);
|
---|
| 183 | }
|
---|
| 184 | }
|
---|
| 185 | }
|
---|
| 186 | return eventCode;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | /**
|
---|
| 190 | * Retrieves all event codes for a given document
|
---|
| 191 | *
|
---|
| 192 | * @param document Reference document object
|
---|
| 193 | * @return EventCode list
|
---|
| 194 | */
|
---|
| 195 | @SuppressWarnings("unchecked")
|
---|
| 196 | public List<EventCode> findForDocument(Document document)
|
---|
| 197 | {
|
---|
| 198 | List<EventCode> eventCodes = null;
|
---|
| 199 |
|
---|
| 200 | Session sess = null;
|
---|
| 201 | try
|
---|
| 202 | {
|
---|
| 203 | SessionFactory fact = HibernateUtil.getSessionFactory();
|
---|
| 204 | if (fact != null)
|
---|
| 205 | {
|
---|
| 206 | sess = fact.openSession();
|
---|
| 207 | if (sess != null)
|
---|
| 208 | {
|
---|
| 209 | Criteria criteria = sess.createCriteria(EventCode.class);
|
---|
| 210 | criteria.add(Restrictions.eq("document", document));
|
---|
| 211 | eventCodes = criteria.list();
|
---|
| 212 | }
|
---|
| 213 | else
|
---|
| 214 | {
|
---|
| 215 | log.error("Failed to obtain a session from the sessionFactory");
|
---|
| 216 | }
|
---|
| 217 | }
|
---|
| 218 | else
|
---|
| 219 | {
|
---|
| 220 | log.error("Session factory was null");
|
---|
| 221 | }
|
---|
| 222 | }
|
---|
| 223 | finally
|
---|
| 224 | {
|
---|
| 225 | if (sess != null)
|
---|
| 226 | {
|
---|
| 227 | try
|
---|
| 228 | {
|
---|
| 229 | sess.close();
|
---|
| 230 | }
|
---|
| 231 | catch (Throwable t)
|
---|
| 232 | {
|
---|
| 233 | log.error("Failed to close session: " + t.getMessage(), t);
|
---|
| 234 | }
|
---|
| 235 | }
|
---|
| 236 | }
|
---|
| 237 | return eventCodes;
|
---|
| 238 | }
|
---|
| 239 | }
|
---|