dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #27817
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 13859: minor refactor in hibernate eventlistener
------------------------------------------------------------
revno: 13859
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2014-01-27 15:02:13 +0700
message:
minor refactor in hibernate eventlistener
modified:
dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateEventListenerWiring.java
--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk
Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateEventListenerWiring.java'
--- dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateEventListenerWiring.java 2014-01-27 05:23:09 +0000
+++ dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateEventListenerWiring.java 2014-01-27 08:02:13 +0000
@@ -32,6 +32,8 @@
import org.hibernate.SessionFactory;
import org.hibernate.event.service.spi.EventListenerRegistry;
import org.hibernate.event.spi.EventType;
+import org.hibernate.event.spi.PostDeleteEvent;
+import org.hibernate.event.spi.PostDeleteEventListener;
import org.hibernate.event.spi.PostInsertEvent;
import org.hibernate.event.spi.PostInsertEventListener;
import org.hibernate.event.spi.PostUpdateEvent;
@@ -65,92 +67,105 @@
private Set<IdentifiableObject> identifiableObjects = new HashSet<IdentifiableObject>();
@PostConstruct
- @SuppressWarnings( "unchecked" )
public void registerListeners()
{
EventListenerRegistry registry = ((SessionFactoryImpl) sessionFactory).getServiceRegistry()
.getService( EventListenerRegistry.class );
- registry.getEventListenerGroup( EventType.PRE_COLLECTION_UPDATE ).appendListener( new PreCollectionUpdateEventListener()
- {
- @Override
- public void onPreUpdateCollection( PreCollectionUpdateEvent event )
- {
- if ( event.getAffectedOwnerOrNull() != null )
- {
- if ( event.getAffectedOwnerOrNull() instanceof IdentifiableObject )
- {
- identifiableObjects.add( (IdentifiableObject) event.getAffectedOwnerOrNull() );
- }
- }
-
- Object newValue = event.getCollection().getValue();
- Serializable oldValue = event.getCollection().getStoredSnapshot();
-
- Collection newCol = new ArrayList();
- Collection oldCol = new ArrayList();
-
- if ( Collection.class.isInstance( newValue ) )
- {
- newCol = new ArrayList( (Collection) newValue );
-
- if ( !newCol.isEmpty() )
- {
- Object next = newCol.iterator().next();
-
- if ( !(next instanceof IdentifiableObject) )
- {
- newCol = new ArrayList();
- }
- }
- }
-
- Map map = (Map) oldValue;
-
- for ( Object o : map.keySet() )
- {
- if ( o instanceof IdentifiableObject )
- {
- oldCol.add( o );
- }
- }
-
- Collection removed = CollectionUtils.subtract( oldCol, newCol );
- Collection added = CollectionUtils.subtract( newCol, oldCol );
-
- identifiableObjects.addAll( removed );
- identifiableObjects.addAll( added );
- }
- } );
-
- registry.getEventListenerGroup( EventType.POST_COMMIT_UPDATE ).appendListener( new PostUpdateEventListener()
- {
- @Override
- public void onPostUpdate( PostUpdateEvent event )
- {
- if ( identifiableObjects.isEmpty() )
- {
- return;
- }
-
- // objectManager.update( new ArrayList<IdentifiableObject>( identifiableObjects ) );
- identifiableObjects.clear();
- }
- } );
-
- registry.getEventListenerGroup( EventType.POST_COMMIT_INSERT ).appendListener( new PostInsertEventListener()
- {
- @Override
- public void onPostInsert( PostInsertEvent event )
- {
- if ( identifiableObjects.isEmpty() )
- {
- return;
- }
-
- // objectManager.update( new ArrayList<IdentifiableObject>( identifiableObjects ) );
- identifiableObjects.clear();
- }
- } );
+ registry.getEventListenerGroup( EventType.PRE_COLLECTION_UPDATE ).appendListener( preCollectionUpdateEventListener );
+ registry.getEventListenerGroup( EventType.POST_COMMIT_UPDATE ).appendListener( postUpdateEventListener );
+ registry.getEventListenerGroup( EventType.POST_COMMIT_INSERT ).appendListener( postInsertEventListener );
+ registry.getEventListenerGroup( EventType.POST_COMMIT_DELETE ).appendListener( postDeleteEventListener );
+ }
+
+ @SuppressWarnings( "unchecked" )
+ private PreCollectionUpdateEventListener preCollectionUpdateEventListener = new PreCollectionUpdateEventListener()
+ {
+ @Override
+ public void onPreUpdateCollection( PreCollectionUpdateEvent event )
+ {
+ if ( event.getAffectedOwnerOrNull() != null )
+ {
+ if ( event.getAffectedOwnerOrNull() instanceof IdentifiableObject )
+ {
+ identifiableObjects.add( (IdentifiableObject) event.getAffectedOwnerOrNull() );
+ }
+ }
+
+ Object newValue = event.getCollection().getValue();
+ Serializable oldValue = event.getCollection().getStoredSnapshot();
+
+ Collection newCol = new ArrayList();
+ Collection oldCol = new ArrayList();
+
+ if ( Collection.class.isInstance( newValue ) )
+ {
+ newCol = new ArrayList( (Collection) newValue );
+
+ if ( !newCol.isEmpty() )
+ {
+ Object next = newCol.iterator().next();
+
+ if ( !(next instanceof IdentifiableObject) )
+ {
+ newCol = new ArrayList();
+ }
+ }
+ }
+
+ Map map = (Map) oldValue;
+
+ for ( Object o : map.keySet() )
+ {
+ if ( o instanceof IdentifiableObject )
+ {
+ oldCol.add( o );
+ }
+ }
+
+ Collection removed = CollectionUtils.subtract( oldCol, newCol );
+ Collection added = CollectionUtils.subtract( newCol, oldCol );
+
+ identifiableObjects.addAll( removed );
+ identifiableObjects.addAll( added );
+ }
+ };
+
+ private PostUpdateEventListener postUpdateEventListener = new PostUpdateEventListener()
+ {
+ @Override
+ public void onPostUpdate( PostUpdateEvent event )
+ {
+ updateIdentifiableObjects();
+ }
+ };
+
+ private PostInsertEventListener postInsertEventListener = new PostInsertEventListener()
+ {
+ @Override
+ public void onPostInsert( PostInsertEvent event )
+ {
+ updateIdentifiableObjects();
+ }
+ };
+
+ private PostDeleteEventListener postDeleteEventListener = new PostDeleteEventListener()
+ {
+ @Override
+ public void onPostDelete( PostDeleteEvent event )
+ {
+ updateIdentifiableObjects();
+ }
+ };
+
+ private void updateIdentifiableObjects()
+ {
+ if ( identifiableObjects.isEmpty() )
+ {
+ return;
+ }
+
+ // objectManager.update( new ArrayList<IdentifiableObject>( identifiableObjects ) );
+ identifiableObjects.clear();
}
}