1 21 22 package com.jaspersoft.jasperserver.api.metadata.common.service.impl; 23 24 import java.util.Date ; 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 39 public class HibernateDaoImpl extends HibernateDaoSupport { 40 41 private static final Log log = LogFactory.getLog(HibernateDaoImpl.class); 42 43 private final ThreadLocal operationDate; 44 45 public HibernateDaoImpl() 46 { 47 operationDate = new ThreadLocal (); 48 } 49 50 protected static interface DaoCallback { 51 Object execute(); 52 } 53 54 protected final Object executeCallback(final DaoCallback callback) { 55 try { 56 Object 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 executeWriteCallback(final DaoCallback callback) { 65 return executeWriteCallback(callback, true); 66 } 67 68 protected final Object 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 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 ()); 91 } 92 93 protected Date getOperationTimestamp() { 94 return (Date ) operationDate.get(); 95 } 96 97 protected void endOperation() { 98 operationDate.set(null); 99 } 100 } 101 | Popular Tags |