1 package org.hibernate.ce.auction.persistence.audit; 2 3 import org.hibernate.*; 4 import org.hibernate.type.Type; 5 6 import java.io.Serializable ; 7 import java.util.*; 8 9 import org.hibernate.ce.auction.model.*; 10 import org.apache.commons.logging.*; 11 12 public class AuditLogInterceptor implements Interceptor { 13 14 private static Log log = LogFactory.getLog(AuditLogInterceptor.class); 15 16 private Session session; 17 private Long userId; 18 19 private Set inserts = new HashSet(); 20 private Set updates = new HashSet(); 21 22 public void setSession(Session session) { 23 this.session=session; 24 } 25 public void setUserId(Long userId) { 26 this.userId=userId; 27 } 28 29 public boolean onSave(Object entity, 30 Serializable id, 31 Object [] state, 32 String [] propertyNames, 33 Type[] types) 34 throws CallbackException { 35 36 if (entity instanceof Auditable) 37 inserts.add(entity); 38 39 return false; 40 } 41 42 public boolean onFlushDirty(Object entity, 43 Serializable id, 44 Object [] currentState, 45 Object [] previousState, 46 String [] propertyNames, 47 Type[] types) 48 throws CallbackException { 49 50 if (entity instanceof Auditable) 51 updates.add(entity); 52 53 return false; 54 } 55 56 public boolean onLoad(Object o, Serializable serializable, Object [] objects, String [] strings, Type[] types) throws CallbackException { 57 return false; 58 } 59 60 public void onDelete(Object o, Serializable serializable, Object [] objects, String [] strings, Type[] types) throws CallbackException { 61 } 62 63 public void preFlush(Iterator iterator) throws CallbackException { 64 } 65 66 public void postFlush(Iterator iterator) throws CallbackException { 67 try { 68 for (Iterator it = inserts.iterator(); it.hasNext();) { 69 Auditable entity = (Auditable) it.next(); 70 log.debug("Intercepted creation of : " + entity); 71 AuditLog.logEvent("create", 72 entity, 73 userId, 74 session.connection()); 75 } 76 for (Iterator it = updates.iterator(); it.hasNext();) { 77 Auditable entity = (Auditable) it.next(); 78 log.debug("Intercepted modification of : " + entity); 79 AuditLog.logEvent("update", 80 entity, 81 userId, 82 session.connection()); 83 } 84 } catch (HibernateException ex) { 85 throw new CallbackException(ex); 86 } finally { 87 inserts.clear(); 88 updates.clear(); 89 } 90 } 91 92 public int[] findDirty(Object o, Serializable serializable, Object [] objects, Object [] objects1, String [] strings, Type[] types) { 93 return null; 94 } 95 96 public Object instantiate(Class aClass, Serializable serializable) throws CallbackException { 97 return null; 98 } 99 100 public Boolean isTransient(Object o) { 101 return null; 102 } 103 104 public Object instantiate(String s, Serializable serializable) throws CallbackException { 105 return null; 106 } 107 108 public String getEntityName(Object o) throws CallbackException { 109 return null; 110 } 111 112 public Object getEntity(String s, Serializable serializable) throws CallbackException { 113 return null; 114 } 115 116 public Object instantiate(String entityName, EntityMode entityMode, Serializable id) throws CallbackException { 117 return null; } 119 120 public void afterTransactionBegin(Transaction tx) { 121 } 123 124 public void beforeTransactionCompletion(Transaction tx) { 125 } 127 128 public void afterTransactionCompletion(Transaction tx) { 129 } 131 } 132
| Popular Tags
|