← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 2051: Speed-up orgunit pruning function.

 

------------------------------------------------------------
revno: 2051
committer: Quang <Quang@Quang-PC>
branch nick: trunk
timestamp: Mon 2010-06-28 22:31:34 +0700
message:
  Speed-up orgunit pruning function.
added:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataprune/DataPruneStore.java
  dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/dataprune/jdbc/
  dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/dataprune/jdbc/JdbcDataPruneStore.java
modified:
  dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/dataprune/DefaultDataPruneService.java
  dhis-2/dhis-services/dhis-service-administration/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
=== added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataprune/DataPruneStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataprune/DataPruneStore.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataprune/DataPruneStore.java	2010-06-28 15:31:34 +0000
@@ -0,0 +1,42 @@
+package org.hisp.dhis.dataprune;
+
+/*
+ * Copyright (c) 2004-2010, 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 java.util.List;
+
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+
+/**
+ * @author Quang Nguyen
+ * @version Apr 6, 2010 5:44:47 PM
+ */
+
+public interface DataPruneStore
+{
+    void deleteMultiOrganisationUnit(List<OrganisationUnit> orgUnits);
+}

=== modified file 'dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/dataprune/DefaultDataPruneService.java'
--- dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/dataprune/DefaultDataPruneService.java	2010-04-21 12:14:08 +0000
+++ dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/dataprune/DefaultDataPruneService.java	2010-06-28 15:31:34 +0000
@@ -27,12 +27,14 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
 import java.util.Set;
 
-import org.hisp.dhis.datavalue.DataValueService;
-import org.hisp.dhis.hierarchy.HierarchyViolationException;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.transaction.annotation.Transactional;
 
 /**
@@ -54,12 +56,8 @@
         this.organisationUnitService = organisationUnitService;
     }
 
-    private DataValueService dataValueService;
-
-    public void setDataValueService( DataValueService dataValueService )
-    {
-        this.dataValueService = dataValueService;
-    }
+    @Autowired
+    private DataPruneStore dataPruneStore;
 
     // -------------------------------------------------------------------------
     // DataPruneService implementation
@@ -74,16 +72,29 @@
             organisationUnitService.updateOrganisationUnit( organisationUnit );
         }
 
+        List<OrganisationUnit> deletedOrgUnits = pruneOrganisationUnitLocal( organisationUnit );
+
+        Collections.reverse( deletedOrgUnits );
+
+        dataPruneStore.deleteMultiOrganisationUnit( deletedOrgUnits );
+    }
+
+    private List<OrganisationUnit> pruneOrganisationUnitLocal( OrganisationUnit organisationUnit )
+    {
+        List<OrganisationUnit> deleteOrgUnits = new ArrayList<OrganisationUnit>();
+
         for ( OrganisationUnit eachRoot : organisationUnitService.getRootOrganisationUnits() )
         {
             if ( !eachRoot.equals( organisationUnit ) )
             {
-                deleteBranch( eachRoot );
+                deleteBranch( eachRoot, deleteOrgUnits );
             }
         }
+
+        return deleteOrgUnits;
     }
 
-    private void deleteBranch( OrganisationUnit organisationUnit )
+    private void deleteBranch( OrganisationUnit organisationUnit, List<OrganisationUnit> deletedOrgUnits )
     {
         if ( !organisationUnit.getChildren().isEmpty() )
         {
@@ -92,19 +103,10 @@
 
             for ( Object eachChild : childrenAsArray )
             {
-                deleteBranch( (OrganisationUnit) eachChild );
+                deleteBranch( (OrganisationUnit) eachChild, deletedOrgUnits );
             }
         }
 
-        dataValueService.deleteDataValuesBySource( organisationUnit );
-
-        try
-        {
-            organisationUnitService.deleteOrganisationUnit( organisationUnit );
-        }
-        catch ( HierarchyViolationException e )
-        {
-            e.printStackTrace();
-        }
+        deletedOrgUnits.add( organisationUnit );
     }
 }

=== added directory 'dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/dataprune/jdbc'
=== added file 'dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/dataprune/jdbc/JdbcDataPruneStore.java'
--- dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/dataprune/jdbc/JdbcDataPruneStore.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/dataprune/jdbc/JdbcDataPruneStore.java	2010-06-28 15:31:34 +0000
@@ -0,0 +1,92 @@
+package org.hisp.dhis.dataprune.jdbc;
+
+/*
+ * Copyright (c) 2004-2010, 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 java.util.List;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.hisp.dhis.dataprune.DataPruneStore;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.system.util.ConversionUtils;
+import org.hisp.dhis.system.util.TextUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.stereotype.Component;
+
+/**
+ * @author Quang Nguyen
+ * @version Apr 6, 2010 5:48:15 PM
+ */
+
+@Component
+public class JdbcDataPruneStore implements DataPruneStore
+{
+    private static final Log log = LogFactory.getLog( JdbcDataPruneStore.class );
+    
+    // -------------------------------------------------------------------------
+    // Dependencies
+    // -------------------------------------------------------------------------
+
+    @Autowired
+    private JdbcTemplate jdbcTemplate;
+    
+    // -------------------------------------------------------------------------
+    // DataPruneService implementation
+    // -------------------------------------------------------------------------
+
+    public void deleteMultiOrganisationUnit(List<OrganisationUnit> orgUnits) {
+        
+        String orgUnitIds = TextUtils.getCommaDelimitedString( ConversionUtils.getIdentifiers( OrganisationUnit.class, orgUnits )) ;
+        
+        String sql = "delete from datasetlocksource where sourceid in (" + orgUnitIds + ");" + 
+                        "delete from completedatasetregistration where sourceid in (" + orgUnitIds + ");" +
+                        "delete from datasetsource where sourceid in (" + orgUnitIds + ");" +
+                        "delete from frequencyoverrideassociation where sourceid in (" + orgUnitIds + ");" +
+                        "delete from minmaxdataelement where sourceid in (" + orgUnitIds + ");" +
+                        "delete from orgunitgroupmembers where organisationunitid in (" + orgUnitIds + ");" +
+                        "delete from usermembership where organisationunitid in (" + orgUnitIds + ");" +
+                        "delete from datamartexportorgunits where orgunitid in (" + orgUnitIds + ");" +
+                        "delete from excelgroup_associations where organisationid in (" + orgUnitIds + ");" +
+                        "delete from reportexcel_associations where organisationid in (" + orgUnitIds + ");" +
+                        "delete from maporganisationunitrelation where organisationunitid in (" + orgUnitIds + ");" +
+                        "delete from maporganisationunitrelation where mapid in (select mapid from map where organisationunitid in (" + orgUnitIds + "));" +
+                        "delete from map where organisationunitid in (" + orgUnitIds + ");" +
+                        "delete from patientidentifier where organisationunitid in (" + orgUnitIds + ");" +
+                        "delete from program_organisationunits where organisationunitid in (" + orgUnitIds + ");" +
+                        "delete from chart_organisationunits where organisationunitid in (" + orgUnitIds + ");" +
+                        "delete from reporttable_organisationunits where organisationunitid in (" + orgUnitIds + ");" +
+                        "delete from datavalue_audit where (dataelementid, periodid, sourceid, categoryoptioncomboid) in (select dataelementid, periodid, sourceid, categoryoptioncomboid from datavalue where sourceid in (" + orgUnitIds + "));" + 
+                        "delete from datavalue where sourceid in (" + orgUnitIds + ");" + 
+                        "delete from organisationunit where organisationunitid in (" + orgUnitIds + ");" +
+                        "delete from source where sourceid in (" + orgUnitIds + ");";
+        
+        long before = System.currentTimeMillis();
+        jdbcTemplate.execute( sql );
+        log.info( "Deleting " + orgUnits.size() + " organisations units sucessfully in " + (System.currentTimeMillis() - before) + " ms.");
+    }
+}

=== modified file 'dhis-2/dhis-services/dhis-service-administration/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-services/dhis-service-administration/src/main/resources/META-INF/dhis/beans.xml	2010-05-18 18:37:07 +0000
+++ dhis-2/dhis-services/dhis-service-administration/src/main/resources/META-INF/dhis/beans.xml	2010-06-28 15:31:34 +0000
@@ -112,8 +112,6 @@
     class="org.hisp.dhis.dataprune.DefaultDataPruneService">
     <property name="organisationUnitService"
       ref="org.hisp.dhis.organisationunit.OrganisationUnitService"/>
-	<property name="dataValueService"
-      ref="org.hisp.dhis.datavalue.DataValueService"/>
   </bean> 
   
   <!-- Data archive -->