TimestampInterceptor.java

  1. package org.itracker.persistence.dao;

  2. import org.apache.log4j.Logger;
  3. import org.hibernate.EmptyInterceptor;
  4. import org.hibernate.type.Type;
  5. import org.itracker.model.AbstractEntity;

  6. import java.io.Serializable;
  7. import java.util.Date;

  8. /**
  9.  * A Hibernate Interceptor that automatically sets the <code>createDate</code>
  10.  * and <code>lastModifiedDate</code> of any AbstractEntity instance that is
  11.  * inserted or updated.
  12.  *
  13.  * @author johnny
  14.  */
  15. public class TimestampInterceptor extends EmptyInterceptor {

  16.     /**
  17.      *
  18.      */
  19.     private static final long serialVersionUID = 1L;

  20.     /**
  21.      * The name of the AbstractEntity.createDate property
  22.      */
  23.     private static final String CREATE_DATE_PROPERTY = "createDate";

  24.     /**
  25.      * The name of the AbstractEntity.lastModifiedDate property
  26.      */
  27.     private static final String LAST_MODIFIED_DATE_PROPERTY = "lastModifiedDate";

  28.     @SuppressWarnings("unused")
  29.     private static final transient Logger logger = Logger.getLogger(TimestampInterceptor.class);

  30.     /**
  31.      *
  32.      */
  33.     public TimestampInterceptor() {
  34.     }

  35.     /**
  36.      * Called before inserting an item in the datastore.
  37.      * <p/>
  38.      * <p>The interceptor may modify the state, which will be used for
  39.      * the SQL INSERT and propagated to the persistent object. </p>
  40.      * <p/>
  41.      * <p>Automatically sets the createDate and lastModifiedDate properties. </p>
  42.      *
  43.      * @return true if the timestamp was set and thus the state was modified
  44.      */
  45.     public boolean onSave(Object entity, Serializable id,
  46.                           Object[] state, String[] propertyNames, Type[] types) {

  47.         if (entity instanceof AbstractEntity) {
  48.             final Date timestamp = new Date();

  49.             // Number of properties to set.
  50.             int propertiesSet = 0;

  51.             // Find createDate property to set.
  52.             for (int i = 0; i < propertyNames.length; i++) {

  53.                 if (CREATE_DATE_PROPERTY.equals(propertyNames[i])
  54.                         || LAST_MODIFIED_DATE_PROPERTY.equals(propertyNames[i])) {
  55.                     state[i] = timestamp;

  56. //                    if (this.logger.isDebugEnabled()) {
  57. //                        this.logger.debug("Setting " + propertyNames[i]
  58. //                                + " property of " + entity);
  59. //                    }

  60.                     if (++propertiesSet == 2) {
  61.                         break;
  62.                     }
  63.                 }
  64.             }
  65.             return (propertiesSet > 0);
  66.         }
  67.         return false;
  68.     }

  69.     /**
  70.      * Called before updating the datastore.
  71.      * <p/>
  72.      * <p>Called when an object is detected to be dirty, during a flush.
  73.      * The interceptor may modify the detected currentState, which will be
  74.      * propagated to both the database and the persistent object. <br>
  75.      * Note that not all flushes end in actual synchronization
  76.      * with the database, in which case the new currentState will be
  77.      * propagated to the object, but not necessarily (immediately)
  78.      * to the database. It is strongly recommended that the interceptor
  79.      * not modify the previousState. </p>
  80.      * <p/>
  81.      * <p>Automatically sets the lastModifiedDate property. </p>
  82.      *
  83.      * @return true if the timestamp was set and thus the currentState was modified
  84.      */
  85.     public boolean onFlushDirty(Object entity, Serializable id,
  86.                                 Object[] currentState, Object[] previousState,
  87.                                 String[] propertyNames, Type[] types) {

  88.         if (entity instanceof AbstractEntity) {
  89.             final Date timestamp = new Date();

  90.             // Find lastModifiedDate property to set.
  91.             for (int i = 0; i < propertyNames.length; i++) {

  92.                 if (LAST_MODIFIED_DATE_PROPERTY.equals(propertyNames[i])) {
  93.                     currentState[i] = timestamp;

  94. //                    if (this.logger.isDebugEnabled()) {
  95. //                        this.logger.debug("Setting " + propertyNames[i]
  96. //                                + " property of " + entity);
  97. //                    }
  98.                     return true;
  99.                 }
  100.             }
  101.         }
  102.         return false;
  103.     }

  104. }