These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Interceptor.onFlushDirty() is being called for inserts
PostPosted: Wed Jun 01, 2011 2:12 pm 
Newbie

Joined: Wed Oct 27, 2010 11:13 am
Posts: 7
Hello-

My interceptor's onFlushDirty() method is being invoked even for newly created entities. My understanding is onSave() is for inserts and onFlushDirty() for updates.

Any pointers will be highly appreciated.
Thanks.
Code:
public class AuditLogInterceptor extends EmptyInterceptor {
   /**
    *
    */
   private static final long serialVersionUID = -634733855120326803L;

   /**
    *
    */
   private XLogger logger = XLoggerFactory
         .getXLogger(AuditLogInterceptor.class.getName());

   private Set<Auditable> inserts = new HashSet<Auditable>();
   private Set<Auditable> updates = new HashSet<Auditable>();

   public AuditLogInterceptor() {
   }

   @Override
   public boolean onSave(Object entity, Serializable id, Object[] state,
         String[] propertyNames, Type[] types) throws CallbackException {
      logger.entry(entity, id, state, propertyNames);

      boolean retVal = false;
      
      if (entity instanceof Auditable) {
         for (int i = 0; i < propertyNames.length; i++) {
            if ("createdOn".equals(propertyNames[i])) {
               state[i] = new Date();
               retVal = true;
            }

            if ("createdBy".equals(propertyNames[i])) {
               state[i] = MLUtils.getUsername();
               retVal = true;
            }
         }

         inserts.add((Auditable) entity);
      }

      logger.exit();

      return retVal;
   }

   @Override
   public boolean onFlushDirty(Object entity, Serializable id,
         Object[] currentState, Object[] previousState,
         String[] propertyNames, Type[] types) throws CallbackException {
      logger.entry(entity, id, currentState, previousState);

      boolean retVal = false;
      
      if (entity instanceof Auditable) {
         for (int i = 0; i < propertyNames.length; i++) {
            if ("updatedOn".equals(propertyNames[i])) {
               currentState[i] = new Date();
               retVal = true;
            }

            if ("updatedBy".equals(propertyNames[i])) {
               currentState[i] = MLUtils.getUsername();
               retVal = true;
            }
            
            System.err.println(propertyNames[i] + ": current sate = " + currentState[i] + ", previous state = " + previousState[i]);
         }

         updates.add((Auditable) entity);
      }

      logger.exit();

      return retVal;
   }

   @Override
   public void postFlush(@SuppressWarnings("rawtypes") Iterator iterator)
         throws CallbackException {
      logger.entry(iterator);

      while (iterator.hasNext()) {
         Object o = iterator.next();
         if (!(o instanceof Auditable)) {
            return;
         }
      }

      try {
         for (Auditable entity : inserts) {
            AuditLog.logEvent("create", entity, MLUtils.getUsername());
         }

         for (Auditable entity : updates) {
            AuditLog.logEvent("update", entity, MLUtils.getUsername());
         }

      } finally {
         inserts.clear();
         updates.clear();
      }

      logger.exit();
   }

}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.