← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 20097: Organisation unit search, added param for max levels

 

------------------------------------------------------------
revno: 20097
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2015-09-11 21:29:32 +0200
message:
  Organisation unit search, added param for max levels
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitQueryParams.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-08 21:20:48 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitQueryParams.java	2015-09-11 19:29:32 +0000
@@ -31,6 +31,8 @@
 import java.util.HashSet;
 import java.util.Set;
 
+import org.hisp.dhis.common.CodeGenerator;
+
 import com.google.common.base.MoreObjects;
 
 /**
@@ -38,16 +40,41 @@
  */
 public class OrganisationUnitQueryParams
 {
+    /**
+     * Query string to match like name and exactly on UID and code.
+     */
     private String query;
     
+    /**
+     * The parent organisation units for which to include all children.
+     */
     private Set<OrganisationUnit> parents = new HashSet<>();
     
+    /**
+     * The groups for which members to include.
+     */
     private Set<OrganisationUnitGroup> groups = new HashSet<>();
 
+    /**
+     * The maximum number of organisation unit levels to fetch, relative to the
+     * real root of the hierarchy.
+     */
+    private Integer maxLevels;
+    
+    /**
+     * The first result to include.
+     */
     private Integer first;
     
+    /**
+     * The max number of results to include.
+     */
     private Integer max;
 
+    // -------------------------------------------------------------------------
+    // Constructor
+    // -------------------------------------------------------------------------
+
     public OrganisationUnitQueryParams()
     {
     }
@@ -60,12 +87,17 @@
     {
         return parents != null && !parents.isEmpty();
     }
-    
+        
     public boolean hasGroups()
     {
         return groups != null && !groups.isEmpty();
     }
     
+    public int getMaxLevelsPathLength()
+    {
+        return maxLevels != null ? ( ( CodeGenerator.CODESIZE + 1 ) * maxLevels ) : -1;
+    }
+    
     @Override
     public String toString()
     {
@@ -73,6 +105,7 @@
             add( "query", query ).
             add( "parents", parents ).
             add( "groups", groups ).
+            add( "maxLevels", maxLevels ).
             add( "first", first ).
             add( "max", max ).toString();
     }
@@ -111,6 +144,16 @@
         this.groups = groups;
     }
 
+    public Integer getMaxLevels()
+    {
+        return maxLevels;
+    }
+
+    public void setMaxLevels( Integer maxLevels )
+    {
+        this.maxLevels = maxLevels;
+    }
+
     public Integer getFirst()
     {
         return first;

=== 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-09 08:59:43 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/hibernate/HibernateOrganisationUnitStore.java	2015-09-11 19:29:32 +0000
@@ -183,6 +183,11 @@
             hql = TextUtils.removeLastOr( hql ) + ") ";
         }
         
+        if ( params.getMaxLevels() != null )
+        {
+            hql += hlp.whereAnd() + " length(o.path) <= :pathLength ";
+        }
+        
         hql += "order by o.name";
         
         Query query = getQuery( hql );
@@ -208,6 +213,11 @@
                 query.setString( parent.getUid(), "%" + parent.getUid() + "%" );
             }
         }
+        
+        if ( params.getMaxLevels() != null )
+        {
+            query.setInteger( "pathLength", params.getMaxLevelsPathLength() );
+        }
 
         if ( params.getFirst() != null )
         {

=== 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-09 08:41:15 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/organisationunit/OrganisationUnitStoreTest.java	2015-09-11 19:29:32 +0000
@@ -127,7 +127,43 @@
         ous = orgUnitStore.getOrganisationUnits( params );
 
         assertEquals( 1, ous.size() );
-        assertTrue( ous.containsAll( Sets.newHashSet( ouG ) ) );        
+        assertTrue( ous.containsAll( Sets.newHashSet( ouG ) ) );
+        
+        params = new OrganisationUnitQueryParams();
+        params.setParents( Sets.newHashSet( ouA ) );
+        params.setMaxLevels( 2 );
+
+        ous = orgUnitStore.getOrganisationUnits( params );
+
+        assertEquals( 3, ous.size() );
+        assertTrue( ous.containsAll( Sets.newHashSet( ouA, ouB, ouC ) ) );
+
+        params = new OrganisationUnitQueryParams();
+        params.setParents( Sets.newHashSet( ouA ) );
+        params.setMaxLevels( 3 );
+
+        ous = orgUnitStore.getOrganisationUnits( params );
+
+        assertEquals( 7, ous.size() );
+        assertTrue( ous.containsAll( Sets.newHashSet( ouA, ouB, ouC, ouD, ouE, ouF, ouG ) ) );
+
+        params = new OrganisationUnitQueryParams();
+        params.setParents( Sets.newHashSet( ouA ) );
+        params.setMaxLevels( 1 );
+
+        ous = orgUnitStore.getOrganisationUnits( params );
+
+        assertEquals( 1, ous.size() );
+        assertTrue( ous.containsAll( Sets.newHashSet( ouA ) ) );
+
+        params = new OrganisationUnitQueryParams();
+        params.setParents( Sets.newHashSet( ouB ) );
+        params.setMaxLevels( 3 );
+
+        ous = orgUnitStore.getOrganisationUnits( params );
+
+        assertEquals( 3, ous.size() );
+        assertTrue( ous.containsAll( Sets.newHashSet( ouB, ouD, ouE ) ) );
     }
     
     // -------------------------------------------------------------------------