← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 13848: dependency updater for collection, wip (updating of deps have been disabled for now)

 

------------------------------------------------------------
revno: 13848
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Sat 2014-01-25 16:38:07 +0700
message:
  dependency updater for collection, wip (updating of deps have been disabled for now)
added:
  dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateEntityInterceptorWiring.java
modified:
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitGroupService.java
  dhis-2/dhis-support/dhis-support-hibernate/src/main/resources/META-INF/dhis/beans.xml


--
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-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitGroupService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitGroupService.java	2014-01-23 08:56:39 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitGroupService.java	2014-01-25 09:38:07 +0000
@@ -28,9 +28,6 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import org.apache.commons.collections.CollectionUtils;
-import org.hisp.dhis.common.IdentifiableObject;
-import org.hisp.dhis.common.IdentifiableObjectManager;
 import org.hisp.dhis.i18n.I18nService;
 import org.hisp.dhis.system.util.Filter;
 import org.hisp.dhis.system.util.FilterUtils;
@@ -96,36 +93,22 @@
         this.organisationUnitService = organisationUnitService;
     }
 
-    private IdentifiableObjectManager objectManager;
-
-    @Autowired
-    public void setObjectManager( IdentifiableObjectManager objectManager )
-    {
-        this.objectManager = objectManager;
-    }
-
     // -------------------------------------------------------------------------
     // OrganisationUnitGroup
     // -------------------------------------------------------------------------
 
     public int addOrganisationUnitGroup( OrganisationUnitGroup organisationUnitGroup )
     {
-        objectManager.update( new ArrayList<IdentifiableObject>( organisationUnitGroup.getMembers() ) );
-
         return organisationUnitGroupStore.save( organisationUnitGroup );
     }
 
     public void updateOrganisationUnitGroup( OrganisationUnitGroup organisationUnitGroup )
     {
-        objectManager.update( new ArrayList<IdentifiableObject>( organisationUnitGroup.getMembers() ) );
-
         organisationUnitGroupStore.update( organisationUnitGroup );
     }
 
     public void deleteOrganisationUnitGroup( OrganisationUnitGroup organisationUnitGroup )
     {
-        objectManager.update( new ArrayList<IdentifiableObject>( organisationUnitGroup.getMembers() ) );
-
         organisationUnitGroupStore.delete( organisationUnitGroup );
     }
 
@@ -364,7 +347,6 @@
     }
 
     @Override
-    @SuppressWarnings("unchecked")
     public void mergeWithCurrentUserOrganisationUnits( OrganisationUnitGroup organisationUnitGroup, Collection<OrganisationUnit> mergeOrganisationUnits )
     {
         Set<OrganisationUnit> organisationUnits = new HashSet<OrganisationUnit>( organisationUnitGroup.getMembers() );
@@ -379,9 +361,6 @@
         organisationUnits.removeAll( userOrganisationUnits );
         organisationUnits.addAll( mergeOrganisationUnits );
 
-        Collection<OrganisationUnit> removedOrgUnits = CollectionUtils.subtract( organisationUnitGroup.getMembers(), organisationUnits );
-        objectManager.update( new ArrayList<IdentifiableObject>( removedOrgUnits ) );
-
         organisationUnitGroup.updateOrganisationUnits( organisationUnits );
 
         updateOrganisationUnitGroup( organisationUnitGroup );

=== added 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	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateEntityInterceptorWiring.java	2014-01-25 09:38:07 +0000
@@ -0,0 +1,146 @@
+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 <mortenoh@xxxxxxxxx>
+ */
+public class HibernateEntityInterceptorWiring
+{
+    @Autowired
+    private SessionFactory sessionFactory;
+
+    @Autowired
+    private IdentifiableObjectManager objectManager;
+
+    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 ( 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<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();
+            }
+        } );
+    }
+}

=== 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	2012-12-14 13:46:47 +0000
+++ dhis-2/dhis-support/dhis-support-hibernate/src/main/resources/META-INF/dhis/beans.xml	2014-01-25 09:38:07 +0000
@@ -42,6 +42,8 @@
     <property name="hibernateConfigurationProvider" ref="hibernateConfigurationProvider" />
   </bean>
 
+  <bean id="hibernateEntityInterceptorWiring" class="org.hisp.dhis.hibernate.HibernateEntityInterceptorWiring" />
+
   <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
     <property name="dataSource" ref="dataSource" />
     <property name="hibernateProperties" ref="hibernateProperties" />