← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 11670: Analytics, made org unit level and org unit group dimension specification more flexible.

 

------------------------------------------------------------
revno: 11670
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2013-08-15 11:32:41 +0200
message:
  Analytics, made org unit level and org unit group dimension specification more flexible.
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseAnalyticalObject.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DimensionalObjectUtils.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitService.java
  dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/DataQueryParams.java
  dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultAnalyticsService.java
  dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/DataQueryParamsTest.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/DefaultDimensionService.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/common/DimensionServiceTest.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/common/BaseAnalyticalObject.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseAnalyticalObject.java	2013-08-09 14:10:43 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseAnalyticalObject.java	2013-08-15 09:32:41 +0000
@@ -427,12 +427,9 @@
             
             if ( organisationUnitLevel != null )
             {
-                for ( OrganisationUnit unit : organisationUnits )
-                {
-                    String id = KEY_LEVEL + organisationUnitLevel + DimensionalObject.DIMENSION_SEP + unit.getUid();
-                    
-                    ouList.add( new BaseNameableObject( id, id, id ) );
-                }
+                String id = KEY_LEVEL + organisationUnitLevel;
+                
+                ouList.add( new BaseNameableObject( id, id, id ) );
             }
             
             if ( itemOrganisationUnitGroups != null && !itemOrganisationUnitGroups.isEmpty() )

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DimensionalObjectUtils.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DimensionalObjectUtils.java	2013-08-09 14:10:43 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DimensionalObjectUtils.java	2013-08-15 09:32:41 +0000
@@ -147,27 +147,6 @@
     }
     
     /**
-     * Retrieves the boundary dimension item from a level parameter string, which
-     * is on the format LEVEL-<level>-<item> .
-     */
-    public static String getBoundaryFromLevelParam( String param )
-    {
-        if ( param == null )   
-        {
-            return null;
-        }
-        
-        String[] split = param.split( "-" );
-        
-        if ( split.length > 2 && split[2] != null )
-        {
-            return split[2];
-        }
-        
-        return null;
-    }
-
-    /**
      * Retrieves the uid from an org unit group parameter string, which is on
      * the format OU_GROUP-<uid> .
      */

=== 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-08-14 19:22:59 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitService.java	2013-08-15 09:32:41 +0000
@@ -227,6 +227,18 @@
     Collection<OrganisationUnit> getLeafOrganisationUnits( int id );
 
     /**
+     * Returns the intersection of the members of the given OrganisationUnitGroup
+     * and the OrganisationUnits which are children of the given collection of
+     * parents in the hierarchy. If the given parents are null or empty, the
+     * members of the group are returned.
+     * 
+     * @param group the OrganisationUnitGroup.
+     * @param parents the collection of OrganisationUnit parents in the hierarchy.
+     * @return collection of OrganisationUnits.
+     */
+    Collection<OrganisationUnit> getOrganisationUnits( OrganisationUnitGroup group, Collection<OrganisationUnit> parents );
+    
+    /**
      * Returns an OrganisationUnit and all its children.
      *
      * @param uid the uid of the parent OrganisationUnit in the subtree.
@@ -235,6 +247,16 @@
      *         OrganisationUnits match.
      */
     Collection<OrganisationUnit> getOrganisationUnitsWithChildren( String uid );
+
+    /**
+     * Returns the OrganisationUnits and all their children.
+     *
+     * @param uids the uids of the parent OrganisationUnits.
+     * @return a collection containing the OrganisationUnit with the given id
+     *         and all its children, or an empty collection if no
+     *         OrganisationUnits match.
+     */
+    Collection<OrganisationUnit> getOrganisationUnitsWithChildren( Collection<String> uids );
     
     /**
      * Returns an OrganisationUnit and all its children.

=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/DataQueryParams.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/DataQueryParams.java	2013-08-09 14:10:43 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/DataQueryParams.java	2013-08-15 09:32:41 +0000
@@ -817,27 +817,6 @@
     }
     
     /**
-     * Retrieves the boundary dimension item from a level parameter string, which
-     * is on the format LEVEL-<level>-<item> .
-     */
-    public static String getBoundaryFromLevelParam( String param )
-    {
-        if ( param == null )   
-        {
-            return null;
-        }
-        
-        String[] split = param.split( ITEM_SEP );
-        
-        if ( split.length > 2 && split[2] != null )
-        {
-            return split[2];
-        }
-        
-        return null;
-    }
-
-    /**
      * Retrieves the uid from an org unit group parameter string, which is on
      * the format OU_GROUP-<uid> .
      */

=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultAnalyticsService.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultAnalyticsService.java	2013-08-09 14:10:43 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultAnalyticsService.java	2013-08-15 09:32:41 +0000
@@ -82,6 +82,7 @@
 import org.hisp.dhis.analytics.QueryPlanner;
 import org.hisp.dhis.common.BaseAnalyticalObject;
 import org.hisp.dhis.common.BaseDimensionalObject;
+import org.hisp.dhis.common.CodeGenerator;
 import org.hisp.dhis.common.CombinationGenerator;
 import org.hisp.dhis.common.DimensionType;
 import org.hisp.dhis.common.DimensionalObject;
@@ -90,6 +91,7 @@
 import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.common.IdentifiableObjectUtils;
 import org.hisp.dhis.common.NameableObject;
+import org.hisp.dhis.common.NameableObjectUtils;
 import org.hisp.dhis.constant.ConstantService;
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.dataelement.DataElementCategory;
@@ -123,6 +125,7 @@
 import org.hisp.dhis.system.util.MathUtils;
 import org.hisp.dhis.system.util.SystemUtils;
 import org.hisp.dhis.system.util.Timer;
+import org.hisp.dhis.system.util.UniqueArrayList;
 import org.hisp.dhis.user.CurrentUserService;
 import org.hisp.dhis.user.User;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -800,7 +803,9 @@
         {
             User user = currentUserService.getCurrentUser();
             
-            List<NameableObject> ous = new ArrayList<NameableObject>();
+            List<NameableObject> ous = new UniqueArrayList<NameableObject>();
+            List<Integer> levels = new UniqueArrayList<Integer>();
+            List<OrganisationUnitGroup> groups = new UniqueArrayList<OrganisationUnitGroup>();
             
             for ( String ou : items )
             {
@@ -816,43 +821,63 @@
                 {
                     int level = DataQueryParams.getLevelFromLevelParam( ou );
                     
-                    String boundaryId = DataQueryParams.getBoundaryFromLevelParam( ou );
-                    
-                    OrganisationUnit boundary = null;
-                    
-                    if ( level > 0 && boundaryId != null && ( boundary = organisationUnitService.getOrganisationUnit( boundaryId ) ) != null )
-                    {                        
-                        ous.addAll( organisationUnitService.getOrganisationUnitsAtLevel( level, boundary ) );
+                    if ( level > 0 )
+                    {
+                        levels.add( level );
                     }
                 }
                 else if ( ou != null && ou.startsWith( KEY_ORGUNIT_GROUP ) )
                 {
                     String uid = DataQueryParams.getUidFromOrgUnitGroupParam( ou );
                     
-                    OrganisationUnitGroup group = null;
+                    OrganisationUnitGroup group = organisationUnitGroupService.getOrganisationUnitGroup( uid );
                     
-                    if ( uid != null && ( group = organisationUnitGroupService.getOrganisationUnitGroup( uid ) ) != null )
+                    if ( uid != null )
                     {
-                        ous.addAll( group.getMembers() );
+                        groups.add( group );
                     }
                 }
-                else
+                else if ( CodeGenerator.isValidCode( ou ) )
                 {
                     OrganisationUnit unit = organisationUnitService.getOrganisationUnit( ou );
                     
-                    if ( unit != null && !ous.contains( unit ) )
+                    if ( unit != null )
                     {
                         ous.add( unit );
                     }
                 }
             }
             
-            if ( ous.isEmpty() )
+            List<NameableObject> orgUnits = new UniqueArrayList<NameableObject>();
+            List<OrganisationUnit> ousList = NameableObjectUtils.asTypedList( ous );
+            
+            if ( !levels.isEmpty() )
+            {
+                for ( Integer level : levels )
+                {
+                    orgUnits.addAll( organisationUnitService.getOrganisationUnitsAtLevel( level, ousList ) );
+                }
+            }
+            
+            if ( !groups.isEmpty() )
+            {
+                for ( OrganisationUnitGroup group : groups )
+                {                    
+                    orgUnits.addAll( organisationUnitService.getOrganisationUnits( group, ousList ) );
+                }
+            }
+            
+            if ( levels.isEmpty() && groups.isEmpty() )
+            {
+                orgUnits.addAll( ous );
+            }            
+            
+            if ( orgUnits.isEmpty() )
             {
                 throw new IllegalQueryException( "Dimension ou is present in query without any valid dimension options" );
             }
             
-            DimensionalObject object = new BaseDimensionalObject( dimension, DimensionType.ORGANISATIONUNIT, null, DISPLAY_NAME_ORGUNIT, ous );
+            DimensionalObject object = new BaseDimensionalObject( dimension, DimensionType.ORGANISATIONUNIT, null, DISPLAY_NAME_ORGUNIT, orgUnits );
             
             return Arrays.asList( object );
         }

=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/DataQueryParamsTest.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/DataQueryParamsTest.java	2013-05-24 11:48:35 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/DataQueryParamsTest.java	2013-08-15 09:32:41 +0000
@@ -71,15 +71,7 @@
         assertEquals( 0, DataQueryParams.getLevelFromLevelParam( "LEVEL" ) );
         assertEquals( 0, DataQueryParams.getLevelFromLevelParam( "LEVEL-gFd" ) );        
     }
-    
-    @Test
-    public void testGetBoundaryFromLevelParam()
-    {
-        assertEquals( "dFsdfejdf2", DataQueryParams.getBoundaryFromLevelParam( "LEVEL-4-dFsdfejdf2" ) );
-        assertEquals( null, DataQueryParams.getBoundaryFromLevelParam( "LEVEL-4-" ) );
-        assertEquals( null, DataQueryParams.getBoundaryFromLevelParam( "LEVEL-4" ) );
-    }
-    
+        
     @Test
     public void testGetMeasureCriteriaFromParam()
     {

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/DefaultDimensionService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/DefaultDimensionService.java	2013-08-09 14:10:43 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/DefaultDimensionService.java	2013-08-15 09:32:41 +0000
@@ -291,14 +291,9 @@
                         {
                             int level = DimensionalObjectUtils.getLevelFromLevelParam( ou );
                             
-                            String boundary = DimensionalObjectUtils.getBoundaryFromLevelParam( ou );
-
-                            OrganisationUnit unit = null;
-                            
-                            if ( level > 0 && boundary != null && ( unit = identifiableObjectManager.get( OrganisationUnit.class, boundary ) ) != null )
+                            if ( level > 0 )
                             {
                                 object.setOrganisationUnitLevel( level );
-                                ous.add( unit );
                             }
                         }
                         else if ( ou != null && ou.startsWith( KEY_ORGUNIT_GROUP ) )

=== 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-14 19:22:59 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java	2013-08-15 09:32:41 +0000
@@ -30,7 +30,6 @@
 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;
@@ -42,6 +41,7 @@
 import java.util.Set;
 
 import org.apache.commons.collections.CollectionUtils;
+import org.hisp.dhis.common.IdentifiableObjectUtils;
 import org.hisp.dhis.dataset.DataSet;
 import org.hisp.dhis.hierarchy.HierarchyViolationException;
 import org.hisp.dhis.i18n.I18nService;
@@ -271,6 +271,32 @@
         } );
     }
 
+    public Collection<OrganisationUnit> getOrganisationUnits( OrganisationUnitGroup group, Collection<OrganisationUnit> parents )
+    {
+        Set<OrganisationUnit> members = new HashSet<OrganisationUnit>( group.getMembers() );
+        
+        if ( parents != null && !parents.isEmpty() )
+        {
+            Collection<OrganisationUnit> children = getOrganisationUnitsWithChildren( IdentifiableObjectUtils.getUids( parents ) );
+            
+            members.retainAll( children );
+        }
+        
+        return members;
+    }
+    
+    public Collection<OrganisationUnit> getOrganisationUnitsWithChildren( Collection<String> parentUids )
+    {
+        Set<OrganisationUnit> units = new HashSet<OrganisationUnit>();
+        
+        for ( String uid : parentUids )
+        {
+            units.addAll( getOrganisationUnitsWithChildren( uid ) );
+        }
+        
+        return units;
+    }
+    
     public Collection<OrganisationUnit> getOrganisationUnitsWithChildren( String uid )
     {
         return getOrganisationUnitWithChildren( getOrganisationUnit( uid ).getId() );
@@ -377,7 +403,7 @@
             throw new IllegalArgumentException( "Level must be greater than zero" );
         }
         
-        if ( parents == null )
+        if ( parents == null || parents.isEmpty() )
         {
             parents = new HashSet<OrganisationUnit>( getRootOrganisationUnits() );
         }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/common/DimensionServiceTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/common/DimensionServiceTest.java	2013-07-04 15:41:57 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/common/DimensionServiceTest.java	2013-08-15 09:32:41 +0000
@@ -189,7 +189,7 @@
         ReportTable reportTable = new ReportTable();
 
         reportTable.getColumns().add( new BaseDimensionalObject( DimensionalObject.DATAELEMENT_DIM_ID, Arrays.asList( deA, deB ) ) );
-        reportTable.getRows().add( new BaseDimensionalObject( DimensionalObject.ORGUNIT_DIM_ID, Arrays.asList( ouLevel2 ) ) );
+        reportTable.getRows().add( new BaseDimensionalObject( DimensionalObject.ORGUNIT_DIM_ID, Arrays.asList( ouLevel2, ouA ) ) );
         reportTable.getFilters().add( new BaseDimensionalObject( DimensionalObject.PERIOD_DIM_ID, Arrays.asList( peA ) ) );
         
         dimensionService.mergeAnalyticalObject( reportTable );