← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 11667: OrganisationUnitService, get by level methods, changed code to proper overloading and changed beh...

 

------------------------------------------------------------
revno: 11667
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2013-08-14 21:22:59 +0200
message:
  OrganisationUnitService, get by level methods, changed code to proper overloading and changed behavior so that root org units are used in case parent is null
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitService.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/test/java/org/hisp/dhis/organisationunit/OrganisationUnitServiceTest.java
  dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisConvenienceTest.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/OrganisationUnitService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitService.java	2013-07-04 15:41:57 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitService.java	2013-08-14 19:22:59 +0000
@@ -272,6 +272,7 @@
     /**
      * Returns all OrganisationUnits which are children of the given unit and are
      * at the given hierarchical level. The root OrganisationUnits are at level 1.
+     * If parent is null, then all OrganisationUnits at the given level are returned.
      *
      * @param level  the hierarchical level.
      * @param parent the parent unit.
@@ -284,6 +285,7 @@
     /**
      * Returns all OrganisationUnits which are children of the given units and are
      * at the given hierarchical level. The root OrganisationUnits are at level 1.
+     * If parents is null, then all OrganisationUnits at the given level are returned.
      *
      * @param level  the hierarchical level.
      * @param parent the parent units.

=== 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	2013-08-12 09:30:23 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java	2013-08-14 19:22:59 +0000
@@ -30,6 +30,7 @@
 import static org.hisp.dhis.i18n.I18nUtils.i18n;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Date;
@@ -351,67 +352,61 @@
 
     public Collection<OrganisationUnit> getOrganisationUnitsAtLevel( int level )
     {
-        if ( level < 1 )
-        {
-            throw new IllegalArgumentException( "Level must be greater than zero" );
-        }
-
+        Collection<OrganisationUnit> roots = getRootOrganisationUnits();
+        
         if ( level == 1 )
         {
-            return getRootOrganisationUnits();
-        }
-
-        Set<OrganisationUnit> result = new HashSet<OrganisationUnit>();
-
-        for ( OrganisationUnit root : organisationUnitStore.getRootOrganisationUnits() )
-        {
-            addOrganisationUnitChildrenAtLevel( root, 2, level, result );
-        }
-
-        return result;
+            return roots;
+        }
+
+        return getOrganisationUnitsAtLevel( level, roots );
     }
 
     public Collection<OrganisationUnit> getOrganisationUnitsAtLevel( int level, OrganisationUnit parent )
     {
-        if ( level < 1 )
-        {
-            throw new IllegalArgumentException( "Level must be greater than zero" );
-        }
-
-        int parentLevel = parent.getOrganisationUnitLevel();
-
-        if ( level < parentLevel )
-        {
-            throw new IllegalArgumentException(
-                "Level must be greater than or equal to level of parent OrganisationUnit" );
-        }
-
-        Set<OrganisationUnit> result = new HashSet<OrganisationUnit>();
-
-        if ( level == parentLevel )
-        {
-            result.add( parent );
-        }
-        else
-        {
-            addOrganisationUnitChildrenAtLevel( parent, parentLevel + 1, level, result );
-        }
-
-        for ( OrganisationUnit unit : result )
-        {
-            unit.setLevel( level );
-        }
+        Set<OrganisationUnit> set = new HashSet<OrganisationUnit>();
+        set.add( parent );
         
-        return result;
+        return getOrganisationUnitsAtLevel( level, parent != null ? set : null );
     }
 
     public Collection<OrganisationUnit> getOrganisationUnitsAtLevel( int level, Collection<OrganisationUnit> parents )
     {
+        if ( level < 1 )
+        {
+            throw new IllegalArgumentException( "Level must be greater than zero" );
+        }
+        
+        if ( parents == null )
+        {
+            parents = new HashSet<OrganisationUnit>( getRootOrganisationUnits() );
+        }
+        
         Set<OrganisationUnit> result = new HashSet<OrganisationUnit>();
         
         for ( OrganisationUnit parent : parents )
         {
-            result.addAll( getOrganisationUnitsAtLevel( level, parent ) );
+            int parentLevel = parent.getOrganisationUnitLevel();
+
+            if ( level < parentLevel )
+            {
+                throw new IllegalArgumentException(
+                    "Level must be greater than or equal to level of parent OrganisationUnit" );
+            }
+
+            if ( level == parentLevel )
+            {
+                result.add( parent );
+            }
+            else
+            {
+                addOrganisationUnitChildrenAtLevel( parent, parentLevel + 1, level, result );
+            }
+
+            for ( OrganisationUnit unit : result )
+            {
+                unit.setLevel( level );
+            }
         }
         
         return result;

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/organisationunit/OrganisationUnitServiceTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/organisationunit/OrganisationUnitServiceTest.java	2013-06-08 08:02:30 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/organisationunit/OrganisationUnitServiceTest.java	2013-08-14 19:22:59 +0000
@@ -31,6 +31,7 @@
 import org.junit.Ignore;
 import org.junit.Test;
 
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Date;
@@ -338,9 +339,74 @@
         organisationUnitService.addOrganisationUnit( unitN );
         organisationUnitService.addOrganisationUnit( unitO );
 
+        Collection<OrganisationUnit> nill = null;
+        
         assertTrue( equals( organisationUnitService.getOrganisationUnitsAtLevel( 2, unitB ), unitB ) );
         assertTrue( equals( organisationUnitService.getOrganisationUnitsAtLevel( 3, unitB ), unitD, unitE ) );
         assertTrue( equals( organisationUnitService.getOrganisationUnitsAtLevel( 4, unitB ), unitH, unitI, unitJ, unitK ) );
+        assertTrue( equals( organisationUnitService.getOrganisationUnitsAtLevel( 2, nill ), unitB, unitC ) );
+    }
+
+    @Test
+    public void testGetOrganisationUnitAtLevelAndBranches()
+        throws Exception
+    {
+        OrganisationUnit unitA = createOrganisationUnit( 'A' );
+        OrganisationUnit unitB = createOrganisationUnit( 'B', unitA );
+        OrganisationUnit unitC = createOrganisationUnit( 'C', unitA );
+        OrganisationUnit unitD = createOrganisationUnit( 'D', unitB );
+        OrganisationUnit unitE = createOrganisationUnit( 'E', unitB );
+        OrganisationUnit unitF = createOrganisationUnit( 'F', unitC );
+        OrganisationUnit unitG = createOrganisationUnit( 'G', unitC );
+        OrganisationUnit unitH = createOrganisationUnit( 'H', unitD );
+        OrganisationUnit unitI = createOrganisationUnit( 'I', unitD );
+        OrganisationUnit unitJ = createOrganisationUnit( 'J', unitE );
+        OrganisationUnit unitK = createOrganisationUnit( 'K', unitE );
+        OrganisationUnit unitL = createOrganisationUnit( 'L', unitF );
+        OrganisationUnit unitM = createOrganisationUnit( 'M', unitF );
+        OrganisationUnit unitN = createOrganisationUnit( 'N', unitG );
+        OrganisationUnit unitO = createOrganisationUnit( 'O', unitG );
+
+        unitA.getChildren().add( unitB );
+        unitA.getChildren().add( unitC );
+        unitB.getChildren().add( unitD );
+        unitB.getChildren().add( unitE );
+        unitC.getChildren().add( unitF );
+        unitC.getChildren().add( unitG );
+        unitD.getChildren().add( unitH );
+        unitD.getChildren().add( unitI );
+        unitE.getChildren().add( unitJ );
+        unitE.getChildren().add( unitK );
+        unitF.getChildren().add( unitL );
+        unitF.getChildren().add( unitM );
+        unitG.getChildren().add( unitN );
+        unitG.getChildren().add( unitO );
+
+        organisationUnitService.addOrganisationUnit( unitA );
+        organisationUnitService.addOrganisationUnit( unitB );
+        organisationUnitService.addOrganisationUnit( unitC );
+        organisationUnitService.addOrganisationUnit( unitD );
+        organisationUnitService.addOrganisationUnit( unitE );
+        organisationUnitService.addOrganisationUnit( unitF );
+        organisationUnitService.addOrganisationUnit( unitG );
+        organisationUnitService.addOrganisationUnit( unitH );
+        organisationUnitService.addOrganisationUnit( unitI );
+        organisationUnitService.addOrganisationUnit( unitJ );
+        organisationUnitService.addOrganisationUnit( unitK );
+        organisationUnitService.addOrganisationUnit( unitL );
+        organisationUnitService.addOrganisationUnit( unitM );
+        organisationUnitService.addOrganisationUnit( unitN );
+        organisationUnitService.addOrganisationUnit( unitO );
+
+        List<OrganisationUnit> unitsA = new ArrayList<OrganisationUnit>( Arrays.asList( unitB, unitC ) );
+        List<OrganisationUnit> unitsB = new ArrayList<OrganisationUnit>( Arrays.asList( unitD, unitE ) );
+        
+        OrganisationUnit nill = null;
+        
+        assertTrue( equals( organisationUnitService.getOrganisationUnitsAtLevel( 3, unitsA ), unitD, unitE, unitF, unitG ) );
+        assertTrue( equals( organisationUnitService.getOrganisationUnitsAtLevel( 4, unitsA ), unitH, unitI, unitJ, unitK, unitL, unitM, unitN, unitO ) );
+        assertTrue( equals( organisationUnitService.getOrganisationUnitsAtLevel( 4, unitsB ), unitH, unitI, unitJ, unitK ) );
+        assertTrue( equals( organisationUnitService.getOrganisationUnitsAtLevel( 2, nill ), unitB, unitC ) );
     }
 
     @Test

=== modified file 'dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisConvenienceTest.java'
--- dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisConvenienceTest.java	2013-08-12 13:41:59 +0000
+++ dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisConvenienceTest.java	2013-08-14 19:22:59 +0000
@@ -37,6 +37,8 @@
 import javax.xml.xpath.XPathExpressionException;
 import javax.xml.xpath.XPathFactory;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.hisp.dhis.aggregation.AggregatedDataValueService;
 import org.hisp.dhis.aggregation.AggregatedOrgUnitDataValueService;
 import org.hisp.dhis.chart.Chart;
@@ -110,6 +112,8 @@
  */
 public abstract class DhisConvenienceTest
 {
+    protected static final Log log = LogFactory.getLog( DhisConvenienceTest.class );
+    
     protected static final String BASE_UID = "123456789a";
     protected static final String BASE_IN_UID = "inabcdefgh";
     protected static final String BASE_DE_UID = "deabcdefgh";
@@ -252,6 +256,7 @@
 
         if ( actual.size() != collection.size() )
         {
+            log.warn( "Actual collection has different size compared to reference collection: " + actual.size() + " / " + collection.size() );
             return false;
         }
 
@@ -259,6 +264,7 @@
         {
             if ( !collection.contains( object ) )
             {
+                log.warn( "Object in actual collection not part of reference collection: " + object );
                 return false;
             }
         }
@@ -267,6 +273,7 @@
         {
             if ( !actual.contains( object ) )
             {
+                log.warn( "Object in reference collection not part of actual collection: " + object );
                 return false;
             }
         }
@@ -995,6 +1002,7 @@
         
         return constant;
     }
+    
     // -------------------------------------------------------------------------
     // Supportive methods
     // -------------------------------------------------------------------------
@@ -1041,7 +1049,7 @@
     }
 
     // -------------------------------------------------------------------------
-    // Allow xpath testing of dxf2
+    // Allow xpath testing of DXF2
     // -------------------------------------------------------------------------
 
     protected String xpathTest( String xpathString, String xml )
@@ -1052,10 +1060,9 @@
         XPath xpath = factory.newXPath();
         xpath.setNamespaceContext( new Dxf2NamespaceResolver() );
 
-        return (String) xpath.evaluate( xpathString, source );
+        return xpath.evaluate( xpathString, source );
     }
 
-    // we need this to resolve dxf2 namespace in xpath
     protected class Dxf2NamespaceResolver
         implements NamespaceContext
     {
@@ -1083,20 +1090,13 @@
         @Override
         public String getPrefix( String namespaceURI )
         {
-            // Not needed in this context.
             return null;
         }
 
         @Override
-        public Iterator getPrefixes( String namespaceURI )
+        public Iterator<?> getPrefixes( String namespaceURI )
         {
-            // Not needed in this context.
             return null;
         }
     }
-
-    // -------------------------------------------------------------------------
-    // Validation Criteria
-    // -------------------------------------------------------------------------
-
 }