← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 16647: wip, changing from hql to criteria for sharing queries, also adds more tests for queries and exte...

 

------------------------------------------------------------
revno: 16647
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2014-09-08 16:06:17 +0700
message:
  wip, changing from hql to criteria for sharing queries, also adds more tests for queries and extends idObjectManager
modified:
  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/IdentifiableObjectManager.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectManager.java	2014-07-01 13:50:42 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectManager.java	2014-09-08 09:06:17 +0000
@@ -68,6 +68,10 @@
 
     <T extends IdentifiableObject> Collection<T> getAll( Class<T> clazz );
 
+    <T extends IdentifiableObject> Collection<T> getAllByName( Class<T> clazz, String name );
+
+    <T extends IdentifiableObject> Collection<T> getAllByNameIgnoreCase( Class<T> clazz, String name );
+
     <T extends IdentifiableObject> Collection<T> getAllSorted( Class<T> clazz );
 
     <T extends IdentifiableObject> List<T> getByUid( Class<T> clazz, Collection<String> uids );

=== 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-08-26 10:51:19 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/DefaultIdentifiableObjectManager.java	2014-09-08 09:06:17 +0000
@@ -257,6 +257,34 @@
 
     @Override
     @SuppressWarnings("unchecked")
+    public <T extends IdentifiableObject> Collection<T> getAllByName( Class<T> clazz, String name )
+    {
+        GenericIdentifiableObjectStore<IdentifiableObject> store = getIdentifiableObjectStore( clazz );
+
+        if ( store == null )
+        {
+            return new ArrayList<>();
+        }
+
+        return (Collection<T>) store.getAllEqName( name );
+    }
+
+    @Override
+    @SuppressWarnings("unchecked")
+    public <T extends IdentifiableObject> Collection<T> getAllByNameIgnoreCase( Class<T> clazz, String name )
+    {
+        GenericIdentifiableObjectStore<IdentifiableObject> store = getIdentifiableObjectStore( clazz );
+
+        if ( store == null )
+        {
+            return new ArrayList<>();
+        }
+
+        return (Collection<T>) store.getAll();
+    }
+
+    @Override
+    @SuppressWarnings("unchecked")
     public <T extends IdentifiableObject> Collection<T> getAllSorted( Class<T> clazz )
     {
         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-05 06:57:32 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/hibernate/HibernateIdentifiableObjectStore.java	2014-09-08 09:06:17 +0000
@@ -179,128 +179,36 @@
     @SuppressWarnings( "unchecked" )
     public List<T> getAllEqName( String name )
     {
-        Query query = sharingEnabled() ? getQueryAllEqNameAcl( name ) : getQueryAllEqName( name );
-
-        return query.list();
-    }
-
-    private Query getQueryAllEqNameAcl( String name )
-    {
-        String hql = "select distinct c from " + clazz.getName() + " c"
-            + " where name = :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 );
-
-        return query;
-    }
-
-    private Query getQueryAllEqName( String name )
-    {
-        Query query = getQuery( "from " + clazz.getName() + " c where name = :name order by c.name" );
-        query.setString( "name", name );
-
-        return query;
+        return getSharingCriteria()
+            .add( Restrictions.eq( "name", name ) )
+            .list();
     }
 
     @Override
     @SuppressWarnings( "unchecked" )
     public List<T> getAllEqNameIgnoreCase( String name )
     {
-        Query query = sharingEnabled() ? getQueryAllEqNameAclIgnoreCase( name ) : getQueryAllEqNameIgnoreCase( name );
-
-        return query.list();
-    }
-
-    private Query getQueryAllEqNameAclIgnoreCase( String name )
-    {
-        String hql = "select distinct c from " + clazz.getName() + " c"
-            + " where lower(name) = :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 getQueryAllEqNameIgnoreCase( String name )
-    {
-        Query query = getQuery( "from " + clazz.getName() + " c where lower(name) = :name order by c.name" );
-        query.setString( "name", name.toLowerCase() );
-
-        return query;
+        return getSharingCriteria()
+            .add( Restrictions.eq( "name", name ).ignoreCase() )
+            .list();
     }
 
     @Override
     @SuppressWarnings( "unchecked" )
     public List<T> getAllEqShortName( String shortName )
     {
-        Query query = sharingEnabled() ? getQueryAllEqShortNameAcl( shortName ) : getQueryAllEqShortName( shortName );
-
-        return query.list();
-    }
-
-    private Query getQueryAllEqShortNameAcl( String shortName )
-    {
-        String hql = "select distinct c from " + clazz.getName() + " c"
-            + " where shortName = :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 );
-
-        return query;
-    }
-
-    private Query getQueryAllEqShortName( String shortName )
-    {
-        Query query = getQuery( "from " + clazz.getName() + " c where shortName = :shortName order by c.shortName" );
-        query.setString( "shortName", shortName );
-
-        return query;
+        return getSharingCriteria()
+            .add( Restrictions.eq( "shortName", shortName ) )
+            .list();
     }
 
     @Override
     @SuppressWarnings( "unchecked" )
     public List<T> getAllEqShortNameIgnoreCase( String shortName )
     {
-        Query query = sharingEnabled() ? getQueryAllEqShortNameAclIgnoreCase( shortName ) : getQueryAllEqShortNameIgnoreCase( shortName );
-
-        return query.list();
-    }
-
-    private Query getQueryAllEqShortNameAclIgnoreCase( String shortName )
-    {
-        String hql = "select distinct c from " + clazz.getName() + " c"
-            + " where lower(shortName) = :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 getQueryAllEqShortNameIgnoreCase( String shortName )
-    {
-        Query query = getQuery( "from " + clazz.getName() + " c where lower(shortName) = :shortName order by c.shortName" );
-        query.setString( "shortName", shortName.toLowerCase() );
-
-        return query;
+        return getSharingCriteria()
+            .add( Restrictions.eq( "shortName", shortName ).ignoreCase() )
+            .list();
     }
 
     @Override
@@ -403,14 +311,9 @@
     @SuppressWarnings( "unchecked" )
     public List<T> getAllOrderedName( int first, int max )
     {
-        // return getSharingCriteria().setFirstResult( first ).setMaxResults( max ).list();
-
-        Query query = sharingEnabled() ? getQueryAllOrderedNameAcl() : getQueryAllOrderedName();
-
-        query.setFirstResult( first );
-        query.setMaxResults( max );
-
-        return query.list();
+        return getSharingCriteria()
+            .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-05 04:49:59 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/common/SharingTest.java	2014-09-08 09:06:17 +0000
@@ -32,9 +32,11 @@
 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;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.user.User;
 import org.hisp.dhis.user.UserGroup;
 import org.hisp.dhis.user.UserGroupAccess;
@@ -76,6 +78,49 @@
     }
 
     @Test
+    public void getEqualToName()
+    {
+        DataElement dataElement = createDataElement( 'A' );
+        identifiableObjectManager.save( dataElement );
+
+        assertNotNull( identifiableObjectManager.getByName( DataElement.class, "DataElementA" ) );
+        assertNull( identifiableObjectManager.getByName( DataElement.class, "DataElementB" ) );
+        assertEquals( dataElement, identifiableObjectManager.getByName( DataElement.class, "DataElementA" ) );
+    }
+
+    @Test
+    public void getAllEqualToName()
+    {
+        OrganisationUnit organisationUnitA1 = createOrganisationUnit( 'A' );
+        organisationUnitA1.setCode( null );
+        identifiableObjectManager.save( organisationUnitA1 );
+
+        OrganisationUnit organisationUnitA2 = createOrganisationUnit( 'B' );
+        organisationUnitA2.setName( "OrganisationUnitA" );
+        organisationUnitA2.setCode( null );
+        identifiableObjectManager.save( organisationUnitA2 );
+
+        assertEquals( 2, identifiableObjectManager.getAllByName( OrganisationUnit.class, "OrganisationUnitA" ).size() );
+        assertEquals( 0, identifiableObjectManager.getAllByName( OrganisationUnit.class, "organisationunita" ).size() );
+    }
+
+    @Test
+    public void getAllEqualToNameIgnoreCase()
+    {
+        OrganisationUnit organisationUnitA1 = createOrganisationUnit( 'A' );
+        organisationUnitA1.setCode( null );
+        identifiableObjectManager.save( organisationUnitA1 );
+
+        OrganisationUnit organisationUnitA2 = createOrganisationUnit( 'B' );
+        organisationUnitA2.setName( "OrganisationUnitA" );
+        organisationUnitA2.setCode( null );
+        identifiableObjectManager.save( organisationUnitA2 );
+
+        assertEquals( 2, identifiableObjectManager.getAllByNameIgnoreCase( OrganisationUnit.class, "OrganisationUnitA" ).size() );
+        assertEquals( 2, identifiableObjectManager.getAllByNameIgnoreCase( OrganisationUnit.class, "organisationunita" ).size() );
+    }
+
+    @Test
     public void userIsCurrentIfNoUserSet()
     {
         User user = createUserAndInjectSecurityContext( true );
@@ -113,14 +158,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-05 06:57:32 +0000
+++ dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateGenericStore.java	2014-09-08 09:06:17 +0000
@@ -38,6 +38,7 @@
 import org.hibernate.criterion.Criterion;
 import org.hibernate.criterion.DetachedCriteria;
 import org.hibernate.criterion.Disjunction;
+import org.hibernate.criterion.Projections;
 import org.hibernate.criterion.Property;
 import org.hibernate.criterion.Restrictions;
 import org.hibernate.criterion.Subqueries;
@@ -391,22 +392,7 @@
     @SuppressWarnings( "unchecked" )
     public final List<T> getAll()
     {
-        Query query = sharingEnabled() ? getQueryAllAcl() : getQueryAll();
-
-        return query.list();
-    }
-
-    private Query getQueryAllAcl()
-    {
-        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%')";
-
-        Query query = getQuery( hql );
-        query.setEntity( "user", currentUserService.getCurrentUser() );
-
-        return query;
+        return getSharingCriteria().list();
     }
 
     /**
@@ -455,28 +441,7 @@
     @Override
     public int getCount()
     {
-        // return ((Number) getSharingCriteria().setProjection( Projections.countDistinct( "id" ) ).uniqueResult()).intValue();
-
-        Query query = sharingEnabled() ? getQueryCountAcl() : getQueryCount();
-        return ((Long) query.uniqueResult()).intValue();
-    }
-
-    private Query getQueryCountAcl()
-    {
-        String hql = "select count(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%')";
-
-        Query query = getQuery( hql );
-        query.setEntity( "user", currentUserService.getCurrentUser() );
-
-        return query;
-    }
-
-    private Query getQueryCount()
-    {
-        return getQuery( "select count(distinct c) from " + clazz.getName() + " c" );
+        return ((Number) getSharingCriteria().setProjection( Projections.countDistinct( "id" ) ).uniqueResult()).intValue();
     }
 
     //----------------------------------------------------------------------------------------------------------------