KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > ce > auction > persistence > audit > AuditLog


1 package org.hibernate.ce.auction.persistence.audit;
2
3 import org.hibernate.*;
4
5 import java.io.Serializable JavaDoc;
6 import java.sql.Connection JavaDoc;
7
8 import org.hibernate.ce.auction.persistence.HibernateUtil;
9 import org.hibernate.ce.auction.model.Auditable;
10
11 /**
12  * The audit log helper that logs actual events.
13  * <p>
14  * The <tt>logEvent()</tt> method needs a JDBC connection, it will
15  * open a new Hibernate <tt>Session</tt> on that connection and
16  * persist the event. The temporary <tt>Session</tt> is then closed,
17  * transaction handling is left to the client calling this method.
18  *
19  * @author Christian Bauer <christian@hibernate.org>
20  */

21 public class AuditLog {
22
23     public static void logEvent(
24         String JavaDoc message,
25         Auditable entity,
26         Long JavaDoc userId,
27         Connection JavaDoc connection)
28         throws CallbackException {
29
30         Session tempSession =
31           HibernateUtil.getSessionFactory().openSession(connection);
32
33         try {
34             AuditLogRecord record =
35                 new AuditLogRecord(message,
36                                    entity.getId(),
37                                    entity.getClass(),
38                                    userId );
39
40             tempSession.save(record);
41             tempSession.flush();
42
43         } catch (Exception JavaDoc ex) {
44             throw new CallbackException(ex);
45
46         } finally {
47             try {
48                 tempSession.close();
49             } catch (HibernateException ex) {
50                 throw new CallbackException(ex);
51             }
52         }
53     }
54 }
55
Popular Tags