← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 2484: Impl aggregation strategy in GIS

 

------------------------------------------------------------
revno: 2484
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2011-01-07 13:59:06 +0100
message:
  Impl aggregation strategy in GIS
modified:
  dhis-2/dhis-services/dhis-service-mapping/src/main/java/org/hisp/dhis/mapping/DefaultMappingService.java
  dhis-2/dhis-services/dhis-service-mapping/src/main/resources/META-INF/dhis/beans.xml
  dhis-2/dhis-web/dhis-web-mapping/src/main/webapp/dhis-web-mapping/jsonInitialize.vm


--
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-mapping/src/main/java/org/hisp/dhis/mapping/DefaultMappingService.java'
--- dhis-2/dhis-services/dhis-service-mapping/src/main/java/org/hisp/dhis/mapping/DefaultMappingService.java	2011-01-07 12:11:50 +0000
+++ dhis-2/dhis-services/dhis-service-mapping/src/main/java/org/hisp/dhis/mapping/DefaultMappingService.java	2011-01-07 12:59:06 +0000
@@ -32,6 +32,7 @@
 import java.util.HashSet;
 import java.util.Set;
 
+import org.hisp.dhis.aggregation.AggregatedDataValueService;
 import org.hisp.dhis.aggregation.AggregatedMapValue;
 import org.hisp.dhis.aggregation.AggregationService;
 import org.hisp.dhis.dataelement.DataElement;
@@ -40,6 +41,7 @@
 import org.hisp.dhis.indicator.Indicator;
 import org.hisp.dhis.indicator.IndicatorGroup;
 import org.hisp.dhis.indicator.IndicatorService;
+import org.hisp.dhis.options.SystemSettingManager;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitLevel;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
@@ -49,6 +51,9 @@
 import org.hisp.dhis.system.util.MathUtils;
 import org.hisp.dhis.user.UserSettingService;
 import org.springframework.transaction.annotation.Transactional;
+import org.springframework.util.Assert;
+
+import static org.hisp.dhis.options.SystemSettingManager.*;
 
 /**
  * @author Jan Henrik Overland
@@ -111,6 +116,20 @@
         this.aggregationService = aggregationService;
     }
 
+    private AggregatedDataValueService aggregatedDataValueService;
+    
+    public void setAggregatedDataValueService( AggregatedDataValueService aggregatedDataValueService )
+    {
+        this.aggregatedDataValueService = aggregatedDataValueService;
+    }
+
+    private SystemSettingManager systemSettingManager;
+
+    public void setSystemSettingManager( SystemSettingManager systemSettingManager )
+    {
+        this.systemSettingManager = systemSettingManager;
+    }
+
     // -------------------------------------------------------------------------
     // MappingService implementation
     // -------------------------------------------------------------------------
@@ -124,13 +143,13 @@
     {
         Period period = periodService.getPeriod( periodId );
 
-        return getIndicatorMapValues( indicatorId, period.getStartDate(), period.getEndDate(), parentOrganisationUnitId );
+        return getIndicatorMapValuesInternal( indicatorId, period, null, null, parentOrganisationUnitId, null );
     }
 
     public Collection<AggregatedMapValue> getIndicatorMapValues( int indicatorId, Date startDate, Date endDate,
         int parentOrganisationUnitId )
     {
-        return getIndicatorMapValuesInternal( indicatorId, startDate, endDate, parentOrganisationUnitId, null );
+        return getIndicatorMapValuesInternal( indicatorId, null, startDate, endDate, parentOrganisationUnitId, null );
     }
 
     public Collection<AggregatedMapValue> getIndicatorMapValues( int indicatorId, int periodId,
@@ -138,60 +157,62 @@
     {
         Period period = periodService.getPeriod( periodId );
 
-        return getIndicatorMapValues( indicatorId, period.getStartDate(), period.getEndDate(),
-            parentOrganisationUnitId, level );
+        return getIndicatorMapValuesInternal( indicatorId, period, null, null, parentOrganisationUnitId, level );
     }
 
     public Collection<AggregatedMapValue> getIndicatorMapValues( int indicatorId, Date startDate, Date endDate,
         int parentOrganisationUnitId, int level )
     {
-        return getIndicatorMapValuesInternal( indicatorId, startDate, endDate, parentOrganisationUnitId, level );
+        return getIndicatorMapValuesInternal( indicatorId, null, startDate, endDate, parentOrganisationUnitId, level );
     }
 
     public Collection<AggregatedMapValue> getIndicatorMapValuesByLevel( int indicatorId, int periodId, int level )
     {
         Period period = periodService.getPeriod( periodId );
 
-        return getIndicatorMapValuesByLevel( indicatorId, period.getStartDate(), period.getEndDate(), level );
+        return getIndicatorMapValuesInternal( indicatorId, period, null, null, null, level );
     }
     
     public Collection<AggregatedMapValue> getIndicatorMapValuesByLevel( int indicatorId, Date startDate, Date endDate, 
         int level )
     {
-        return getIndicatorMapValuesInternal( indicatorId, startDate, endDate, null, level );
+        return getIndicatorMapValuesInternal( indicatorId, null, startDate, endDate, null, level );
     }
 
-    private Collection<AggregatedMapValue> getIndicatorMapValuesInternal( Integer indicatorId, Date startDate, Date endDate, 
+    /**
+     * Generates a collection AggregatedMapValues. Only one of Period and start/end
+     * date can be specified. At least one of parent organisation unit and level
+     * must be specified. Period should be specified with "real time" aggregation
+     * strategy, any may be specified with "batch" aggregation strategy.
+     * 
+     * @param indicatorId the Indicator identifier.
+     * @param period the Period identifier. Ignored if null.
+     * @param startDate the start date. Ignored if null.
+     * @param endDate the end date. Ignored if null.
+     * @param parentOrganisationUnitId the parent OrganisationUnit identifier. Ignored if null.
+     * @param level the OrganisationUnit level. Ignored if null.
+     * @return a collection of AggregatedMapValues.
+     */
+    private Collection<AggregatedMapValue> getIndicatorMapValuesInternal( Integer indicatorId, Period period, Date startDate, Date endDate, 
         Integer parentOrganisationUnitId, Integer level )
     {
+        String aggregationStrategy = (String) systemSettingManager.getSystemSetting( KEY_AGGREGATION_STRATEGY, DEFAULT_AGGREGATION_STRATEGY );
+        
+        Assert.isTrue( !( period != null && ( startDate != null || endDate != null ) ) );
+        Assert.isTrue( !( aggregationStrategy.equals( AGGREGATION_STRATEGY_BATCH ) && period == null ) );
+        Assert.isTrue( !( parentOrganisationUnitId == null && level == null ) );
+        
         Collection<AggregatedMapValue> values = new HashSet<AggregatedMapValue>();
 
         Indicator indicator = indicatorService.getIndicator( indicatorId );
-        
-        Collection<OrganisationUnit> organisationUnits = null;
-        
-        if ( parentOrganisationUnitId != null && level != null )
-        {
-            organisationUnits = organisationUnitService.getOrganisationUnitsAtLevel( level, organisationUnitService.getOrganisationUnit( parentOrganisationUnitId ) );
-        }
-        else if ( level != null )
-        {
-            organisationUnits = organisationUnitService.getOrganisationUnitsAtLevel( level );
-        }
-        else if ( parentOrganisationUnitId != null )
-        {
-            organisationUnits = organisationUnitService.getOrganisationUnit( parentOrganisationUnitId ).getChildren();
-        }
-        else
-        {
-            throw new IllegalArgumentException( "Parent organisation unit or level must be specified" );
-        }
-
-        for ( OrganisationUnit organisationUnit : organisationUnits )
+                
+        for ( OrganisationUnit organisationUnit : getOrganisationUnits( parentOrganisationUnitId, level ) )
         {
             if ( organisationUnit.hasCoordinates() )
             {
-                Double value = aggregationService.getAggregatedIndicatorValue( indicator, startDate, endDate, organisationUnit );
+                Double value = aggregationStrategy.equals( AGGREGATION_STRATEGY_REAL_TIME ) ? 
+                    aggregationService.getAggregatedIndicatorValue( indicator, startDate, endDate, organisationUnit ) :
+                        aggregatedDataValueService.getAggregatedValue( indicator, period, organisationUnit );
 
                 value = value != null ? value : 0; // TODO improve
 
@@ -207,6 +228,34 @@
         return values;        
     }
     
+    /**
+     * Returns the relevant OrganisationUnits for the given parent identifier
+     * and / or level.
+     * 
+     * @param parentOrganisationUnitId the OrganisationUnit level.
+     * @param level the OrganisationUnit level.
+     * @return a collection of OrganisationUnits.
+     */
+    private Collection<OrganisationUnit> getOrganisationUnits( Integer parentOrganisationUnitId, Integer level )
+    {
+        Collection<OrganisationUnit> organisationUnits = null;
+        
+        if ( parentOrganisationUnitId != null && level != null )
+        {
+            organisationUnits = organisationUnitService.getOrganisationUnitsAtLevel( level, organisationUnitService.getOrganisationUnit( parentOrganisationUnitId ) );
+        }
+        else if ( level != null )
+        {
+            organisationUnits = organisationUnitService.getOrganisationUnitsAtLevel( level );
+        }
+        else if ( parentOrganisationUnitId != null )
+        {
+            organisationUnits = organisationUnitService.getOrganisationUnit( parentOrganisationUnitId ).getChildren();
+        }
+
+        return organisationUnits;
+    }
+    
     // -------------------------------------------------------------------------
     // DataMapValues
     // -------------------------------------------------------------------------
@@ -216,14 +265,13 @@
     {
         Period period = periodService.getPeriod( periodId );
 
-        return getDataElementMapValues( dataElementId, period.getStartDate(), period.getEndDate(),
-            parentOrganisationUnitId );
+        return getDataElementMapValuesInternal( dataElementId, period, null, null, parentOrganisationUnitId, null );
     }
 
     public Collection<AggregatedMapValue> getDataElementMapValues( int dataElementId, Date startDate, Date endDate,
         int parentOrganisationUnitId )
     {
-        return getDataElementMapValuesInternal( dataElementId, startDate, endDate, parentOrganisationUnitId, null );
+        return getDataElementMapValuesInternal( dataElementId, null, startDate, endDate, parentOrganisationUnitId, null );
     }
 
     public Collection<AggregatedMapValue> getDataElementMapValues( int dataElementId, int periodId,
@@ -231,56 +279,56 @@
     {
         Period period = periodService.getPeriod( periodId );
 
-        return getDataElementMapValues( dataElementId, period.getStartDate(), period.getEndDate(),
-            parentOrganisationUnitId, level );
+        return getDataElementMapValuesInternal( dataElementId, period, null, null, parentOrganisationUnitId, level );
     }
 
     public Collection<AggregatedMapValue> getDataElementMapValues( int dataElementId, Date startDate, Date endDate,
         int parentOrganisationUnitId, int level )
     {
-        return getDataElementMapValuesInternal( dataElementId, startDate, endDate, parentOrganisationUnitId, level );
+        return getDataElementMapValuesInternal( dataElementId, null, startDate, endDate, parentOrganisationUnitId, level );
     }
 
     public Collection<AggregatedMapValue> getDataElementMapValuesByLevel( int dataElementId, int periodId, int level )
     {
         Period period = periodService.getPeriod( periodId );
 
-        return getDataElementMapValuesByLevel( dataElementId, period.getStartDate(), period.getEndDate(), level );
+        return getDataElementMapValuesInternal( dataElementId, period, null, null, null, level );
     }
 
     public Collection<AggregatedMapValue> getDataElementMapValuesByLevel( int dataElementId, Date startDate,
         Date endDate, int level )
     {
-        return getDataElementMapValuesInternal( dataElementId, startDate, endDate, null, level );
+        return getDataElementMapValuesInternal( dataElementId, null, startDate, endDate, null, level );
     }
 
-    public Collection<AggregatedMapValue> getDataElementMapValuesInternal( Integer dataElementId, Date startDate, Date endDate,
+    /**
+     * Generates a collection AggregatedMapValues. Only one of Period and start/end
+     * date can be specified. At least one of parent organisation unit and level
+     * must be specified. Period should be specified with "real time" aggregation
+     * strategy, any may be specified with "batch" aggregation strategy.
+     * 
+     * @param indicatorId the Indicator identifier.
+     * @param period the Period identifier. Ignored if null.
+     * @param startDate the start date. Ignored if null.
+     * @param endDate the end date. Ignored if null.
+     * @param parentOrganisationUnitId the parent OrganisationUnit identifier. Ignored if null.
+     * @param level the OrganisationUnit level. Ignored if null.
+     * @return a collection of AggregatedMapValues.
+     */
+    public Collection<AggregatedMapValue> getDataElementMapValuesInternal( Integer dataElementId, Period period, Date startDate, Date endDate,
         Integer parentOrganisationUnitId, Integer level )
     {
+        String aggregationStrategy = (String) systemSettingManager.getSystemSetting( KEY_AGGREGATION_STRATEGY, DEFAULT_AGGREGATION_STRATEGY );
+        
+        Assert.isTrue( !( period != null && ( startDate != null || endDate != null ) ) );
+        Assert.isTrue( !( aggregationStrategy.equals( AGGREGATION_STRATEGY_BATCH ) && period == null ) );
+        Assert.isTrue( !( parentOrganisationUnitId == null && level == null ) );
+        
         Collection<AggregatedMapValue> values = new HashSet<AggregatedMapValue>();
 
         DataElement dataElement = dataElementService.getDataElement( dataElementId );
         
-        Collection<OrganisationUnit> organisationUnits = null;
-        
-        if ( parentOrganisationUnitId != null && level != null )
-        {
-            organisationUnits = organisationUnitService.getOrganisationUnitsAtLevel( level, organisationUnitService.getOrganisationUnit( parentOrganisationUnitId ) );
-        }
-        else if ( level != null )
-        {
-            organisationUnits = organisationUnitService.getOrganisationUnitsAtLevel( level );
-        }
-        else if ( parentOrganisationUnitId != null )
-        {
-            organisationUnits = organisationUnitService.getOrganisationUnit( parentOrganisationUnitId ).getChildren();
-        }
-        else
-        {
-            throw new IllegalArgumentException( "Parent organisation unit or level must be specified" );
-        }
-
-        for ( OrganisationUnit organisationUnit : organisationUnits )
+        for ( OrganisationUnit organisationUnit : getOrganisationUnits( parentOrganisationUnitId, level ) )
         {
             if ( organisationUnit.hasCoordinates() )
             {

=== modified file 'dhis-2/dhis-services/dhis-service-mapping/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-services/dhis-service-mapping/src/main/resources/META-INF/dhis/beans.xml	2010-11-15 16:26:51 +0000
+++ dhis-2/dhis-services/dhis-service-mapping/src/main/resources/META-INF/dhis/beans.xml	2011-01-07 12:59:06 +0000
@@ -24,6 +24,10 @@
       ref="org.hisp.dhis.user.UserSettingService"/>
 	<property name="aggregationService"
 	  ref="org.hisp.dhis.aggregation.AggregationService"/>
+	<property name="systemSettingManager"
+	  ref="org.hisp.dhis.options.SystemSettingManager"/>
+	<property name="aggregatedDataValueService"
+	  ref="org.hisp.dhis.aggregation.AggregatedDataValueService"/>
   </bean>
 		
   <!-- Store definitions -->

=== modified file 'dhis-2/dhis-web/dhis-web-mapping/src/main/webapp/dhis-web-mapping/jsonInitialize.vm'
--- dhis-2/dhis-web/dhis-web-mapping/src/main/webapp/dhis-web-mapping/jsonInitialize.vm	2010-12-24 15:51:48 +0000
+++ dhis-2/dhis-web/dhis-web-mapping/src/main/webapp/dhis-web-mapping/jsonInitialize.vm	2011-01-07 12:59:06 +0000
@@ -36,6 +36,9 @@
   "userSettings": {
     "mapDateType": "$!encoder.jsonEncode( ${mapDateType} )"
   },
+  "systemSettings": {
+    "aggregationStrategy": "$!encoder.jsonEncode( ${aggregationStrategy} )"
+  },
   "baseLayers": [
 #foreach( $baseLayer in $baseLayers )
     {