View Javadoc

1   package com.melloware.jukes.db.audit;
2   
3   import java.io.Serializable;
4   import java.util.Date;
5   import java.util.Iterator;
6   
7   import org.apache.commons.lang.StringUtils;
8   import org.apache.commons.lang.SystemUtils;
9   import org.apache.commons.logging.Log;
10  import org.apache.commons.logging.LogFactory;
11  import org.hibernate.CallbackException;
12  import org.hibernate.EntityMode;
13  import org.hibernate.Interceptor;
14  import org.hibernate.Transaction;
15  import org.hibernate.type.Type;
16  
17  /**
18   * Audit inteceptor so any Hibernate insert or update will update the
19   * CREATED_USER and MODIFIED_USER as well as CREATED_DATE on inserts.
20   * <p>
21   * Copyright (c) 1999-2007 Melloware, Inc. <http://www.melloware.com>
22   * @author Emil A. Lefkof III <info@melloware.com>
23   * @version 4.0
24   * @see com.melloware.jukes.db.audit.Auditable
25   */
26  public class AuditInterceptor
27      implements Interceptor,
28                 Serializable {
29  
30      private static final Log LOG = LogFactory.getLog(AuditInterceptor.class);
31      private final String user;
32  
33      /**
34       * Default constructor
35       */
36      public AuditInterceptor() {
37          super();
38          LOG.info("AuditInterceptor is created.");
39          user = StringUtils.defaultIfEmpty(SystemUtils.USER_NAME, "java");
40      }
41  
42      /* (non-Javadoc)
43       * @see org.hibernate.Interceptor#getEntity(java.lang.String, java.io.Serializable)
44       */
45      public Object getEntity(String arg0, Serializable arg1)
46                       throws CallbackException {
47          return null;
48      }
49  
50      /* (non-Javadoc)
51       * @see org.hibernate.Interceptor#getEntityName(java.lang.Object)
52       */
53      public String getEntityName(Object arg0)
54                           throws CallbackException {
55          return null;
56      }
57  
58      /* (non-Javadoc)
59       * @see org.hibernate.Interceptor#isTransient(java.lang.Object)
60       */
61      public Boolean isTransient(Object arg0) {
62          return null;
63      }
64  
65      /* (non-Javadoc)
66       * @see org.hibernate.Interceptor#afterTransactionBegin(org.hibernate.Transaction)
67       */
68      public void afterTransactionBegin(Transaction arg0) {
69          // do nothing
70  
71      }
72  
73      /* (non-Javadoc)
74       * @see org.hibernate.Interceptor#afterTransactionCompletion(org.hibernate.Transaction)
75       */
76      public void afterTransactionCompletion(Transaction arg0) {
77          // do nothing
78  
79      }
80  
81      /* (non-Javadoc)
82       * @see org.hibernate.Interceptor#beforeTransactionCompletion(org.hibernate.Transaction)
83       */
84      public void beforeTransactionCompletion(Transaction arg0) {
85          // do nothing
86  
87      }
88  
89      /* (non-Javadoc)
90       * @see org.hibernate.Interceptor#findDirty(java.lang.Object, java.io.Serializable, java.lang.Object[],
91       * java.lang.Object[], java.lang.String[], org.hibernate.type.Type[])
92       */
93      public int[] findDirty(Object arg0, Serializable arg1, Object[] arg2, Object[] arg3, String[] arg4, Type[] arg5) {
94          return null;
95      }
96  
97      /* (non-Javadoc)
98       * @see org.hibernate.Interceptor#instantiate(java.lang.String, org.hibernate.EntityMode, java.io.Serializable)
99       */
100     public Object instantiate(String arg0, EntityMode arg1, Serializable arg2)
101                        throws CallbackException {
102         return null;
103     }
104 
105     /* (non-Javadoc)
106      * @see org.hibernate.Interceptor#onCollectionRecreate(java.lang.Object, java.io.Serializable)
107      */
108     public void onCollectionRecreate(Object arg0, Serializable arg1)
109                               throws CallbackException {
110         // do nothing
111 
112     }
113 
114     /* (non-Javadoc)
115      * @see org.hibernate.Interceptor#onCollectionRemove(java.lang.Object, java.io.Serializable)
116      */
117     public void onCollectionRemove(Object arg0, Serializable arg1)
118                             throws CallbackException {
119         // do nothing
120     }
121 
122     /* (non-Javadoc)
123      * @see org.hibernate.Interceptor#onCollectionUpdate(java.lang.Object, java.io.Serializable)
124      */
125     public void onCollectionUpdate(Object arg0, Serializable arg1)
126                             throws CallbackException {
127         // do nothing
128 
129     }
130 
131     /* (non-Javadoc)
132      * @see org.hibernate.Interceptor#onDelete(java.lang.Object, java.io.Serializable, java.lang.Object[],
133      * java.lang.String[], org.hibernate.type.Type[])
134      */
135     public void onDelete(Object arg0, Serializable arg1, Object[] arg2, String[] arg3, Type[] arg4)
136                   throws CallbackException {
137         // do nothing
138 
139     }
140 
141     public boolean onFlushDirty(Object entity,
142                                 Serializable id,
143                                 Object[] currentState,
144                                 Object[] previousState,
145                                 String[] propertyNames,
146                                 Type[] types)
147                          throws CallbackException {
148 
149         boolean result = false;
150 
151         if (entity instanceof Auditable) {
152             for (int i = 0; i < propertyNames.length; i++) {
153                 if ("modifiedUser".equals(propertyNames[i])) {
154                     currentState[i] = this.user;
155                     result = true;
156                 }
157             }
158         }
159         return result;
160     }
161 
162     /* (non-Javadoc)
163      * @see org.hibernate.Interceptor#onLoad(java.lang.Object, java.io.Serializable, java.lang.Object[],
164      * java.lang.String[], org.hibernate.type.Type[])
165      */
166     public boolean onLoad(Object arg0, Serializable arg1, Object[] arg2, String[] arg3, Type[] arg4)
167                    throws CallbackException {
168         return false;
169     }
170 
171     /* (non-Javadoc)
172      * @see org.hibernate.Interceptor#onPrepareStatement(java.lang.String)
173      */
174     public String onPrepareStatement(String arg0) {
175         return arg0;
176     }
177 
178     public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types)
179                    throws CallbackException {
180 
181         boolean result = false;
182 
183         if (entity instanceof Auditable) {
184             for (int i = 0; i < propertyNames.length; i++) {
185                 if ("createdUser".equals(propertyNames[i])) {
186                     state[i] = this.user;
187                     result = true;
188                 }
189                 if ("modifiedUser".equals(propertyNames[i])) {
190                     state[i] = this.user;
191                     result = true;
192                 }
193                 if (("createdDate".equals(propertyNames[i])) && (state[i] == null)) {
194                     state[i] = new Date();
195                     result = true;
196                 }
197                 if ("modifiedDate".equals(propertyNames[i])) {
198                     state[i] = new Date();
199                     result = true;
200                 }
201             }
202         }
203         return result;
204     }
205 
206     /* (non-Javadoc)
207      * @see org.hibernate.Interceptor#postFlush(java.util.Iterator)
208      */
209     public void postFlush(Iterator arg0)
210                    throws CallbackException {
211         // do nothing
212 
213     }
214 
215     /* (non-Javadoc)
216      * @see org.hibernate.Interceptor#preFlush(java.util.Iterator)
217      */
218     public void preFlush(Iterator arg0)
219                   throws CallbackException {
220         // do nothing
221 
222     }
223 
224 }