← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 19400: Analytics, data query params, returning empty lists instead of nulls. Removed null checks for col...

 

------------------------------------------------------------
revno: 19400
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Sun 2015-06-14 19:51:03 +0200
message:
  Analytics, data query params, returning empty lists instead of nulls. Removed null checks for collections. Avoids hiding issues and makes code easier to debug.
modified:
  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/DefaultQueryPlanner.java
  dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/dimension/DefaultDimensionService.java
  dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEventQueryPlanner.java
  dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEventAnalyticsManager.java
  dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/security/DefaultAnalyticsSecurityManager.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-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	2015-06-14 15:30:53 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/DataQueryParams.java	2015-06-14 17:51:03 +0000
@@ -80,9 +80,9 @@
 import org.hisp.dhis.period.PeriodType;
 import org.hisp.dhis.program.Program;
 import org.hisp.dhis.program.ProgramStage;
+import org.hisp.dhis.system.util.MathUtils;
 import org.hisp.dhis.util.CollectionUtils;
 import org.hisp.dhis.util.ListUtils;
-import org.hisp.dhis.system.util.MathUtils;
 
 /**
  * @author Lars Helge Overland
@@ -555,7 +555,7 @@
         List<NameableObject> dimOpts = getDimensionOptions( PERIOD_DIM_ID );
         List<NameableObject> filterOpts = getFilterOptions( PERIOD_DIM_ID );
         
-        return ( dimOpts != null && !dimOpts.isEmpty() ) || ( filterOpts != null && !filterOpts.isEmpty() );
+        return !dimOpts.isEmpty() || !filterOpts.isEmpty();
     }
     
     /**
@@ -566,7 +566,7 @@
         List<NameableObject> dimOpts = getDimensionOptions( ORGUNIT_DIM_ID );
         List<NameableObject> filterOpts = getFilterOptions( ORGUNIT_DIM_ID );
         
-        return ( dimOpts != null && !dimOpts.isEmpty() ) || ( filterOpts != null && !filterOpts.isEmpty() );
+        return !dimOpts.isEmpty() || !filterOpts.isEmpty();
     }
     
     /**
@@ -577,7 +577,7 @@
     {
         List<NameableObject> filterPeriods = getFilterPeriods();
         
-        if ( filterPeriods != null && !filterPeriods.isEmpty() )
+        if ( !filterPeriods.isEmpty() )
         {
             return ( (Period) filterPeriods.get( 0 ) ).getPeriodType();
         }
@@ -593,7 +593,7 @@
     {
         List<NameableObject> filterPeriods = getFilterPeriods();
         
-        if ( filterPeriods != null && !filterPeriods.isEmpty() )
+        if ( !filterPeriods.isEmpty() )
         {
             return (Period) filterPeriods.get( 0 );
         }
@@ -716,7 +716,7 @@
         {
             this.periodType = this.dataPeriodType.getName();
             
-            if ( getPeriods() != null ) // Period is dimension
+            if ( !getPeriods().isEmpty() ) // Period is dimension
             {
                 setDimensionOptions( PERIOD_DIM_ID, DimensionType.PERIOD, dataPeriodType.getName().toLowerCase(), new ArrayList<>( dataPeriodAggregationPeriodMap.keySet() ) );
             }
@@ -758,14 +758,14 @@
     }
 
     /**
-     * Retrieves the options for the given dimension identifier. Returns null if
-     * the dimension is not present.
+     * Retrieves the options for the given dimension identifier. Returns an empty
+     * list if the dimension is not present.
      */
     public List<NameableObject> getDimensionOptions( String dimension )
     {
         int index = dimensions.indexOf( new BaseDimensionalObject( dimension ) );
         
-        return index != -1 ? dimensions.get( index ).getItems() : null;
+        return index != -1 ? dimensions.get( index ).getItems() : new ArrayList<NameableObject>();
     }
     
     /**
@@ -799,13 +799,14 @@
     }
     
     /**
-     * Retrieves the options for the given filter.
+     * Retrieves the options for the given filter. Returns an empty list if the
+     * filter is not present.
      */
     public List<NameableObject> getFilterOptions( String filter )
     {
         int index = filters.indexOf( new BaseDimensionalObject( filter ) );
         
-        return index != -1 ? filters.get( index ).getItems() : null;
+        return index != -1 ? filters.get( index ).getItems() : new ArrayList<NameableObject>();
     }
 
     /**
@@ -864,7 +865,7 @@
         
         for ( DimensionalObject filter : filters )
         {
-            if ( filter != null && filter.getItems() != null )
+            if ( filter != null && filter.hasItems() )
             {
                 filterItems.addAll( filter.getItems() );
             }
@@ -970,6 +971,14 @@
     {
         return program != null;
     }
+
+    /**
+     * Indicates whether this object has a program stage.
+     */
+    public boolean hasProgramStage()
+    {
+        return programStage != null;
+    }
     
     // -------------------------------------------------------------------------
     // Static methods
@@ -1182,7 +1191,7 @@
     }
     
     // -------------------------------------------------------------------------
-    // Get and set methods for serialize properties
+    // Get and set methods for serialized properties
     // -------------------------------------------------------------------------
 
     public List<DimensionalObject> getDimensions()
@@ -1405,11 +1414,13 @@
   
     /**
      * Retrieves the options for the the dimension or filter with the given 
-     * identifier. Returns null if the dimension or filter is not present.
+     * identifier. Returns an empty list if the dimension or filter is not present.
      */
     public List<NameableObject> getDimensionOrFilter( String key )
     {
-        return getDimensionOptions( key ) != null ? getDimensionOptions( key ) : getFilterOptions( key );
+        List<NameableObject> dimensionOptions = getDimensionOptions( key );
+        
+        return !dimensionOptions.isEmpty() ? dimensionOptions : getFilterOptions( key );
     }
         
     /**
@@ -1425,16 +1436,16 @@
         
         if ( DATA_X_DIM_ID.equals( dimension ) )
         {
-            items.addAll( getDimensionOptionsNullSafe( INDICATOR_DIM_ID ) );
-            items.addAll( getDimensionOptionsNullSafe( DATAELEMENT_DIM_ID ) );
-            items.addAll( getDimensionOptionsNullSafe( DATAELEMENT_OPERAND_ID ) );
-            items.addAll( getDimensionOptionsNullSafe( DATASET_DIM_ID ) );
+            items.addAll( getDimensionOptions( INDICATOR_DIM_ID ) );
+            items.addAll( getDimensionOptions( DATAELEMENT_DIM_ID ) );
+            items.addAll( getDimensionOptions( DATAELEMENT_OPERAND_ID ) );
+            items.addAll( getDimensionOptions( DATASET_DIM_ID ) );
         }
         else if ( CATEGORYOPTIONCOMBO_DIM_ID.equals( dimension ) )
         {
             List<NameableObject> des = getDimensionOrFilter( DATAELEMENT_DIM_ID );
             
-            if ( des != null && !des.isEmpty() )
+            if ( !des.isEmpty() )
             {
                 Set<DataElementCategoryCombo> categoryCombos = new HashSet<>();
                 
@@ -1451,20 +1462,11 @@
         }
         else
         {
-            items.addAll( getDimensionOptionsNullSafe( dimension ) );
+            items.addAll( getDimensionOptions( dimension ) );
         }
         
         return items.toArray( new NameableObject[0] );
     }
-
-    /**
-     * Retrieves the options for the given dimension identifier. Returns an empty
-     * list if the dimension is not present.
-     */
-    public List<NameableObject> getDimensionOptionsNullSafe( String dimension )
-    {
-        return getDimensionOptions( dimension ) != null ? getDimensionOptions( dimension ) : new ArrayList<NameableObject>();
-    }
     
     /**
      * Indicates whether a dimension or filter with the given identifier exists.
@@ -1480,8 +1482,7 @@
      */
     public boolean hasDimensionOrFilterWithItems( String key )
     {
-        List<NameableObject> items = getDimensionOrFilter( key );
-        return items != null && !items.isEmpty();
+        return !getDimensionOrFilter( key ).isEmpty();
     }
     
     /**
@@ -1634,7 +1635,7 @@
     
     public boolean isCategoryOptionCombosEnabled()
     {
-        return getDimensionOrFilter( CATEGORYOPTIONCOMBO_DIM_ID ) != null;
+        return !getDimensionOrFilter( CATEGORYOPTIONCOMBO_DIM_ID ).isEmpty();
     }
     
     // -------------------------------------------------------------------------
@@ -1685,5 +1686,4 @@
     {
         setFilterOptions( filter, type, null, getList( item ) );
     }
-    
 }

=== 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	2015-06-14 15:30:53 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultAnalyticsService.java	2015-06-14 17:51:03 +0000
@@ -291,7 +291,7 @@
      */
     private void addIndicatorValues( DataQueryParams params, Grid grid )
     {
-        if ( params.getIndicators() != null )
+        if ( !params.getIndicators().isEmpty() )
         {
             DataQueryParams dataSourceParams = params.instance();
             dataSourceParams.removeDimension( DATAELEMENT_DIM_ID );
@@ -364,7 +364,7 @@
      */
     private void addDataElementValues( DataQueryParams params, Grid grid )
     {
-        if ( params.getDataElements() != null )
+        if ( !params.getDataElements().isEmpty() )
         {
             DataQueryParams dataSourceParams = params.instance();
             dataSourceParams.removeDimension( INDICATOR_DIM_ID );
@@ -391,7 +391,7 @@
      */
     private void addProgramDataElementValues( DataQueryParams params, Grid grid )
     {
-        if ( params.getProgramDataElements() != null )
+        if ( !params.getProgramDataElements().isEmpty() )
         {
             DataQueryParams dataSourceParams = params.instance();
             dataSourceParams.removeDimension( INDICATOR_DIM_ID );
@@ -415,7 +415,7 @@
      */
     private void addDataSetValues( DataQueryParams params, Grid grid )
     {
-        if ( params.getDataSets() != null )
+        if ( !params.getDataSets().isEmpty() )
         {
             // -----------------------------------------------------------------
             // Get complete data set registrations
@@ -490,7 +490,7 @@
      */
     private void addDynamicDimensionValues( DataQueryParams params, Grid grid )
     {
-        if ( params.getIndicators() == null && params.getDataElements() == null && params.getDataSets() == null && params.getProgramDataElements() == null )
+        if ( params.getIndicators().isEmpty() && params.getDataElements().isEmpty() && params.getDataSets().isEmpty() && params.getProgramDataElements().isEmpty() )
         {
             Map<String, Double> aggregatedDataMap = getAggregatedDataValueMap( params.instance() );
 
@@ -1431,7 +1431,7 @@
             {
                 DataElement dataElement = (DataElement) de;
 
-                if ( dataElement.getCategoryCombo() != null )
+                if ( dataElement.hasCategoryCombo() )
                 {
                     categoryCombos.add( dataElement.getCategoryCombo() );
                 }
@@ -1460,7 +1460,7 @@
     {
         Integer cores = (Integer) systemSettingManager.getSystemSetting( SystemSettingManager.KEY_DATABASE_SERVER_CPUS );
 
-        return (cores == null || cores == 0) ? SystemUtils.getCpuCores() : cores;
+        return ( cores == null || cores == 0 ) ? SystemUtils.getCpuCores() : cores;
     }
 
     /**

=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultQueryPlanner.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultQueryPlanner.java	2015-06-14 15:30:53 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultQueryPlanner.java	2015-06-14 17:51:03 +0000
@@ -163,7 +163,7 @@
         
         if ( violation != null )
         {
-            log.warn( "Validation failed: " + violation );
+            log.warn( "Analytics validation failed: " + violation );
             
             throw new IllegalQueryException( violation );
         }
@@ -386,7 +386,7 @@
             params.setPartitions( new Partitions().add( tableName ) );
             queries.add( params );
         }
-        else if ( params.getPeriods() != null && !params.getPeriods().isEmpty() )
+        else if ( !params.getPeriods().isEmpty() )
         {
             ListMap<Partitions, NameableObject> partitionPeriodMap = PartitionUtils.getPartitionPeriodMap( params.getPeriods(), tableName, tableSuffix, validPartitions );
             
@@ -401,7 +401,7 @@
                 }
             }
         }
-        else if ( params.getFilterPeriods() != null && !params.getFilterPeriods().isEmpty() )
+        else if ( !params.getFilterPeriods().isEmpty() )
         {
             Partitions partitions = PartitionUtils.getPartitions( params.getFilterPeriods(), tableName, tableSuffix, validPartitions );
             
@@ -441,7 +441,7 @@
         {
             queries.add( params );
         }
-        else if ( params.getPeriods() != null && !params.getPeriods().isEmpty() )
+        else if ( !params.getPeriods().isEmpty() )
         {
             ListMap<String, NameableObject> periodTypePeriodMap = PartitionUtils.getPeriodTypePeriodMap( params.getPeriods() );
     
@@ -453,7 +453,7 @@
                 queries.add( query );
             }
         }
-        else if ( params.getFilterPeriods() != null && !params.getFilterPeriods().isEmpty() )
+        else if ( !params.getFilterPeriods().isEmpty() )
         {
             DimensionalObject filter = params.getFilter( PERIOD_DIM_ID );
             
@@ -488,7 +488,7 @@
     {
         List<DataQueryParams> queries = new ArrayList<>();
 
-        if ( params.getOrganisationUnits() != null && !params.getOrganisationUnits().isEmpty() )
+        if ( !params.getOrganisationUnits().isEmpty() )
         {
             ListMap<Integer, NameableObject> levelOrgUnitMap = getLevelOrgUnitMap( params.getOrganisationUnits() );
             
@@ -499,7 +499,7 @@
                 queries.add( query );
             }
         }
-        else if ( params.getFilterOrganisationUnits() != null && !params.getFilterOrganisationUnits().isEmpty() )
+        else if ( !params.getFilterOrganisationUnits().isEmpty() )
         {
             DimensionalObject filter = params.getFilter( ORGUNIT_DIM_ID );
             
@@ -533,7 +533,7 @@
     {
         List<DataQueryParams> queries = new ArrayList<>();
         
-        if ( params.getDataElements() != null && !params.getDataElements().isEmpty() )
+        if ( !params.getDataElements().isEmpty() )
         {
             ListMap<DataType, NameableObject> dataTypeDataElementMap = getDataTypeDataElementMap( params.getDataElements() );
             
@@ -584,13 +584,13 @@
     {
         List<DataQueryParams> queries = new ArrayList<>();
         
-        if ( params.getAggregationType() != null )
+        if ( params.hasAggregationType() )
         {
             queries.add( params.instance() );
             return queries;
         }
         
-        if ( params.getDataElements() != null && !params.getDataElements().isEmpty() )
+        if ( !params.getDataElements().isEmpty() )
         {
             PeriodType periodType = PeriodType.getPeriodTypeByName( params.getPeriodType() );
             
@@ -604,7 +604,7 @@
                 queries.add( query );
             }
         }
-        else if ( params.getDataElementGroupSets() != null && !params.getDataElementGroupSets().isEmpty() )
+        else if ( !params.getDataElementGroupSets().isEmpty() )
         {
             DimensionalObject degs = params.getDataElementGroupSets().get( 0 );
             DataElementGroup deg = (DataElementGroup) ( degs.hasItems() ? degs.getItems().get( 0 ) : null );
@@ -646,7 +646,7 @@
     {
         List<DataQueryParams> queries = new ArrayList<>();
 
-        if ( params.getDataElements() == null || params.getDataElements().isEmpty() )
+        if ( params.getDataElements().isEmpty() )
         {
             queries.add( params.instance() );
             return queries;

=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/dimension/DefaultDimensionService.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/dimension/DefaultDimensionService.java	2015-05-28 15:04:54 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/dimension/DefaultDimensionService.java	2015-06-14 17:51:03 +0000
@@ -195,7 +195,7 @@
 
         List<NameableObject> items = new ArrayList<>();
 
-        if ( dimension != null && dimension.getItems() != null )
+        if ( dimension != null && dimension.hasItems() )
         {            
             User user = currentUserService.getCurrentUser();
 

=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEventQueryPlanner.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEventQueryPlanner.java	2015-06-13 21:26:59 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEventQueryPlanner.java	2015-06-14 17:51:03 +0000
@@ -151,7 +151,7 @@
         
         if ( violation != null )
         {
-            log.warn( "Validation failed: " + violation );
+            log.warn( "Event analytics validation failed: " + violation );
             
             throw new IllegalQueryException( violation );
         }

=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEventAnalyticsManager.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEventAnalyticsManager.java	2015-06-14 14:37:29 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEventAnalyticsManager.java	2015-06-14 17:51:03 +0000
@@ -476,7 +476,7 @@
         // Program stage
         // ---------------------------------------------------------------------
 
-        if ( params.getProgramStage() != null )
+        if ( params.hasProgramStage() )
         {
             sql += "and ps = '" + params.getProgramStage().getUid() + "' ";
         }

=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/security/DefaultAnalyticsSecurityManager.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/security/DefaultAnalyticsSecurityManager.java	2015-05-03 12:29:50 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/security/DefaultAnalyticsSecurityManager.java	2015-06-14 17:51:03 +0000
@@ -91,7 +91,7 @@
         
         List<NameableObject> queryOrgUnits = params.getDimensionOrFilter( DimensionalObject.ORGUNIT_DIM_ID );
         
-        if ( queryOrgUnits == null || queryOrgUnits.isEmpty() || user == null || !user.hasDataViewOrganisationUnit() )
+        if ( queryOrgUnits.isEmpty() || user == null || !user.hasDataViewOrganisationUnit() )
         {
             return; // Allow if no 
         }