← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 14203: re-purposed searchUsersByName to search for users by username, firstName, surname. Support search...

 

------------------------------------------------------------
revno: 14203
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2014-03-13 11:55:13 +0100
message:
  re-purposed searchUsersByName to search for users by username, firstName, surname. Support search by username, firstName, surname in user list.
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserCredentialsStore.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserService.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/HibernateUserCredentialsStore.java
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-user/src/main/java/org/hisp/dhis/user/action/GetUserListAction.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/user/UserCredentialsStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserCredentialsStore.java	2014-03-05 05:52:03 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserCredentialsStore.java	2014-03-13 10:55:13 +0000
@@ -28,25 +28,25 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+
 import java.util.Collection;
 import java.util.Date;
 
-import org.hisp.dhis.organisationunit.OrganisationUnit;
-
 /**
  * @author Lars Helge Overland
  */
 public interface UserCredentialsStore
 {
     String ID = UserCredentialsStore.class.getName();
-    
+
     // -------------------------------------------------------------------------
     // UserCredentials
     // -------------------------------------------------------------------------
 
     /**
      * Adds a UserCredentials.
-     * 
+     *
      * @param userCredentials the UserCredentials to add.
      * @return the User which the UserCredentials is associated with.
      */
@@ -54,14 +54,14 @@
 
     /**
      * Updates a UserCredentials.
-     * 
+     *
      * @param userCredentials the UserCredentials to update.
      */
     void updateUserCredentials( UserCredentials userCredentials );
 
     /**
      * Retrieves the UserCredentials of the given User.
-     * 
+     *
      * @param user the User.
      * @return the UserCredentials.
      */
@@ -70,7 +70,7 @@
     /**
      * Retrieves the UserCredentials associated with the User with the given
      * name.
-     * 
+     *
      * @param username the name of the User.
      * @return the UserCredentials.
      */
@@ -78,20 +78,22 @@
 
     /**
      * Retrieves all UserCredentials.
-     * 
+     *
      * @return a Collection of UserCredentials.
      */
     Collection<UserCredentials> getAllUserCredentials();
 
     /**
      * Deletes a UserCredentials.
-     * 
+     *
      * @param userCredentials the UserCredentials.
      */
     void deleteUserCredentials( UserCredentials userCredentials );
 
     Collection<UserCredentials> searchUsersByName( String key );
 
+    Collection<UserCredentials> searchUsersByName( String key, int first, int max );
+
     Collection<UserCredentials> getUsersBetween( int first, int max );
 
     Collection<UserCredentials> getUsersBetweenByName( String name, int first, int max );
@@ -106,11 +108,11 @@
         int first, int max );
 
     Collection<UserCredentials> getSelfRegisteredUserCredentials( int first, int max );
-    
+
     int getSelfRegisteredUserCredentialsCount();
-    
+
     Collection<UserCredentials> getInactiveUsers( Date date );
-    
+
     Collection<UserCredentials> getInactiveUsers( Date date, int first, int max );
 
     int getInactiveUsersCount( Date date );
@@ -135,14 +137,14 @@
 
     /**
      * Adds a UserSetting.
-     * 
+     *
      * @param userSetting the UserSetting to add.
      */
     void addUserSetting( UserSetting userSetting );
 
     /**
      * Updates a UserSetting.
-     * 
+     *
      * @param userSetting the UserSetting to update.
      */
     void updateUserSetting( UserSetting userSetting );
@@ -150,7 +152,7 @@
     /**
      * Retrieves the UserSetting associated with the given User for the given
      * UserSetting name.
-     * 
+     *
      * @param user the User.
      * @param name the name of the UserSetting.
      * @return the UserSetting.
@@ -159,7 +161,7 @@
 
     /**
      * Retrieves all UserSettings for the given User.
-     * 
+     *
      * @param user the User.
      * @return a Collection of UserSettings.
      */
@@ -167,19 +169,19 @@
 
     /**
      * Deletes a UserSetting.
-     * 
+     *
      * @param userSetting the UserSetting to delete.
      */
     void deleteUserSetting( UserSetting userSetting );
-    
+
     /**
      * Returns all UserSettings with the given name.
-     * 
+     *
      * @param name the name.
      * @return a Collection of UserSettings.
      */
     Collection<UserSetting> getUserSettings( String name );
-    
+
     Collection<String> getUsernames( String key, Integer max );
 
     UserCredentials getUserCredentialsByOpenID( String openId );

=== 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-03-05 05:52:03 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserService.java	2014-03-13 10:55:13 +0000
@@ -96,7 +96,7 @@
     Collection<User> getAllUsers();
 
     List<User> getAllUsersBetween( int first, int max );
-    
+
     List<User> getAllUsersBetweenByName( String name, int first, int max );
 
     Collection<User> getUsersByLastUpdated( Date lastUpdated );
@@ -113,7 +113,6 @@
      * Returns a Collection of the Users which are associated with OrganisationUnits.
      *
      * @param units a Collection of the organization units.
-     *
      * @return a Collection of Users.
      */
     Collection<User> getUsersByOrganisationUnits( Collection<OrganisationUnit> units );
@@ -144,11 +143,11 @@
     int getUsersByOrganisationUnitCount( OrganisationUnit orgUnit );
 
     int getUsersByOrganisationUnitCountByName( OrganisationUnit orgUnit, String name );
-    
+
     List<User> getUsersByUid( List<String> uids );
-    
+
     User searchForUser( String query );
-    
+
     List<User> queryForUsers( String query );
 
     // -------------------------------------------------------------------------
@@ -221,6 +220,8 @@
 
     Collection<UserCredentials> searchUsersByName( String key );
 
+    Collection<UserCredentials> searchUsersByName( String name, int first, int max );
+
     Collection<UserCredentials> getUsersBetween( int first, int max );
 
     Collection<UserCredentials> getUsersBetweenByName( String name, int first, int max );
@@ -234,9 +235,9 @@
     Collection<UserCredentials> getUsersByOrganisationUnitBetweenByName( OrganisationUnit orgUnit, String name, int first, int max );
 
     Collection<UserCredentials> getSelfRegisteredUserCredentials( int first, int max );
-    
+
     int getSelfRegisteredUserCredentialsCount();
-    
+
     Collection<UserCredentials> getInactiveUsers( int months );
 
     Collection<UserCredentials> getInactiveUsers( int months, int first, int max );
@@ -244,7 +245,7 @@
     int getInactiveUsersCount( int months );
 
     int getActiveUsersCount( int days );
-    
+
     int getActiveUsersCount( Date since );
 
     // -------------------------------------------------------------------------
@@ -338,11 +339,11 @@
     /**
      * If a matching UserSetting exists, based on its user and name, it will be
      * updated, if not, the given UserSetting will be added.
-     * 
+     *
      * @param userSetting the UserSetting.
      */
     void addOrUpdateUserSetting( UserSetting userSetting );
-    
+
     /**
      * Updates a UserSetting.
      *
@@ -364,14 +365,14 @@
      * Retrieves a user setting value for the given user and setting name. Returns
      * the given default value if the setting does not exist or the setting value
      * is null.
-     * 
-     * @param user the user.
-     * @param name the setting name.
+     *
+     * @param user         the user.
+     * @param name         the setting name.
      * @param defaultValue the default value.
      * @return a setting value.
      */
     Serializable getUserSettingValue( User user, String name, Serializable defaultValue );
-    
+
     /**
      * Retrieves all UserSettings for the given User.
      *
@@ -379,7 +380,7 @@
      * @return a Collection of UserSettings.
      */
     Collection<UserSetting> getAllUserSettings( User user );
-    
+
     Collection<UserSetting> getUserSettings( String name );
 
     /**
@@ -401,13 +402,13 @@
 
     /**
      * Removes all user settings associated with the given user.
-     * 
+     *
      * @param user the user.
      */
     void removeUserSettings( User user );
-    
-    Collection<User> getUsersByName( String name );  
-    
+
+    Collection<User> getUsersByName( String name );
+
     Collection<String> getUsernames( String query, Integer max );
 
     int countDataSetUserAuthorityGroups( DataSet dataSet );

=== 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-03-05 05:52:03 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/DefaultUserService.java	2014-03-13 10:55:13 +0000
@@ -464,9 +464,14 @@
         return userCredentialsStore.getUsersWithoutOrganisationUnitBetweenByName( username, first, max );
     }
 
-    public Collection<UserCredentials> searchUsersByName( String username )
-    {
-        return userCredentialsStore.searchUsersByName( username );
+    public Collection<UserCredentials> searchUsersByName( String name )
+    {
+        return userCredentialsStore.searchUsersByName( name );
+    }
+
+    public Collection<UserCredentials> searchUsersByName( String name, int first, int max )
+    {
+        return userCredentialsStore.searchUsersByName( name, first, max );
     }
 
     public void setLastLogin( String username )

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/hibernate/HibernateUserCredentialsStore.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/hibernate/HibernateUserCredentialsStore.java	2014-03-05 05:52:03 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/hibernate/HibernateUserCredentialsStore.java	2014-03-13 10:55:13 +0000
@@ -32,6 +32,7 @@
 import org.hibernate.Query;
 import org.hibernate.Session;
 import org.hibernate.SessionFactory;
+import org.hibernate.criterion.Disjunction;
 import org.hibernate.criterion.Order;
 import org.hibernate.criterion.Projections;
 import org.hibernate.criterion.Restrictions;
@@ -162,15 +163,43 @@
         return rs != null ? rs.intValue() : 0;
     }
 
-    @SuppressWarnings("unchecked")
+    @SuppressWarnings( "unchecked" )
     public Collection<UserCredentials> searchUsersByName( String key )
     {
         Session session = sessionFactory.getCurrentSession();
 
         Criteria criteria = session.createCriteria( UserCredentials.class );
-
-        criteria.add( Restrictions.ilike( "username", "%" + key + "%" ) );
-        criteria.addOrder( Order.asc( "username" ) );
+        criteria.createAlias( "user", "user" );
+
+        Disjunction disjunction = Restrictions.disjunction();
+        disjunction.add( Restrictions.ilike( "user.surname", "%" + key + "%" ) );
+        disjunction.add( Restrictions.ilike( "user.firstName", "%" + key + "%" ) );
+        disjunction.add( Restrictions.ilike( "username", "%" + key + "%" ) );
+
+        criteria.add( disjunction );
+        criteria.addOrder( Order.asc( "username" ) );
+
+        return criteria.list();
+    }
+
+    @SuppressWarnings("unchecked")
+    public Collection<UserCredentials> searchUsersByName( String key, int first, int max )
+    {
+        Session session = sessionFactory.getCurrentSession();
+
+        Criteria criteria = session.createCriteria( UserCredentials.class );
+        criteria.createAlias( "user", "user" );
+
+        Disjunction disjunction = Restrictions.disjunction();
+        disjunction.add( Restrictions.ilike( "user.surname", "%" + key + "%" ) );
+        disjunction.add( Restrictions.ilike( "user.firstName", "%" + key + "%" ) );
+        disjunction.add( Restrictions.ilike( "username", "%" + key + "%" ) );
+
+        criteria.add( disjunction );
+        criteria.addOrder( Order.asc( "username" ) );
+
+        criteria.setFirstResult( first );
+        criteria.setMaxResults( max );
 
         return criteria.list();
     }

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-user/src/main/java/org/hisp/dhis/user/action/GetUserListAction.java'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-user/src/main/java/org/hisp/dhis/user/action/GetUserListAction.java	2013-08-23 16:05:01 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-user/src/main/java/org/hisp/dhis/user/action/GetUserListAction.java	2014-03-13 10:55:13 +0000
@@ -28,12 +28,6 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import static org.apache.commons.lang.StringUtils.isNotBlank;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-
 import org.hisp.dhis.paging.ActionPagingSupport;
 import org.hisp.dhis.system.filter.UserCredentialsCanUpdateFilter;
 import org.hisp.dhis.system.util.FilterUtils;
@@ -43,6 +37,12 @@
 import org.hisp.dhis.user.UserService;
 import org.hisp.dhis.user.comparator.UsernameComparator;
 
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import static org.apache.commons.lang.StringUtils.isNotBlank;
+
 /**
  * @author Torgeir Lorange Ostby
  * @version $Id: GetUserListAction.java 2869 2007-02-20 14:26:09Z andegje $
@@ -133,8 +133,11 @@
         {
             this.paging = createPaging( userService.getUserCountByName( key ) );
 
-            userCredentialsList = new ArrayList<UserCredentials>( userService.getUsersBetweenByName( key, paging
-                .getStartPos(), paging.getPageSize() ) );
+            //userCredentialsList = new ArrayList<UserCredentials>( userService.getUsersBetweenByName( key, paging
+            //    .getStartPos(), paging.getPageSize() ) );
+
+            userCredentialsList = new ArrayList<UserCredentials>( userService.searchUsersByName( key, paging.getStartPos(),
+                paging.getPageSize() ) );
 
             Collections.sort( userCredentialsList, new UsernameComparator() );
         }
@@ -148,7 +151,7 @@
         else if ( Boolean.TRUE.equals( selfRegistered ) )
         {
             this.paging = createPaging( userService.getSelfRegisteredUserCredentialsCount() );
-            
+
             userCredentialsList = new ArrayList<UserCredentials>( userService.getSelfRegisteredUserCredentials( paging.
                 getStartPos(), paging.getPageSize() ) );
         }