dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #31157
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 15847: Generic store, added method for getting a query instance which respects sharing.
------------------------------------------------------------
revno: 15847
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2014-06-25 14:34:42 +0200
message:
Generic store, added method for getting a query instance which respects sharing.
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-03-18 08:10:10 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/hibernate/HibernateCategoryComboStore.java 2014-06-25 12:34:42 +0000
@@ -30,7 +30,6 @@
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,8 +44,7 @@
@SuppressWarnings("unchecked")
public Collection<DataElementCategoryCombo> getCategoryCombosByDimensionType( String dimensionType )
{
- return getCriteria( Restrictions.or(
- Restrictions.eq( "dimensionType", dimensionType ),
- Restrictions.eq( "name", "default" ) ) ).list();
+ return getQueryWithSelect( "dimensionType = :dimensionType or name = :name" ).
+ setString( "dimensionType", dimensionType).setString( "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-03-26 18:56:37 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/hibernate/HibernateCategoryStore.java 2014-06-25 12:34:42 +0000
@@ -30,7 +30,6 @@
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 +44,6 @@
@SuppressWarnings("unchecked")
public Collection<DataElementCategory> getCategoriesByDimensionType( String dimensionType )
{
- return getCriteria( Restrictions.eq( "dataDimensionType", dimensionType ) ).list();
+ return getQueryWithSelect( "dataDimensionType = :dimensionType" ).setString( "dimensionType", 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-03-27 10:14:49 +0000
+++ dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateGenericStore.java 2014-06-25 12:34:42 +0000
@@ -358,6 +358,44 @@
return query;
}
+ /**
+ * 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;
+ }
+
private Query getQueryAll()
{
return getQuery( "from " + clazz.getName() + " c" );