← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 16901: Added separate UserSettingStore

 

------------------------------------------------------------
revno: 16901
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2014-10-01 12:19:12 +0200
message:
  Added separate UserSettingStore
added:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserSettingStore.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/hibernate/HibernateUserSettingStore.java
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserCredentialsStore.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-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/user/UserSettingServiceTest.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-10-01 09:50:38 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserCredentialsStore.java	2014-10-01 10:19:12 +0000
@@ -131,57 +131,6 @@
 
     int getUsersByOrganisationUnitCountByName( OrganisationUnit orgUnit, String name );
 
-    // -------------------------------------------------------------------------
-    // UserSettings
-    // -------------------------------------------------------------------------
-
-    /**
-     * 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 );
-
-    /**
-     * 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.
-     */
-    UserSetting getUserSetting( User user, String name );
-
-    /**
-     * Retrieves all UserSettings for the given User.
-     *
-     * @param user the User.
-     * @return a Collection of UserSettings.
-     */
-    Collection<UserSetting> getAllUserSettings( User user );
-
-    /**
-     * 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 );

=== added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserSettingStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserSettingStore.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserSettingStore.java	2014-10-01 10:19:12 +0000
@@ -0,0 +1,53 @@
+package org.hisp.dhis.user;
+
+import java.util.Collection;
+
+public interface UserSettingStore
+{
+    /**
+     * 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 );
+
+    /**
+     * 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.
+     */
+    UserSetting getUserSetting( User user, String name );
+
+    /**
+     * Retrieves all UserSettings for the given User.
+     *
+     * @param user the User.
+     * @return a Collection of UserSettings.
+     */
+    Collection<UserSetting> getAllUserSettings( User user );
+
+    /**
+     * 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 );
+}

=== 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-09-08 13:02:43 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/DefaultUserService.java	2014-10-01 10:19:12 +0000
@@ -93,6 +93,13 @@
     {
         this.userAuthorityGroupStore = userAuthorityGroupStore;
     }
+    
+    private UserSettingStore userSettingStore;
+
+    public void setUserSettingStore( UserSettingStore userSettingStore )
+    {
+        this.userSettingStore = userSettingStore;
+    }
 
     private CurrentUserService currentUserService;
 
@@ -622,7 +629,7 @@
 
     public void addUserSetting( UserSetting userSetting )
     {
-        userCredentialsStore.addUserSetting( userSetting );
+        userSettingStore.addUserSetting( userSetting );
     }
 
     public void addOrUpdateUserSetting( UserSetting userSetting )
@@ -642,27 +649,27 @@
 
     public void updateUserSetting( UserSetting userSetting )
     {
-        userCredentialsStore.updateUserSetting( userSetting );
+        userSettingStore.updateUserSetting( userSetting );
     }
 
     public void deleteUserSetting( UserSetting userSetting )
     {
-        userCredentialsStore.deleteUserSetting( userSetting );
+        userSettingStore.deleteUserSetting( userSetting );
     }
 
     public Collection<UserSetting> getAllUserSettings( User user )
     {
-        return userCredentialsStore.getAllUserSettings( user );
+        return userSettingStore.getAllUserSettings( user );
     }
 
     public Collection<UserSetting> getUserSettings( String name )
     {
-        return userCredentialsStore.getUserSettings( name );
+        return userSettingStore.getUserSettings( name );
     }
 
     public UserSetting getUserSetting( User user, String name )
     {
-        return userCredentialsStore.getUserSetting( user, name );
+        return userSettingStore.getUserSetting( user, name );
     }
 
     public Serializable getUserSettingValue( User user, String name, Serializable defaultValue )
@@ -676,7 +683,7 @@
     {
         Map<User, Serializable> map = new HashMap<>();
 
-        for ( UserSetting setting : userCredentialsStore.getUserSettings( name ) )
+        for ( UserSetting setting : userSettingStore.getUserSettings( name ) )
         {
             map.put( setting.getUser(), setting.getValue() != null ? setting.getValue() : defaultValue );
         }

=== 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-10-01 09:50:38 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/hibernate/HibernateUserCredentialsStore.java	2014-10-01 10:19:12 +0000
@@ -28,6 +28,11 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Date;
+import java.util.List;
+
 import org.hibernate.Criteria;
 import org.hibernate.Query;
 import org.hibernate.Session;
@@ -41,12 +46,6 @@
 import org.hisp.dhis.user.UserCredentials;
 import org.hisp.dhis.user.UserCredentialsStore;
 import org.hisp.dhis.user.UserService;
-import org.hisp.dhis.user.UserSetting;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Date;
-import java.util.List;
 
 /**
  * TODO extend BaseIdentifiableObjectStore
@@ -275,8 +274,7 @@
 
     public Collection<UserCredentials> getUsersWithoutOrganisationUnitBetweenByName( String name, int first, int max )
     {
-        return getBlockUser( findByName( toUserCredentials( userService.getUsersWithoutOrganisationUnit() ), name ),
-            first, max );
+        return getBlockUser( findByName( toUserCredentials( userService.getUsersWithoutOrganisationUnit() ), name ), first, max );
     }
 
     public int getUsersWithoutOrganisationUnitCount()
@@ -354,64 +352,6 @@
         return rs != null ? rs.intValue() : 0;
     }
 
-    // -------------------------------------------------------------------------
-    // UserSettings
-    // -------------------------------------------------------------------------
-
-    public void addUserSetting( UserSetting userSetting )
-    {
-        Session session = sessionFactory.getCurrentSession();
-
-        session.save( userSetting );
-    }
-
-    public void updateUserSetting( UserSetting userSetting )
-    {
-        Session session = sessionFactory.getCurrentSession();
-
-        session.update( userSetting );
-    }
-
-    public UserSetting getUserSetting( User user, String name )
-    {
-        Session session = sessionFactory.getCurrentSession();
-
-        Query query = session.createQuery( "from UserSetting us where us.user = :user and us.name = :name" );
-
-        query.setEntity( "user", user );
-        query.setString( "name", name );
-        query.setCacheable( true );
-
-        return (UserSetting) query.uniqueResult();
-    }
-
-    @SuppressWarnings("unchecked")
-    public Collection<UserSetting> getAllUserSettings( User user )
-    {
-        Session session = sessionFactory.getCurrentSession();
-        Query query = session.createQuery( "from UserSetting us where us.user = :user" );
-        query.setEntity( "user", user );
-
-        return query.list();
-    }
-
-    @SuppressWarnings("unchecked")
-    public Collection<UserSetting> getUserSettings( String name )
-    {
-        Session session = sessionFactory.getCurrentSession();
-        Query query = session.createQuery( "from UserSetting us where us.name = :name" );
-        query.setString( "name", name );
-
-        return query.list();
-    }
-
-    public void deleteUserSetting( UserSetting userSetting )
-    {
-        Session session = sessionFactory.getCurrentSession();
-
-        session.delete( userSetting );
-    }
-
     @SuppressWarnings("unchecked")
     public Collection<String> getUsernames( String key, Integer max )
     {
@@ -433,7 +373,7 @@
 
         return query.list();
     }
-
+    
     // -------------------------------------------------------------------------
     // Supportive methods
     // -------------------------------------------------------------------------

=== added file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/hibernate/HibernateUserSettingStore.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/hibernate/HibernateUserSettingStore.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/hibernate/HibernateUserSettingStore.java	2014-10-01 10:19:12 +0000
@@ -0,0 +1,114 @@
+package org.hisp.dhis.user.hibernate;
+
+/*
+ * 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.Collection;
+
+import org.hibernate.Query;
+import org.hibernate.Session;
+import org.hibernate.SessionFactory;
+import org.hisp.dhis.user.User;
+import org.hisp.dhis.user.UserSetting;
+import org.hisp.dhis.user.UserSettingStore;
+
+/**
+ * @author Lars Helge Overland
+ */
+public class HibernateUserSettingStore
+    implements UserSettingStore
+{
+    // -------------------------------------------------------------------------
+    // Dependencies
+    // -------------------------------------------------------------------------
+
+    private SessionFactory sessionFactory;
+
+    public void setSessionFactory( SessionFactory sessionFactory )
+    {
+        this.sessionFactory = sessionFactory;
+    }
+
+    // -------------------------------------------------------------------------
+    // UserSettingStore implementation
+    // -------------------------------------------------------------------------
+
+    public void addUserSetting( UserSetting userSetting )
+    {
+        Session session = sessionFactory.getCurrentSession();
+
+        session.save( userSetting );
+    }
+
+    public void updateUserSetting( UserSetting userSetting )
+    {
+        Session session = sessionFactory.getCurrentSession();
+
+        session.update( userSetting );
+    }
+
+    public UserSetting getUserSetting( User user, String name )
+    {
+        Session session = sessionFactory.getCurrentSession();
+
+        Query query = session.createQuery( "from UserSetting us where us.user = :user and us.name = :name" );
+
+        query.setEntity( "user", user );
+        query.setString( "name", name );
+        query.setCacheable( true );
+
+        return (UserSetting) query.uniqueResult();
+    }
+
+    @SuppressWarnings("unchecked")
+    public Collection<UserSetting> getAllUserSettings( User user )
+    {
+        Session session = sessionFactory.getCurrentSession();
+        Query query = session.createQuery( "from UserSetting us where us.user = :user" );
+        query.setEntity( "user", user );
+
+        return query.list();
+    }
+
+    @SuppressWarnings("unchecked")
+    public Collection<UserSetting> getUserSettings( String name )
+    {
+        Session session = sessionFactory.getCurrentSession();
+        Query query = session.createQuery( "from UserSetting us where us.name = :name" );
+        query.setString( "name", name );
+
+        return query.list();
+    }
+
+    public void deleteUserSetting( UserSetting userSetting )
+    {
+        Session session = sessionFactory.getCurrentSession();
+
+        session.delete( userSetting );
+    }
+}

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml	2014-09-10 04:36:51 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml	2014-10-01 10:19:12 +0000
@@ -260,6 +260,10 @@
     <property name="cacheable" value="true" />
   </bean>
 
+  <bean id="org.hisp.dhis.user.UserSettingStore" class="org.hisp.dhis.user.hibernate.HibernateUserSettingStore">
+    <property name="sessionFactory" ref="sessionFactory" />
+  </bean>
+
   <bean id="org.hisp.dhis.user.UserAuthorityGroupStore" class="org.hisp.dhis.user.hibernate.HibernateUserAuthorityGroupStore">
     <property name="sessionFactory" ref="sessionFactory" />
     <property name="clazz" value="org.hisp.dhis.user.UserAuthorityGroup" />
@@ -605,6 +609,7 @@
     <property name="userStore" ref="org.hisp.dhis.user.UserStore" />
     <property name="userCredentialsStore" ref="org.hisp.dhis.user.UserCredentialsStore" />
     <property name="userAuthorityGroupStore" ref="org.hisp.dhis.user.UserAuthorityGroupStore" />
+    <property name="userSettingStore" ref="org.hisp.dhis.user.UserSettingStore" />
     <property name="currentUserService" ref="org.hisp.dhis.user.CurrentUserService" />
     <property name="securityService" ref="org.hisp.dhis.security.SecurityService" />
     <property name="systemSettingManager" ref="org.hisp.dhis.setting.SystemSettingManager" />

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/user/UserSettingServiceTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/user/UserSettingServiceTest.java	2014-05-06 12:15:56 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/user/UserSettingServiceTest.java	2014-10-01 10:19:12 +0000
@@ -67,22 +67,13 @@
     }
 
     @Test
-    public void testSaveUserPreferences()
-        throws Exception
+    public void testShouldGetUserSettings()
     {
-        userSettingService.saveUserSetting( "mykey", "myvalue", "username" );
-        UserSetting setting = userCredentialStore.getUserSetting( testUser, "mykey" );
-        assertEquals( "myvalue", setting.getValue() );
-        assertEquals( "mykey", setting.getName() );
+        UserCredentials userCredentials = testUser.getUserCredentials();
+        userCredentials.setUser( testUser );
+        userCredentialStore.addUserCredentials( userCredentials );
+        userSettingService.saveUserSetting( "mykey", "value", "username" );
+        Serializable preference = userSettingService.getUserSetting( "mykey", "username" );
+        assertEquals( preference, "value" );
     }
-
-   @Test
-   public void testShouldGetUserSettings() {
-       UserCredentials userCredentials = testUser.getUserCredentials();
-       userCredentials.setUser( testUser );
-       userCredentialStore.addUserCredentials( userCredentials );
-       userSettingService.saveUserSetting("mykey", "value", "username");
-       Serializable preference = userSettingService.getUserSetting("mykey", "username");
-       assertEquals(preference, "value");
-   }
 }