KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > ejb > callback > EJB3FlushEntityEventListener


1 //$Id: EJB3FlushEntityEventListener.java,v 1.2 2005/07/29 23:21:55 epbernard Exp $
2
package org.hibernate.ejb.callback;
3
4 import org.hibernate.EntityMode;
5 import org.hibernate.SessionFactory;
6 import org.hibernate.engine.EntityEntry;
7 import org.hibernate.engine.SessionImplementor;
8 import org.hibernate.engine.Status;
9 import org.hibernate.event.def.DefaultFlushEntityEventListener;
10 import org.hibernate.metadata.ClassMetadata;
11 import org.hibernate.persister.entity.EntityPersister;
12 import org.hibernate.type.Type;
13
14 /**
15  * Overrides the LifeCycle OnSave call to call the PreUpdate operation
16  *
17  * @author Emmanuel Bernard
18  */

19 public class EJB3FlushEntityEventListener extends DefaultFlushEntityEventListener {
20     private EntityCallbackHandler callbackHandler;
21
22     public EJB3FlushEntityEventListener(EntityCallbackHandler callbackHandler) {
23         super();
24         this.callbackHandler = callbackHandler;
25     }
26
27     @Override JavaDoc protected boolean invokeInterceptor(
28             SessionImplementor session, Object JavaDoc entity, EntityEntry entry, Object JavaDoc[] values, EntityPersister persister
29             ) {
30         boolean isDirty = false;
31         if ( entry.getStatus() != Status.DELETED ) {
32             if ( callbackHandler.preUpdate( entity ) ) {
33                 isDirty = copyState( entity, persister.getPropertyTypes(), values, session.getFactory() );
34             }
35         }
36         return super.invokeInterceptor( session, entity, entry, values, persister ) || isDirty;
37     }
38
39     /**
40      * copy the entity state into the state array and return true if the state has changed
41      */

42     private boolean copyState(Object JavaDoc entity, Type[] types, Object JavaDoc[] state, SessionFactory sf) {
43         ClassMetadata metadata = sf.getClassMetadata( entity.getClass() );
44         Object JavaDoc[] newState = metadata.getPropertyValues( entity, EntityMode.POJO );
45         int size = newState.length;
46         boolean isDirty = false;
47         for ( int index = 0 ; index < size ; index++ ) {
48             if ( !types[index].isEqual( state[index], newState[index], EntityMode.POJO ) ) {
49                 isDirty = true;
50                 state[index] = newState[index];
51             }
52         }
53         return isDirty;
54     }
55 }
56
Popular Tags