package auction.persistence.audit;
import org.hibernate.*;
import org.hibernate.type.Type;
import java.io.Serializable;
import java.util.*;
import auction.model.*;
import org.apache.commons.logging.*;
/**
* Logs a simple audit trail for entity classes marked with Auditable.
*
* When objects are updated or stored, this interceptor puts them in queues.
* After flushing is complete, this interceptor iterates through the queues and
* stores AuditLogRecord instances with the helper class AuditLog.
* Because you can't access a Session inside its own interceptor, a
* temporary second Session is opened on the same JDBC connection and
* inside the same transaction, just to store the audit log objects.
*
* @see Auditable
* @see AuditLog
* @see AuditLogRecord
* @author Christian Bauer
*/
public class AuditLogInterceptor extends EmptyInterceptor {
private static Log log = LogFactory.getLog(AuditLogInterceptor.class);
private Session session;
private Long userId;
private Set inserts = new HashSet();
private Set updates = new HashSet();
public void setSession(Session session) {
this.session=session;
}
public void setUserId(Long userId) {
this.userId=userId;
}
public boolean onSave(Object entity,
Serializable id,
Object[] state,
String[] propertyNames,
Type[] types)
throws CallbackException {
if (entity instanceof Auditable)
inserts.add((Auditable)entity);
return false;
}
public boolean onFlushDirty(Object entity,
Serializable id,
Object[] currentState,
Object[] previousState,
String[] propertyNames,
Type[] types)
throws CallbackException {
if (entity instanceof Auditable)
updates.add((Auditable)entity);
return false;
}
public void postFlush(Iterator iterator) throws CallbackException {
try {
for (Auditable entity : inserts) {
log.debug("Intercepted creation of : " + entity);
AuditLog.logEvent("create",
entity,
userId,
session.connection());
}
for (Auditable entity : updates) {
log.debug("Intercepted modification of : " + entity);
AuditLog.logEvent("update",
entity,
userId,
session.connection());
}
} finally {
inserts.clear();
updates.clear();
}
}
}