← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 14631: Applied patch from Kiran Prakash. Adds support for setting user settings for specific users.

 

Merge authors:
  Kiran Prakash (kiran-f)
------------------------------------------------------------
revno: 14631 [merge]
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2014-04-03 14:22:48 +0200
message:
  Applied patch from Kiran Prakash. Adds support for setting user settings for specific users.
added:
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/user/DefaultUserSettingServiceTest.java
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserSettingService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/DefaultUserSettingService.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/UserSettingController.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/UserSettingService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserSettingService.java	2014-03-18 08:10:10 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserSettingService.java	2014-04-03 11:32:51 +0000
@@ -71,6 +71,16 @@
     void saveUserSetting( String name, Serializable value );
 
     /**
+     * Saves the name/value pair as a user setting connected to user identified by username
+     *
+     * @param name the name/handle of the value.
+     * @param value the value to store.
+     * @param username the username of user.
+     * @throws NoCurrentUserException if there is no user.
+     */
+    void saveUserSetting( String name, Serializable value, String username );
+
+    /**
      * Returns the value of the user setting specified by the given name.
      * 
      * @param name the name of the user setting.

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/DefaultUserSettingService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/DefaultUserSettingService.java	2014-03-18 08:10:10 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/DefaultUserSettingService.java	2014-04-03 12:22:48 +0000
@@ -59,7 +59,7 @@
     {
         this.userService = userService;
     }
-    
+
     // -------------------------------------------------------------------------
     // UserSettingService implementation
     // -------------------------------------------------------------------------
@@ -67,18 +67,33 @@
     public void saveUserSetting( String name, Serializable value )
     {
         User currentUser = currentUserService.getCurrentUser();
-
-        if ( currentUser == null )
+        
+        save( name, value, currentUser );
+    }
+
+    public void saveUserSetting( String name, Serializable value, String username )
+    {
+        UserCredentials credentials = userService.getUserCredentialsByUsername( username );
+        
+        if ( credentials != null )
+        {        
+            save( name, value, credentials.getUser() );
+        }
+    }
+
+    private void save( String name, Serializable value, User user )
+    {
+        if ( user == null )
         {
             return;
         }
 
-        UserSetting userSetting = userService.getUserSetting( currentUser, name );
+        UserSetting userSetting = userService.getUserSetting( user, name );
 
         if ( userSetting == null )
         {
             userSetting = new UserSetting();
-            userSetting.setUser( currentUser );
+            userSetting.setUser( user );
             userSetting.setName( name );
             userSetting.setValue( value );
 
@@ -149,6 +164,6 @@
         if ( currentUser != null )
         {
             userService.deleteUserSetting( userService.getUserSetting( currentUser, name ) );
-        }        
+        }
     }
 }

=== added file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/user/DefaultUserSettingServiceTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/user/DefaultUserSettingServiceTest.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/user/DefaultUserSettingServiceTest.java	2014-04-03 12:22:48 +0000
@@ -0,0 +1,72 @@
+package org.hisp.dhis.user;
+
+/*
+ * Copyright (c) 2004-2013, 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 static org.junit.Assert.assertEquals;
+
+import org.hisp.dhis.DhisSpringTest;
+import org.junit.Test;
+
+/**
+ * @author Kiran Prakash
+ */
+public class DefaultUserSettingServiceTest
+    extends DhisSpringTest
+{
+    private UserSettingService userSettingService;
+
+    private UserStore userStore;
+
+    private UserCredentialsStore userCredentialStore;
+
+    @Override
+    protected void setUpTest()
+        throws Exception
+    {
+        userStore = (UserStore) getBean( UserStore.ID );
+        userService = (UserService) getBean( UserService.ID );
+        userSettingService = (UserSettingService) getBean( UserSettingService.ID );
+        userCredentialStore = (UserCredentialsStore) getBean( UserCredentialsStore.ID );
+    }
+
+    @Test
+    public void testSaveUserPreferences()
+        throws Exception
+    {
+        User testUser = createUser( 'D' );
+        userStore.save( testUser );
+        UserCredentials userCredentials = testUser.getUserCredentials();
+        userCredentials.setUser( testUser );
+        userCredentialStore.addUserCredentials( userCredentials );
+        userSettingService.saveUserSetting( "mykey", "myvalue", "username" );
+        UserSetting setting = userCredentialStore.getUserSetting( testUser, "mykey" );
+        assertEquals( "myvalue", setting.getValue() );
+        assertEquals( "mykey", setting.getName() );
+    }
+}

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/UserSettingController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/UserSettingController.java	2014-03-18 08:10:10 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/UserSettingController.java	2014-04-03 12:22:48 +0000
@@ -52,9 +52,10 @@
     private UserSettingService userSettingService;
 
     @RequestMapping( value = "/{key}", method = RequestMethod.POST, consumes = { ContextUtils.CONTENT_TYPE_TEXT, ContextUtils.CONTENT_TYPE_HTML } )
-    public void setUserSetting( 
-        @PathVariable String key, 
-        @RequestParam(required = false) String value, 
+    public void setUserSetting(
+        @PathVariable String key,
+        @RequestParam(value = "user", required = false) String username,
+        @RequestParam(value = "value",required = false) String value,
         @RequestBody(required=false) String valuePayload, HttpServletResponse response )
     {
         if ( key == null )
@@ -66,21 +67,29 @@
         if ( value == null && valuePayload == null )
         {
             ContextUtils.conflictResponse( response, "Value must be specified as query param or as payload" );
+            return;
         }
 
         value = value != null ? value : valuePayload;
-        
-        userSettingService.saveUserSetting( key, value );
-        
+
+        if ( username == null )
+        {
+            userSettingService.saveUserSetting( key, value );
+        }
+        else
+        {
+            userSettingService.saveUserSetting( key, value, username );
+        }
+
         ContextUtils.okResponse( response, "User setting saved" );
     }
-    
+
     @RequestMapping( value = "/{key}", method = RequestMethod.GET, produces = ContextUtils.CONTENT_TYPE_TEXT )
     public @ResponseBody String getSystemSetting( @PathVariable( "key" ) String key )
     {
         return (String) userSettingService.getUserSetting( key );
     }
-    
+
     @RequestMapping( value = "/{key}", method = RequestMethod.DELETE )
     public void removeSystemSetting( @PathVariable( "key" ) String key )
     {