← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 17160: Category/CategoryComboStore, migrated queries to the sharing criteria solution

 

------------------------------------------------------------
revno: 17160
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2014-10-17 19:55:45 +0200
message:
  Category/CategoryComboStore, migrated queries to the sharing criteria solution
modified:
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/hibernate/HibernateCategoryComboStore.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/hibernate/HibernateCategoryStore.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/dataelement/hibernate/HibernateCategoryComboStore.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/hibernate/HibernateCategoryComboStore.java	2014-10-16 06:17:19 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/hibernate/HibernateCategoryComboStore.java	2014-10-17 17:55:45 +0000
@@ -30,6 +30,7 @@
 
 import java.util.Collection;
 
+import org.hibernate.criterion.Restrictions;
 import org.hisp.dhis.common.hibernate.HibernateIdentifiableObjectStore;
 import org.hisp.dhis.dataelement.CategoryComboStore;
 import org.hisp.dhis.dataelement.DataElementCategoryCombo;
@@ -45,7 +46,6 @@
     @SuppressWarnings("unchecked")
     public Collection<DataElementCategoryCombo> getCategoryCombosByDimensionType( String dimensionType )
     {
-        return getQueryWithSelect( "dimensionType = :dimensionType or name = :name" ).
-            setString( "dimensionType", dimensionType).setString( "name", "default" ).list();
+        return getSharingCriteria( Restrictions.or( Restrictions.eq( "dimensionType", dimensionType ), Restrictions.eq( "name", "default" ) ) ).list();
     }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/hibernate/HibernateCategoryStore.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/hibernate/HibernateCategoryStore.java	2014-10-16 06:17:19 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/hibernate/HibernateCategoryStore.java	2014-10-17 17:55:45 +0000
@@ -30,6 +30,7 @@
 
 import java.util.Collection;
 
+import org.hibernate.criterion.Restrictions;
 import org.hisp.dhis.common.hibernate.HibernateDimensionalObjectStore;
 import org.hisp.dhis.dataelement.CategoryStore;
 import org.hisp.dhis.dataelement.DataElementCategory;
@@ -45,6 +46,6 @@
     @SuppressWarnings("unchecked")
     public Collection<DataElementCategory> getCategoriesByDimensionType( String dimensionType )
     {
-        return getQueryWithSelect( "dataDimensionType = :dimensionType" ).setString( "dimensionType", dimensionType ).list();
+        return getSharingCriteria( Restrictions.eq( "dataDimensionType", dimensionType ) ).list();
     }
 }

=== 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-10-16 06:17:19 +0000
+++ dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateGenericStore.java	2014-10-17 17:55:45 +0000
@@ -249,6 +249,25 @@
     }
 
     /**
+     * Creates a sharing Criteria for the implementation Class type restricted by the
+     * given Criterions.
+     *
+     * @param expressions the Criterions for the Criteria.
+     * @return a Criteria instance.
+     */
+    protected final Criteria getSharingCriteria( Criterion... expressions )
+    {
+        Criteria criteria = getSharingCriteria();
+
+        for ( Criterion expression : expressions )
+        {
+            criteria.add( expression );
+        }
+
+        criteria.setCacheable( cacheable );
+        return criteria;
+    }
+    /**
      * Retrieves an object based on the given Criterions.
      *
      * @param expressions the Criterions for the Criteria.
@@ -390,44 +409,6 @@
         return getSharingCriteria().list();
     }
 
-    /**
-     * Returns a Query instance. Allows for injecting a criteria part, such as
-     * "code = :code and name = :name". Note that the bound values must be set
-     * on the query before executing it.
-     *
-     * @param hqlCriteria the HQL criteria.
-     * @return a Query.
-     */
-    protected Query getQueryWithSelect( String hqlCriteria )
-    {
-        boolean sharingEnabled = sharingEnabled();
-
-        String hql = "select distinct c from " + clazz.getName() + " c";
-
-        if ( hqlCriteria != null )
-        {
-            hql += " where " + hqlCriteria;
-        }
-
-        if ( sharingEnabled )
-        {
-            String criteria = hqlCriteria != null ? "and" : "where";
-
-            hql += " " + criteria + " ( 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 );
-
-        if ( sharingEnabled )
-        {
-            query.setEntity( "user", currentUserService.getCurrentUser() );
-        }
-
-        return query;
-    }
-
     @Override
     public int getCount()
     {