001: /*
002: * Copyright 2004,2005 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.wso2.esb.persistence.dao;
017:
018: import org.apache.log4j.Logger;
019: import org.hibernate.*;
020: import org.hibernate.exception.ConstraintViolationException;
021: import org.wso2.esb.persistence.dataobject.BaseDO;
022: import org.wso2.esb.util.HibernateConfig;
023:
024: import java.util.Date;
025: import java.util.List;
026:
027: /**
028: * The base DataAccessObject from which each DAO of the ESB is extended
029: */
030: public abstract class BaseDAO {
031:
032: protected Logger log = Logger.getLogger(BaseDAO.class);
033:
034: /**
035: * The HibernateConfiguration
036: */
037: protected HibernateConfig hbConfig;
038:
039: public BaseDAO(HibernateConfig hbConfig) {
040: this .hbConfig = hbConfig;
041: }
042:
043: /**
044: * To save a entity
045: *
046: * @param trasientInstance
047: * @return The entity ID
048: */
049: public Long create(BaseDO trasientInstance) {
050: Session session = hbConfig.currentSession();
051: Transaction tx = session.beginTransaction();
052: try {
053: trasientInstance.setLastUpdatedTime(new Date());
054: session.persist(trasientInstance);
055: session.flush();
056: tx.commit();
057: } catch (ConstraintViolationException e) {
058: String msg = "Trying to create duplicate entity : "
059: + trasientInstance;
060: log.warn(msg, e);
061: } catch (HibernateException e) {
062: tx.rollback();
063: handleException("Cannot create entity " + trasientInstance,
064: e);
065: } finally {
066: hbConfig.closeSession();
067: }
068:
069: return trasientInstance.getId();
070: }
071:
072: /**
073: * To create a new entity or update already existed entity
074: *
075: * @param instance
076: * @return The entity ID
077: */
078: public Long createOrUpdate(BaseDO instance) {
079: Session session = hbConfig.currentSession();
080: Transaction tx = session.beginTransaction();
081: try {
082: instance.setLastUpdatedTime(new Date());
083: session.saveOrUpdate(instance);
084: session.flush();
085: tx.commit();
086: } catch (ConstraintViolationException e) {
087: String msg = "Trying to create duplicate entity "
088: + instance;
089: log.warn(msg, e);
090: } catch (HibernateException e) {
091: tx.rollback();
092: handleException("Cannot create entity " + instance, e);
093: } finally {
094: hbConfig.closeSession();
095: }
096:
097: return instance.getId();
098: }
099:
100: /**
101: * To update a already existed entity
102: *
103: * @param abstractDO
104: */
105: public void update(BaseDO abstractDO) {
106: Session session = hbConfig.currentSession();
107: Transaction tx = session.beginTransaction();
108: try {
109: abstractDO.setLastUpdatedTime(new Date());
110: session.merge(abstractDO);
111: session.flush();
112: tx.commit();
113: } catch (HibernateException e) {
114: tx.rollback();
115: handleException("Unable to update " + abstractDO, e);
116: } finally {
117: hbConfig.closeSession();
118: }
119: }
120:
121: /**
122: * To delete a entity
123: *
124: * @param abstractDO
125: */
126: public void delete(BaseDO abstractDO) {
127: Session session = hbConfig.currentSession();
128: Transaction tx = session.beginTransaction();
129: try {
130: session.delete(abstractDO);
131: session.flush();
132: tx.commit();
133: } catch (HibernateException e) {
134: tx.rollback();
135: handleException("Unable to delete " + abstractDO, e);
136: } finally {
137: hbConfig.closeSession();
138: }
139: }
140:
141: /**
142: * To Retrieves entities as a List
143: *
144: * @param hqlQuery
145: * @return The resultset
146: */
147: public List select(String hqlQuery) {
148: Session session = hbConfig.currentSession();
149: Transaction tx = session.beginTransaction();
150: Query query;
151: List result = null;
152: try {
153: query = session.createQuery(hqlQuery);
154: result = query.list();
155: } catch (HibernateException e) {
156: tx.rollback();
157: handleException("Error executing HQL query : " + hqlQuery,
158: e);
159: } finally {
160: hbConfig.closeSession();
161: }
162: return result;
163: }
164:
165: /**
166: * To execute a query
167: *
168: * @param hqlQuery
169: * @return The resultset
170: */
171: public int execute(String hqlQuery) {
172: Session session = hbConfig.currentSession();
173: Transaction tx = session.beginTransaction();
174: Query query;
175: int result = -1;
176: try {
177: query = session.createQuery(hqlQuery);
178: result = query.executeUpdate();
179: } catch (HibernateException e) {
180: tx.rollback();
181: handleException("Error executing HQL query : " + hqlQuery,
182: e);
183: } finally {
184: hbConfig.closeSession();
185: }
186: return result;
187: }
188:
189: /**
190: * To Retrieves one unique entity as a Object
191: *
192: * @param hqlQuery
193: * @return The entity
194: */
195: public Object selectUnique(String hqlQuery) {
196:
197: Session session = hbConfig.currentSession();
198: Transaction tx = session.beginTransaction();
199: Query query;
200: Object result = null;
201: try {
202: query = session.createQuery(hqlQuery);
203: result = query.uniqueResult();
204: } catch (HibernateException e) {
205: tx.rollback();
206: handleException("Error executing HQL query : " + hqlQuery,
207: e);
208: } finally {
209: hbConfig.closeSession();
210: }
211: return result;
212: }
213:
214: /**
215: * To Retrieves entities as a List using SQL
216: *
217: * @param sqlQuery
218: * @param aClass
219: * @return The resultset
220: */
221: public List selectBySQL(String sqlQuery, Class aClass) {
222: Session session = hbConfig.currentSession();
223: List result = null;
224:
225: Transaction tx = session.beginTransaction();
226: try {
227: SQLQuery query = session.createSQLQuery(sqlQuery)
228: .addEntity(aClass);
229: result = query.list();
230:
231: } catch (HibernateException e) {
232: tx.rollback();
233: handleException("Error executing SQL : " + sqlQuery, e);
234: } finally {
235: if (session != null) {
236: try {
237: hbConfig.closeSession();
238: } catch (HibernateException ignore) {
239: log.warn(ignore);
240: }
241: }
242: }
243: return result;
244: }
245:
246: private void handleException(String msg, Exception e) {
247: log.error(msg, e);
248: throw new RuntimeException(msg, e);
249: }
250: }
|