← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 14239: Moved util methods from DataQueryParams to DimensionalObjectUtils

 

------------------------------------------------------------
revno: 14239
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2014-03-17 13:07:44 +0100
message:
  Moved util methods from DataQueryParams to DimensionalObjectUtils
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DimensionalObjectUtils.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/main/java/org/hisp/dhis/analytics/data/JdbcAnalyticsManager.java
  dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEventAnalyticsService.java
  dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/DataQueryParamsTest.java
  dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AnalyticsController.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/DimensionalObjectUtils.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DimensionalObjectUtils.java	2013-08-23 15:56:19 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DimensionalObjectUtils.java	2014-03-17 12:07:44 +0000
@@ -32,15 +32,24 @@
 import static org.hisp.dhis.common.DimensionalObject.DATA_X_DIM_ID;
 
 import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.regex.Pattern;
 
 /**
  * @author Lars Helge Overland
  */
 public class DimensionalObjectUtils
 {
+    public static final String DIMENSION_NAME_SEP = ":";
+    public static final String OPTION_SEP = ";";
+    public static final String ITEM_SEP = "-";
+    
+    private static final Pattern INT_PATTERN = Pattern.compile( "^(0|-?[1-9]\\d*)$" );
+    
     /**
      * Converts a concrete dimensional class identifier to a dimension identifier.
      * 
@@ -127,6 +136,79 @@
     }
 
     /**
+     * Retrieves the dimension name from the given string. Returns the part of
+     * the string preceding the dimension name separator, or the whole string if
+     * the separator is not present.
+     */
+    public static String getDimensionFromParam( String param )
+    {
+        if ( param == null )
+        {
+            return null;
+        }
+        
+        return param.split( DIMENSION_NAME_SEP ).length > 0 ? param.split( DIMENSION_NAME_SEP )[0] : param;
+    }
+    
+    /**
+     * Retrieves the dimension options from the given string. Looks for the part
+     * succeeding the dimension name separator, if exists, splits the string part
+     * on the option separator and returns the resulting values. If the dimension
+     * name separator does not exist an empty list is returned, indicating that
+     * all dimension options should be used.
+     */
+    public static List<String> getDimensionItemsFromParam( String param )
+    {
+        if ( param == null )
+        {
+            return null;
+        }
+        
+        if ( param.split( DIMENSION_NAME_SEP ).length > 1 )
+        {
+            return new ArrayList<String>( Arrays.asList( param.split( DIMENSION_NAME_SEP )[1].split( OPTION_SEP ) ) );
+        }
+        
+        return new ArrayList<String>();
+    }
+    
+    /**
+     * Splits the given string on the ; character and returns the items in a 
+     * list. Returns null if the given string is null.
+     */
+    public static List<String> getDimensionsFromParam( String param )
+    {
+        if ( param == null )
+        {
+            return null;
+        }
+        
+        return new ArrayList<String>( Arrays.asList( param.split( OPTION_SEP ) ) );
+    }
+
+    /**
+     * Indicates whether at least one of the given dimenions has at least one
+     * item.
+     */
+    public static boolean anyDimensionHasItems( Collection<DimensionalObject> dimensions )
+    {
+        if ( dimensions == null || dimensions.isEmpty() )
+        {
+            return false;
+        }
+        
+        for ( DimensionalObject dim : dimensions )
+        {
+            if ( dim.hasItems() )
+            {
+                return true;
+            }
+        }
+        
+        return false;
+    }
+    
+    /**
      * Retrieves the level from a level parameter string, which is on the format
      * LEVEL-<level>-<item> .
      */
@@ -137,9 +219,9 @@
             return 0;
         }
         
-        String[] split = param.split( "-" );
+        String[] split = param.split( ITEM_SEP );
         
-        if ( split.length > 1 ) // TODO check if valid integer
+        if ( split.length > 1 && INT_PATTERN.matcher( split[1] ).matches() )
         {
             return Integer.parseInt( split[1] );
         }
@@ -158,7 +240,7 @@
             return null;
         }
         
-        String[] split = param.split( "-" );
+        String[] split = param.split( ITEM_SEP );
         
         if ( split.length > 1 && split[1] != null )
         {

=== 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	2014-02-14 13:48:40 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/DataQueryParams.java	2014-03-17 12:07:44 +0000
@@ -859,101 +859,6 @@
     // -------------------------------------------------------------------------
 
     /**
-     * Retrieves the dimension name from the given string. Returns the part of
-     * the string preceding the dimension name separator, or the whole string if
-     * the separator is not present.
-     */
-    public static String getDimensionFromParam( String param )
-    {
-        if ( param == null )
-        {
-            return null;
-        }
-        
-        return param.split( DIMENSION_NAME_SEP ).length > 0 ? param.split( DIMENSION_NAME_SEP )[0] : param;
-    }
-    
-    /**
-     * Retrieves the dimension options from the given string. Looks for the part
-     * succeeding the dimension name separator, if exists, splits the string part
-     * on the option separator and returns the resulting values. If the dimension
-     * name separator does not exist an empty list is returned, indicating that
-     * all dimension options should be used.
-     */
-    public static List<String> getDimensionItemsFromParam( String param )
-    {
-        if ( param == null )
-        {
-            return null;
-        }
-        
-        if ( param.split( DIMENSION_NAME_SEP ).length > 1 )
-        {
-            return new ArrayList<String>( Arrays.asList( param.split( DIMENSION_NAME_SEP )[1].split( OPTION_SEP ) ) );
-        }
-        
-        return new ArrayList<String>();
-    }
-    
-    // TODO move these to DimensionalObjectUtils or API, duplication of code
-    
-    /**
-     * Retrieves the level from a level parameter string, which is on the format
-     * LEVEL-<level>-<item> .
-     */
-    public static int getLevelFromLevelParam( String param )
-    {
-        if ( param == null )   
-        {
-            return 0;
-        }
-        
-        String[] split = param.split( ITEM_SEP );
-        
-        if ( split.length > 1 && MathUtils.isInteger( split[1] ) )
-        {
-            return Integer.parseInt( split[1] );
-        }
-        
-        return 0;
-    }
-    
-    /**
-     * Retrieves the uid from an org unit group parameter string, which is on
-     * the format OU_GROUP-<uid> .
-     */
-    public static String getUidFromOrgUnitGroupParam( String param )
-    {
-        if ( param == null )
-        {
-            return null;
-        }
-        
-        String[] split = param.split( ITEM_SEP );
-        
-        if ( split.length > 1 && split[1] != null )
-        {
-            return String.valueOf( split[1] );
-        }
-        
-        return null;
-    }
-    
-    /**
-     * Splits the given string on the ; character and returns the items in a 
-     * list. Returns null if the given string is null.
-     */
-    public static List<String> getDimensionsFromParam( String param )
-    {
-        if ( param == null )
-        {
-            return null;
-        }
-        
-        return new ArrayList<String>( Arrays.asList( param.split( OPTION_SEP ) ) );
-    }
-    
-    /**
      * Retrieves the measure criteria from the given string. Criteria are separated
      * by the option separator, while the criterion filter and value are separated
      * with the dimension name separator.
@@ -984,28 +889,6 @@
         return map;
     }
     
-    /**
-     * Indicates whether at least one of the given dimenions has at least one
-     * item.
-     */
-    public static boolean anyDimensionHasItems( Collection<DimensionalObject> dimensions )
-    {
-        if ( dimensions == null || dimensions.isEmpty() )
-        {
-            return false;
-        }
-        
-        for ( DimensionalObject dim : dimensions )
-        {
-            if ( dim.hasItems() )
-            {
-                return true;
-            }
-        }
-        
-        return false;
-    }
-    
     // -------------------------------------------------------------------------
     // Supportive methods
     // -------------------------------------------------------------------------

=== 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	2014-03-17 10:04:44 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultAnalyticsService.java	2014-03-17 12:07:44 +0000
@@ -37,8 +37,6 @@
 import static org.hisp.dhis.analytics.DataQueryParams.DISPLAY_NAME_ORGUNIT;
 import static org.hisp.dhis.analytics.DataQueryParams.DISPLAY_NAME_PERIOD;
 import static org.hisp.dhis.analytics.DataQueryParams.FIXED_DIMS;
-import static org.hisp.dhis.analytics.DataQueryParams.getDimensionFromParam;
-import static org.hisp.dhis.analytics.DataQueryParams.getDimensionItemsFromParam;
 import static org.hisp.dhis.common.DimensionalObject.CATEGORYOPTIONCOMBO_DIM_ID;
 import static org.hisp.dhis.common.DimensionalObject.DATAELEMENT_DIM_ID;
 import static org.hisp.dhis.common.DimensionalObject.DATASET_DIM_ID;
@@ -91,6 +89,7 @@
 import org.hisp.dhis.common.CombinationGenerator;
 import org.hisp.dhis.common.DimensionType;
 import org.hisp.dhis.common.DimensionalObject;
+import org.hisp.dhis.common.DimensionalObjectUtils;
 import org.hisp.dhis.common.Grid;
 import org.hisp.dhis.common.GridHeader;
 import org.hisp.dhis.common.IdentifiableObject;
@@ -670,8 +669,8 @@
         {
             for ( String param : dimensionParams )
             {
-                String dimension = getDimensionFromParam( param );
-                List<String> options = getDimensionItemsFromParam( param );
+                String dimension = DimensionalObjectUtils.getDimensionFromParam( param );
+                List<String> options = DimensionalObjectUtils.getDimensionItemsFromParam( param );
                 
                 if ( dimension != null && options != null )
                 {
@@ -684,8 +683,8 @@
         {
             for ( String param : filterParams )
             {
-                String dimension = DataQueryParams.getDimensionFromParam( param );
-                List<String> options = DataQueryParams.getDimensionItemsFromParam( param );
+                String dimension = DimensionalObjectUtils.getDimensionFromParam( param );
+                List<String> options = DimensionalObjectUtils.getDimensionItemsFromParam( param );
                 
                 if ( dimension != null && options != null )
                 {
@@ -886,7 +885,7 @@
                 }
                 else if ( ou != null && ou.startsWith( KEY_LEVEL ) )
                 {
-                    int level = DataQueryParams.getLevelFromLevelParam( ou );
+                    int level = DimensionalObjectUtils.getLevelFromLevelParam( ou );
                     
                     if ( level > 0 )
                     {
@@ -895,7 +894,7 @@
                 }
                 else if ( ou != null && ou.startsWith( KEY_ORGUNIT_GROUP ) )
                 {
-                    String uid = DataQueryParams.getUidFromOrgUnitGroupParam( ou );
+                    String uid = DimensionalObjectUtils.getUidFromOrgUnitGroupParam( ou );
                     
                     OrganisationUnitGroup group = organisationUnitGroupService.getOrganisationUnitGroup( uid );
                     

=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/JdbcAnalyticsManager.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/JdbcAnalyticsManager.java	2014-01-31 18:08:43 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/JdbcAnalyticsManager.java	2014-03-17 12:07:44 +0000
@@ -57,6 +57,7 @@
 import org.hisp.dhis.analytics.DataQueryParams;
 import org.hisp.dhis.analytics.MeasureFilter;
 import org.hisp.dhis.common.DimensionalObject;
+import org.hisp.dhis.common.DimensionalObjectUtils;
 import org.hisp.dhis.common.ListMap;
 import org.hisp.dhis.common.NameableObject;
 import org.hisp.dhis.jdbc.StatementBuilder;
@@ -270,7 +271,7 @@
         {
             List<DimensionalObject> filters = filterMap.get( dimension );
             
-            if ( DataQueryParams.anyDimensionHasItems( filters ) )
+            if ( DimensionalObjectUtils.anyDimensionHasItems( filters ) )
             {
                 sql += sqlHelper.whereAnd() + " (";
                 

=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEventAnalyticsService.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEventAnalyticsService.java	2014-03-17 10:04:44 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEventAnalyticsService.java	2014-03-17 12:07:44 +0000
@@ -46,7 +46,6 @@
 import java.util.Set;
 
 import org.hisp.dhis.analytics.AnalyticsService;
-import org.hisp.dhis.analytics.DataQueryParams;
 import org.hisp.dhis.analytics.SortOrder;
 import org.hisp.dhis.analytics.event.EventAnalyticsManager;
 import org.hisp.dhis.analytics.event.EventAnalyticsService;
@@ -54,6 +53,7 @@
 import org.hisp.dhis.analytics.event.EventQueryPlanner;
 import org.hisp.dhis.common.DimensionType;
 import org.hisp.dhis.common.DimensionalObject;
+import org.hisp.dhis.common.DimensionalObjectUtils;
 import org.hisp.dhis.common.Grid;
 import org.hisp.dhis.common.GridHeader;
 import org.hisp.dhis.common.IdentifiableObject;
@@ -324,11 +324,11 @@
         {
             for ( String dim : dimension )
             {
-                String dimensionId = DataQueryParams.getDimensionFromParam( dim );
+                String dimensionId = DimensionalObjectUtils.getDimensionFromParam( dim );
 
                 if ( ORGUNIT_DIM_ID.equals( dimensionId ) || PERIOD_DIM_ID.equals( dimensionId ) )
                 {
-                    List<String> items = DataQueryParams.getDimensionItemsFromParam( dim );
+                    List<String> items = DimensionalObjectUtils.getDimensionItemsFromParam( dim );
                     params.getDimensions().addAll( analyticsService.getDimension( dimensionId, items, date, format ) );
                 }
                 else
@@ -342,11 +342,11 @@
         {
             for ( String dim : filter )
             {
-                String dimensionId = DataQueryParams.getDimensionFromParam( dim );
+                String dimensionId = DimensionalObjectUtils.getDimensionFromParam( dim );
 
                 if ( ORGUNIT_DIM_ID.equals( dimensionId ) || PERIOD_DIM_ID.equals( dimensionId ) )
                 {
-                    List<String> items = DataQueryParams.getDimensionItemsFromParam( dim );
+                    List<String> items = DimensionalObjectUtils.getDimensionItemsFromParam( dim );
                     params.getFilters().addAll( analyticsService.getDimension( dimensionId, items, date, format ) );
                 }
                 else

=== 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	2014-02-04 08:24:06 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/DataQueryParamsTest.java	2014-03-17 12:07:44 +0000
@@ -44,6 +44,7 @@
 import org.hisp.dhis.common.BaseDimensionalObject;
 import org.hisp.dhis.common.DimensionType;
 import org.hisp.dhis.common.DimensionalObject;
+import org.hisp.dhis.common.DimensionalObjectUtils;
 import org.hisp.dhis.common.NameableObject;
 import org.hisp.dhis.period.Period;
 import org.hisp.dhis.system.util.ListUtils;
@@ -58,7 +59,7 @@
     @Test
     public void testGetDimensionFromParam()
     {
-        assertEquals( DATAELEMENT_DIM_ID, DataQueryParams.getDimensionFromParam( "de:D348asd782j;kj78HnH6hgT;9ds9dS98s2" ) );
+        assertEquals( DATAELEMENT_DIM_ID, DimensionalObjectUtils.getDimensionFromParam( "de:D348asd782j;kj78HnH6hgT;9ds9dS98s2" ) );
     }
     
     @Test
@@ -66,15 +67,15 @@
     {
         List<String> expected = new ArrayList<String>( Arrays.asList( "D348asd782j", "kj78HnH6hgT", "9ds9dS98s2" ) );
         
-        assertEquals( expected, DataQueryParams.getDimensionItemsFromParam( "de:D348asd782j;kj78HnH6hgT;9ds9dS98s2" ) );        
+        assertEquals( expected, DimensionalObjectUtils.getDimensionItemsFromParam( "de:D348asd782j;kj78HnH6hgT;9ds9dS98s2" ) );        
     }
     
     @Test
     public void testGetLevelFromLevelParam()
     {
-        assertEquals( 4, DataQueryParams.getLevelFromLevelParam( "LEVEL-4-dFsdfejdf2" ) );
-        assertEquals( 0, DataQueryParams.getLevelFromLevelParam( "LEVEL" ) );
-        assertEquals( 0, DataQueryParams.getLevelFromLevelParam( "LEVEL-gFd" ) );        
+        assertEquals( 4, DimensionalObjectUtils.getLevelFromLevelParam( "LEVEL-4-dFsdfejdf2" ) );
+        assertEquals( 0, DimensionalObjectUtils.getLevelFromLevelParam( "LEVEL" ) );
+        assertEquals( 0, DimensionalObjectUtils.getLevelFromLevelParam( "LEVEL-gFd" ) );        
     }
         
     @Test

=== modified file 'dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java'
--- dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java	2014-03-17 11:30:30 +0000
+++ dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java	2014-03-17 12:07:44 +0000
@@ -37,6 +37,7 @@
 import java.util.Set;
 
 import org.apache.commons.lang.StringUtils;
+import org.hisp.dhis.common.DimensionalObjectUtils;
 import org.hisp.dhis.common.Grid;
 import org.hisp.dhis.common.GridHeader;
 import org.hisp.dhis.common.IllegalQueryException;
@@ -167,6 +168,11 @@
             ous.add( organisationUnit );
         }
         
+        for ( String item : items )
+        {
+            String id = DimensionalObjectUtils.getDimensionFromParam( item );
+        }
+        
         params.setOrganisationUnitMode( ouMode );
         params.setPage( page );
         params.setPageSize( pageSize );
@@ -261,9 +267,6 @@
         Set<TrackedEntityInstance> entityInstances = new HashSet<TrackedEntityInstance>();
         entityInstances.addAll( getTrackedEntityInstancesByPhone( searchText, 0, Integer.MAX_VALUE ) );
 
-        // if an org-unit has been selected, filter out every instance that has
-        // a
-        // different org-unit
         if ( orgUnitId != 0 )
         {
             Set<TrackedEntityInstance> toRemoveList = new HashSet<TrackedEntityInstance>();

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AnalyticsController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AnalyticsController.java	2014-03-17 10:04:44 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AnalyticsController.java	2014-03-17 12:07:44 +0000
@@ -29,7 +29,7 @@
  */
 
 import static org.hisp.dhis.analytics.AnalyticsService.NAMES_META_KEY;
-import static org.hisp.dhis.analytics.DataQueryParams.getDimensionsFromParam;
+import static org.hisp.dhis.common.DimensionalObjectUtils.getDimensionsFromParam;
 
 import java.util.Map;
 import java.util.Set;