← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 16654: wip, replace hql with criteria in Generic stores + tests

 

------------------------------------------------------------
revno: 16654
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2014-09-08 19:34:18 +0700
message:
  wip, replace hql with criteria in Generic stores + tests
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/GenericIdentifiableObjectStore.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectManager.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/DefaultIdentifiableObjectManager.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/hibernate/HibernateIdentifiableObjectStore.java
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/common/SharingTest.java
  dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateGenericStore.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/common/GenericIdentifiableObjectStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/GenericIdentifiableObjectStore.java	2014-03-18 08:10:10 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/GenericIdentifiableObjectStore.java	2014-09-08 12:34:18 +0000
@@ -140,6 +140,13 @@
     List<T> getAllLikeShortName( String shortName );
 
     /**
+     * The returned list is ordered by the last updated property descending.
+     *
+     * @return List of objects.
+     */
+    List<T> getAllOrderedLastUpdated();
+
+    /**
      * Retrieves the objects determined by the given first result and max result.
      * The returned list is ordered by the last updated property descending.
      *

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectManager.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectManager.java	2014-09-08 09:06:17 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectManager.java	2014-09-08 12:34:18 +0000
@@ -74,6 +74,8 @@
 
     <T extends IdentifiableObject> Collection<T> getAllSorted( Class<T> clazz );
 
+    <T extends IdentifiableObject> Collection<T> getAllSortedByLastUpdated( Class<T> clazz );
+
     <T extends IdentifiableObject> List<T> getByUid( Class<T> clazz, Collection<String> uids );
 
     <T extends IdentifiableObject> Collection<T> getLikeName( Class<T> clazz, String name );

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/DefaultIdentifiableObjectManager.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/DefaultIdentifiableObjectManager.java	2014-09-08 09:13:17 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/DefaultIdentifiableObjectManager.java	2014-09-08 12:34:18 +0000
@@ -299,6 +299,20 @@
 
     @Override
     @SuppressWarnings("unchecked")
+    public <T extends IdentifiableObject> Collection<T> getAllSortedByLastUpdated( Class<T> clazz )
+    {
+        GenericIdentifiableObjectStore<IdentifiableObject> store = getIdentifiableObjectStore( clazz );
+
+        if ( store == null )
+        {
+            return new ArrayList<>();
+        }
+
+        return (Collection<T>) store.getAllOrderedLastUpdated();
+    }
+
+    @Override
+    @SuppressWarnings("unchecked")
     public <T extends IdentifiableObject> List<T> getByUid( Class<T> clazz, Collection<String> uids )
     {
         GenericIdentifiableObjectStore<IdentifiableObject> store = getIdentifiableObjectStore( clazz );

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/hibernate/HibernateIdentifiableObjectStore.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/hibernate/HibernateIdentifiableObjectStore.java	2014-09-08 09:06:17 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/hibernate/HibernateIdentifiableObjectStore.java	2014-09-08 12:34:18 +0000
@@ -31,6 +31,7 @@
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.hibernate.Query;
+import org.hibernate.criterion.Order;
 import org.hibernate.criterion.Restrictions;
 import org.hisp.dhis.common.AuditLogUtil;
 import org.hisp.dhis.common.BaseIdentifiableObject;
@@ -181,6 +182,7 @@
     {
         return getSharingCriteria()
             .add( Restrictions.eq( "name", name ) )
+            .addOrder( Order.asc( "name" ) )
             .list();
     }
 
@@ -190,6 +192,7 @@
     {
         return getSharingCriteria()
             .add( Restrictions.eq( "name", name ).ignoreCase() )
+            .addOrder( Order.asc( "name" ) )
             .list();
     }
 
@@ -199,6 +202,7 @@
     {
         return getSharingCriteria()
             .add( Restrictions.eq( "shortName", shortName ) )
+            .addOrder( Order.asc( "shortName" ) )
             .list();
     }
 
@@ -208,6 +212,7 @@
     {
         return getSharingCriteria()
             .add( Restrictions.eq( "shortName", shortName ).ignoreCase() )
+            .addOrder( Order.asc( "shortName" ) )
             .list();
     }
 
@@ -215,32 +220,10 @@
     @SuppressWarnings( "unchecked" )
     public List<T> getAllLikeName( String name )
     {
-        Query query = sharingEnabled() ? getQueryAllLikeNameAcl( name ) : getQueryAllLikeName( name );
-
-        return query.list();
-    }
-
-    private Query getQueryAllLikeNameAcl( String name )
-    {
-        String hql = "select distinct c from " + clazz.getName() + " c"
-            + " where lower(name) like :name and ( c.publicAccess like 'r%' or c.user IS NULL or c.user=:user"
-            + " or exists "
-            + "     (from c.userGroupAccesses uga join uga.userGroup ug join ug.members ugm where ugm = :user and uga.access like 'r%')"
-            + " ) order by c.name";
-
-        Query query = getQuery( hql );
-        query.setEntity( "user", currentUserService.getCurrentUser() );
-        query.setString( "name", "%" + name.toLowerCase() + "%" );
-
-        return query;
-    }
-
-    private Query getQueryAllLikeName( String name )
-    {
-        Query query = getQuery( "from " + clazz.getName() + " c where lower(name) like :name order by c.name" );
-        query.setString( "name", "%" + name.toLowerCase() + "%" );
-
-        return query;
+        return getSharingCriteria()
+            .add( Restrictions.like( "name", "%" + name + "%" ).ignoreCase() )
+            .addOrder( Order.asc( "name" ) )
+            .list();
     }
 
     @Override
@@ -249,62 +232,22 @@
     {
         if ( NameableObject.class.isAssignableFrom( clazz ) )
         {
-            Query query = sharingEnabled() ? getQueryAllLikeShortNameAcl( shortName ) : getQueryAllLikeShortName( shortName );
-            return query.list();
+            return getSharingCriteria()
+                .add( Restrictions.like( "shortName", "%" + shortName + "%" ).ignoreCase() )
+                .addOrder( Order.asc( "shortName" ) )
+                .list();
         }
 
         return getAllLikeName( shortName ); // Fallback to name
     }
 
-    private Query getQueryAllLikeShortNameAcl( String shortName )
-    {
-        String hql = "select distinct c from " + clazz.getName() + " c"
-            + " where lower(shortName) like :shortName and ( c.publicAccess like 'r%' or c.user IS NULL or c.user=:user"
-            + " or exists "
-            + "     (from c.userGroupAccesses uga join uga.userGroup ug join ug.members ugm where ugm = :user and uga.access like 'r%')"
-            + " ) order by c.shortName";
-
-        Query query = getQuery( hql );
-        query.setEntity( "user", currentUserService.getCurrentUser() );
-        query.setString( "shortName", "%" + shortName.toLowerCase() + "%" );
-
-        return query;
-    }
-
-    private Query getQueryAllLikeShortName( String shortName )
-    {
-        Query query = getQuery( "from " + clazz.getName() + " c where lower(shortName) like :shortName order by c.shortName" );
-        query.setString( "shortName", "%" + shortName.toLowerCase() + "%" );
-
-        return query;
-    }
-
     @Override
     @SuppressWarnings( "unchecked" )
     public List<T> getAllOrderedName()
     {
-        Query query = sharingEnabled() ? getQueryAllOrderedNameAcl() : getQueryAllOrderedName();
-
-        return query.list();
-    }
-
-    private Query getQueryAllOrderedNameAcl()
-    {
-        String hql = "select distinct c from " + clazz.getName() + " c"
-            + " where c.publicAccess like 'r%' or c.user IS NULL or c.user=:user"
-            + " or exists "
-            + "     (from c.userGroupAccesses uga join uga.userGroup ug join ug.members ugm where ugm = :user and uga.access like 'r%')"
-            + " order by c.name";
-
-        Query query = getQuery( hql );
-        query.setEntity( "user", currentUserService.getCurrentUser() );
-
-        return query;
-    }
-
-    private Query getQueryAllOrderedName()
-    {
-        return getQuery( "from " + clazz.getName() + " c order by c.name" );
+        return getSharingCriteria()
+            .addOrder( Order.asc( "name" ) )
+            .list();
     }
 
     @Override
@@ -312,39 +255,28 @@
     public List<T> getAllOrderedName( int first, int max )
     {
         return getSharingCriteria()
+            .addOrder( Order.asc( "name" ) )
             .setFirstResult( first ).setMaxResults( max )
             .list();
     }
 
     @Override
     @SuppressWarnings( "unchecked" )
+    public List<T> getAllOrderedLastUpdated()
+    {
+        return getSharingCriteria()
+            .addOrder( Order.desc( "lastUpdated" ) )
+            .list();
+    }
+
+    @Override
+    @SuppressWarnings( "unchecked" )
     public List<T> getAllOrderedLastUpdated( int first, int max )
     {
-        Query query = sharingEnabled() ? getQueryAllOrderedLastUpdatedAcl() : getQueryAllOrderedLastUpdated();
-
-        query.setFirstResult( first );
-        query.setMaxResults( max );
-
-        return query.list();
-    }
-
-    private Query getQueryAllOrderedLastUpdatedAcl()
-    {
-        String hql = "select distinct c from " + clazz.getName() + " c"
-            + " where c.publicAccess like 'r%' or c.user IS NULL or c.user=:user"
-            + " or exists "
-            + "     (from c.userGroupAccesses uga join uga.userGroup ug join ug.members ugm where ugm = :user and uga.access like 'r%')"
-            + " order by c.lastUpdated desc";
-
-        Query query = getQuery( hql );
-        query.setEntity( "user", currentUserService.getCurrentUser() );
-
-        return query;
-    }
-
-    private Query getQueryAllOrderedLastUpdated()
-    {
-        return getQuery( "from " + clazz.getName() + " c order by lastUpdated desc" );
+        return getSharingCriteria()
+            .addOrder( Order.desc( "lastUpdated" ) )
+            .setFirstResult( first ).setMaxResults( max )
+            .list();
     }
 
     @Override

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/common/SharingTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/common/SharingTest.java	2014-09-08 09:06:17 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/common/SharingTest.java	2014-09-08 12:34:18 +0000
@@ -32,7 +32,6 @@
 import org.hibernate.SessionFactory;
 import org.hisp.dhis.DhisSpringTest;
 import org.hisp.dhis.acl.AccessStringHelper;
-import org.hisp.dhis.constant.Constant;
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.hibernate.exception.CreateAccessDeniedException;
 import org.hisp.dhis.hibernate.exception.DeleteAccessDeniedException;
@@ -78,6 +77,31 @@
     }
 
     @Test
+    public void getCount()
+    {
+        identifiableObjectManager.save( createDataElement( 'A' ) );
+        identifiableObjectManager.save( createDataElement( 'B' ) );
+        identifiableObjectManager.save( createDataElement( 'C' ) );
+        identifiableObjectManager.save( createDataElement( 'D' ) );
+
+        assertEquals( 4, identifiableObjectManager.getCount( DataElement.class ) );
+    }
+
+    @Test
+    public void getCountByName()
+    {
+        identifiableObjectManager.save( createDataElement( 'A' ) );
+        identifiableObjectManager.save( createDataElement( 'B' ) );
+        identifiableObjectManager.save( createDataElement( 'C' ) );
+        identifiableObjectManager.save( createDataElement( 'D' ) );
+
+        assertEquals( 1, identifiableObjectManager.getCountByName( DataElement.class, "DataElementA" ) );
+        assertEquals( 1, identifiableObjectManager.getCountByName( DataElement.class, "DataElementB" ) );
+        assertEquals( 1, identifiableObjectManager.getCountByName( DataElement.class, "DataElementC" ) );
+        assertEquals( 1, identifiableObjectManager.getCountByName( DataElement.class, "DataElementD" ) );
+    }
+
+    @Test
     public void getEqualToName()
     {
         DataElement dataElement = createDataElement( 'A' );
@@ -121,6 +145,64 @@
     }
 
     @Test
+    public void getAllLikeName()
+    {
+        identifiableObjectManager.save( createDataElement( 'A' ) );
+        identifiableObjectManager.save( createDataElement( 'B' ) );
+        identifiableObjectManager.save( createDataElement( 'C' ) );
+        identifiableObjectManager.save( createDataElement( 'D' ) );
+
+        assertEquals( 4, identifiableObjectManager.getLikeName( DataElement.class, "DataElement" ).size() );
+        assertEquals( 4, identifiableObjectManager.getLikeName( DataElement.class, "dataElement" ).size() );
+    }
+
+    @Test
+    public void getAllLikeShortName()
+    {
+        identifiableObjectManager.save( createDataElement( 'A' ) );
+        identifiableObjectManager.save( createDataElement( 'B' ) );
+        identifiableObjectManager.save( createDataElement( 'C' ) );
+        identifiableObjectManager.save( createDataElement( 'D' ) );
+
+        assertEquals( 4, identifiableObjectManager.getLikeShortName( DataElement.class, "DataElementShort" ).size() );
+        assertEquals( 4, identifiableObjectManager.getLikeShortName( DataElement.class, "dataElementSHORT" ).size() );
+    }
+
+    @Test
+    public void getAllOrderedName()
+    {
+        identifiableObjectManager.save( createDataElement( 'D' ) );
+        identifiableObjectManager.save( createDataElement( 'B' ) );
+        identifiableObjectManager.save( createDataElement( 'C' ) );
+        identifiableObjectManager.save( createDataElement( 'A' ) );
+
+        List<DataElement> dataElements = new ArrayList<>( identifiableObjectManager.getAllSorted( DataElement.class ) );
+
+        assertEquals( 4, dataElements.size() );
+        assertEquals( "DataElementA", dataElements.get( 0 ).getName() );
+        assertEquals( "DataElementB", dataElements.get( 1 ).getName() );
+        assertEquals( "DataElementC", dataElements.get( 2 ).getName() );
+        assertEquals( "DataElementD", dataElements.get( 3 ).getName() );
+    }
+
+    @Test
+    public void getAllOrderedLastUpdated()
+    {
+        identifiableObjectManager.save( createDataElement( 'A' ) );
+        identifiableObjectManager.save( createDataElement( 'B' ) );
+        identifiableObjectManager.save( createDataElement( 'C' ) );
+        identifiableObjectManager.save( createDataElement( 'D' ) );
+
+        List<DataElement> dataElements = new ArrayList<>( identifiableObjectManager.getAllSortedByLastUpdated( DataElement.class ) );
+
+        assertEquals( 4, dataElements.size() );
+        assertEquals( "DataElementD", dataElements.get( 0 ).getName() );
+        assertEquals( "DataElementC", dataElements.get( 1 ).getName() );
+        assertEquals( "DataElementB", dataElements.get( 2 ).getName() );
+        assertEquals( "DataElementA", dataElements.get( 3 ).getName() );
+    }
+
+    @Test
     public void userIsCurrentIfNoUserSet()
     {
         User user = createUserAndInjectSecurityContext( true );
@@ -158,14 +240,14 @@
         assertFalse( AccessStringHelper.canWrite( dataElement.getPublicAccess() ) );
     }
 
-    @Test(expected = CreateAccessDeniedException.class)
+    @Test( expected = CreateAccessDeniedException.class )
     public void userDeniedCreateObject()
     {
         createUserAndInjectSecurityContext( false );
         identifiableObjectManager.save( createDataElement( 'A' ) );
     }
 
-    @Test(expected = DeleteAccessDeniedException.class)
+    @Test( expected = DeleteAccessDeniedException.class )
     public void userDeniedDeleteObject()
     {
         createUserAndInjectSecurityContext( false, "F_DATAELEMENT_PUBLIC_ADD", "F_USER_ADD" );

=== modified file 'dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateGenericStore.java'
--- dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateGenericStore.java	2014-09-08 09:06:17 +0000
+++ dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateGenericStore.java	2014-09-08 12:34:18 +0000
@@ -189,7 +189,7 @@
 
     protected final Criteria getSharingCriteria( User user, String access )
     {
-        Criteria criteria = sessionFactory.getCurrentSession().createCriteria( getClazz(), "c" ).setCacheable( false );
+        Criteria criteria = sessionFactory.getCurrentSession().createCriteria( getClazz(), "c" ).setCacheable( cacheable );
 
         if ( !sharingEnabled() )
         {
@@ -433,11 +433,6 @@
         return query;
     }
 
-    private Query getQueryAll()
-    {
-        return getQuery( "from " + clazz.getName() + " c" );
-    }
-
     @Override
     public int getCount()
     {