← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 11700: added level info for getOrgUnitWithChildren

 

------------------------------------------------------------
revno: 11700
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2013-08-19 13:47:12 +0200
message:
  added level info for getOrgUnitWithChildren
modified:
  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


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java	2013-08-15 14:45:40 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java	2013-08-19 11:47:12 +0000
@@ -318,8 +318,9 @@
 
         List<OrganisationUnit> result = new ArrayList<OrganisationUnit>();
 
-        int rootLevel = 1;
+        int rootLevel = organisationUnit.getOrganisationUnitLevel();
 
+        organisationUnit.setLevel( rootLevel );
         result.add( organisationUnit );
 
         addOrganisationUnitChildren( organisationUnit, result, rootLevel );
@@ -342,12 +343,11 @@
 
         for ( OrganisationUnit child : childList )
         {
+            child.setLevel( level );
             result.add( child );
 
             addOrganisationUnitChildren( child, result, level );
         }
-
-        level--;
     }
 
     public List<OrganisationUnit> getOrganisationUnitBranch( int id )

=== 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-08-15 14:45:40 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/organisationunit/OrganisationUnitServiceTest.java	2013-08-19 11:47:12 +0000
@@ -137,13 +137,38 @@
         organisationUnitService.addOrganisationUnit( unit3 );
         organisationUnitService.addOrganisationUnit( unit4 );
 
-        Collection<OrganisationUnit> actual = organisationUnitService.getOrganisationUnitWithChildren( id1 );
+        List<OrganisationUnit> actual = new ArrayList<OrganisationUnit>( organisationUnitService.getOrganisationUnitWithChildren( id1 ) );
+
         assertEquals( 3, actual.size() );
         assertTrue( actual.contains( unit1 ) );
         assertTrue( actual.contains( unit2 ) );
     }
 
     @Test
+    public void testGetOrganisationUnitWithChildrenWithCorrectLevel()
+        throws Exception
+    {
+        OrganisationUnit unit1 = createOrganisationUnit( 'A' );
+        OrganisationUnit unit2 = createOrganisationUnit( 'B', unit1 );
+        OrganisationUnit unit3 = createOrganisationUnit( 'C', unit2 );
+
+        int id1 = organisationUnitService.addOrganisationUnit( unit1 );
+        unit1.getChildren().add( unit2 );
+        int id2 = organisationUnitService.addOrganisationUnit( unit2 );
+        organisationUnitService.addOrganisationUnit( unit3 );
+
+        List<OrganisationUnit> actual1 = new ArrayList<OrganisationUnit>( organisationUnitService.getOrganisationUnitWithChildren( id1 ) );
+        List<OrganisationUnit> actual2 = new ArrayList<OrganisationUnit>( organisationUnitService.getOrganisationUnitWithChildren( id2 ) );
+
+        assertEquals( 1, actual1.get( 0 ).getLevel() );
+        assertEquals( 2, actual1.get( 1 ).getLevel() );
+        assertEquals( 3, actual1.get( 2 ).getLevel() );
+
+        assertEquals( 2, actual2.get( 0 ).getLevel() );
+        assertEquals( 3, actual2.get( 1 ).getLevel() );
+    }
+
+    @Test
     public void testGetOrganisationUnitsByFields()
         throws Exception
     {
@@ -333,12 +358,12 @@
         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 ) );
-        
+
         assertEquals( 2, unitB.getLevel() );
         assertEquals( 3, unitD.getLevel() );
         assertEquals( 3, unitE.getLevel() );
@@ -401,9 +426,9 @@
 
         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 ) );
@@ -819,7 +844,7 @@
         assertNull( organisationUnitService.getOrganisationUnitLevel( idA ) );
         assertNull( organisationUnitService.getOrganisationUnitLevel( idB ) );
     }
-    
+
     @Test
     public void getMaxLevels()
     {