← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 10747: Added implementation of DataSetReportStore based on analytics api

 

------------------------------------------------------------
revno: 10747
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2013-05-02 22:51:27 +0200
message:
  Added implementation of DataSetReportStore based on analytics api
added:
  dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/datasetreport/jdbc/AnalyticsDataSetReportStore.java
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


--
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	2013-05-02 20:10:05 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/DataQueryParams.java	2013-05-02 20:51:27 +0000
@@ -31,8 +31,8 @@
 import static org.hisp.dhis.analytics.DimensionType.DATASET;
 import static org.hisp.dhis.analytics.DimensionType.ORGANISATIONUNIT;
 import static org.hisp.dhis.analytics.DimensionType.ORGANISATIONUNIT_GROUPSET;
+import static org.hisp.dhis.common.IdentifiableObjectUtils.asList;
 import static org.hisp.dhis.system.util.CollectionUtils.emptyIfNull;
-import static org.hisp.dhis.common.IdentifiableObjectUtils.asList;
 
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -48,8 +48,11 @@
 import org.hisp.dhis.common.CombinationGenerator;
 import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.common.ListMap;
+import org.hisp.dhis.dataelement.DataElementCategory;
+import org.hisp.dhis.dataelement.DataElementGroupSet;
 import org.hisp.dhis.dataelement.DataElementOperand;
 import org.hisp.dhis.dataset.DataSet;
+import org.hisp.dhis.organisationunit.OrganisationUnitGroupSet;
 import org.hisp.dhis.period.Period;
 import org.hisp.dhis.period.PeriodType;
 import org.hisp.dhis.system.util.CollectionUtils;
@@ -1021,6 +1024,21 @@
         return list;
     }
     
+    public void setDataElementGroupSet( DataElementGroupSet groupSet )
+    {
+        setDimensionOptions( groupSet.getUid(), DimensionType.DATAELEMENT_GROUPSET, null, new ArrayList<IdentifiableObject>( groupSet.getDimensionItems() ) );
+    }
+    
+    public void setOrganisationUnitGroupSet( OrganisationUnitGroupSet groupSet )
+    {
+        setDimensionOptions( groupSet.getUid(), DimensionType.ORGANISATIONUNIT_GROUPSET, null, new ArrayList<IdentifiableObject>( groupSet.getDimensionItems() ) );
+    }
+
+    public void setCategory( DataElementCategory category )
+    {
+        setDimensionOptions( category.getUid(), DimensionType.CATEGORY, null, new ArrayList<IdentifiableObject>( category.getDimensionItems() ) );
+    }
+    
     public void enableCategoryOptionCombos()
     {
         setDimensionOptions( CATEGORYOPTIONCOMBO_DIM_ID, DimensionType.CATEGORY_OPTION_COMBO, null, new ArrayList<IdentifiableObject>() );

=== 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-05-02 13:32:45 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultAnalyticsService.java	2013-05-02 20:51:27 +0000
@@ -120,7 +120,8 @@
     private static final int PERCENT = 100;
     private static final int MAX_QUERIES = 8;
 
-    //TODO make sure data x dims are successive    
+    //TODO make sure data x dims are successive
+    //TODO swith from IdentifiableObject to DimensionalObject
     //TODO completeness on time
     
     @Autowired

=== added file 'dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/datasetreport/jdbc/AnalyticsDataSetReportStore.java'
--- dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/datasetreport/jdbc/AnalyticsDataSetReportStore.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/datasetreport/jdbc/AnalyticsDataSetReportStore.java	2013-05-02 20:51:27 +0000
@@ -0,0 +1,189 @@
+package org.hisp.dhis.datasetreport.jdbc;
+
+/*
+ * Copyright (c) 2004-2012, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright notice, this
+ *   list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ *   this list of conditions and the following disclaimer in the documentation
+ *   and/or other materials provided with the distribution.
+ * * Neither the name of the HISP project nor the names of its contributors may
+ *   be used to endorse or promote products derived from this software without
+ *   specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import static org.hisp.dhis.common.IdentifiableObjectUtils.getList;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import org.hisp.dhis.analytics.AnalyticsService;
+import org.hisp.dhis.analytics.DataQueryParams;
+import org.hisp.dhis.dataelement.DataElement;
+import org.hisp.dhis.dataelement.DataElementCategory;
+import org.hisp.dhis.dataset.DataSet;
+import org.hisp.dhis.dataset.Section;
+import org.hisp.dhis.datasetreport.DataSetReportStore;
+import org.hisp.dhis.indicator.Indicator;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.period.Period;
+import org.hisp.dhis.system.filter.AggregatableDataElementFilter;
+import org.hisp.dhis.system.util.FilterUtils;
+
+/**
+ * @author Lars Helge Overland
+ */
+public class AnalyticsDataSetReportStore
+    implements DataSetReportStore
+{
+    private AnalyticsService analyticsService;
+    
+    public void setAnalyticsService( AnalyticsService analyticsService )
+    {
+        this.analyticsService = analyticsService;
+    }
+
+    // -------------------------------------------------------------------------
+    // DataSetReportStore implementation
+    // -------------------------------------------------------------------------
+
+    @Override
+    public Map<String, Double> getAggregatedValues( DataSet dataSet, Period period, OrganisationUnit unit,
+        boolean rawData )
+    {
+        List<DataElement> dataElements = new ArrayList<DataElement>( dataSet.getDataElements() );
+
+        FilterUtils.filter( dataElements, new AggregatableDataElementFilter() );
+
+        if ( dataElements.isEmpty() )
+        {
+            return new HashMap<String, Double>();
+        }
+        
+        DataQueryParams params = new DataQueryParams();
+        
+        params.setDataElements( dataElements );
+        params.setPeriods( getList( period ) );
+        params.setOrganisationUnits( getList( unit ) );
+        params.enableCategoryOptionCombos();
+        
+        Map<String, Double> map = analyticsService.getAggregatedDataValueMap( params );
+        
+        Map<String, Double> dataMap = new HashMap<String, Double>();
+        
+        for ( Entry<String, Double> entry : map.entrySet() )
+        {
+            String[] split = entry.getKey().split( SEPARATOR );            
+            dataMap.put( split[0] + SEPARATOR + split[3], entry.getValue() );
+        }
+        
+        return dataMap;
+    }
+
+    @Override
+    public Map<String, Double> getAggregatedSubTotals( DataSet dataSet, Period period, OrganisationUnit unit )
+    {
+        Map<String, Double> dataMap = new HashMap<String, Double>();
+        
+        for ( Section section : dataSet.getSections() )
+        {
+            List<DataElement> dataElements = new ArrayList<DataElement>( section.getDataElements() );
+            List<DataElementCategory> categories = section.getCategoryCombo().getCategories();
+
+            FilterUtils.filter( dataElements, new AggregatableDataElementFilter() );
+
+            if ( dataElements.isEmpty() || categories.isEmpty() )
+            {
+                continue;
+            }
+            
+            for ( DataElementCategory category : categories )
+            {
+                DataQueryParams params = new DataQueryParams();
+                
+                params.setDataElements( dataElements );
+                params.setPeriods( getList( period ) );
+                params.setOrganisationUnits( getList( unit ) );
+                params.setCategory( category );            
+
+                Map<String, Double> map = analyticsService.getAggregatedDataValueMap( params );
+                
+                for ( Entry<String, Double> entry : map.entrySet() )
+                {
+                    String[] split = entry.getKey().split( SEPARATOR );            
+                    dataMap.put( split[0] + SEPARATOR + split[3], entry.getValue() );
+                }
+            }
+        }
+        
+        return dataMap;
+    }
+
+    @Override
+    public Map<String, Double> getAggregatedTotals( DataSet dataSet, Period period, OrganisationUnit unit )
+    {
+        List<DataElement> dataElements = new ArrayList<DataElement>( dataSet.getDataElements() );
+
+        FilterUtils.filter( dataElements, new AggregatableDataElementFilter() );
+
+        if ( dataElements.isEmpty() )
+        {
+            return new HashMap<String, Double>();
+        }
+        
+        DataQueryParams params = new DataQueryParams();
+        
+        params.setDataElements( dataElements );
+        params.setPeriods( getList( period ) );
+        params.setOrganisationUnits( getList( unit ) );
+        
+        Map<String, Double> map = analyticsService.getAggregatedDataValueMap( params );
+
+        Map<String, Double> dataMap = new HashMap<String, Double>();
+        
+        for ( Entry<String, Double> entry : map.entrySet() )
+        {
+            String[] split = entry.getKey().split( SEPARATOR );            
+            dataMap.put( split[0], entry.getValue() );
+        }
+        
+        return dataMap;
+    }
+
+    @Override
+    public Map<String, Double> getAggregatedIndicatorValues( DataSet dataSet, Period period, OrganisationUnit unit )
+    {
+        List<Indicator> indicators = new ArrayList<Indicator>( dataSet.getIndicators() );
+        
+        if ( indicators.isEmpty() )
+        {
+            return new HashMap<String, Double>();
+        }        
+
+        DataQueryParams params = new DataQueryParams();
+        
+        params.setIndicators( indicators );
+        params.setPeriods( getList( period ) );
+        params.setOrganisationUnits( getList( unit ) );
+        
+        return analyticsService.getAggregatedDataValueMap( params );
+    }
+}