← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21783: support totalPages parameter in /api/trackedEntityInstances

 

------------------------------------------------------------
revno: 21783
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2016-01-20 10:58:55 +0700
message:
  support totalPages parameter in /api/trackedEntityInstances
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/startup/TableAlteror.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityInstanceStore.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/AbstractTrackedEntityInstanceService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/TrackedEntityInstanceService.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/TrackedEntityInstanceController.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/TrackedEntityInstanceService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceService.java	2016-01-04 02:27:49 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceService.java	2016-01-20 03:58:55 +0000
@@ -119,6 +119,8 @@
      */
     List<TrackedEntityInstance> getTrackedEntityInstances( TrackedEntityInstanceQueryParams params );
 
+    int getTrackedEntityInstanceCount( TrackedEntityInstanceQueryParams params );
+
     /**
      * Returns a TrackedEntityInstanceQueryParams based on the given input.
      *

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/startup/TableAlteror.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/startup/TableAlteror.java	2016-01-18 10:29:14 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/startup/TableAlteror.java	2016-01-20 03:58:55 +0000
@@ -892,7 +892,6 @@
         executeSql( "alter table dataapproval add constraint dataapproval_unique_key unique (dataapprovallevelid,workflowid,periodid,organisationunitid,attributeoptioncomboid)" );
 
         upgradeImplicitAverageMonitoringRules();
-
         updateOptions();
 
         upgradeAggregationType( "reporttable" );

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java	2016-01-04 11:15:14 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityInstanceService.java	2016-01-20 03:58:55 +0000
@@ -68,7 +68,8 @@
 import java.util.Map;
 import java.util.Set;
 
-import static org.hisp.dhis.common.OrganisationUnitSelectionMode.*;
+import static org.hisp.dhis.common.OrganisationUnitSelectionMode.ACCESSIBLE;
+import static org.hisp.dhis.common.OrganisationUnitSelectionMode.ALL;
 import static org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams.*;
 
 /**
@@ -122,7 +123,7 @@
         validate( params );
 
         params.setUser( currentUserService.getCurrentUser() );
-        
+
         if ( !params.isPaging() && !params.isSkipPaging() )
         {
             params.setDefaultPaging();
@@ -131,6 +132,17 @@
         return trackedEntityInstanceStore.getTrackedEntityInstances( params );
     }
 
+    @Override
+    public int getTrackedEntityInstanceCount( TrackedEntityInstanceQueryParams params )
+    {
+        decideAccess( params );
+        validate( params );
+
+        params.setUser( currentUserService.getCurrentUser() );
+
+        return trackedEntityInstanceStore.countTrackedEntityInstances( params );
+    }
+
     // TODO lower index on attribute value?
     @Override
     public Grid getTrackedEntityInstancesGrid( TrackedEntityInstanceQueryParams params )
@@ -139,7 +151,7 @@
         validate( params );
 
         params.setUser( currentUserService.getCurrentUser() );
-        
+
         // ---------------------------------------------------------------------
         // If params of type query and no attributes or filters defined, use
         // attributes from program if program is defined, if not, use 
@@ -269,7 +281,7 @@
 
         User user = currentUserService.getCurrentUser();
 
-        if ( !params.hasOrganisationUnits() && !( params.isOrganisationUnitMode( ALL ) || params.isOrganisationUnitMode( ACCESSIBLE ) ) )
+        if ( !params.hasOrganisationUnits() && !(params.isOrganisationUnitMode( ALL ) || params.isOrganisationUnitMode( ACCESSIBLE )) )
         {
             violation = "At least one organisation unit must be specified";
         }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityInstanceStore.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityInstanceStore.java	2016-01-19 04:52:39 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityInstanceStore.java	2016-01-20 03:58:55 +0000
@@ -94,8 +94,7 @@
     @Override
     public int countTrackedEntityInstances( TrackedEntityInstanceQueryParams params )
     {
-        String hql = buildTrackedEntityInstanceHql( params );
-        
+        String hql = buildTrackedEntityInstanceCountHql( params );
         Query query = getQuery( hql );
 
         return ((Number) query.iterate().next()).intValue();
@@ -106,7 +105,6 @@
     public List<TrackedEntityInstance> getTrackedEntityInstances( TrackedEntityInstanceQueryParams params )
     {
         String hql = buildTrackedEntityInstanceHql( params );
-        
         Query query = getQuery( hql );
 
         if ( params.isPaging() )
@@ -118,6 +116,11 @@
         return query.list();
     }
 
+    private String buildTrackedEntityInstanceCountHql( TrackedEntityInstanceQueryParams params )
+    {
+        return buildTrackedEntityInstanceHql( params ).replaceFirst( "select distinct tei from", "select count(distinct tei) from" );
+    }
+
     private String buildTrackedEntityInstanceHql( TrackedEntityInstanceQueryParams params )
     {
         String hql = "select distinct tei from TrackedEntityInstance tei left join tei.trackedEntityAttributeValues";
@@ -131,11 +134,11 @@
         if ( params.hasOrganisationUnits() )
         {
             params.handleOrganisationUnits();
-            
+
             if ( params.isOrganisationUnitMode( OrganisationUnitSelectionMode.DESCENDANTS ) )
             {
                 String ouClause = "(";
-                
+
                 SqlHelper orHlp = new SqlHelper( true );
 
                 for ( OrganisationUnit organisationUnit : params.getOrganisationUnits() )
@@ -162,7 +165,7 @@
                     String filter = queryFilter.getSqlFilter( StringUtils.lowerCase( queryFilter.getFilter() ) );
 
                     hql += hlp.whereAnd() + " exists (from TrackedEntityAttributeValue teav where teav.entityInstance=tei";
-                    
+
                     hql += " and teav.attribute.uid='" + queryItem.getItemId() + "'";
 
                     if ( queryItem.isNumeric() )
@@ -181,7 +184,7 @@
         if ( params.hasProgram() )
         {
             hql += hlp.whereAnd() + "exists (from ProgramInstance pi where pi.entityInstance=tei";
-            
+
             hql += " and pi.program.uid = '" + params.getProgram().getUid() + "'";
 
             if ( params.hasProgramStatus() )
@@ -219,14 +222,14 @@
         // Select clause
         // ---------------------------------------------------------------------
 
-        String sql = 
-            "select tei.uid as " + TRACKED_ENTITY_INSTANCE_ID + ", " + 
-            "tei.created as " + CREATED_ID + ", " + 
-            "tei.lastupdated as " + LAST_UPDATED_ID + ", " + 
-            "ou.uid as " + ORG_UNIT_ID + ", " +
-            "ou.name as " + ORG_UNIT_NAME + ", " +
-            "te.uid as " + TRACKED_ENTITY_ID + ", " + 
-            "tei.inactive as " + INACTIVE_ID + ", ";
+        String sql =
+            "select tei.uid as " + TRACKED_ENTITY_INSTANCE_ID + ", " +
+                "tei.created as " + CREATED_ID + ", " +
+                "tei.lastupdated as " + LAST_UPDATED_ID + ", " +
+                "ou.uid as " + ORG_UNIT_ID + ", " +
+                "ou.name as " + ORG_UNIT_NAME + ", " +
+                "te.uid as " + TRACKED_ENTITY_ID + ", " +
+                "tei.inactive as " + INACTIVE_ID + ", ";
 
         for ( QueryItem item : params.getAttributes() )
         {
@@ -358,7 +361,7 @@
         }
 
         params.handleOrganisationUnits();
-        
+
         if ( params.isOrganisationUnitMode( OrganisationUnitSelectionMode.ALL ) )
         {
             // No restriction

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/AbstractTrackedEntityInstanceService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/AbstractTrackedEntityInstanceService.java	2016-01-04 02:27:49 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/AbstractTrackedEntityInstanceService.java	2016-01-20 03:58:55 +0000
@@ -121,6 +121,12 @@
     }
 
     @Override
+    public int getTrackedEntityInstanceCount( TrackedEntityInstanceQueryParams params )
+    {
+        return entityInstanceService.getTrackedEntityInstanceCount( params );
+    }
+
+    @Override
     public TrackedEntityInstance getTrackedEntityInstance( String uid )
     {
         return getTrackedEntityInstance( teiService.getTrackedEntityInstance( uid ) );

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/TrackedEntityInstanceService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/TrackedEntityInstanceService.java	2016-01-04 02:27:49 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/TrackedEntityInstanceService.java	2016-01-20 03:58:55 +0000
@@ -50,6 +50,8 @@
 
     List<TrackedEntityInstance> getTrackedEntityInstances( TrackedEntityInstanceQueryParams params );
 
+    int getTrackedEntityInstanceCount( TrackedEntityInstanceQueryParams params );
+
     TrackedEntityInstance getTrackedEntityInstance( String uid );
 
     TrackedEntityInstance getTrackedEntityInstance( org.hisp.dhis.trackedentity.TrackedEntityInstance entityInstance );

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/TrackedEntityInstanceController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/TrackedEntityInstanceController.java	2016-01-04 02:27:49 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/TrackedEntityInstanceController.java	2016-01-20 03:58:55 +0000
@@ -32,6 +32,7 @@
 import org.hisp.dhis.common.DxfNamespaces;
 import org.hisp.dhis.common.Grid;
 import org.hisp.dhis.common.OrganisationUnitSelectionMode;
+import org.hisp.dhis.common.Pager;
 import org.hisp.dhis.common.cache.CacheStrategy;
 import org.hisp.dhis.commons.util.TextUtils;
 import org.hisp.dhis.dxf2.common.ImportOptions;
@@ -148,6 +149,14 @@
         List<TrackedEntityInstance> trackedEntityInstances = trackedEntityInstanceService.getTrackedEntityInstances( params );
 
         RootNode rootNode = NodeUtils.createMetadata();
+
+        if ( params.isPaging() && params.isTotalPages() )
+        {
+            int count = trackedEntityInstanceService.getTrackedEntityInstanceCount( params );
+            Pager pager = new Pager( params.getPage(), count, params.getPageSize() );
+            rootNode.addChild( NodeUtils.createPager( pager ) );
+        }
+
         rootNode.addChild( fieldFilterService.filter( TrackedEntityInstance.class, trackedEntityInstances, fields ) );
 
         return rootNode;