Code:
public class AuditLogRecordInterceptor implements Interceptor {
private Log log = LogFactory.getLog(HibernateProjectDAO.class);
private SessionFactory sessionFactory;
private AuditLog auditLog;
private Set inserts = new HashSet();
private Set updates = new HashSet();
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public SessionFactory getSessionFactory() {
return this.sessionFactory;
}
public void setAuditLog(AuditLog auditLog) {
this.auditLog = auditLog;
}
public AuditLog getAuditLog() {
return this.auditLog;
}
/* (non-Javadoc)
* @see net.sf.hibernate.Interceptor#onLoad(java.lang.Object, java.io.Serializable, java.lang.Object[], java.lang.String[], net.sf.hibernate.type.Type[])
*/
public boolean onLoad(Object arg0, Serializable arg1, Object[] arg2, String[] arg3, Type[] arg4) throws CallbackException {
// TODO Auto-generated method stub
return false;
}
/* (non-Javadoc)
* @see net.sf.hibernate.Interceptor#onFlushDirty(java.lang.Object, java.io.Serializable, java.lang.Object[], java.lang.Object[], java.lang.String[], net.sf.hibernate.type.Type[])
*/
public boolean onFlushDirty(Object arg0, Serializable arg1, Object[] arg2, Object[] arg3, String[] arg4, Type[] arg5) throws CallbackException {
if(arg0 instanceof Auditable)
updates.add(arg0);
return false;
}
/* (non-Javadoc)
* @see net.sf.hibernate.Interceptor#onSave(java.lang.Object, java.io.Serializable, java.lang.Object[], java.lang.String[], net.sf.hibernate.type.Type[])
*/
public boolean onSave(Object arg0, Serializable arg1, Object[] arg2, String[] arg3, Type[] arg4) throws CallbackException {
if(arg0 instanceof Auditable)
inserts.add(arg0);
return false;
}
/* (non-Javadoc)
* @see net.sf.hibernate.Interceptor#onDelete(java.lang.Object, java.io.Serializable, java.lang.Object[], java.lang.String[], net.sf.hibernate.type.Type[])
*/
public void onDelete(Object arg0, Serializable arg1, Object[] arg2, String[] arg3, Type[] arg4) throws CallbackException {
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see net.sf.hibernate.Interceptor#preFlush(java.util.Iterator)
*/
public void preFlush(Iterator arg0) throws CallbackException {
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see net.sf.hibernate.Interceptor#postFlush(java.util.Iterator)
*/
public void postFlush(Iterator arg0) throws CallbackException {
log.debug("In postFlush#########..");
try {
for(Iterator itr = inserts.iterator(); itr.hasNext(); ) {
Auditable entity = (Auditable)itr.next();
auditLog.logEvent("create", entity, new Long(1), sessionFactory);
}
for(Iterator itr = updates.iterator(); itr.hasNext(); ) {
Auditable entity = (Auditable)itr.next();
auditLog.logEvent("update", entity, new Long(1), sessionFactory);
}
} catch(HibernateException e) {
} finally {
inserts.clear();
updates.clear();
}
}
/* (non-Javadoc)
* @see net.sf.hibernate.Interceptor#isUnsaved(java.lang.Object)
*/
public Boolean isUnsaved(Object arg0) {
// TODO Auto-generated method stub
return null;
}
/* (non-Javadoc)
* @see net.sf.hibernate.Interceptor#findDirty(java.lang.Object, java.io.Serializable, java.lang.Object[], java.lang.Object[], java.lang.String[], net.sf.hibernate.type.Type[])
*/
public int[] findDirty(Object arg0, Serializable arg1, Object[] arg2, Object[] arg3, String[] arg4, Type[] arg5) {
// TODO Auto-generated method stub
return null;
}
/* (non-Javadoc)
* @see net.sf.hibernate.Interceptor#instantiate(java.lang.Class, java.io.Serializable)
*/
public Object instantiate(Class arg0, Serializable arg1) throws CallbackException {
// TODO Auto-generated method stub
return null;
}
public void logEvent(String message, Auditable entity, Long userId)
throws CallbackException {
Session session = null;
try {
AuditLogRecord record = new AuditLogRecord(message, entity.getId(),
entity.getClass(), userId);
session = sessionFactory.openSession();
session.save(record);
session.flush();
} catch(Exception ex) {
throw new CallbackException(ex);
} finally {
try {
session.close();
} catch(HibernateException ex) {
throw new CallbackException(ex);
}
}
}
}
The AuditLog is the util class which has the logEvent method that logs the event in the DB.