← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 4210: Filter orgunit - dataset associations in order to load only what is necessary in data entry modul...

 

------------------------------------------------------------
revno: 4210
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2011-07-26 09:39:20 +0200
message:
  Filter orgunit - dataset associations in order to load only what is necessary in data entry module. Improved performance of algorithm which finds children of orgunits in the hierarchy.
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitHierarchy.java
  dhis-2/dhis-api/src/test/java/org/hisp/dhis/organisationunit/OrganisationUnitHierarchyTest.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.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/organisationunit/OrganisationUnitHierarchy.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitHierarchy.java	2010-12-02 21:24:43 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitHierarchy.java	2011-07-26 07:39:20 +0000
@@ -36,25 +36,35 @@
 import java.util.Set;
 
 /**
- * The purpose of the OrganisationUnitHierarchy object is to store the parent-child relationship of the
- * registered organisation units together with a timestamp. The parent-child relationships are
- * stored in a Map, where the key column stores the organisation unit id and the value column
- * stores the id of the parent organisation unit.
- * 
  * @author Lars Helge Overland
- * @version $Id: OrganisationUnitHierarchy.java 2869 2007-02-20 14:26:09Z andegje $
  */
 public class OrganisationUnitHierarchy
 {
     private Map<Integer, Collection<Integer>> preparedRelationships = new HashMap<Integer, Collection<Integer>>();
     
-    private Collection<OrganisationUnitRelationship> relationships;
+    private Map<Integer, Set<Integer>> relationships = new HashMap<Integer, Set<Integer>>();
     
-    public OrganisationUnitHierarchy( Collection<OrganisationUnitRelationship> relationships )
+    public OrganisationUnitHierarchy( Map<Integer, Set<Integer>> relationships )
     {
         this.relationships = relationships;
     }
     
+    public OrganisationUnitHierarchy( Collection<OrganisationUnitRelationship> relations )
+    {
+        for ( OrganisationUnitRelationship relation : relations )
+        {
+            Set<Integer> children = relationships.get( relation.getParentId() );
+            
+            if ( children == null )
+            {
+                children = new HashSet<Integer>();
+                relationships.put( relation.getParentId(), children );
+            }
+            
+            children.add( relation.getChildId() );
+        }
+    }
+    
     public OrganisationUnitHierarchy prepareChildren( Collection<OrganisationUnit> parents )
     {
         for ( OrganisationUnit unit : parents )
@@ -87,12 +97,13 @@
         
         for ( int i = 0; i < childCounter; i++ )
         {
-            for ( OrganisationUnitRelationship entry : relationships )
+            Set<Integer> currentChildren = relationships.get( children.get( i ) );
+            
+            if ( currentChildren != null )
             {
-                if ( entry.getParentId() == children.get( i ) )
-                {
-                    children.add( childCounter++, entry.getChildId() );
-                }
+                children.addAll( currentChildren );
+            
+                childCounter += currentChildren.size();
             }
         }
         

=== modified file 'dhis-2/dhis-api/src/test/java/org/hisp/dhis/organisationunit/OrganisationUnitHierarchyTest.java'
--- dhis-2/dhis-api/src/test/java/org/hisp/dhis/organisationunit/OrganisationUnitHierarchyTest.java	2010-06-08 19:47:40 +0000
+++ dhis-2/dhis-api/src/test/java/org/hisp/dhis/organisationunit/OrganisationUnitHierarchyTest.java	2011-07-26 07:39:20 +0000
@@ -1,17 +1,52 @@
 package org.hisp.dhis.organisationunit;
 
+/*
+ * 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 static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertFalse;
 import static junit.framework.Assert.assertTrue;
 
 import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Map;
+import java.util.Set;
 
 import org.junit.Test;
 
+/**
+ * @author Lars Helge Overland
+ */
 public class OrganisationUnitHierarchyTest
 {
     @Test
-    public void testGetChildren()
+    public void testGetChildrenA()
     {
         List<OrganisationUnitRelationship> relationships = new ArrayList<OrganisationUnitRelationship>();
         
@@ -26,9 +61,29 @@
         relationships.add( new OrganisationUnitRelationship( 4, 10 ) );
         relationships.add( new OrganisationUnitRelationship( 4, 11 ) );
         relationships.add( new OrganisationUnitRelationship( 4, 12 ) );
-                
-        OrganisationUnitHierarchy hierarchy = new OrganisationUnitHierarchy( relationships );
-        
+        
+        OrganisationUnitHierarchy hierarchy = new OrganisationUnitHierarchy( relationships );
+        
+        testHierarchy( hierarchy );
+    }
+    
+    @Test
+    public void testGetChildrenB()
+    {
+        Map<Integer, Set<Integer>> relationships = new HashMap<Integer, Set<Integer>>();
+        
+        relationships.put( 1, getSet( 2, 3 ) );
+        relationships.put( 2, getSet( 4, 5, 6 ) );
+        relationships.put( 3, getSet( 7, 8, 9 ) );
+        relationships.put( 4, getSet( 10, 11, 12 ) );
+
+        OrganisationUnitHierarchy hierarchy = new OrganisationUnitHierarchy( relationships );
+        
+        testHierarchy( hierarchy );
+    }
+    
+    private void testHierarchy( OrganisationUnitHierarchy hierarchy )
+    {
         assertEquals( 12, hierarchy.getChildren( 1 ).size() );
         
         assertEquals( 7, hierarchy.getChildren( 2 ).size() );
@@ -41,12 +96,33 @@
         assertTrue( hierarchy.getChildren( 2 ).contains( 12 ) );
         
         assertEquals( 4, hierarchy.getChildren( 3 ).size() );
-        assertTrue( hierarchy.getChildren( 2 ).contains( 4 ) );
-        assertTrue( hierarchy.getChildren( 2 ).contains( 10 ) );
-        assertTrue( hierarchy.getChildren( 2 ).contains( 11 ) );
-        assertTrue( hierarchy.getChildren( 2 ).contains( 12 ) );
+        assertTrue( hierarchy.getChildren( 3 ).contains( 3 ) );
+        assertTrue( hierarchy.getChildren( 3 ).contains( 7 ) );
+        assertTrue( hierarchy.getChildren( 3 ).contains( 8 ) );
+        assertTrue( hierarchy.getChildren( 3 ).contains( 9 ) );
+
+        assertEquals( 4, hierarchy.getChildren( 4 ).size() );
+        assertTrue( hierarchy.getChildren( 4 ).contains( 4 ) );
+        assertTrue( hierarchy.getChildren( 4 ).contains( 10 ) );
+        assertTrue( hierarchy.getChildren( 4 ).contains( 11 ) );
+        assertTrue( hierarchy.getChildren( 4 ).contains( 12 ) );
 
         assertEquals( 1, hierarchy.getChildren( 11 ).size() );
         assertTrue( hierarchy.getChildren( 11 ).contains( 11 ) );
+        
+        assertFalse( hierarchy.getChildren( 2 ).contains( 3 ) );
+        assertFalse( hierarchy.getChildren( 2 ).contains( 8 ) );
+    }
+    
+    private Set<Integer> getSet( Integer... ints )
+    {
+        Set<Integer> set = new HashSet<Integer>();
+        
+        for ( Integer i : ints )
+        {
+            set.add( i );
+        }
+        
+        return set;
     }
 }

=== 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	2011-07-25 20:12:49 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java	2011-07-26 07:39:20 +0000
@@ -423,11 +423,10 @@
     
     public OrganisationUnitDataSetAssociationSet getOrganisationUnitDataSetAssociationSet()
     {
-        //TODO hierarchy
-        
         Map<Integer, Set<Integer>> associationSet = organisationUnitStore.getOrganisationUnitDataSetAssocationMap();
         
         filterUserDataSets( associationSet );
+        filterChildOrganisationUnits( associationSet );
         
         OrganisationUnitDataSetAssociationSet set = new OrganisationUnitDataSetAssociationSet();
         
@@ -462,6 +461,20 @@
         }
     }
     
+    private void filterChildOrganisationUnits( Map<Integer, Set<Integer>> associatonMap )
+    {
+        User currentUser = currentUserService.getCurrentUser();
+        
+        if ( currentUser != null )
+        {
+            Collection<Integer> parentIds = ConversionUtils.getIdentifiers( OrganisationUnit.class, currentUser.getOrganisationUnits() );
+            
+            Collection<Integer> children = getOrganisationUnitHierarchy().getChildren( parentIds );
+        
+            associatonMap.keySet().retainAll( children );
+        }
+    }
+    
     // -------------------------------------------------------------------------
     // OrganisationUnitHierarchy
     // -------------------------------------------------------------------------