I dont see any log messages at . I was assuming the interceptor would be called when i execute getHibernateTemplate().update(ffe) method in DAO class.
My session-factory.xml file looks like this
<bean id="freeFlagErrorSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource-etrans"/>
</property>
<property name="mappingResources">
<list>
<value>com/wmg/digital/esdc/freeflag/domain/FreeFlagError.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
<prop key="show_sql">true</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<prop key="hibernate.max_fetch_depth">3</prop>
<prop key="hibernate.cglib.use_reflection_optimizer">false</prop>
</props>
</property>
<property name="entityInterceptor">
<bean class="com.wmg.core.audit.AuditInterceptor" />
</property>
Audit interceptor code is
package com.wmg.core.audit;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Date;
import org.acegisecurity.context.SecurityContextHolder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.EmptyInterceptor;
import org.hibernate.Transaction;
import org.hibernate.type.Type;
import com.wmg.core.domain.AuditableDomainObject;
public class AuditInterceptor extends EmptyInterceptor {
private static final long serialVersionUID = 3780988863118850885L;
private static Log log = LogFactory.getLog(AuditInterceptor.class);
private int updates;
private int creates;
public boolean onFlushDirty(Object entity,
Serializable id,
Object[] currentState,
Object[] previousState,
String[] propertyNames,
Type[] types) {
if ( entity instanceof AuditableDomainObject ) {
updates++;
boolean hasDate = false;
boolean hasModifier = false;
if (log.isDebugEnabled() )
log.debug("Found AuditableDomainObject named '" + entity.getClass().getName() + "'");
for ( int i=0; i < propertyNames.length; i++ ) {
if ( "modificationDate".equals( propertyNames[i] ) ) {
if (log.isDebugEnabled() ) logModifiedPre( propertyNames[i], currentState[i] );
currentState[i] = new Timestamp(new Date().getTime());
if (log.isDebugEnabled() ) logModifiedPost( currentState[i] );
hasDate = true;
}
if ( "modifier".equals( propertyNames[i] ) ) {
if (log.isDebugEnabled() ) logModifiedPre( propertyNames[i], currentState[i] );
currentState[i] = getUsername();
if (log.isDebugEnabled() ) logModifiedPost( currentState[i] );
hasModifier = true;
}
if ( hasDate && hasModifier ) {
if (log.isDebugEnabled()) log.debug("found modificationDate and modifier properties");
return true;
}
}
}
return false;
}
private void logModifiedPre( String propertyName, Object state ) {
log.debug("Property '" + propertyName + "' with value of '" + state + "'" );
}
private void logModifiedPost( Object state ) {
log.debug("has changed to '" + state + "'" );
}
public boolean onSave(Object entity,
Serializable id,
Object[] state,
String[] propertyNames,
Type[] types) {
log.debug("INside START OF ONSAVE IN INTERCEPTOR " + SecurityContextHolder.getContext().getAuthentication().getName());
if ( entity instanceof AuditableDomainObject ) {
creates++;
boolean hasCreateDate = false;
boolean hasCreator = false;
boolean hasModificationDate = false;
boolean hasModifier = false;
System.out.println("INside onSave of AuditInterceptor");
if (log.isDebugEnabled() )
log.debug("Found AuditableDomainObject named '" + entity.getClass().getName() + "'");
for ( int i=0; i<propertyNames.length; i++ ) {
if ( "creationDate".equals( propertyNames[i] ) ) {
System.out.println("INside onSave creationDate of AuditInterceptor");
state[i] = new Timestamp(new Date().getTime());
hasCreateDate = true;
if (log.isDebugEnabled()) logNew( propertyNames[i], state[i] );
}
if ( "creator".equals( propertyNames[i] ) ) {
System.out.println("INside onSave creator of AuditInterceptor");
state[i] = getUsername();
hasCreator = true;
if (log.isDebugEnabled()) logNew( propertyNames[i], state[i] );
}
if ( "modificationDate".equals( propertyNames[i] ) ) {
System.out.println("INside onSave modificationDate of AuditInterceptor");
state[i] = new Timestamp(new Date().getTime());
if (log.isDebugEnabled()) logNew( propertyNames[i], state[i] );
hasModificationDate = true;
}
if ( "modifier".equals( propertyNames[i] ) ) {
System.out.println("INside onSave modifier of AuditInterceptor");
state[i] = getUsername();
hasModifier = true;
if (log.isDebugEnabled()) logNew( propertyNames[i], state[i] );
}
if ( hasCreateDate && hasModificationDate && hasCreator && hasModifier ) {
if (log.isDebugEnabled()) log.debug("found creationDate, creator, modificationDate, and modifier properties");
System.out.println("INside onSave ALL FLAGS TRUE of AuditInterceptor");
return true;
}
}
}
return false;
}
private void logNew( String propertyName, Object state ) {
log.debug("Property '" + propertyName + "' is set to '" + state + "'" );
}
public void afterTransactionCompletion(Transaction tx) {
if ( log.isDebugEnabled() ) {
if ( tx.wasCommitted() ) {
log.debug("Creations: " + creates + ", Updates: " + updates);
}
}
updates=0;
creates=0;
}
private String getUsername() {
log.debug("INside AuditInterceptor getUsername " + SecurityContextHolder.getContext().getAuthentication().getName());
return SecurityContextHolder.getContext().getAuthentication().getName();
}
}
|