← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 17807: Added class USerQueyParams. More convenient to work with compared to a large number of fixed para...

 

------------------------------------------------------------
revno: 17807
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Sat 2014-12-27 16:11:19 +0100
message:
  Added class USerQueyParams. More convenient to work with compared to a large number of fixed parameters.
added:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserQueryParams.java
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserService.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserStore.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/DefaultUserService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/hibernate/HibernateUserStore.java
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/user/UserServiceTest.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
=== added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserQueryParams.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserQueryParams.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserQueryParams.java	2014-12-27 15:11:19 +0000
@@ -0,0 +1,171 @@
+package org.hisp.dhis.user;
+
+/*
+ * Copyright (c) 2004-2014, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * Neither the name of the HISP project nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import java.util.Date;
+
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+
+/**
+ * @author Lars Helge Overland
+ */
+public class UserQueryParams
+{
+    private String searchKey;
+    
+    private User user;
+    
+    private boolean canManage;
+    
+    private boolean authSubset;
+    
+    private Date inactiveSince;
+    
+    private Integer inactiveMonths;
+    
+    private boolean selfRegistered;
+    
+    private OrganisationUnit organisationUnit;
+    
+    private Integer first;
+    
+    private Integer max;
+
+    // -------------------------------------------------------------------------
+    // Constructors
+    // -------------------------------------------------------------------------
+
+    public UserQueryParams()
+    {
+    }
+
+    // -------------------------------------------------------------------------
+    // Getters and setters
+    // -------------------------------------------------------------------------
+
+    public String getSearchKey()
+    {
+        return searchKey;
+    }
+
+    public void setSearchKey( String searchKey )
+    {
+        this.searchKey = searchKey;
+    }
+
+    public User getUser()
+    {
+        return user;
+    }
+
+    public void setUser( User user )
+    {
+        this.user = user;
+    }
+
+    public boolean isCanManage()
+    {
+        return canManage;
+    }
+
+    public void setCanManage( boolean canManage )
+    {
+        this.canManage = canManage;
+    }
+
+    public boolean isAuthSubset()
+    {
+        return authSubset;
+    }
+
+    public void setAuthSubset( boolean authSubset )
+    {
+        this.authSubset = authSubset;
+    }
+
+    public Date getInactiveSince()
+    {
+        return inactiveSince;
+    }
+
+    public void setInactiveSince( Date inactiveSince )
+    {
+        this.inactiveSince = inactiveSince;
+    }
+
+    public Integer getInactiveMonths()
+    {
+        return inactiveMonths;
+    }
+
+    public void setInactiveMonths( Integer inactiveMonths )
+    {
+        this.inactiveMonths = inactiveMonths;
+    }
+
+    public boolean isSelfRegistered()
+    {
+        return selfRegistered;
+    }
+
+    public void setSelfRegistered( boolean selfRegistered )
+    {
+        this.selfRegistered = selfRegistered;
+    }
+
+    public OrganisationUnit getOrganisationUnit()
+    {
+        return organisationUnit;
+    }
+
+    public void setOrganisationUnit( OrganisationUnit organisationUnit )
+    {
+        this.organisationUnit = organisationUnit;
+    }
+
+    public Integer getFirst()
+    {
+        return first;
+    }
+
+    public void setFirst( Integer first )
+    {
+        this.first = first;
+    }
+
+    public Integer getMax()
+    {
+        return max;
+    }
+
+    public void setMax( Integer max )
+    {
+        this.max = max;
+    }    
+}

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserService.java	2014-12-27 12:32:35 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserService.java	2014-12-27 15:11:19 +0000
@@ -173,26 +173,12 @@
     Collection<User> getManagedUsersBetween( User user, int first, int max );
 
     /**
-     * Returns all users which are managed by the given user through its managed
-     * groups association.
+     * Returns a list of users based on the given query parameters.
      * 
-     * @param searchKey the string to search by first name, surname and user name, 
-     *        no search if null.
-     * @param user the user.
-     * @param constrainManagedGroups constrain the result to users within managed groups.
-     * @param constrainAuthSubset constrain the result to users with a subset of
-     *        authorities.
-     * @param inactiveMonths number of months since user last logged in, null if none.
-     * @param selfRegistered constrain the result to self-registered users.
-     * @param organisationUnit constrain the result to users associated with the
-     *        organisation unit.
-     * @param first the first record to return, null if 0.
-     * @param max the max number of records to return, null if none.
+     * @param params the user query parameters.
      * @return a List of users.
      */
-    Collection<User> getManagedUsersBetween( String searchKey, User user, 
-        boolean constrainManagedGroups, boolean constrainAuthSubset, 
-        Integer inactiveMonths, boolean selfRegistered, OrganisationUnit organisationUnit, Integer first, Integer max );
+    Collection<User> getUsers( UserQueryParams params );
     
     /**
      * Tests whether the current user is allowed to create a user associated

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserStore.java	2014-12-27 12:32:35 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserStore.java	2014-12-27 15:11:19 +0000
@@ -29,11 +29,9 @@
  */
 
 import java.util.Collection;
-import java.util.Date;
 import java.util.List;
 
 import org.hisp.dhis.common.GenericIdentifiableObjectStore;
-import org.hisp.dhis.organisationunit.OrganisationUnit;
 
 /**
  * @author Nguyen Hong Duc
@@ -61,24 +59,10 @@
     Collection<User> getUsersByName( String name );
 
     /**
-     * Returns all users which are managed by the given user through its managed
-     * groups association.
+     * Returns a list of users based on the given query parameters.
      * 
-     * @param searchKey the string to search by first name, surname and user name, 
-     *        no search if null.
-     * @param user the user.
-     * @param constrainManagedGroups constrain the result to users within managed groups.
-     * @param constrainAuthSubset constrain the result to users with a subset of
-     *        authorities.
-     * @param inactiveSince date for last login.
-     * @param selfRegistered constrain the result to self-registered users.
-     * @param organisationUnit constrain the result to users associated with the
-     *        organisation unit.
-     * @param first the first record to return, null if 0.
-     * @param max the max number of records to return, null if none.
+     * @param params the user query parameters.
      * @return a List of users.
      */
-    List<User> getManagedUsersBetween( String searchKey, User user, 
-        boolean constrainManagedGroups, boolean constrainAuthSubset, 
-        Date inactiveSince, boolean selfRegistered, OrganisationUnit organisationUnit, Integer first, Integer max );
+    List<User> getUsers( UserQueryParams params );
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/DefaultUserService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/DefaultUserService.java	2014-12-27 12:32:35 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/DefaultUserService.java	2014-12-27 15:11:19 +0000
@@ -206,36 +206,44 @@
     @Override
     public Collection<User> getManagedUsers( User user )
     {
-        return userStore.getManagedUsersBetween( null, user, true, true, null, false, null, null, null );
+        UserQueryParams params = new UserQueryParams();
+        params.setUser( user );
+        params.setCanManage( true );
+        params.setAuthSubset( true );
+        
+        return userStore.getUsers( params );
     }
 
     @Override
     public Collection<User> getManagedUsersBetween( User user, int first, int max )
     {
-        return userStore.getManagedUsersBetween( null, user, true, true, null, false, null, first, max );
+        UserQueryParams params = new UserQueryParams();
+        params.setUser( user );
+        params.setCanManage( true );
+        params.setAuthSubset( true );
+        params.setFirst( first );
+        params.setMax( max );
+        
+        return userStore.getUsers( params );
     }
 
     @Override
-    public Collection<User> getManagedUsersBetween( String searchKey, User user, 
-        boolean constrainManagedGroups, boolean constrainAuthSubset, 
-        Integer inactiveMonths, boolean selfRegistered, OrganisationUnit organisationUnit, Integer first, Integer max )
+    public Collection<User> getUsers( UserQueryParams params )
     {
-        Date inactiveSince = null;
-        
-        if ( inactiveMonths != null )
+        if ( params.getInactiveMonths() != null )
         {
             Calendar cal = PeriodType.createCalendarInstance();
-            cal.add( Calendar.MONTH, ( inactiveMonths * -1 ) );
-            inactiveSince = cal.getTime();
+            cal.add( Calendar.MONTH, ( params.getInactiveMonths() * -1 ) );
+            params.setInactiveSince( cal.getTime() );
         }
         
-        if ( user != null && user.isSuper() )
+        if ( params.getUser() != null && params.getUser().isSuper() )
         {
-            return userStore.getManagedUsersBetween( searchKey, user, false, false, inactiveSince, selfRegistered, organisationUnit, first, max );
+            params.setCanManage( false );
+            params.setAuthSubset( false );
         }
         
-        return userStore.getManagedUsersBetween( searchKey, user, 
-            constrainManagedGroups, constrainAuthSubset, inactiveSince, selfRegistered, organisationUnit, first, max );
+        return userStore.getUsers( params );
     }
 
     @Override

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/hibernate/HibernateUserStore.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/hibernate/HibernateUserStore.java	2014-12-27 12:32:35 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/hibernate/HibernateUserStore.java	2014-12-27 15:11:19 +0000
@@ -29,7 +29,6 @@
  */
 
 import java.util.Collection;
-import java.util.Date;
 import java.util.List;
 import java.util.Set;
 
@@ -39,9 +38,9 @@
 import org.hibernate.criterion.Restrictions;
 import org.hisp.dhis.common.IdentifiableObjectUtils;
 import org.hisp.dhis.common.hibernate.HibernateIdentifiableObjectStore;
-import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.system.util.SqlHelper;
 import org.hisp.dhis.user.User;
+import org.hisp.dhis.user.UserQueryParams;
 import org.hisp.dhis.user.UserStore;
 
 /**
@@ -105,9 +104,7 @@
     
     @Override
     @SuppressWarnings("unchecked")
-    public List<User> getManagedUsersBetween( String searchKey, User user, 
-        boolean constrainManagedGroups, boolean constrainAuthSubset, 
-        Date inactiveSince, boolean selfRegistered, OrganisationUnit organisationUnit, Integer first, Integer max )
+    public List<User> getUsers( UserQueryParams params )
     {
         SqlHelper hlp = new SqlHelper();
         
@@ -116,7 +113,7 @@
             "inner join u.userCredentials uc " +
             "left join u.groups g ";
 
-        if ( searchKey != null )
+        if ( params.getSearchKey() != null )
         {
             hql += hlp.whereAnd() + " (" +
                 "lower(u.firstName) like :key " +
@@ -124,12 +121,12 @@
                 "or lower(uc.username) like :key) ";
         }
         
-        if ( constrainManagedGroups )
+        if ( params.isCanManage() )
         {
             hql += hlp.whereAnd() + " g.id in (:ids) ";
         }
         
-        if ( constrainAuthSubset )
+        if ( params.isAuthSubset() )
         {
             hql += hlp.whereAnd() + " not exists (" +
                 "select uc2 from UserCredentials uc2 " +
@@ -141,17 +138,17 @@
         
         //TODO constrain by own user roles
 
-        if ( inactiveSince != null )
+        if ( params.getInactiveSince() != null )
         {
             hql += hlp.whereAnd() + " uc.lastLogin < :inactiveSince ";
         }
         
-        if ( selfRegistered )
+        if ( params.isSelfRegistered() )
         {
             hql += hlp.whereAnd() + " uc.selfRegistered = true ";
         }
         
-        if ( organisationUnit != null )
+        if ( params.getOrganisationUnit() != null )
         {
             hql += hlp.whereAnd() + " :organisationUnit in elements(u.organisationUnits) ";
         }
@@ -160,43 +157,43 @@
         
         Query query = sessionFactory.getCurrentSession().createQuery( hql );
         
-        if ( searchKey != null )
+        if ( params.getSearchKey() != null )
         {
-            query.setString( "key", "%" + searchKey.toLowerCase() + "%" );
+            query.setString( "key", "%" + params.getSearchKey().toLowerCase() + "%" );
         }
         
-        if ( constrainManagedGroups )
+        if ( params.isCanManage() && params.getUser() != null )
         {
-            Collection<Integer> managedGroups = IdentifiableObjectUtils.getIdentifiers( user.getManagedGroups() );
+            Collection<Integer> managedGroups = IdentifiableObjectUtils.getIdentifiers( params.getUser().getManagedGroups() );
 
             query.setParameterList( "ids", managedGroups );
         }
         
-        if ( constrainAuthSubset )
+        if ( params.isAuthSubset() && params.getUser() != null )
         {
-            Set<String> auths = user.getUserCredentials().getAllAuthorities();
+            Set<String> auths = params.getUser().getUserCredentials().getAllAuthorities();
             
             query.setParameterList( "auths", auths );
         }
         
-        if ( inactiveSince != null )
-        {
-            query.setDate( "inactiveSince", inactiveSince );
-        }
-        
-        if ( organisationUnit != null )
-        {
-            query.setEntity( "organisationUnit", organisationUnit );
-        }
-        
-        if ( first != null )
-        {
-            query.setFirstResult( first );
-        }
-        
-        if ( max != null )
-        {
-            query.setMaxResults( max ).list();
+        if ( params.getInactiveSince() != null )
+        {
+            query.setDate( "inactiveSince", params.getInactiveSince() );
+        }
+        
+        if ( params.getOrganisationUnit() != null )
+        {
+            query.setEntity( "organisationUnit", params.getOrganisationUnit() );
+        }
+        
+        if ( params.getFirst() != null )
+        {
+            query.setFirstResult( params.getFirst() );
+        }
+        
+        if ( params.getMax() != null )
+        {
+            query.setMaxResults( params.getMax() ).list();
         }
         
         return query.list();

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/user/UserServiceTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/user/UserServiceTest.java	2014-12-27 12:32:35 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/user/UserServiceTest.java	2014-12-27 15:11:19 +0000
@@ -394,8 +394,11 @@
         userService.addUserCredentials( credentialsD );
         userService.addUserCredentials( credentialsE );
         userService.addUserCredentials( credentialsF );
+
+        UserQueryParams params = new UserQueryParams();
+        params.setSearchKey( "rstnameA" );
         
-        Collection<User> users = userService.getManagedUsersBetween( "rstnameA", null, false, false, null, false, null, null, null );
+        Collection<User> users = userService.getUsers( params );
         
         assertEquals( 1, users.size() );
         assertTrue( users.contains( userA ) );
@@ -427,7 +430,10 @@
         userService.addUserCredentials( credentialsC );
         userService.addUserCredentials( credentialsD );
         
-        Collection<User> users = userService.getManagedUsersBetween( null, null, false, false, null, true, null, null, null );
+        UserQueryParams params = new UserQueryParams();
+        params.setSelfRegistered( true );
+        
+        Collection<User> users = userService.getUsers( params );
         
         assertEquals( 2, users.size() );
         assertTrue( users.contains( userA ) );
@@ -462,8 +468,11 @@
         userService.addUserCredentials( credentialsB );
         userService.addUserCredentials( credentialsC );
         userService.addUserCredentials( credentialsD );
+
+        UserQueryParams params = new UserQueryParams();
+        params.setOrganisationUnit( unit1 );
         
-        Collection<User> users = userService.getManagedUsersBetween( null, null, false, false, null, false, unit1, null, null );
+        Collection<User> users = userService.getUsers( params );
         
         assertEquals( 2, users.size() );
         assertTrue( users.contains( userA ) );