← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 11865: Event analytics api, supporting org unit selection

 

------------------------------------------------------------
revno: 11865
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2013-09-02 19:38:21 +0200
message:
  Event analytics api, supporting org unit selection
modified:
  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/EventQueryParams.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/main/java/org/hisp/dhis/analytics/event/data/JdbcEventAnalyticsManager.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-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-08-23 16:05:01 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/EventAnalyticsService.java	2013-09-02 17:38:21 +0000
@@ -39,6 +39,6 @@
 {
     Grid getEvents( EventQueryParams params );
 
-    EventQueryParams getFromUrl( String program, String stage, String startDate, String endDate, String ou, 
+    EventQueryParams getFromUrl( String program, String stage, String startDate, String endDate, String ou, String ouMode,
         Set<String> item, Set<String> asc, Set<String> desc, Integer page, Integer pageSize );
 }

=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/EventQueryParams.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/EventQueryParams.java	2013-08-23 16:05:01 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/EventQueryParams.java	2013-09-02 17:38:21 +0000
@@ -30,7 +30,9 @@
 
 import java.util.ArrayList;
 import java.util.Date;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
 
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.program.Program;
@@ -41,6 +43,10 @@
  */
 public class EventQueryParams
 {
+    public static final String OU_MODE_SELECTED = "selected";
+    public static final String OU_MODE_CHILDREN = "children";
+    public static final String OU_MODE_DESCENDANTS = "descendants";
+    
     private Program program;
     
     private ProgramStage programStage;
@@ -56,6 +62,8 @@
     private List<String> desc = new ArrayList<String>();
     
     private List<OrganisationUnit> organisationUnits = new ArrayList<OrganisationUnit>();
+
+    private String organisationUnitMode;
     
     private String tableName;
     
@@ -90,9 +98,21 @@
     // Logic
     // -------------------------------------------------------------------------
 
-    public boolean hasOrganisationUnits()
-    {
-        return organisationUnits != null && !organisationUnits.isEmpty();
+    public boolean isOrganisationUnitMode( String mode )
+    {
+        return organisationUnitMode != null && organisationUnitMode.equalsIgnoreCase( mode );
+    }
+    
+    public Set<OrganisationUnit> getOrganisationUnitChildren()
+    {
+        Set<OrganisationUnit> children = new HashSet<OrganisationUnit>();
+        
+        for ( OrganisationUnit unit : organisationUnits )
+        {
+            children.addAll( unit.getChildren() );
+        }
+        
+        return children;
     }
     
     public boolean isSorting()
@@ -204,6 +224,16 @@
         this.organisationUnits = organisationUnits;
     }
 
+    public String getOrganisationUnitMode()
+    {
+        return organisationUnitMode;
+    }
+
+    public void setOrganisationUnitMode( String organisationUnitMode )
+    {
+        this.organisationUnitMode = organisationUnitMode;
+    }
+
     public String getTableName()
     {
         return tableName;

=== 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-08-27 09:29:59 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEventAnalyticsService.java	2013-09-02 17:38:21 +0000
@@ -143,7 +143,7 @@
         return grid;
     }
     
-    public EventQueryParams getFromUrl( String program, String stage, String startDate, String endDate, String ou, 
+    public EventQueryParams getFromUrl( String program, String stage, String startDate, String endDate, String ou, String ouMode,
         Set<String> item, Set<String> asc, Set<String> desc, Integer page, Integer pageSize )
     {
         EventQueryParams params = new EventQueryParams();
@@ -233,11 +233,18 @@
                 
                 if ( orgUnit != null )
                 {
+                    orgUnit.setLevel( organisationUnitService.getLevelOfOrganisationUnit( orgUnit.getId() ) );
+                    
                     params.getOrganisationUnits().add( orgUnit );
                 }
             }
         }
         
+        if ( params.getOrganisationUnits().isEmpty() )
+        {
+            throw new IllegalQueryException( "At least one organisation unit must be specified" );
+        }
+        
         if ( page != null && page <= 0 )
         {
             throw new IllegalQueryException( "Page number must be positive: " + page );
@@ -252,6 +259,7 @@
         params.setProgramStage( ps );
         params.setStartDate( start );
         params.setEndDate( end );
+        params.setOrganisationUnitMode( ouMode );
         params.setPage( page );
         
         if ( pageSize != null )

=== 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	2013-08-27 09:19:47 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEventAnalyticsManager.java	2013-09-02 17:38:21 +0000
@@ -38,6 +38,8 @@
 import org.hisp.dhis.analytics.event.QueryItem;
 import org.hisp.dhis.common.Grid;
 import org.hisp.dhis.common.IdentifiableObject;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.system.util.TextUtils;
 import org.hisp.dhis.system.util.Timer;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.jdbc.core.JdbcTemplate;
@@ -73,14 +75,29 @@
         
         sql = removeLast( sql, 1 ) + " ";
         
-        sql += "from " + params.getTableName() + " ";        
+        sql += "from " + params.getTableName() + " ";
         sql += "where executiondate >= '" + getMediumDateString( params.getStartDate() ) + "' ";
         sql += "and executiondate <= '" + getMediumDateString( params.getEndDate() ) + "' ";
         
-        if ( params.hasOrganisationUnits() )
+        if ( params.isOrganisationUnitMode( EventQueryParams.OU_MODE_SELECTED ) )
         {
             sql += "and ou in (" + getQuotedCommaDelimitedString( getUids( params.getOrganisationUnits() ) ) + ") ";
         }
+        else if ( params.isOrganisationUnitMode( EventQueryParams.OU_MODE_CHILDREN ) )
+        {
+            sql += "and ou in (" + getQuotedCommaDelimitedString( getUids( params.getOrganisationUnitChildren() ) ) + ") ";
+        }
+        else // Descendants
+        {
+            sql += "and (";
+            
+            for ( OrganisationUnit unit : params.getOrganisationUnits() )
+            {
+                sql += "uidlevel" + unit.getLevel() + " = '" + unit.getUid() + "' or ";
+            }
+            
+            sql = TextUtils.removeLast( sql, 3 ) + ") ";
+        }
         
         if ( params.getProgramStage() != null )
         {

=== 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-08-23 16:00:30 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/EventAnalyticsController.java	2013-09-02 17:38:21 +0000
@@ -69,7 +69,8 @@
         @RequestParam(required=false) String stage,
         @RequestParam String startDate,
         @RequestParam String endDate,
-        @RequestParam(required=false) String ou,
+        @RequestParam String ou,
+        @RequestParam(required=false) String ouMode,
         @RequestParam Set<String> item,
         @RequestParam(required=false) Set<String> asc,
         @RequestParam(required=false) Set<String> desc,
@@ -78,7 +79,7 @@
         Model model,
         HttpServletResponse response ) throws Exception
     {
-        EventQueryParams params = analyticsService.getFromUrl( program, stage, startDate, endDate, ou, item, asc, desc, page, pageSize );
+        EventQueryParams params = analyticsService.getFromUrl( program, stage, startDate, endDate, ou, ouMode, item, asc, desc, page, pageSize );
         
         contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_JSON, CacheStrategy.RESPECT_SYSTEM_SETTING );
         Grid grid = analyticsService.getEvents( params );
@@ -93,7 +94,8 @@
         @RequestParam(required=false) String stage,
         @RequestParam String startDate,
         @RequestParam String endDate,
-        @RequestParam(required=false) String ou,
+        @RequestParam String ou,
+        @RequestParam(required=false) String ouMode,
         @RequestParam Set<String> item,
         @RequestParam(required=false) Set<String> asc,
         @RequestParam(required=false) Set<String> desc,
@@ -102,7 +104,7 @@
         Model model,
         HttpServletResponse response ) throws Exception
     {
-        EventQueryParams params = analyticsService.getFromUrl( program, stage, startDate, endDate, ou, item, asc, desc, page, pageSize );
+        EventQueryParams params = analyticsService.getFromUrl( program, stage, startDate, endDate, ou, ouMode, item, asc, desc, page, pageSize );
         
         contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_EXCEL, CacheStrategy.RESPECT_SYSTEM_SETTING, "events.xls", true );
         Grid grid = analyticsService.getEvents( params );