=== removed file 'dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateEntityInterceptorWiring.java' --- dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateEntityInterceptorWiring.java 2014-01-27 04:34:27 +0000 +++ dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateEntityInterceptorWiring.java 1970-01-01 00:00:00 +0000 @@ -1,156 +0,0 @@ -package org.hisp.dhis.hibernate; - -/* - * Copyright (c) 2004-2013, University of Oslo - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * Neither the name of the HISP project nor the names of its contributors may - * be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -import org.apache.commons.collections.CollectionUtils; -import org.hibernate.SessionFactory; -import org.hibernate.event.service.spi.EventListenerRegistry; -import org.hibernate.event.spi.EventType; -import org.hibernate.event.spi.PostInsertEvent; -import org.hibernate.event.spi.PostInsertEventListener; -import org.hibernate.event.spi.PostUpdateEvent; -import org.hibernate.event.spi.PostUpdateEventListener; -import org.hibernate.event.spi.PreCollectionUpdateEvent; -import org.hibernate.event.spi.PreCollectionUpdateEventListener; -import org.hibernate.internal.SessionFactoryImpl; -import org.hisp.dhis.common.IdentifiableObject; -import org.hisp.dhis.common.IdentifiableObjectManager; -import org.springframework.beans.factory.annotation.Autowired; - -import javax.annotation.PostConstruct; -import java.io.Serializable; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - -/** - * @author Morten Olav Hansen - */ -public class HibernateEntityInterceptorWiring -{ - @Autowired - private SessionFactory sessionFactory; - - @Autowired - private IdentifiableObjectManager objectManager; - - private Set identifiableObjects = new HashSet(); - - @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(); - } - } - } - - if ( Map.class.isInstance( oldValue ) ) - { - Map map = (Map) oldValue; - - for ( Object o : map.keySet() ) - { - 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( 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( identifiableObjects ) ); - identifiableObjects.clear(); - } - } ); - } -} === added 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 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateEventListenerWiring.java 2014-01-27 04:59:20 +0000 @@ -0,0 +1,156 @@ +package org.hisp.dhis.hibernate; + +/* + * Copyright (c) 2004-2013, University of Oslo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * Neither the name of the HISP project nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +import org.apache.commons.collections.CollectionUtils; +import org.hibernate.SessionFactory; +import org.hibernate.event.service.spi.EventListenerRegistry; +import org.hibernate.event.spi.EventType; +import org.hibernate.event.spi.PostInsertEvent; +import org.hibernate.event.spi.PostInsertEventListener; +import org.hibernate.event.spi.PostUpdateEvent; +import org.hibernate.event.spi.PostUpdateEventListener; +import org.hibernate.event.spi.PreCollectionUpdateEvent; +import org.hibernate.event.spi.PreCollectionUpdateEventListener; +import org.hibernate.internal.SessionFactoryImpl; +import org.hisp.dhis.common.IdentifiableObject; +import org.hisp.dhis.common.IdentifiableObjectManager; +import org.springframework.beans.factory.annotation.Autowired; + +import javax.annotation.PostConstruct; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +/** + * @author Morten Olav Hansen + */ +public class HibernateEventListenerWiring +{ + @Autowired + private SessionFactory sessionFactory; + + @Autowired + private IdentifiableObjectManager objectManager; + + private Set identifiableObjects = new HashSet(); + + @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(); + } + } + } + + if ( Map.class.isInstance( oldValue ) ) + { + Map map = (Map) oldValue; + + for ( Object o : map.keySet() ) + { + 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( 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( identifiableObjects ) ); + identifiableObjects.clear(); + } + } ); + } +} === modified file 'dhis-2/dhis-support/dhis-support-hibernate/src/main/resources/META-INF/dhis/beans.xml' --- dhis-2/dhis-support/dhis-support-hibernate/src/main/resources/META-INF/dhis/beans.xml 2014-01-25 09:38:07 +0000 +++ dhis-2/dhis-support/dhis-support-hibernate/src/main/resources/META-INF/dhis/beans.xml 2014-01-27 04:59:20 +0000 @@ -42,7 +42,7 @@ - +