Search in sources :

Example 1 with AuditLogRecord

use of org.jpwh.model.filtering.interceptor.AuditLogRecord in project microservices by pwillhan.

the class AuditLogInterceptor method postFlush.

/* 
       This method is called after flushing of the persistence context is complete.
       Here, you write the audit log records for all insertions and updates you
       collected earlier.
     */
public void postFlush(Iterator iterator) throws CallbackException {
    /* 
           You are not allowed to access the original persistence context, the
           <code>Session</code> that is currently executing this interceptor.
           The <code>Session</code> is in a fragile state during interceptor calls.
           Hibernate allows you to create a new <code>Session</code> that
           inherits some information from the original <code>Session</code> with
           the <code>sessionWithOptions()</code> method. Here the new temporary
           <code>Session</code> works with the same transaction and database
           connection as the original <code>Session</code>.
         */
    Session tempSession = currentSession.sessionWithOptions().transactionContext().connection().openSession();
    try {
        /* 
               You store a new <code>AuditLogRecord</code> for each insertion and
               update using the temporary <code>Session</code>.
             */
        for (Auditable entity : inserts) {
            tempSession.persist(new AuditLogRecord("insert", entity, currentUserId));
        }
        for (Auditable entity : updates) {
            tempSession.persist(new AuditLogRecord("update", entity, currentUserId));
        }
        /* 
               You flush and close the temporary <code>Session</code>
               independently from the original <code>Session</code>.
             */
        tempSession.flush();
    } finally {
        tempSession.close();
        inserts.clear();
        updates.clear();
    }
}
Also used : Auditable(org.jpwh.model.filtering.interceptor.Auditable) AuditLogRecord(org.jpwh.model.filtering.interceptor.AuditLogRecord) Session(org.hibernate.Session)

Example 2 with AuditLogRecord

use of org.jpwh.model.filtering.interceptor.AuditLogRecord in project microservices by pwillhan.

the class AuditLogging method writeAuditLog.

@Test
public void writeAuditLog() throws Throwable {
    UserTransaction tx = TM.getUserTransaction();
    try {
        Long CURRENT_USER_ID;
        {
            tx.begin();
            EntityManager em = JPA.createEntityManager();
            User currentUser = new User("johndoe");
            em.persist(currentUser);
            tx.commit();
            em.close();
            CURRENT_USER_ID = currentUser.getId();
        }
        EntityManagerFactory emf = JPA.getEntityManagerFactory();
        Map<String, String> properties = new HashMap<String, String>();
        properties.put(org.hibernate.jpa.AvailableSettings.SESSION_INTERCEPTOR, AuditLogInterceptor.class.getName());
        EntityManager em = emf.createEntityManager(properties);
        Session session = em.unwrap(Session.class);
        AuditLogInterceptor interceptor = (AuditLogInterceptor) ((SessionImplementor) session).getInterceptor();
        interceptor.setCurrentSession(session);
        interceptor.setCurrentUserId(CURRENT_USER_ID);
        tx.begin();
        em.joinTransaction();
        Item item = new Item("Foo");
        em.persist(item);
        tx.commit();
        em.clear();
        tx.begin();
        em.joinTransaction();
        List<AuditLogRecord> logs = em.createQuery("select lr from AuditLogRecord lr", AuditLogRecord.class).getResultList();
        assertEquals(logs.size(), 1);
        assertEquals(logs.get(0).getMessage(), "insert");
        assertEquals(logs.get(0).getEntityClass(), Item.class);
        assertEquals(logs.get(0).getEntityId(), item.getId());
        assertEquals(logs.get(0).getUserId(), CURRENT_USER_ID);
        em.createQuery("delete AuditLogRecord").executeUpdate();
        tx.commit();
        em.clear();
        tx.begin();
        em.joinTransaction();
        item = em.find(Item.class, item.getId());
        item.setName("Bar");
        tx.commit();
        em.clear();
        tx.begin();
        em.joinTransaction();
        logs = em.createQuery("select lr from AuditLogRecord lr", AuditLogRecord.class).getResultList();
        assertEquals(logs.size(), 1);
        assertEquals(logs.get(0).getMessage(), "update");
        assertEquals(logs.get(0).getEntityClass(), Item.class);
        assertEquals(logs.get(0).getEntityId(), item.getId());
        assertEquals(logs.get(0).getUserId(), CURRENT_USER_ID);
        tx.commit();
        em.close();
    } finally {
        TM.rollback();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) User(org.jpwh.model.filtering.interceptor.User) HashMap(java.util.HashMap) Item(org.jpwh.model.filtering.interceptor.Item) AuditLogRecord(org.jpwh.model.filtering.interceptor.AuditLogRecord) EntityManager(javax.persistence.EntityManager) EntityManagerFactory(javax.persistence.EntityManagerFactory) Session(org.hibernate.Session) JPATest(org.jpwh.env.JPATest) Test(org.testng.annotations.Test)

Aggregations

Session (org.hibernate.Session)2 AuditLogRecord (org.jpwh.model.filtering.interceptor.AuditLogRecord)2 HashMap (java.util.HashMap)1 EntityManager (javax.persistence.EntityManager)1 EntityManagerFactory (javax.persistence.EntityManagerFactory)1 UserTransaction (javax.transaction.UserTransaction)1 JPATest (org.jpwh.env.JPATest)1 Auditable (org.jpwh.model.filtering.interceptor.Auditable)1 Item (org.jpwh.model.filtering.interceptor.Item)1 User (org.jpwh.model.filtering.interceptor.User)1 Test (org.testng.annotations.Test)1