← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 14581: TEI query, including pager in response for paging

 

------------------------------------------------------------
revno: 14581
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2014-04-01 10:58:32 +0200
message:
  TEI query, including pager in response for paging
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceQueryParams.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceStore.java
  dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java
  dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityInstanceStore.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/trackedentity/TrackedEntityInstanceQueryParams.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceQueryParams.java	2014-03-30 20:09:40 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceQueryParams.java	2014-04-01 08:58:32 +0000
@@ -54,6 +54,7 @@
     public static final String TRACKED_ENTITY_ATTRIBUTE_VALUE_ID = "tevalue";
     
     public static final String META_DATA_NAMES_KEY = "names";
+    public static final String PAGER_META_KEY = "pager";
     
     /**
      * Query value, will apply to all relevant attributes.

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceStore.java	2014-03-24 14:03:51 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceStore.java	2014-04-01 08:58:32 +0000
@@ -52,6 +52,8 @@
 
     List<Map<String, String>> getTrackedEntityInstances( TrackedEntityInstanceQueryParams params );
     
+    int getTrackedEntityInstanceCount( TrackedEntityInstanceQueryParams params );
+    
     /**
      * Search entityInstances who registered in a certain organisation unit
      * 

=== modified file 'dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java'
--- dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java	2014-03-30 20:09:40 +0000
+++ dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java	2014-04-01 08:58:32 +0000
@@ -32,7 +32,7 @@
 import static org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams.LAST_UPDATED_ID;
 import static org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams.ORG_UNIT_ID;
 import static org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams.TRACKED_ENTITY_ID;
-import static org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams.TRACKED_ENTITY_INSTANCE_ID;
+import static org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams.*;
 import static org.hisp.dhis.common.OrganisationUnitSelectionMode.*;
 
 import java.lang.reflect.Type;
@@ -54,6 +54,7 @@
 import org.hisp.dhis.common.GridHeader;
 import org.hisp.dhis.common.IllegalQueryException;
 import org.hisp.dhis.common.OrganisationUnitSelectionMode;
+import org.hisp.dhis.common.Pager;
 import org.hisp.dhis.common.QueryItem;
 import org.hisp.dhis.i18n.I18n;
 import org.hisp.dhis.i18n.I18nFormat;
@@ -228,10 +229,19 @@
                 grid.addValue( entity.get( item.getItemId() ) );
             }
         }
+
+        Map<Object, Object> metaData = new HashMap<Object, Object>();   
+
+        if ( params.isPaging() )
+        {
+            int count = trackedEntityInstanceStore.getTrackedEntityInstanceCount( params );
+            
+            Pager pager = new Pager( params.getPageWithDefault(), count, params.getPageSizeWithDefault() );
+            metaData.put( PAGER_META_KEY, pager );
+        }
         
         if ( !params.isSkipMeta() )
-        {
-            Map<Object, Object> metaData = new HashMap<Object, Object>();            
+        {         
             Map<String, String> names = new HashMap<String, String>();
             
             for ( String te : tes )
@@ -240,9 +250,10 @@
                 names.put( te, entity != null ? entity.getDisplayName() : null );
             }
             
-            metaData.put( TrackedEntityInstanceQueryParams.META_DATA_NAMES_KEY, names );
-            grid.setMetaData( metaData );
+            metaData.put( META_DATA_NAMES_KEY, names );
         }
+
+        grid.setMetaData( metaData );
         
         return grid;
     }

=== modified file 'dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityInstanceStore.java'
--- dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityInstanceStore.java	2014-03-31 07:42:19 +0000
+++ dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityInstanceStore.java	2014-04-01 08:58:32 +0000
@@ -153,11 +153,90 @@
         sql = sql.substring( 0, sql.length() - 2 ) + " "; // Remove last comma
 
         // ---------------------------------------------------------------------
-        // From, join and where clause. For attribute params, restriction is set
-        // in inner join. For query params, restriction is set in where clause.
-        // ---------------------------------------------------------------------
-        
-        sql +=        
+        // From and where clause
+        // ---------------------------------------------------------------------
+
+        sql += getFromWhereClause( params, hlp );
+        
+        // ---------------------------------------------------------------------
+        // Paging clause
+        // ---------------------------------------------------------------------
+
+        if ( params.isPaging() )
+        {
+            sql += "limit " + params.getPageSizeWithDefault() + " offset " + params.getOffset();
+        }
+
+        // ---------------------------------------------------------------------
+        // Query
+        // ---------------------------------------------------------------------
+        
+        Timer t = new Timer().start();
+        
+        SqlRowSet rowSet = jdbcTemplate.queryForRowSet( sql );
+        
+        t.getTime( "Tracked entity instance query SQL: " + sql );
+        
+        List<Map<String, String>> list = new ArrayList<Map<String,String>>();
+        
+        while ( rowSet.next() )
+        {
+            final Map<String, String> map = new HashMap<String, String>();
+            
+            map.put( TRACKED_ENTITY_INSTANCE_ID, rowSet.getString( TRACKED_ENTITY_INSTANCE_ID ) );
+            map.put( CREATED_ID, rowSet.getString( CREATED_ID ) );
+            map.put( LAST_UPDATED_ID, rowSet.getString( LAST_UPDATED_ID ) );
+            map.put( ORG_UNIT_ID, rowSet.getString( ORG_UNIT_ID ) );
+            map.put( TRACKED_ENTITY_ID, rowSet.getString( TRACKED_ENTITY_ID ) );
+            
+            for ( QueryItem item : params.getAttributes() )
+            {
+                map.put( item.getItemId(), rowSet.getString( item.getItemId() ) );
+            }
+            
+            list.add( map );
+        }
+        
+        return list;
+    }
+
+    @Override
+    public int getTrackedEntityInstanceCount( TrackedEntityInstanceQueryParams params )
+    {
+        SqlHelper hlp = new SqlHelper();
+
+        // ---------------------------------------------------------------------
+        // Select clause
+        // ---------------------------------------------------------------------
+        
+        String sql = "select count(tei.uid) as " + TRACKED_ENTITY_INSTANCE_ID + " ";
+
+        // ---------------------------------------------------------------------
+        // From and where clause
+        // ---------------------------------------------------------------------
+
+        sql += getFromWhereClause( params, hlp );
+
+        // ---------------------------------------------------------------------
+        // Query
+        // ---------------------------------------------------------------------
+        
+        Timer t = new Timer().start();
+        
+        Integer count = jdbcTemplate.queryForObject( sql, Integer.class );
+        
+        t.getTime( "Tracked entity instance count SQL: " + sql );
+        
+        return count;        
+    }
+    
+    /**
+     * From, join and where clause. For attribute params, restriction is set
+     * in inner join. For query params, restriction is set in where clause.
+     */
+    private String getFromWhereClause( TrackedEntityInstanceQueryParams params, SqlHelper hlp )
+    {
+        String sql =        
             "from trackedentityinstance tei " +
             "inner join trackedentity te on tei.trackedentityid = te.trackedentityid " +
             "inner join organisationunit ou on tei.organisationunitid = ou.organisationunitid ";
@@ -243,46 +322,7 @@
             sql = sql.substring( 0, sql.length() - 3 ) + ") "; // Remove last or
         }
 
-        // ---------------------------------------------------------------------
-        // Paging clause
-        // ---------------------------------------------------------------------
-
-        if ( params.isPaging() )
-        {
-            sql += "limit " + params.getPageSizeWithDefault() + " offset " + params.getOffset();
-        }
-
-        // ---------------------------------------------------------------------
-        // Query
-        // ---------------------------------------------------------------------
-        
-        Timer t = new Timer().start();
-        
-        SqlRowSet rowSet = jdbcTemplate.queryForRowSet( sql );
-        
-        t.getTime( "Tracked entity instance query SQL: " + sql );
-        
-        List<Map<String, String>> list = new ArrayList<Map<String,String>>();
-        
-        while ( rowSet.next() )
-        {
-            final Map<String, String> map = new HashMap<String, String>();
-            
-            map.put( TRACKED_ENTITY_INSTANCE_ID, rowSet.getString( TRACKED_ENTITY_INSTANCE_ID ) );
-            map.put( CREATED_ID, rowSet.getString( CREATED_ID ) );
-            map.put( LAST_UPDATED_ID, rowSet.getString( LAST_UPDATED_ID ) );
-            map.put( ORG_UNIT_ID, rowSet.getString( ORG_UNIT_ID ) );
-            map.put( TRACKED_ENTITY_ID, rowSet.getString( TRACKED_ENTITY_ID ) );
-            
-            for ( QueryItem item : params.getAttributes() )
-            {
-                map.put( item.getItemId(), rowSet.getString( item.getItemId() ) );
-            }
-            
-            list.add( map );
-        }
-        
-        return list;
+        return sql;
     }
     
     @Override