← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 9702: extended HibernateGenericStore: Added sharing based get by name, get by shortname (returns collec...

 

------------------------------------------------------------
revno: 9702
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2013-02-04 10:14:27 +0700
message:
  extended HibernateGenericStore: Added sharing based get by name, get by shortname (returns collection), also added non-sharing versions of get count by name, get count by shortname (will be used when name uniqueness is removed from the system)
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/GenericNameableObjectStore.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	2013-01-16 13:50:06 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/GenericIdentifiableObjectStore.java	2013-02-04 03:14:27 +0000
@@ -80,6 +80,25 @@
     Collection<T> getAllOrderedName( int first, int max );
 
     /**
+     * Retrieves a Collection of objects where the name is equal the given name.
+     *
+     * @param name the name.
+     * @return a Collection of objects.
+     */
+    Collection<T> getAllEqName( String name );
+
+    /**
+     * Return the number of objects where the name is equal the given name.
+     *
+     * This count is _unfiltered_ (no ACL!), so this is not the same as
+     * getAllEqName().size().
+     *
+     * @param name the name.
+     * @return Count of objects.
+     */
+    int getCountEqNameNoAcl( String name );
+
+    /**
      * Retrieves a Collection of objects where the name is like the given name.
      *
      * @param name the name.

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/GenericNameableObjectStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/GenericNameableObjectStore.java	2013-01-16 13:50:06 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/GenericNameableObjectStore.java	2013-02-04 03:14:27 +0000
@@ -27,6 +27,8 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import java.util.Collection;
+
 /**
  * @author Lars Helge Overland
  * @version $Id$
@@ -41,4 +43,23 @@
      * @return the object with the given short name.
      */
     T getByShortName( String shortName );
+
+    /**
+     * Return the number of objects where the name is equal the given name.
+     * <p/>
+     * This count is _unfiltered_ (no ACL!), so this is not the same as
+     * getAllEqShortName().size().
+     *
+     * @param shortName the name.
+     * @return Count of objects.
+     */
+    int getCountEqShortNameNoAcl( String shortName );
+
+    /**
+     * Retrieves a Collection of objects where the name is like the given name.
+     *
+     * @param shortName the name.
+     * @return a Collection of objects.
+     */
+    Collection<T> getAllEqShortName( String shortName );
 }

=== 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	2013-01-22 12:09:12 +0000
+++ dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateGenericStore.java	2013-02-04 03:14:27 +0000
@@ -305,6 +305,7 @@
     }
 
     @Override
+    @Deprecated
     public final T getByName( String name )
     {
         T object = getObject( Restrictions.eq( "name", name ) );
@@ -320,6 +321,7 @@
     }
 
     @Override
+    @Deprecated
     public final T getByShortName( String shortName )
     {
         T object = getObject( Restrictions.eq( "shortName", shortName ) );
@@ -390,7 +392,69 @@
     }
 
     @Override
-    @SuppressWarnings("unchecked")
+    public Collection<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;
+    }
+
+    @Override
+    public Collection<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;
+    }
+
+    @Override
+    @SuppressWarnings( "unchecked" )
     public Collection<T> getAllLikeName( String name )
     {
         Query query = sharingEnabled() ? getQueryAllLikeNameACL( name ) : getQueryAllLikeName( name );
@@ -415,7 +479,7 @@
 
     private Query getQueryAllLikeName( String name )
     {
-        Query query = getQuery( "from " + clazz.getName() + " c where lower(name) like :name" );
+        Query query = getQuery( "from " + clazz.getName() + " c where lower(name) like :name order by c.name" );
         query.setString( "name", "%" + name.toLowerCase() + "%" );
 
         return query;
@@ -446,7 +510,7 @@
 
     private Query getQueryAllOrderedName()
     {
-        return getQuery( "from " + clazz.getName() + " c order by name" );
+        return getQuery( "from " + clazz.getName() + " c order by c.name" );
     }
 
     @Override
@@ -800,6 +864,28 @@
     }
 
     //----------------------------------------------------------------------------------------------------------------
+    // No ACL (unfiltered methods)
+    //----------------------------------------------------------------------------------------------------------------
+
+    @Override
+    public int getCountEqNameNoAcl( String name )
+    {
+        Query query = getQuery( "select count(distinct c) from " + clazz.getName() + " c where c.name = :name" );
+        query.setParameter( "name", name );
+
+        return ((Long) query.uniqueResult()).intValue();
+    }
+
+    @Override
+    public int getCountEqShortNameNoAcl( String shortName )
+    {
+        Query query = getQuery( "select count(distinct c) from " + clazz.getName() + " c where c.shortName = :shortName" );
+        query.setParameter( "shortName", shortName );
+
+        return ((Long) query.uniqueResult()).intValue();
+    }
+
+    //----------------------------------------------------------------------------------------------------------------
     // Helpers
     //----------------------------------------------------------------------------------------------------------------