← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 16625: fixes for sharing criteria

 

------------------------------------------------------------
revno: 16625
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2014-09-04 14:04:22 +0700
message:
  fixes for sharing criteria
modified:
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/hibernate/HibernateIdentifiableObjectStore.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-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-02 05:23:24 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/hibernate/HibernateIdentifiableObjectStore.java	2014-09-04 07:04:22 +0000
@@ -404,10 +404,7 @@
     public List<T> getAllOrderedName( int first, int max )
     {
         /*
-        return getSharingCriteria()
-            .setFirstResult( first )
-            .setMaxResults( max )
-            .list();
+        return getSharingCriteria().setFirstResult( first ).setMaxResults( max ).list();
         */
 
         Query query = sharingEnabled() ? getQueryAllOrderedNameAcl() : getQueryAllOrderedName();

=== 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-02 06:24:44 +0000
+++ dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateGenericStore.java	2014-09-04 07:04:22 +0000
@@ -39,8 +39,10 @@
 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;
+import org.hibernate.sql.JoinType;
 import org.hisp.dhis.acl.AccessStringHelper;
 import org.hisp.dhis.acl.AclService;
 import org.hisp.dhis.common.AuditLogUtil;
@@ -171,7 +173,7 @@
 
     /**
      * Creates a Criteria for the implementation Class type.
-     *
+     * <p/>
      * Please note that sharing is not considered.
      *
      * @return a Criteria instance.
@@ -181,35 +183,6 @@
         return getClazzCriteria().setCacheable( cacheable );
     }
 
-    protected final Disjunction getSharingDisjunction()
-    {
-        return getSharingDisjunction( currentUserService.getCurrentUser() );
-    }
-
-    protected final Disjunction getSharingDisjunction( User user )
-    {
-        Assert.notNull( user, "User argument can't be null." );
-
-        Disjunction disjunction = Restrictions.disjunction();
-
-        disjunction.add( Restrictions.like( "publicAccess", "r%" ) );
-        disjunction.add( Restrictions.isNull( "user" ) );
-        disjunction.add( Restrictions.eq( "user", user ) );
-
-        DetachedCriteria detachedCriteria = DetachedCriteria.forClass( UserGroupAccess.class, "uga" );
-        detachedCriteria.createAlias( "uga.userGroup", "ug" );
-        detachedCriteria.createAlias( "ug.members", "ugm" );
-
-        detachedCriteria.add( Restrictions.like( "uga.access", "r%" ) );
-        detachedCriteria.add( Restrictions.eq( "ugm.id", user.getId() ) );
-
-        detachedCriteria.setProjection( Projections.id() );
-
-        disjunction.add( Subqueries.exists( detachedCriteria ) );
-
-        return disjunction;
-    }
-
     protected final Criteria getSharingCriteria()
     {
         return getSharingCriteria( currentUserService.getCurrentUser() );
@@ -217,7 +190,7 @@
 
     protected final Criteria getSharingCriteria( User user )
     {
-        Criteria criteria = getCriteria();
+        Criteria criteria = sessionFactory.getCurrentSession().createCriteria( getClazz(), "c" ).setCacheable( false );
 
         if ( !sharingEnabled() )
         {
@@ -226,7 +199,26 @@
 
         Assert.notNull( user, "User argument can't be null." );
 
-        criteria.add( getSharingDisjunction( user ) );
+        Disjunction disjunction = Restrictions.disjunction();
+
+        disjunction.add( Restrictions.like( "c.publicAccess", "r%" ) );
+        disjunction.add( Restrictions.isNull( "c.user.id" ) );
+        disjunction.add( Restrictions.eq( "c.user.id", user.getId() ) );
+
+        DetachedCriteria detachedCriteria = DetachedCriteria.forClass( getClazz(), "dc" );
+        detachedCriteria.createCriteria( "dc.userGroupAccesses", "uga" );
+        detachedCriteria.createCriteria( "uga.userGroup", "ug" );
+        detachedCriteria.createCriteria( "ug.members", "ugm" );
+
+        detachedCriteria.add( Restrictions.eqProperty( "dc.id", "c.id" ) );
+        detachedCriteria.add( Restrictions.eq( "ugm.id", user.getId() ) );
+        detachedCriteria.add( Restrictions.like( "uga.access", "r%" ) );
+
+        detachedCriteria.setProjection( Property.forName( "uga.id" ) );
+
+        disjunction.add( Subqueries.exists( detachedCriteria ) );
+
+        criteria.add( disjunction );
 
         return criteria;
     }
@@ -464,10 +456,11 @@
     @Override
     public int getCount()
     {
-        // return getSharingCriteria().list().size();
+        /*
+        return ((Number) getSharingCriteria().setProjection( Projections.countDistinct( "id" ) ).uniqueResult()).intValue();
+        */
 
         Query query = sharingEnabled() ? getQueryCountAcl() : getQueryCount();
-
         return ((Long) query.uniqueResult()).intValue();
     }