← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 18643: added count() on QueryService/QueryEngine, used in AbstractCrudController to get accurate count o...

 

------------------------------------------------------------
revno: 18643
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2015-03-20 10:00:33 +0700
message:
  added count() on QueryService/QueryEngine, used in AbstractCrudController to get accurate count of objects
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/query/Query.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/query/QueryEngine.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/query/QueryService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/CriteriaQueryEngine.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/DefaultQueryService.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AbstractCrudController.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/query/Query.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/query/Query.java	2015-02-23 04:59:43 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/query/Query.java	2015-03-20 03:00:33 +0000
@@ -172,11 +172,11 @@
         Optional<Property> name = Optional.fromNullable( schema.getProperty( "name" ) );
         Optional<Property> created = Optional.fromNullable( schema.getProperty( "created" ) );
 
-        if ( name.isPresent() )
+        if ( name.isPresent() && name.get().isPersisted() )
         {
             addOrder( Order.asc( name.get() ) );
         }
-        else if ( created.isPresent() )
+        else if ( created.isPresent() && created.get().isPersisted() )
         {
             addOrder( Order.desc( created.get() ) );
         }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/query/QueryEngine.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/query/QueryEngine.java	2015-02-23 13:06:26 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/query/QueryEngine.java	2015-03-20 03:00:33 +0000
@@ -36,4 +36,6 @@
 public interface QueryEngine<T>
 {
     List<T> query( Query query );
+
+    int count( Query query );
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/query/QueryService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/query/QueryService.java	2015-02-20 09:14:02 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/query/QueryService.java	2015-03-20 03:00:33 +0000
@@ -39,5 +39,7 @@
 
     Result query( Query query, ResultTransformer transformer );
 
+    int count( Query query );
+
     Query getQueryFromUrl( Class<?> klass, List<String> filters, List<Order> orders );
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/CriteriaQueryEngine.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/CriteriaQueryEngine.java	2015-03-10 12:19:24 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/CriteriaQueryEngine.java	2015-03-20 03:00:33 +0000
@@ -31,6 +31,7 @@
 import org.hibernate.Criteria;
 import org.hibernate.criterion.Criterion;
 import org.hibernate.criterion.Disjunction;
+import org.hibernate.criterion.Projections;
 import org.hibernate.criterion.Restrictions;
 import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.hibernate.HibernateGenericStore;
@@ -84,6 +85,40 @@
         return criteria.list();
     }
 
+    @Override
+    @SuppressWarnings( "unchecked" )
+    public int count( Query query )
+    {
+        Schema schema = query.getSchema();
+
+        // create a copy of this query using only the restrictions
+        Query countQuery = Query.from( query.getSchema() );
+        countQuery.add( query.getRestrictions() );
+
+        if ( schema == null )
+        {
+            return 0;
+        }
+
+        HibernateGenericStore<?> store = getStore( (Class<? extends IdentifiableObject>) schema.getKlass() );
+
+        if ( store == null )
+        {
+            return 0;
+        }
+
+        Criteria criteria = buildCriteria( store.getSharingCriteria(), countQuery );
+
+        if ( criteria == null )
+        {
+            return 0;
+        }
+
+        return ((Number) criteria
+            .setProjection( Projections.countDistinct( "id" ) )
+            .uniqueResult()).intValue();
+    }
+
     private Criteria buildCriteria( Criteria criteria, Query query )
     {
         Map<Operator, List<Restriction>> restrictions = new HashMap<>();

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/DefaultQueryService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/DefaultQueryService.java	2015-03-12 02:34:53 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/DefaultQueryService.java	2015-03-20 03:00:33 +0000
@@ -74,6 +74,12 @@
     }
 
     @Override
+    public int count( Query query )
+    {
+        return queryEngine.count( query );
+    }
+
+    @Override
     public Query getQueryFromUrl( Class<?> klass, List<String> filters, List<Order> orders )
     {
         Query query = Query.from( schemaService.getDynamicSchema( klass ) );

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AbstractCrudController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AbstractCrudController.java	2015-03-12 09:22:48 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AbstractCrudController.java	2015-03-20 03:00:33 +0000
@@ -785,7 +785,7 @@
         }
         else if ( options.hasPaging() && !haveFilters )
         {
-            int count = manager.getCount( getEntityClass() );
+            int count = queryService.count( query );
 
             Pager pager = new Pager( options.getPage(), count, options.getPageSize() );
             metaData.setPager( pager );
@@ -793,8 +793,6 @@
             query.setFirstResult( pager.getOffset() );
             query.setMaxResults( pager.getPageSize() );
             entityList = (List<T>) queryService.query( query ).getItems();
-
-            metaData.setPager( new Pager( options.getPage(), entityList.size(), options.getPageSize() ) );
         }
         else
         {