KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jaspersoft > jasperserver > api > metadata > common > service > impl > HibernateDaoImpl


1 /*
2  * Copyright (C) 2006 JasperSoft http://www.jaspersoft.com
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed WITHOUT ANY WARRANTY; and without the
10  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
15  * or write to:
16  *
17  * Free Software Foundation, Inc.,
18  * 59 Temple Place - Suite 330,
19  * Boston, MA USA 02111-1307
20  */

21
22 package com.jaspersoft.jasperserver.api.metadata.common.service.impl;
23
24 import java.util.Date JavaDoc;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.springframework.dao.DataAccessException;
29 import org.springframework.orm.hibernate3.HibernateAccessor;
30 import org.springframework.orm.hibernate3.HibernateTemplate;
31 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
32
33 import com.jaspersoft.jasperserver.api.JSExceptionWrapper;
34
35 /**
36  * @author swood
37  * @version $Id: HibernateDaoImpl.java 3607 2006-06-08 16:20:56Z lucian $
38  */

39 public class HibernateDaoImpl extends HibernateDaoSupport {
40     
41     private static final Log log = LogFactory.getLog(HibernateDaoImpl.class);
42     
43     private final ThreadLocal JavaDoc operationDate;
44     
45     public HibernateDaoImpl()
46     {
47         operationDate = new ThreadLocal JavaDoc();
48     }
49
50     protected static interface DaoCallback {
51         Object JavaDoc execute();
52     }
53
54     protected final Object JavaDoc executeCallback(final DaoCallback callback) {
55         try {
56             Object JavaDoc ret = callback.execute();
57             return ret;
58         } catch (DataAccessException e) {
59             log.error("Hibernate DataAccessException", e);
60             throw new JSExceptionWrapper(e);
61         }
62     }
63
64     protected final Object JavaDoc executeWriteCallback(final DaoCallback callback) {
65         return executeWriteCallback(callback, true);
66     }
67
68     protected final Object JavaDoc executeWriteCallback(final DaoCallback callback, boolean flush) {
69         startOperation();
70         HibernateTemplate hibernateTemplate = getHibernateTemplate();
71         int origFlushMode = hibernateTemplate.getFlushMode();
72         try {
73             hibernateTemplate.setFlushMode(HibernateAccessor.FLUSH_COMMIT);
74             Object JavaDoc ret = callback.execute();
75             if (flush) {
76                 hibernateTemplate.flush();
77             }
78             return ret;
79         } catch (DataAccessException e) {
80             log.error("Hibernate DataAccessException", e);
81             throw new JSExceptionWrapper(e);
82         }
83         finally {
84             hibernateTemplate.setFlushMode(origFlushMode);
85             endOperation();
86         }
87     }
88
89     protected void startOperation() {
90         operationDate.set(new Date JavaDoc());
91     }
92     
93     protected Date JavaDoc getOperationTimestamp() {
94         return (Date JavaDoc) operationDate.get();
95     }
96     
97     protected void endOperation() {
98         operationDate.set(null);
99     }
100 }
101
Popular Tags