← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 12418: Event analytics, impl support for org unit parent graphs and hiearchy uid-name map through the hi...

 

------------------------------------------------------------
revno: 12418
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2013-10-03 14:17:41 +0200
message:
  Event analytics, impl support for org unit parent graphs and hiearchy uid-name map through the hierarchyMeta parameter, there too.
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DimensionalObject.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnit.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/event/EventAnalyticsService.java
  dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEventAnalyticsService.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/EventAnalyticsController.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/DimensionalObject.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DimensionalObject.java	2013-09-29 16:17:08 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DimensionalObject.java	2013-10-03 12:17:41 +0000
@@ -36,6 +36,7 @@
 * @author Lars Helge Overland
 */
 public interface DimensionalObject
+    extends NameableObject
 {
     final String DATA_X_DIM_ID = "dx"; // in, de, ds, do
     final String INDICATOR_DIM_ID = "in";

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnit.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnit.java	2013-09-28 13:07:29 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnit.java	2013-10-03 12:17:41 +0000
@@ -53,8 +53,10 @@
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.Date;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 import java.util.UUID;
 import java.util.regex.Matcher;
@@ -610,6 +612,22 @@
         return allDataSets;
     }
 
+    /**
+     * Returns a mapping between the uid and the uid parent graph of the given
+     * organisation units.
+     */
+    public static Map<String, String> getParentGrapMap( List<OrganisationUnit> organisationUnits )
+    {
+        Map<String, String> map = new HashMap<String, String>();
+        
+        for ( OrganisationUnit unit : organisationUnits )
+        {
+            map.put( unit.getUid(), unit.getParentGraph() );
+        }
+        
+        return map;
+    }
+
     // -------------------------------------------------------------------------
     // Getters and setters
     // -------------------------------------------------------------------------

=== 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-10-03 10:21:15 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultAnalyticsService.java	2013-10-03 12:17:41 +0000
@@ -59,6 +59,7 @@
 import static org.hisp.dhis.reporttable.ReportTable.IRT2D;
 import static org.hisp.dhis.reporttable.ReportTable.addIfEmpty;
 import static org.hisp.dhis.system.util.DateUtils.daysBetween;
+import static org.hisp.dhis.organisationunit.OrganisationUnit.getParentGrapMap;
 
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -931,29 +932,13 @@
         return map;
     }
 
-    /**
-     * Returns a mapping between the uid and the uid parent graph of the given
-     * organisation units.
-     */
-    private Map<String, String> getParentGrapMap( List<OrganisationUnit> organisationUnits )
-    {
-        Map<String, String> map = new HashMap<String, String>();
-        
-        for ( OrganisationUnit unit : organisationUnits )
-        {
-            map.put( unit.getUid(), unit.getParentGraph() );
-        }
-        
-        return map;
-    }
-
     private Map<String, String> getUidNameMap( List<DimensionalObject> dimensions, boolean hierarchyMeta )
     {
         Map<String, String> map = new HashMap<String, String>();
         
         for ( DimensionalObject dimension : dimensions )
         {
-            List<NameableObject> options = new ArrayList<NameableObject>( dimension.getItems() );
+            List<NameableObject> items = new ArrayList<NameableObject>( dimension.getItems() );
 
             boolean hierarchy = hierarchyMeta && DimensionType.ORGANISATIONUNIT.equals( dimension.getType() );
             
@@ -961,19 +946,19 @@
             // If dimension is not fixed and has no options, insert all options
             // -----------------------------------------------------------------
             
-            if ( !FIXED_DIMS.contains( dimension.getDimension() ) && options.isEmpty() )
+            if ( !FIXED_DIMS.contains( dimension.getDimension() ) && items.isEmpty() )
             {
                 if ( DimensionType.ORGANISATIONUNIT_GROUPSET.equals( dimension.getType() ) )
                 {
-                    options = asList( organisationUnitGroupService.getOrganisationUnitGroupSet( dimension.getDimension() ).getOrganisationUnitGroups() );
+                    items = asList( organisationUnitGroupService.getOrganisationUnitGroupSet( dimension.getDimension() ).getOrganisationUnitGroups() );
                 }
                 else if ( DimensionType.DATAELEMENT_GROUPSET.equals( dimension.getType() ) )
                 {
-                    options = asList( dataElementService.getDataElementGroupSet( dimension.getDimension() ).getMembers() );
+                    items = asList( dataElementService.getDataElementGroupSet( dimension.getDimension() ).getMembers() );
                 }
                 else if ( DimensionType.CATEGORY.equals( dimension.getType() ) )
                 {
-                    options = asList( categoryService.getDataElementCategory( dimension.getDimension() ).getCategoryOptions() );
+                    items = asList( categoryService.getDataElementCategory( dimension.getDimension() ).getCategoryOptions() );
                 }
             }
 
@@ -981,7 +966,7 @@
             // Insert UID and name into map
             // -----------------------------------------------------------------
             
-            for ( IdentifiableObject idObject : options )
+            for ( IdentifiableObject idObject : items )
             {
                 map.put( idObject.getUid(), idObject.getDisplayName() );
                 

=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/EventAnalyticsService.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/EventAnalyticsService.java	2013-10-02 14:40:51 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/EventAnalyticsService.java	2013-10-03 12:17:41 +0000
@@ -44,8 +44,8 @@
     Grid getEvents( EventQueryParams params );
 
     EventQueryParams getFromUrl( String program, String stage, String startDate, String endDate, 
-        Set<String> dimension, Set<String> filter, SortOrder sortOrder, Integer limit, I18nFormat format );
+        Set<String> dimension, Set<String> filter, boolean hierarchyMeta, SortOrder sortOrder, Integer limit, I18nFormat format );
     
-    EventQueryParams getFromUrl( String program, String stage, String startDate, String endDate, 
-        Set<String> dimension, Set<String> filter, String ouMode, Set<String> asc, Set<String> desc, Integer page, Integer pageSize, I18nFormat format );
+    EventQueryParams getFromUrl( String program, String stage, String startDate, String endDate, Set<String> dimension, Set<String> filter, 
+        String ouMode, Set<String> asc, Set<String> desc, boolean hierarchyMeta, Integer page, Integer pageSize, I18nFormat format );
 }

=== 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	2013-10-02 14:40:51 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEventAnalyticsService.java	2013-10-03 12:17:41 +0000
@@ -28,9 +28,12 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import static org.hisp.dhis.analytics.AnalyticsService.NAMES_META_KEY;
 import static org.hisp.dhis.analytics.DataQueryParams.DIMENSION_NAME_SEP;
 import static org.hisp.dhis.common.DimensionalObject.ORGUNIT_DIM_ID;
 import static org.hisp.dhis.common.DimensionalObject.PERIOD_DIM_ID;
+import static org.hisp.dhis.common.NameableObjectUtils.asTypedList;
+import static org.hisp.dhis.organisationunit.OrganisationUnit.getParentGrapMap;
 
 import java.util.ArrayList;
 import java.util.Date;
@@ -49,10 +52,12 @@
 import org.hisp.dhis.analytics.event.EventQueryPlanner;
 import org.hisp.dhis.analytics.event.QueryItem;
 import org.hisp.dhis.common.BaseIdentifiableObject;
+import org.hisp.dhis.common.DimensionType;
 import org.hisp.dhis.common.DimensionalObject;
 import org.hisp.dhis.common.Grid;
 import org.hisp.dhis.common.GridHeader;
 import org.hisp.dhis.common.IdentifiableObject;
+import org.hisp.dhis.common.IdentifiableObjectUtils;
 import org.hisp.dhis.common.NameableObject;
 import org.hisp.dhis.common.Pager;
 import org.hisp.dhis.dataelement.DataElement;
@@ -121,6 +126,7 @@
 
     //TODO order the event analytics tables up front to avoid default sorting in queries
     //TODO filter items support
+    //TODO remove org unit name / code columns and use names / codes from meta data
     
     public Grid getAggregatedEventData( EventQueryParams params )
     {
@@ -160,7 +166,13 @@
         // ---------------------------------------------------------------------
 
         Map<Object, Object> metaData = new HashMap<Object, Object>();        
-        metaData.put( AnalyticsService.NAMES_META_KEY, getUidNameMap( params ) );
+        
+        Map<String, String> uidNameMap = getUidNameMap( params );
+        Map<String, String> ouParentGraphMap = getParentGrapMap( asTypedList( params.getDimensionOrFilter( ORGUNIT_DIM_ID ), OrganisationUnit.class ) );
+        
+        metaData.put( NAMES_META_KEY, uidNameMap );
+        metaData.put( ORGUNIT_DIM_ID, ouParentGraphMap );
+        
         grid.setMetaData( metaData );
 
         return grid;        
@@ -218,8 +230,13 @@
         // Meta-data
         // ---------------------------------------------------------------------
 
-        Map<Object, Object> metaData = new HashMap<Object, Object>();        
-        metaData.put( AnalyticsService.NAMES_META_KEY, getUidNameMap( params ) );
+        Map<Object, Object> metaData = new HashMap<Object, Object>();
+        
+        Map<String, String> uidNameMap = getUidNameMap( params );
+        Map<String, String> ouParentGraphMap = getParentGrapMap( asTypedList( params.getDimensionOrFilter( ORGUNIT_DIM_ID ), OrganisationUnit.class ) );
+        
+        metaData.put( NAMES_META_KEY, uidNameMap );
+        metaData.put( ORGUNIT_DIM_ID, ouParentGraphMap );
 
         if ( params.isPaging() )
         {
@@ -232,18 +249,24 @@
         return grid;
     }
 
+    /**
+     * Used for aggregate query.
+     */
     public EventQueryParams getFromUrl( String program, String stage, String startDate, String endDate, 
-        Set<String> dimension, Set<String> filter, SortOrder sortOrder, Integer limit, I18nFormat format )
+        Set<String> dimension, Set<String> filter, boolean hierarchyMeta, SortOrder sortOrder, Integer limit, I18nFormat format )
     {
-        EventQueryParams params = getFromUrl( program, stage, startDate, endDate, dimension, filter, null, null, null, null, null, format );
+        EventQueryParams params = getFromUrl( program, stage, startDate, endDate, dimension, filter, null, null, null, hierarchyMeta, null, null, format );
         params.setSortOrder( sortOrder );
         params.setLimit( limit );
         
         return params;
     }
     
-    public EventQueryParams getFromUrl( String program, String stage, String startDate, String endDate, 
-        Set<String> dimension, Set<String> filter, String ouMode, Set<String> asc, Set<String> desc, Integer page, Integer pageSize, I18nFormat format )
+    /**
+     * Used for event query.
+     */
+    public EventQueryParams getFromUrl( String program, String stage, String startDate, String endDate, Set<String> dimension, Set<String> filter, 
+        String ouMode, Set<String> asc, Set<String> desc, boolean hierarchyMeta, Integer page, Integer pageSize, I18nFormat format )
     {
         EventQueryParams params = new EventQueryParams();
         
@@ -342,12 +365,9 @@
         params.setStartDate( start );
         params.setEndDate( end );
         params.setOrganisationUnitMode( ouMode );
+        params.setHierarchyMeta( hierarchyMeta );
         params.setPage( page );
-        
-        if ( pageSize != null )
-        {
-            params.setPageSize( pageSize );
-        }
+        params.setPageSize( pageSize );
 
         return params;
     }
@@ -393,6 +413,43 @@
             map.put( stage.getUid(), stage.getName() );
         }
         
+        for ( QueryItem item : params.getItems() )
+        {
+            map.put( item.getItem().getUid(), item.getItem().getDisplayName() );
+        }
+
+        for ( QueryItem item : params.getItemFilters() )
+        {
+            map.put( item.getItem().getUid(), item.getItem().getDisplayName() );
+        }
+        
+        map.putAll( getUidNameMap( params.getDimensions(), params.isHierarchyMeta() ) );
+        map.putAll( getUidNameMap( params.getFilters(), params.isHierarchyMeta() ) );
+                
+        return map;
+    }
+    
+    private Map<String, String> getUidNameMap( List<DimensionalObject> dimensions, boolean hierarchyMeta )
+    {
+        Map<String, String> map = new HashMap<String, String>();
+
+        for ( DimensionalObject dimension : dimensions )
+        {
+            boolean hierarchy = hierarchyMeta && DimensionType.ORGANISATIONUNIT.equals( dimension.getType() );
+            
+            for ( IdentifiableObject idObject : dimension.getItems() )
+            {
+                map.put( idObject.getUid(), idObject.getDisplayName() );
+                
+                if ( hierarchy )
+                {
+                    OrganisationUnit unit = (OrganisationUnit) idObject;
+                    
+                    map.putAll( IdentifiableObjectUtils.getUidNameMap( unit.getAncestors() ) );
+                }
+            }
+        }
+        
         return map;
     }
     

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/EventAnalyticsController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/EventAnalyticsController.java	2013-10-02 14:40:51 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/EventAnalyticsController.java	2013-10-03 12:17:41 +0000
@@ -79,12 +79,13 @@
         @RequestParam(required=false) String endDate,
         @RequestParam Set<String> dimension,
         @RequestParam(required=false) Set<String> filter,
+        @RequestParam(required=false) boolean hierarchyMeta,
         @RequestParam(required=false) Integer limit,
         @RequestParam(required=false) SortOrder sortOrder,
         Model model,
         HttpServletResponse response ) throws Exception
     {
-        EventQueryParams params = analyticsService.getFromUrl( program, stage, startDate, endDate, dimension, filter, sortOrder, limit, i18nManager.getI18nFormat() );
+        EventQueryParams params = analyticsService.getFromUrl( program, stage, startDate, endDate, dimension, filter, hierarchyMeta, sortOrder, limit, i18nManager.getI18nFormat() );
         
         contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_JSON, CacheStrategy.RESPECT_SYSTEM_SETTING );
         Grid grid = analyticsService.getAggregatedEventData( params );
@@ -101,12 +102,13 @@
         @RequestParam(required=false) String endDate,
         @RequestParam Set<String> dimension,
         @RequestParam(required=false) Set<String> filter,
+        @RequestParam(required=false) boolean hierarchyMeta,
         @RequestParam(required=false) Integer limit,
         @RequestParam(required=false) SortOrder sortOrder,
         Model model,
         HttpServletResponse response ) throws Exception
     {
-        EventQueryParams params = analyticsService.getFromUrl( program, stage, startDate, endDate, dimension, filter, sortOrder, limit, i18nManager.getI18nFormat() );
+        EventQueryParams params = analyticsService.getFromUrl( program, stage, startDate, endDate, dimension, filter, hierarchyMeta, sortOrder, limit, i18nManager.getI18nFormat() );
         
         contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_EXCEL, CacheStrategy.RESPECT_SYSTEM_SETTING, "events.xls", true );
         Grid grid = analyticsService.getAggregatedEventData( params );
@@ -128,13 +130,14 @@
         @RequestParam(required=false) String ouMode,
         @RequestParam(required=false) Set<String> asc,
         @RequestParam(required=false) Set<String> desc,
+        @RequestParam(required=false) boolean hierarchyMeta,
         @RequestParam(required=false) Integer page,
         @RequestParam(required=false) Integer pageSize,
         Model model,
         HttpServletResponse response ) throws Exception
     {
         EventQueryParams params = analyticsService.getFromUrl( program, stage, startDate, endDate, dimension, filter, ouMode, 
-            asc, desc, page, pageSize, i18nManager.getI18nFormat() );
+            asc, desc, hierarchyMeta, page, pageSize, i18nManager.getI18nFormat() );
         
         contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_JSON, CacheStrategy.RESPECT_SYSTEM_SETTING );
         Grid grid = analyticsService.getEvents( params );
@@ -154,13 +157,14 @@
         @RequestParam(required=false) String ouMode,
         @RequestParam(required=false) Set<String> asc,
         @RequestParam(required=false) Set<String> desc,
+        @RequestParam(required=false) boolean hierarchyMeta,
         @RequestParam(required=false) Integer page,
         @RequestParam(required=false) Integer pageSize,
         Model model,
         HttpServletResponse response ) throws Exception
     {
         EventQueryParams params = analyticsService.getFromUrl( program, stage, startDate, endDate, dimension, filter, 
-            ouMode, asc, desc, page, pageSize, i18nManager.getI18nFormat() );
+            ouMode, asc, desc, hierarchyMeta, page, pageSize, i18nManager.getI18nFormat() );
         
         contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_EXCEL, CacheStrategy.RESPECT_SYSTEM_SETTING, "events.xls", true );
         Grid grid = analyticsService.getEvents( params );