← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 18973: Event query, implemented paging properly

 

------------------------------------------------------------
revno: 18973
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2015-04-21 22:08:55 +0200
message:
  Event query, implemented paging properly
modified:
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/AbstractEventService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/EventSearchParams.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/EventStore.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/JdbcEventStore.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/EventController.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-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/AbstractEventService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/AbstractEventService.java	2015-04-21 19:00:43 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/AbstractEventService.java	2015-04-21 20:08:55 +0000
@@ -49,6 +49,7 @@
 import org.hisp.dhis.common.IdentifiableProperty;
 import org.hisp.dhis.common.IllegalQueryException;
 import org.hisp.dhis.common.OrganisationUnitSelectionMode;
+import org.hisp.dhis.common.Pager;
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.dataelement.DataElementService;
 import org.hisp.dhis.dxf2.common.IdSchemes;
@@ -419,9 +420,18 @@
             }
         }
 
+        Events events = new Events();
+        
+        if ( params.isPaging() )
+        {
+            int count = eventStore.getEventCount( params, organisationUnits );
+            
+            Pager pager = new Pager( params.getPageWithDefault(), count, params.getPageSizeWithDefault() );
+            events.setPager( pager );
+        }
+        
         List<Event> eventList = eventStore.getEvents( params, organisationUnits );
 
-        Events events = new Events();
         events.setEvents( eventList );
 
         return events;

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/EventSearchParams.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/EventSearchParams.java	2015-04-21 19:00:43 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/EventSearchParams.java	2015-04-21 20:08:55 +0000
@@ -44,6 +44,9 @@
  */
 public class EventSearchParams
 {
+    public static final int DEFAULT_PAGE = 1;
+    public static final int DEFAULT_PAGE_SIZE = 50;
+    
     private Program program;
     
     private ProgramStage programStage;
@@ -91,12 +94,12 @@
 
     public int getPageWithDefault()
     {
-        return page != null && page > 0 ? page : 1;
+        return page != null && page > 0 ? page : DEFAULT_PAGE;
     }
     
     public int getPageSizeWithDefault()
     {
-        return pageSize != null && pageSize >= 0 ? pageSize : 100;
+        return pageSize != null && pageSize >= 0 ? pageSize : DEFAULT_PAGE_SIZE;
     }
 
     public int getOffset()

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/EventStore.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/EventStore.java	2015-03-12 11:03:32 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/EventStore.java	2015-04-21 20:08:55 +0000
@@ -38,4 +38,6 @@
 public interface EventStore
 {
     List<Event> getEvents( EventSearchParams params, List<OrganisationUnit> organisationUnits );
+    
+    int getEventCount( EventSearchParams params, List<OrganisationUnit> organisationUnits );
 }

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/JdbcEventStore.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/JdbcEventStore.java	2015-04-21 19:26:43 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/JdbcEventStore.java	2015-04-21 20:08:55 +0000
@@ -185,6 +185,17 @@
         return events;
     }
 
+    public int getEventCount( EventSearchParams params, List<OrganisationUnit> organisationUnits )
+    {
+        String sql = getEventSelectQuery( params, organisationUnits );
+        
+        sql = sql.replaceFirst( "select .*? from", "select count(*) from" );
+
+        log.info( "Event query count SQL: " + sql );
+
+        return jdbcTemplate.queryForObject( sql, Integer.class );
+    }
+    
     /**
      * Query is based on three sub queries on event, data value and comment, which 
      * are joined using program stage instance id. The purpose of the separate
@@ -194,7 +205,8 @@
     {
         String sql = "select * from (";
         
-        sql += getEventQuery( params, organisationUnits );
+        sql += getEventSelectQuery( params, organisationUnits );
+        sql += getEventPagingQuery( params );
         
         sql += ") as event left join (";
         
@@ -209,7 +221,7 @@
         return sql;
     }
     
-    private String getEventQuery( EventSearchParams params, List<OrganisationUnit> organisationUnits )
+    private String getEventSelectQuery( EventSearchParams params, List<OrganisationUnit> organisationUnits )
     {
         List<Integer> orgUnitIds = getIdList( organisationUnits );
 
@@ -324,20 +336,21 @@
             }
         }
 
-        sql += " order by psi.lastupdated desc ";
+        return sql;
+    }
 
-        // ---------------------------------------------------------------------
-        // Paging
-        // ---------------------------------------------------------------------
+    private String getEventPagingQuery( EventSearchParams params )
+    {
+        String sql = "order by psi.lastupdated desc ";
 
         if ( params.isPaging() )
         {
-            sql += "limit " + params.getPageSizeWithDefault() + " offset " + params.getOffset();
+            sql += "limit " + params.getPageSizeWithDefault() + " offset " + params.getOffset() + " ";
         }
 
         return sql;
     }
-
+    
     private String getDataValueQuery()
     {
         String sql =

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/EventController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/EventController.java	2015-04-21 15:09:13 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/EventController.java	2015-04-21 20:08:55 +0000
@@ -42,8 +42,6 @@
 
 import org.hisp.dhis.common.IdentifiableObjectManager;
 import org.hisp.dhis.common.OrganisationUnitSelectionMode;
-import org.hisp.dhis.common.Pager;
-import org.hisp.dhis.common.PagerUtils;
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.dataelement.DataElementService;
 import org.hisp.dhis.dxf2.common.IdSchemes;
@@ -166,13 +164,6 @@
             }
         }
 
-        if ( options.hasPaging() )
-        {
-            Pager pager = new Pager( options.getPage(), events.getEvents().size(), options.getPageSize() );
-            events.setPager( pager );
-            events.setEvents( PagerUtils.pageCollection( events.getEvents(), pager ) );
-        }
-
         if ( !skipMeta && params.getProgram() != null )
         {
             events.setMetaData( getMetaData( params.getProgram() ) );
@@ -210,20 +201,11 @@
         @RequestParam Map<String, String> parameters,
         IdSchemes idSchemes, Model model, HttpServletResponse response, HttpServletRequest request ) throws IOException
     {
-        WebOptions options = new WebOptions( parameters );
-
         EventSearchParams params = eventService.getFromUrl( program, programStage, programStatus, followUp, orgUnit, ouMode, 
             trackedEntityInstance, startDate, endDate, status, lastUpdated, idSchemes, page, pageSize );
         
         Events events = eventService.getEvents( params );
 
-        if ( options.hasPaging() )
-        {
-            Pager pager = new Pager( options.getPage(), events.getEvents().size(), options.getPageSize() );
-            events.setPager( pager );
-            events.setEvents( PagerUtils.pageCollection( events.getEvents(), pager ) );
-        }
-
         OutputStream outputStream = response.getOutputStream();
         response.setContentType( "application/csv" );
 
@@ -263,13 +245,6 @@
         
         EventRows eventRows = eventRowService.getEventRows( params );
 
-        if ( options.hasPaging() )
-        {
-            Pager pager = new Pager( options.getPage(), eventRows.getEventRows().size(), options.getPageSize() );
-            eventRows.setPager( pager );
-            eventRows.setEventRows( PagerUtils.pageCollection( eventRows.getEventRows(), pager ) );
-        }
-
         model.addAttribute( "model", eventRows );
         model.addAttribute( "viewClass", options.getViewClass( "detailed" ) );