← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 7470: Improved object delete performance by utilizing bi-directional associations

 

------------------------------------------------------------
revno: 7470
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Sun 2012-07-01 09:12:58 +0200
message:
  Improved object delete performance by utilizing bi-directional associations
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/GenericStore.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/CompleteDataSetRegistrationService.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/CompleteDataSetRegistrationStore.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/minmax/MinMaxDataElementService.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/minmax/MinMaxDataElementStore.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserService.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserStore.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/DataElementDeletionHandler.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/DataElementGroupDeletionHandler.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/DataElementGroupSetDeletionHandler.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/CompleteDataSetRegistrationDeletionHandler.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/DataSetDeletionHandler.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/DefaultCompleteDataSetRegistrationService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/SectionDeletionHandler.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/hibernate/HibernateCompleteDataSetRegistrationStore.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/indicator/IndicatorDeletionHandler.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/indicator/IndicatorGroupDeletionHandler.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/indicator/IndicatorGroupSetDeletionHandler.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/minmax/DefaultMinMaxDataElementService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/minmax/MinMaxDataElementDeletionHandler.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/minmax/hibernate/HibernateMinMaxDataElementStore.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitDeletionHandler.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitGroupDeletionHandler.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitGroupSetDeletionHandler.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/translation/TranslationDeletionHandler.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/DefaultUserService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/UserAuthorityGroupDeletionHandler.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/UserCredentialsDeletionHandler.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/UserDeletionHandler.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/UserSettingDeletionHandler.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/hibernate/HibernateUserStore.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/validation/ValidationRuleDeletionHandler.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/validation/ValidationRuleGroupDeletionHandler.java
  dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml
  dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/PatientDeletionHandler.java
  dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/ProgramDeletionHandler.java
  dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/ProgramStageDataElementDeletionHandler.java
  dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/dashboard/DashboardContentDeletionHandler.java
  dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateGenericStore.java
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-user/src/main/java/org/hisp/dhis/user/action/RemoveUserAction.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-api/src/main/java/org/hisp/dhis/common/GenericStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/GenericStore.java	2012-06-06 13:31:45 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/GenericStore.java	2012-07-01 07:12:58 +0000
@@ -79,7 +79,7 @@
      *         proxy.
      */
     T load( int id );
-
+    
     /**
      * Retrieves a Collection of objects where the name is like the given name.
      * 

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/CompleteDataSetRegistrationService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/CompleteDataSetRegistrationService.java	2012-03-29 16:45:18 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/CompleteDataSetRegistrationService.java	2012-07-01 07:12:58 +0000
@@ -123,4 +123,11 @@
      * @param dataSet the DataSet.
      */
     void deleteCompleteDataSetRegistrations( DataSet dataSet );
+
+    /**
+     * Deletes the CompleteDataSetRegistrations associated with the given OrganisationUnit.
+     * 
+     * @param unit the OrganisationUnit.
+     */
+    void deleteCompleteDataSetRegistrations( OrganisationUnit unit );
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/CompleteDataSetRegistrationStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/CompleteDataSetRegistrationStore.java	2012-03-29 16:45:18 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/CompleteDataSetRegistrationStore.java	2012-07-01 07:12:58 +0000
@@ -124,4 +124,11 @@
      * @param dataSet the DataSet.
      */
     void deleteCompleteDataSetRegistrations( DataSet dataSet );
+    
+    /**
+     * Deletes the CompleteDataSetRegistrations associated with the given OrganisationUnit.
+     * 
+     * @param unit the OrganisationUnit.
+     */
+    void deleteCompleteDataSetRegistrations( OrganisationUnit unit );
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/minmax/MinMaxDataElementService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/minmax/MinMaxDataElementService.java	2011-12-26 10:07:59 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/minmax/MinMaxDataElementService.java	2012-07-01 07:12:58 +0000
@@ -56,4 +56,10 @@
     Collection<MinMaxDataElement> getMinMaxDataElements( OrganisationUnit source, Collection<DataElement> dataElements );
     
     Collection<MinMaxDataElement> getAllMinMaxDataElements();
+
+    void removeMinMaxDataElements( OrganisationUnit organisationUnit );
+    
+    void removeMinMaxDataElements( DataElement dataElement );
+    
+    void removeMinMaxDataElements( DataElementCategoryOptionCombo optionCombo );
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/minmax/MinMaxDataElementStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/minmax/MinMaxDataElementStore.java	2011-12-26 10:07:59 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/minmax/MinMaxDataElementStore.java	2012-07-01 07:12:58 +0000
@@ -48,4 +48,10 @@
     Collection<MinMaxDataElement> get( OrganisationUnit source, DataElement dataElement );  
 
     Collection<MinMaxDataElement> get( OrganisationUnit source, Collection<DataElement> dataElements );
+    
+    void delete( OrganisationUnit organisationUnit );
+    
+    void delete( DataElement dataElement );
+    
+    void delete( DataElementCategoryOptionCombo optionCombo );
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserService.java	2012-06-05 15:36:07 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserService.java	2012-07-01 07:12:58 +0000
@@ -361,4 +361,11 @@
      * @return a Map.
      */
     Map<User, Serializable> getUserSettings( String name, Serializable defaultValue );
+
+    /**
+     * Removes all user settings associated with the given user.
+     * 
+     * @param user the user.
+     */
+    void removeUserSettings( User user );
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserStore.java	2012-06-25 18:07:55 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserStore.java	2012-07-01 07:12:58 +0000
@@ -64,4 +64,11 @@
      * @return a Collection of Users.
      */
     Collection<User> getUsersByPhoneNumber( String phoneNumber );
+    
+    /**
+     * Removes all user settings associated with the given user.
+     * 
+     * @param user the user.
+     */
+    void removeUserSettings( User user );
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/DataElementDeletionHandler.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/DataElementDeletionHandler.java	2012-04-26 14:30:09 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/DataElementDeletionHandler.java	2012-07-01 07:12:58 +0000
@@ -29,6 +29,8 @@
 
 import static org.hisp.dhis.dataelement.DataElementCategoryCombo.DEFAULT_CATEGORY_COMBO_NAME;
 
+import java.util.Iterator;
+
 import org.hisp.dhis.dataset.DataSet;
 import org.hisp.dhis.option.OptionSet;
 import org.hisp.dhis.system.deletion.DeletionHandler;
@@ -96,24 +98,26 @@
     @Override
     public void deleteDataSet( DataSet dataSet )
     {
-        for ( DataElement dataElement : dataElementService.getAllDataElements() )
+        Iterator<DataElement> iterator = dataSet.getDataElements().iterator();
+        
+        while ( iterator.hasNext() )
         {
-            if ( dataElement.getDataSets().remove( dataSet ) )
-            {
-                dataElementService.updateDataElement( dataElement );
-            }
+            DataElement element = iterator.next();
+            element.getDataSets().remove( dataSet );
+            dataElementService.updateDataElement( element );            
         }
     }
 
     @Override
     public void deleteDataElementGroup( DataElementGroup group )
     {
-        for ( DataElement dataElement : dataElementService.getAllDataElements() )
+        Iterator<DataElement> iterator = group.getMembers().iterator();
+        
+        while ( iterator.hasNext() )
         {
-            if ( dataElement.getGroups().remove( dataElement ) )
-            {
-                dataElementService.updateDataElement( dataElement );
-            }
+            DataElement element = iterator.next();
+            element.getGroups().remove( group );
+            dataElementService.updateDataElement( element );
         }
     }
     

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/DataElementGroupDeletionHandler.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/DataElementGroupDeletionHandler.java	2011-12-26 10:07:59 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/DataElementGroupDeletionHandler.java	2012-07-01 07:12:58 +0000
@@ -27,6 +27,8 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import java.util.Iterator;
+
 import org.hisp.dhis.system.deletion.DeletionHandler;
 
 /**
@@ -60,12 +62,13 @@
     @Override
     public void deleteDataElement( DataElement dataElement )
     {
-        for ( DataElementGroup group : dataElementService.getAllDataElementGroups() )
+        Iterator<DataElementGroup> iterator = dataElement.getGroups().iterator();
+        
+        while ( iterator.hasNext() )
         {
-            if ( group.getMembers().remove( dataElement ) )
-            {
-                dataElementService.updateDataElementGroup( group );
-            }
+            DataElementGroup group = iterator.next();
+            group.getMembers().remove( dataElement );
+            dataElementService.updateDataElementGroup( group );
         }
     }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/DataElementGroupSetDeletionHandler.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/DataElementGroupSetDeletionHandler.java	2011-12-26 10:07:59 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/DataElementGroupSetDeletionHandler.java	2012-07-01 07:12:58 +0000
@@ -60,12 +60,12 @@
     @Override
     public void deleteDataElementGroup( DataElementGroup dataElementGroup )
     {
-        for ( DataElementGroupSet groupSet : dataElementService.getAllDataElementGroupSets() )
+        DataElementGroupSet groupSet = dataElementGroup.getGroupSet();
+        
+        if ( groupSet != null )
         {
-            if ( groupSet.getMembers().remove( dataElementGroup ) )
-            {
-                dataElementService.updateDataElementGroupSet( groupSet );
-            }
+            groupSet.getMembers().remove( dataElementGroup );
+            dataElementService.updateDataElementGroupSet( groupSet );
         }
     }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/CompleteDataSetRegistrationDeletionHandler.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/CompleteDataSetRegistrationDeletionHandler.java	2011-12-26 10:07:59 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/CompleteDataSetRegistrationDeletionHandler.java	2012-07-01 07:12:58 +0000
@@ -83,14 +83,8 @@
     }
 
     @Override
-    public void deleteOrganisationUnit( OrganisationUnit unit ) // TODO inefficient, use query
+    public void deleteOrganisationUnit( OrganisationUnit unit )
     {
-        for ( CompleteDataSetRegistration registration : completeDataSetRegistrationService.getAllCompleteDataSetRegistrations() )
-        {
-            if ( registration.getSource().equals( unit ) )
-            {
-                completeDataSetRegistrationService.deleteCompleteDataSetRegistration( registration );
-            }
-        }
+        completeDataSetRegistrationService.deleteCompleteDataSetRegistrations( unit );
     }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/DataSetDeletionHandler.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/DataSetDeletionHandler.java	2012-01-26 21:27:44 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/DataSetDeletionHandler.java	2012-07-01 07:12:58 +0000
@@ -96,36 +96,38 @@
     @Override
     public void deleteIndicator( Indicator indicator )
     {
-        for ( DataSet dataSet : dataSetService.getAllDataSets() )
+        Iterator<DataSet> iterator = indicator.getDataSets().iterator();
+        
+        while ( iterator.hasNext() )
         {
-            if ( dataSet.getIndicators().remove( indicator ) )
-            {
-                dataSetService.updateDataSet( dataSet );
-            }
+            DataSet dataSet = iterator.next();
+            dataSet.getIndicators().remove( indicator );
+            dataSetService.updateDataSet( dataSet );
         }
     }
     
     @Override
     public void deleteSection( Section section )
     {
-        for ( DataSet dataSet : dataSetService.getAllDataSets() )
+        DataSet dataSet = section.getDataSet();
+        
+        if ( dataSet != null )
         {
-            if ( dataSet.getSections().remove( section ) )
-            {
-                dataSetService.updateDataSet( dataSet );
-            }
+            dataSet.getSections().remove( section );
+            dataSetService.updateDataSet( dataSet );
         }
     }
 
     @Override
     public void deleteOrganisationUnit( OrganisationUnit unit )
     {
-        for ( DataSet dataSet : dataSetService.getAllDataSets() )
+        Iterator<DataSet> iterator = unit.getDataSets().iterator();
+        
+        while ( iterator.hasNext() )
         {
-            if ( dataSet.getSources().remove( unit ) )
-            {
-                dataSetService.updateDataSet( dataSet );
-            }
+            DataSet dataSet = iterator.next();
+            dataSet.getSources().remove( unit );
+            dataSetService.updateDataSet( dataSet );
         }
     }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/DefaultCompleteDataSetRegistrationService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/DefaultCompleteDataSetRegistrationService.java	2012-03-29 16:45:18 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/DefaultCompleteDataSetRegistrationService.java	2012-07-01 07:12:58 +0000
@@ -117,4 +117,9 @@
     {
         completeDataSetRegistrationStore.deleteCompleteDataSetRegistrations( dataSet );
     }
+    
+    public void deleteCompleteDataSetRegistrations( OrganisationUnit unit )
+    {
+        completeDataSetRegistrationStore.deleteCompleteDataSetRegistrations( unit );
+    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/SectionDeletionHandler.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/SectionDeletionHandler.java	2011-12-26 10:07:59 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/SectionDeletionHandler.java	2012-07-01 07:12:58 +0000
@@ -27,6 +27,8 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import java.util.Iterator;
+
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.system.deletion.DeletionHandler;
 
@@ -73,12 +75,13 @@
     @Override
     public void deleteDataSet( DataSet dataSet )
     {
-        for ( Section section : sectionService.getAllSections() )
+        Iterator<Section> iterator = dataSet.getSections().iterator();
+        
+        while ( iterator.hasNext() )
         {
-            if ( section.getDataSet().equals( dataSet ) )
-            {
-                sectionService.deleteSection( section );
-            }
+            Section section = iterator.next();
+            iterator.remove();
+            sectionService.deleteSection( section );
         }
     }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/hibernate/HibernateCompleteDataSetRegistrationStore.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/hibernate/HibernateCompleteDataSetRegistrationStore.java	2012-03-29 16:45:18 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/hibernate/HibernateCompleteDataSetRegistrationStore.java	2012-07-01 07:12:58 +0000
@@ -184,4 +184,15 @@
         
         query.executeUpdate();
     }
+    
+    public void deleteCompleteDataSetRegistrations( OrganisationUnit unit )
+    {
+        String hql = "delete from CompleteDataSetRegistration c where c.source = :source";
+
+        Query query = sessionFactory.getCurrentSession().createQuery( hql );
+        
+        query.setEntity( "source", unit );
+        
+        query.executeUpdate();
+    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/indicator/IndicatorDeletionHandler.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/indicator/IndicatorDeletionHandler.java	2012-04-26 14:30:09 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/indicator/IndicatorDeletionHandler.java	2012-07-01 07:12:58 +0000
@@ -27,6 +27,7 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import java.util.Iterator;
 import java.util.Set;
 
 import org.hisp.dhis.dataelement.DataElement;
@@ -86,38 +87,28 @@
     }
 
     @Override
-    public void deleteIndicatorType( IndicatorType indicatorType )
-    {
-        for ( Indicator indicator : indicatorService.getAllIndicators() )
-        {
-            if ( indicator.getIndicatorType().equals( indicatorType ) )
-            {
-                indicatorService.deleteIndicator( indicator );
-            }
-        }
-    }
-
-    @Override
     public void deleteIndicatorGroup( IndicatorGroup group )
     {
-        for ( Indicator indicator : indicatorService.getAllIndicators() )
+        Iterator<Indicator> iterator = group.getMembers().iterator();
+        
+        while ( iterator.hasNext() )
         {
-            if ( indicator.getGroups().remove( group ) )
-            {
-                indicatorService.updateIndicator( indicator );
-            }
+            Indicator indicator = iterator.next();
+            indicator.getGroups().remove( group );
+            indicatorService.updateIndicator( indicator );
         }
     }
 
     @Override
     public void deleteDataSet( DataSet dataSet )
     {
-        for ( Indicator indicator : indicatorService.getAllIndicators() )
+        Iterator<Indicator> iterator = dataSet.getIndicators().iterator();
+        
+        while ( iterator.hasNext() )
         {
-            if ( indicator.getDataSets().remove( dataSet ) )
-            {
-                indicatorService.updateIndicator( indicator );
-            }
+            Indicator indicator = iterator.next();
+            indicator.getDataSets().remove( dataSet );
+            indicatorService.updateIndicator( indicator );
         }
     }
 

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/indicator/IndicatorGroupDeletionHandler.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/indicator/IndicatorGroupDeletionHandler.java	2011-12-26 10:07:59 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/indicator/IndicatorGroupDeletionHandler.java	2012-07-01 07:12:58 +0000
@@ -27,6 +27,8 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import java.util.Iterator;
+
 import org.hisp.dhis.system.deletion.DeletionHandler;
 
 /**
@@ -59,12 +61,13 @@
     @Override
     public void deleteIndicator( Indicator indicator )
     {
-        for ( IndicatorGroup group : indicatorService.getAllIndicatorGroups() )
+        Iterator<IndicatorGroup> iterator = indicator.getGroups().iterator();
+        
+        while ( iterator.hasNext() )
         {
-            if ( group.getMembers().remove( indicator ) )
-            {
-                indicatorService.updateIndicatorGroup( group );
-            }
+            IndicatorGroup group = iterator.next();
+            group.getMembers().remove( indicator );
+            indicatorService.updateIndicatorGroup( group );
         }
     }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/indicator/IndicatorGroupSetDeletionHandler.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/indicator/IndicatorGroupSetDeletionHandler.java	2011-12-26 10:07:59 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/indicator/IndicatorGroupSetDeletionHandler.java	2012-07-01 07:12:58 +0000
@@ -58,12 +58,12 @@
     @Override
     public void deleteIndicatorGroup( IndicatorGroup indicatorGroup )
     {
-        for ( IndicatorGroupSet groupSet : indicatorService.getAllIndicatorGroupSets() )
+        IndicatorGroupSet groupSet = indicatorGroup.getGroupSet();
+        
+        if ( groupSet != null )
         {
-            if ( groupSet.getMembers().remove( indicatorGroup ) )
-            {
-                indicatorService.updateIndicatorGroupSet( groupSet );
-            }
+            groupSet.getMembers().remove( indicatorGroup );
+            indicatorService.updateIndicatorGroupSet( groupSet );
         }
     }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/minmax/DefaultMinMaxDataElementService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/minmax/DefaultMinMaxDataElementService.java	2011-12-26 10:07:59 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/minmax/DefaultMinMaxDataElementService.java	2012-07-01 07:12:58 +0000
@@ -96,4 +96,19 @@
     {
         return minMaxDataElementStore.getAll();
     }
+
+    public void removeMinMaxDataElements( OrganisationUnit organisationUnit )
+    {
+        minMaxDataElementStore.delete( organisationUnit );
+    }
+    
+    public void removeMinMaxDataElements( DataElement dataElement )
+    {
+        minMaxDataElementStore.delete( dataElement );
+    }
+    
+    public void removeMinMaxDataElements( DataElementCategoryOptionCombo optionCombo )
+    {
+        minMaxDataElementStore.delete( optionCombo );
+    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/minmax/MinMaxDataElementDeletionHandler.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/minmax/MinMaxDataElementDeletionHandler.java	2011-12-26 10:07:59 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/minmax/MinMaxDataElementDeletionHandler.java	2012-07-01 07:12:58 +0000
@@ -63,36 +63,18 @@
     @Override
     public void deleteDataElement( DataElement dataElement )
     {
-        for ( MinMaxDataElement element : minMaxDataElementService.getAllMinMaxDataElements() )
-        {
-            if ( element.getDataElement().equals( dataElement ) )
-            {
-                minMaxDataElementService.deleteMinMaxDataElement( element );
-            }
-        }
+        minMaxDataElementService.removeMinMaxDataElements( dataElement );
     }
     
     @Override
     public void deleteOrganisationUnit( OrganisationUnit source )
     {
-        for ( MinMaxDataElement element : minMaxDataElementService.getAllMinMaxDataElements() )
-        {
-            if ( element.getSource().equals( source ) )
-            {
-                minMaxDataElementService.deleteMinMaxDataElement( element );
-            }
-        }
+        minMaxDataElementService.removeMinMaxDataElements( source );
     }
     
     @Override
-    public void deleteDataElementCategoryOptionCombo( DataElementCategoryOptionCombo categoryOptionCombo )
+    public void deleteDataElementCategoryOptionCombo( DataElementCategoryOptionCombo optionCombo )
     {
-        for ( MinMaxDataElement element : minMaxDataElementService.getAllMinMaxDataElements() )
-        {
-            if ( element.getOptionCombo().equals( categoryOptionCombo ) )
-            {
-                minMaxDataElementService.deleteMinMaxDataElement( element );
-            }
-        }
+        minMaxDataElementService.removeMinMaxDataElements( optionCombo );
     }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/minmax/hibernate/HibernateMinMaxDataElementStore.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/minmax/hibernate/HibernateMinMaxDataElementStore.java	2011-12-26 10:07:59 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/minmax/hibernate/HibernateMinMaxDataElementStore.java	2012-07-01 07:12:58 +0000
@@ -80,4 +80,25 @@
             Restrictions.eq( "source", source ), 
             Restrictions.in( "dataElement", dataElements ) ).list();
     }
+    
+    public void delete( OrganisationUnit organisationUnit )
+    {
+        String hql = "delete from MinMaxDataElement m where m.source = :source";
+        
+        getQuery( hql ).setEntity( "source", organisationUnit ).executeUpdate();
+    }
+    
+    public void delete( DataElement dataElement )
+    {
+        String hql = "delete from MinMaxDataElement m where m.dataElement = :dataElement";
+        
+        getQuery( hql ).setEntity( "dataElement", dataElement ).executeUpdate();
+    }
+    
+    public void delete( DataElementCategoryOptionCombo optionCombo )
+    {
+        String hql = "delete from MinMaxDataElement m where m.optionCombo = :optionCombo";
+        
+        getQuery( hql ).setEntity( "optionCombo", optionCombo ).executeUpdate();
+    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java	2012-06-21 10:34:24 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java	2012-07-01 07:12:58 +0000
@@ -131,6 +131,8 @@
     public void deleteOrganisationUnit( OrganisationUnit organisationUnit )
         throws HierarchyViolationException
     {
+        organisationUnit = getOrganisationUnit( organisationUnit.getId() );
+        
         if ( !organisationUnit.getChildren().isEmpty() )
         {
             throw new HierarchyViolationException( "Cannot delete an OrganisationUnit with children" );
@@ -538,7 +540,7 @@
     {
         return organisationUnitStore.getBetweenByName( name, first, max );
     }
-
+    
     // -------------------------------------------------------------------------
     // OrganisationUnitHierarchy
     // -------------------------------------------------------------------------

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitDeletionHandler.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitDeletionHandler.java	2012-04-26 14:30:09 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitDeletionHandler.java	2012-07-01 07:12:58 +0000
@@ -27,6 +27,8 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import java.util.Iterator;
+
 import org.hisp.dhis.dataset.DataSet;
 import org.hisp.dhis.system.deletion.DeletionHandler;
 import org.hisp.dhis.user.User;
@@ -61,36 +63,45 @@
     @Override
     public void deleteDataSet( DataSet dataSet )
     {
-        for ( OrganisationUnit unit : organisationUnitService.getAllOrganisationUnits() )
+        Iterator<OrganisationUnit> iterator = dataSet.getSources().iterator();
+        
+        while ( iterator.hasNext() )
         {
-            if ( unit.getDataSets().remove( dataSet ) )
-            {
-                organisationUnitService.updateOrganisationUnit( unit );
-            }
-        }
+            OrganisationUnit unit = iterator.next();
+            
+            unit.getDataSets().remove( unit );
+            
+            organisationUnitService.updateOrganisationUnit( unit );
+        }        
     }
 
     @Override
     public void deleteUser( User user )
     {
-        for ( OrganisationUnit unit : organisationUnitService.getAllOrganisationUnits() )
+        Iterator<OrganisationUnit> iterator = user.getOrganisationUnits().iterator();
+        
+        while ( iterator.hasNext() )
         {
-            if ( unit.getUsers().remove( user ) )
-            {
-                organisationUnitService.updateOrganisationUnit( unit );
-            }
+            OrganisationUnit unit = iterator.next();
+            
+            unit.getUsers().remove( user );
+            
+            organisationUnitService.updateOrganisationUnit( unit );
         }
     }
 
     @Override
     public void deleteOrganisationUnitGroup( OrganisationUnitGroup group )
     {
-        for ( OrganisationUnit unit : organisationUnitService.getAllOrganisationUnits() )
+        Iterator<OrganisationUnit> iterator = group.getMembers().iterator();
+        
+        while ( iterator.hasNext() )
         {
-            if ( unit.getGroups().remove( group ) )
-            {
-                organisationUnitService.updateOrganisationUnit( unit );
-            }
-        }
+            OrganisationUnit unit = iterator.next();
+            
+            unit.getGroups().remove( unit );
+            
+            organisationUnitService.updateOrganisationUnit( unit );
+        }            
     }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitGroupDeletionHandler.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitGroupDeletionHandler.java	2011-12-26 10:07:59 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitGroupDeletionHandler.java	2012-07-01 07:12:58 +0000
@@ -27,6 +27,8 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import java.util.Iterator;
+
 import org.hisp.dhis.system.deletion.DeletionHandler;
 
 /**
@@ -60,12 +62,13 @@
     @Override
     public void deleteOrganisationUnit( OrganisationUnit unit )
     {
-        for ( OrganisationUnitGroup group : organisationUnitGroupService.getAllOrganisationUnitGroups() )
+        Iterator<OrganisationUnitGroup> iterator = unit.getGroups().iterator();
+        
+        while ( iterator.hasNext() )
         {
-            if ( group.getMembers().remove( unit ) )
-            {
-                organisationUnitGroupService.updateOrganisationUnitGroup( group );
-            }
+            OrganisationUnitGroup group = iterator.next();
+            group.getMembers().remove( unit );
+            organisationUnitGroupService.updateOrganisationUnitGroup( group );
         }
     }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitGroupSetDeletionHandler.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitGroupSetDeletionHandler.java	2011-12-26 10:07:59 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitGroupSetDeletionHandler.java	2012-07-01 07:12:58 +0000
@@ -60,12 +60,12 @@
     @Override
     public void deleteOrganisationUnitGroup( OrganisationUnitGroup group )
     {
-        for ( OrganisationUnitGroupSet groupSet : organisationUnitGroupService.getAllOrganisationUnitGroupSets() )
+        OrganisationUnitGroupSet groupSet = group.getGroupSet();
+        
+        if ( groupSet != null )
         {
-            if ( groupSet.getOrganisationUnitGroups().remove( group ) )
-            {
-                organisationUnitGroupService.updateOrganisationUnitGroupSet( groupSet );
-            }
+            groupSet.getOrganisationUnitGroups().remove( group );
+            organisationUnitGroupService.updateOrganisationUnitGroupSet( groupSet );
         }
     }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/translation/TranslationDeletionHandler.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/translation/TranslationDeletionHandler.java	2012-01-27 11:40:18 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/translation/TranslationDeletionHandler.java	2012-07-01 07:12:58 +0000
@@ -103,7 +103,13 @@
     {
         i18nService.removeObject( indicatorGroup );
     }
-    
+
+    @Override
+    public void deleteIndicatorGroupSet( IndicatorGroupSet indicatorGroupSet )
+    {
+        i18nService.removeObject( indicatorGroupSet );
+    }
+
     @Override
     public void deleteIndicatorType( IndicatorType indicatorType )
     {
@@ -111,12 +117,6 @@
     }
 
     @Override
-    public void deleteIndicatorGroupSet( IndicatorGroupSet indicatorGroupSet )
-    {
-        i18nService.removeObject( indicatorGroupSet );
-    }
-
-    @Override
     public void deleteValidationRule( ValidationRule validationRule )
     {
         i18nService.removeObject( validationRule );

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/DefaultUserService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/DefaultUserService.java	2012-06-26 21:04:37 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/DefaultUserService.java	2012-07-01 07:12:58 +0000
@@ -111,11 +111,16 @@
 
     public boolean isLastSuperUser( UserCredentials userCredentials )
     {
+        if ( !isSuperUser( userCredentials ) )
+        {
+            return false; // Cannot be last if not super user
+        }
+        
         Collection<UserCredentials> users = userCredentialsStore.getAllUserCredentials();
 
         for ( UserCredentials user : users )
         {
-            if ( isSuperUser( user ) && user.getId() != userCredentials.getId() )
+            if ( isSuperUser( user ) && !user.equals( userCredentials ) )
             {
                 return false;
             }
@@ -490,4 +495,9 @@
     {
         return userStore.getUsersByOrganisationUnits( units );
     }
+    
+    public void removeUserSettings( User user )
+    {
+        userStore.removeUserSettings( user );
+    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/UserAuthorityGroupDeletionHandler.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/UserAuthorityGroupDeletionHandler.java	2011-12-26 10:07:59 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/UserAuthorityGroupDeletionHandler.java	2012-07-01 07:12:58 +0000
@@ -27,6 +27,8 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import java.util.Iterator;
+
 import org.hisp.dhis.dataset.DataSet;
 import org.hisp.dhis.system.deletion.DeletionHandler;
 
@@ -73,12 +75,13 @@
     @Override
     public void deleteUserCredentials( UserCredentials credentials )
     {
-        for ( UserAuthorityGroup group : userService.getAllUserAuthorityGroups() )
+        Iterator<UserAuthorityGroup> iterator = credentials.getUserAuthorityGroups().iterator();
+        
+        while ( iterator.hasNext() )
         {
-            if ( group.getMembers().remove( credentials ) )
-            {
-                userService.updateUserAuthorityGroup( group );
-            }
+            UserAuthorityGroup group = iterator.next();
+            group.getMembers().remove( credentials );
+            userService.updateUserAuthorityGroup( group );
         }
     }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/UserCredentialsDeletionHandler.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/UserCredentialsDeletionHandler.java	2011-12-26 10:07:59 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/UserCredentialsDeletionHandler.java	2012-07-01 07:12:58 +0000
@@ -27,6 +27,8 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import java.util.Iterator;
+
 import org.hisp.dhis.system.deletion.DeletionHandler;
 
 /**
@@ -59,12 +61,13 @@
     @Override
     public void deleteUserAuthorityGroup( UserAuthorityGroup authorityGroup )
     {
-        for ( UserCredentials credentials : userService.getAllUserCredentials() )
+        Iterator<UserCredentials> iterator = authorityGroup.getMembers().iterator();
+        
+        while ( iterator.hasNext() )
         {
-            if ( credentials.getUserAuthorityGroups().remove( authorityGroup ) )
-            {
-                userService.updateUserCredentials( credentials );
-            }
+            UserCredentials credentials = iterator.next();
+            credentials.getUserAuthorityGroups().remove( authorityGroup );
+            userService.updateUserCredentials( credentials );
         }
     }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/UserDeletionHandler.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/UserDeletionHandler.java	2012-04-26 14:30:09 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/UserDeletionHandler.java	2012-07-01 07:12:58 +0000
@@ -27,6 +27,8 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import java.util.Iterator;
+
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.system.deletion.DeletionHandler;
 
@@ -61,12 +63,13 @@
     @Override
     public void deleteOrganisationUnit( OrganisationUnit unit )
     {
-        for ( User user : userService.getAllUsers() )
+        Iterator<User> iterator = unit.getUsers().iterator();
+        
+        while ( iterator.hasNext() )
         {
-            if ( user.getOrganisationUnits().remove( unit ) )
-            {
-                userService.updateUser( user );
-            }
+            User user = iterator.next();
+            user.getOrganisationUnits().remove( unit );
+            userService.updateUser( user );
         }
     }
 
@@ -75,14 +78,11 @@
     {
         for ( UserCredentials credentials : userService.getAllUserCredentials() )
         {
-            if ( credentials != null && credentials.getUserAuthorityGroups() != null )
+            for ( UserAuthorityGroup role : credentials.getUserAuthorityGroups() )
             {
-                for ( UserAuthorityGroup role : credentials.getUserAuthorityGroups() )
+                if ( role.equals( authorityGroup ) )
                 {
-                    if ( role.equals( authorityGroup ) )
-                    {
-                        return credentials.getName();
-                    }
+                    return credentials.getName();
                 }
             }
         }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/UserSettingDeletionHandler.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/UserSettingDeletionHandler.java	2011-12-26 10:07:59 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/UserSettingDeletionHandler.java	2012-07-01 07:12:58 +0000
@@ -49,6 +49,13 @@
     {
         this.userSettingService = userSettingService;
     }
+    
+    private UserService userService;
+
+    public void setUserService( UserService userService )
+    {
+        this.userService = userService;
+    }
 
     // -------------------------------------------------------------------------
     // DeletionHandler implementation
@@ -74,4 +81,10 @@
             }
         }
     }
+    
+    @Override
+    public void deleteUser( User user )
+    {
+        userService.removeUserSettings( user );
+    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/hibernate/HibernateUserStore.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/hibernate/HibernateUserStore.java	2012-06-25 18:07:55 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/hibernate/HibernateUserStore.java	2012-07-01 07:12:58 +0000
@@ -81,4 +81,11 @@
 
         return sessionFactory.getCurrentSession().createQuery( hql ).setParameterList( "ids", orgunits ).list();
     }
+    
+    public void removeUserSettings( User user )
+    {
+        String hql = "delete from UserSetting us where us.user = :user";
+        
+        getQuery( hql ).setEntity( "user", user ).executeUpdate();
+    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/validation/ValidationRuleDeletionHandler.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/validation/ValidationRuleDeletionHandler.java	2012-06-26 09:13:37 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/validation/ValidationRuleDeletionHandler.java	2012-07-01 07:12:58 +0000
@@ -27,6 +27,8 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import java.util.Iterator;
+
 import org.hisp.dhis.expression.Expression;
 import org.hisp.dhis.system.deletion.DeletionHandler;
 
@@ -61,13 +63,18 @@
     @Override
     public void deleteExpression( Expression expression )
     {
-        for ( ValidationRule rule : validationRuleService.getAllValidationRules() )
+        Iterator<ValidationRule> iterator = validationRuleService.getAllValidationRules().iterator();
+        
+        while ( iterator.hasNext() )
         {
+            ValidationRule rule = iterator.next();
+            
             Expression leftSide = rule.getLeftSide();
             Expression rightSide = rule.getRightSide();
 
             if ( (leftSide != null && leftSide.equals( expression )) || (rightSide != null && rightSide.equals( expression )) )
             {
+                iterator.remove();
                 validationRuleService.deleteValidationRule( rule );
             }
         }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/validation/ValidationRuleGroupDeletionHandler.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/validation/ValidationRuleGroupDeletionHandler.java	2011-12-26 10:07:59 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/validation/ValidationRuleGroupDeletionHandler.java	2012-07-01 07:12:58 +0000
@@ -27,6 +27,8 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import java.util.Iterator;
+
 import org.hisp.dhis.system.deletion.DeletionHandler;
 
 /**
@@ -59,12 +61,13 @@
     
     public void deleteValidationRule( ValidationRule validationRule )
     {
-        for ( ValidationRuleGroup group : validationRuleService.getAllValidationRuleGroups() )
+        Iterator<ValidationRuleGroup> iterator = validationRule.getGroups().iterator();
+        
+        while ( iterator.hasNext() )
         {
-            if ( group.getMembers().remove( validationRule ) )
-            {
-                validationRuleService.saveValidationRule( validationRule );
-            }
+            ValidationRuleGroup group = iterator.next();
+            group.getMembers().remove( validationRule );
+            validationRuleService.updateValidationRuleGroup( group );
         }
     }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml	2012-06-30 16:05:30 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml	2012-07-01 07:12:58 +0000
@@ -806,6 +806,7 @@
 
   <bean id="org.hisp.dhis.user.UserSettingDeletionHandler" class="org.hisp.dhis.user.UserSettingDeletionHandler">
     <property name="userSettingService" ref="org.hisp.dhis.user.UserSettingService" />
+	<property name="userService" ref="org.hisp.dhis.user.UserService" />
   </bean>
 
   <bean id="org.hisp.dhis.dataelement.DataElementCategoryDeletionHandler" class="org.hisp.dhis.dataelement.DataElementCategoryDeletionHandler">

=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/PatientDeletionHandler.java'
--- dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/PatientDeletionHandler.java	2012-06-12 07:46:25 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/PatientDeletionHandler.java	2012-07-01 07:12:58 +0000
@@ -71,12 +71,9 @@
     }
     
     @Override
-    public void deleteOrganisationUnit( OrganisationUnit unit )
+    public String allowDeleteOrganisationUnit( OrganisationUnit unit )
     {
-        for ( Patient patient : patientService.getPatients( unit, null, null ) )
-        {
-            patientService.deletePatient( patient );
-        }
+        return patientService.getPatients( unit, null, null ).size() == 0 ? null : ERROR;
     }
     
     @Override

=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/ProgramDeletionHandler.java'
--- dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/ProgramDeletionHandler.java	2012-01-11 05:31:53 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/ProgramDeletionHandler.java	2012-07-01 07:12:58 +0000
@@ -76,6 +76,8 @@
     @Override
     public void deleteOrganisationUnit( OrganisationUnit unit )
     {
+        //TODO improve performance
+        
         Collection<Program> programs = programService.getAllPrograms();
         
         for ( Program program : programs )

=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/ProgramStageDataElementDeletionHandler.java'
--- dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/ProgramStageDataElementDeletionHandler.java	2012-01-11 05:31:53 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/ProgramStageDataElementDeletionHandler.java	2012-07-01 07:12:58 +0000
@@ -112,14 +112,12 @@
             // TODO use a query which will be more efficient
 
             Collection<ProgramStageDataElement> psDataElements = programStageDEService.getAllProgramStageDataElements();
+            
             for ( ProgramStageDataElement psDataElement : psDataElements )
             {
-                Collection<DataElement> dataElements = programStageDEService.getListDataElement( psDataElement
-                    .getProgramStage() );
-
-                if ( dataElements.contains( dataElement ) )
+                if ( psDataElement.getDataElement() != null && psDataElement.getDataElement().equals( dataElement ) )
                 {
-                    return ERROR;
+                    return dataElement.getName();
                 }
             }
         }

=== modified file 'dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/dashboard/DashboardContentDeletionHandler.java'
--- dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/dashboard/DashboardContentDeletionHandler.java	2012-06-30 16:16:19 +0000
+++ dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/dashboard/DashboardContentDeletionHandler.java	2012-07-01 07:12:58 +0000
@@ -57,6 +57,8 @@
         return DashboardContent.class.getSimpleName();
     }
     
+    //TODO Improve performance with queries
+    
     @Override
     public void deleteReport( Report report )
     {

=== modified file 'dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateGenericStore.java'
--- dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateGenericStore.java	2012-06-06 13:31:45 +0000
+++ dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateGenericStore.java	2012-07-01 07:12:58 +0000
@@ -224,7 +224,7 @@
     {
         return (T) sessionFactory.getCurrentSession().load( getClazz(), id );
     }
-
+    
     @Override
     public final T getByUid( String uid )
     {

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-user/src/main/java/org/hisp/dhis/user/action/RemoveUserAction.java'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-user/src/main/java/org/hisp/dhis/user/action/RemoveUserAction.java	2011-12-26 10:07:59 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-user/src/main/java/org/hisp/dhis/user/action/RemoveUserAction.java	2012-07-01 07:12:58 +0000
@@ -27,20 +27,16 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import java.util.Collection;
-
 import org.hisp.dhis.i18n.I18n;
 import org.hisp.dhis.user.CurrentUserService;
 import org.hisp.dhis.user.User;
 import org.hisp.dhis.user.UserCredentials;
-import org.hisp.dhis.user.UserSetting;
 import org.hisp.dhis.user.UserService;
 
 import com.opensymphony.xwork2.Action;
 
 /**
  * @author Torgeir Lorange Ostby
- * @version $Id: RemoveUserAction.java 5724 2008-09-18 14:37:01Z larshelg $
  */
 public class RemoveUserAction
     implements Action
@@ -101,14 +97,7 @@
 
         boolean isCurrentUser = currentUser != null && currentUser.equals( user );
 
-        Collection<UserSetting> userSettings = userService.getAllUserSettings( user );
-
-        for ( UserSetting userSetting : userSettings )
-        {
-            userService.deleteUserSetting( userSetting );
-        }
-
-        UserCredentials userCredentials = userService.getUserCredentials( user );
+        UserCredentials userCredentials = user.getUserCredentials();
         
         if ( userService.isLastSuperUser( userCredentials ) )
         {
@@ -118,7 +107,7 @@
         }
         else
         {
-            userService.deleteUserCredentials( userService.getUserCredentials( user ) );
+            userService.deleteUserCredentials( user.getUserCredentials() );
             userService.deleteUser( user );
         }