← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 20213: OrganisationUnitStore, implemented method getMaxLevel

 

------------------------------------------------------------
revno: 20213
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2015-09-17 15:42:30 +0200
message:
  OrganisationUnitStore, implemented method getMaxLevel
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitQueryParams.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitStore.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/hibernate/HibernateOrganisationUnitStore.java
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/organisationunit/OrganisationUnitStoreTest.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/OrganisationUnitQueryParams.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitQueryParams.java	2015-09-17 12:25:32 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitQueryParams.java	2015-09-17 13:42:30 +0000
@@ -40,7 +40,7 @@
  */
 public class OrganisationUnitQueryParams
 {
-    private static final int CODE_SEP_LENGTH = CodeGenerator.CODESIZE + 1;
+    public static final int CODE_SEP_LENGTH = CodeGenerator.CODESIZE + 1;
     
     /**
      * Query string to match like name and exactly on UID and code.

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitStore.java	2015-09-09 08:45:33 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitStore.java	2015-09-17 13:42:30 +0000
@@ -170,4 +170,12 @@
     void updatePaths();
 
     void forceUpdatePaths();
+    
+    /**
+     * Returns the number of organsiation unit levels in the database based on
+     * the organisation unit hierarchy.
+     * 
+     * @return number of levels, 0 if no organisation units are present.
+     */
+    int getMaxLevel();
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/hibernate/HibernateOrganisationUnitStore.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/hibernate/HibernateOrganisationUnitStore.java	2015-09-17 13:26:47 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/hibernate/HibernateOrganisationUnitStore.java	2015-09-17 13:42:30 +0000
@@ -345,7 +345,7 @@
             counter++;
         }
     }
-    
+
     @Override
     @SuppressWarnings( "unchecked" )
     public void forceUpdatePaths()
@@ -368,4 +368,21 @@
             counter++;
         }
     }
+
+    @Override
+    public int getMaxLevel()
+    {
+        String hql = "select max(length(ou.path)) from OrganisationUnit ou";
+
+        Integer maxLength = (Integer) getQuery( hql ).uniqueResult();
+        
+        if ( maxLength != null )
+        {        
+            int level = maxLength / OrganisationUnitQueryParams.CODE_SEP_LENGTH;
+        
+            return level;
+        }
+        
+        return 0;
+    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/organisationunit/OrganisationUnitStoreTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/organisationunit/OrganisationUnitStoreTest.java	2015-09-17 12:59:41 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/organisationunit/OrganisationUnitStoreTest.java	2015-09-17 13:42:30 +0000
@@ -69,8 +69,8 @@
         OrganisationUnit ouC = createOrganisationUnit( 'C', ouA ); // 2
         OrganisationUnit ouD = createOrganisationUnit( 'D', ouB ); // 3
         OrganisationUnit ouE = createOrganisationUnit( 'E', ouB ); // 3
-        OrganisationUnit ouF = createOrganisationUnit( 'F', ouC ); // 4
-        OrganisationUnit ouG = createOrganisationUnit( 'G', ouC ); // 4
+        OrganisationUnit ouF = createOrganisationUnit( 'F', ouC ); // 3
+        OrganisationUnit ouG = createOrganisationUnit( 'G', ouC ); // 3
         
         orgUnitStore.save( ouA );
         orgUnitStore.save( ouB );
@@ -271,4 +271,34 @@
         assertNull( orgUnitLevelStore.get( idA ) );
         assertNull( orgUnitLevelStore.get( idB ) );
     }
+
+    @Test
+    public void testGetMaxLevel()
+    {
+        OrganisationUnit ouA = createOrganisationUnit( 'A' ); // 1
+        OrganisationUnit ouB = createOrganisationUnit( 'B', ouA ); // 2
+        OrganisationUnit ouC = createOrganisationUnit( 'C', ouA ); // 2
+        OrganisationUnit ouD = createOrganisationUnit( 'D', ouB ); // 3
+        OrganisationUnit ouE = createOrganisationUnit( 'E', ouB ); // 3
+        OrganisationUnit ouF = createOrganisationUnit( 'F', ouC ); // 3
+        OrganisationUnit ouG = createOrganisationUnit( 'G', ouC ); // 3
+        
+        assertEquals( 0, orgUnitStore.getMaxLevel() );
+        
+        orgUnitStore.save( ouA );
+        
+        assertEquals( 1, orgUnitStore.getMaxLevel() );
+        
+        orgUnitStore.save( ouB );
+        orgUnitStore.save( ouC );
+        
+        assertEquals( 2, orgUnitStore.getMaxLevel() );
+
+        orgUnitStore.save( ouD );
+        orgUnitStore.save( ouE );
+        orgUnitStore.save( ouF );
+        orgUnitStore.save( ouG );
+        
+        assertEquals( 3, orgUnitStore.getMaxLevel() );
+    }        
 }
\ No newline at end of file