From 1f3e2a47c18b8e710e2d70a38ed446e6bdb92f62 Mon Sep 17 00:00:00 2001 From: Roy Wetherall Date: Mon, 23 Jun 2008 09:48:22 +0000 Subject: [PATCH] Preference Service git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@9538 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../preference/preferences.delete.desc.xml | 8 + .../preference/preferences.delete.js | 14 ++ .../preference/preferences.delete.json.ftl | 0 .../preference/preferences.get.desc.xml | 8 + .../repository/preference/preferences.get.js | 20 ++ .../preference/preferences.get.json.ftl | 1 + .../preference/preferences.post.desc.xml | 8 + .../repository/preference/preferences.post.js | 20 ++ .../preference/preferences.post.json.ftl | 0 .../web-scripts-application-context.xml | 5 + .../preference/PreferenceServiceTest.java | 190 ++++++++++++++++++ 11 files changed, 274 insertions(+) create mode 100644 config/alfresco/templates/webscripts/org/alfresco/repository/preference/preferences.delete.desc.xml create mode 100644 config/alfresco/templates/webscripts/org/alfresco/repository/preference/preferences.delete.js create mode 100644 config/alfresco/templates/webscripts/org/alfresco/repository/preference/preferences.delete.json.ftl create mode 100644 config/alfresco/templates/webscripts/org/alfresco/repository/preference/preferences.get.desc.xml create mode 100644 config/alfresco/templates/webscripts/org/alfresco/repository/preference/preferences.get.js create mode 100644 config/alfresco/templates/webscripts/org/alfresco/repository/preference/preferences.get.json.ftl create mode 100644 config/alfresco/templates/webscripts/org/alfresco/repository/preference/preferences.post.desc.xml create mode 100644 config/alfresco/templates/webscripts/org/alfresco/repository/preference/preferences.post.js create mode 100644 config/alfresco/templates/webscripts/org/alfresco/repository/preference/preferences.post.json.ftl create mode 100644 source/java/org/alfresco/repo/web/scripts/preference/PreferenceServiceTest.java diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/preference/preferences.delete.desc.xml b/config/alfresco/templates/webscripts/org/alfresco/repository/preference/preferences.delete.desc.xml new file mode 100644 index 0000000000..c292c25a54 --- /dev/null +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/preference/preferences.delete.desc.xml @@ -0,0 +1,8 @@ + + Preferences + Delete a users preferences + /api/people/{userid}/preferences?pf={preferencefilter?} + + user + required + \ No newline at end of file diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/preference/preferences.delete.js b/config/alfresco/templates/webscripts/org/alfresco/repository/preference/preferences.delete.js new file mode 100644 index 0000000000..a6d69532b8 --- /dev/null +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/preference/preferences.delete.js @@ -0,0 +1,14 @@ +function main() +{ + // Try and get the person + var userid = url.templateArgs.userid; + var person = people.getPerson(userid); + if (person == null) + { + // 404 since person resource could not be found + status.setCode(status.STATUS_NOT_FOUND, "The user " + userid + " could not be found"); + return; + } +} + +main(); \ No newline at end of file diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/preference/preferences.delete.json.ftl b/config/alfresco/templates/webscripts/org/alfresco/repository/preference/preferences.delete.json.ftl new file mode 100644 index 0000000000..e69de29bb2 diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/preference/preferences.get.desc.xml b/config/alfresco/templates/webscripts/org/alfresco/repository/preference/preferences.get.desc.xml new file mode 100644 index 0000000000..5f9485277e --- /dev/null +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/preference/preferences.get.desc.xml @@ -0,0 +1,8 @@ + + Preferences + Get the preferences for a user + /api/people/{userid}/preferences?pf={preferencefilter?} + + user + required + \ No newline at end of file diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/preference/preferences.get.js b/config/alfresco/templates/webscripts/org/alfresco/repository/preference/preferences.get.js new file mode 100644 index 0000000000..b5b0ae7d4f --- /dev/null +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/preference/preferences.get.js @@ -0,0 +1,20 @@ +function main() +{ + // Try and get the person + var userid = url.templateArgs.userid; + var person = people.getPerson(userid); + if (person == null) + { + // 404 since person resource could not be found + status.setCode(status.STATUS_NOT_FOUND, "The user " + userid + " could not be found"); + return; + } + + // Get the preferences for the person + var preferences = preferenceService.getPreferences(userid, args["pf"]); + + // Convert the preferences to JSON and place in the model + model.preferences = jsonUtils.toJSONString(preferences); +} + +main(); \ No newline at end of file diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/preference/preferences.get.json.ftl b/config/alfresco/templates/webscripts/org/alfresco/repository/preference/preferences.get.json.ftl new file mode 100644 index 0000000000..07645b9abb --- /dev/null +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/preference/preferences.get.json.ftl @@ -0,0 +1 @@ +${preferences} \ No newline at end of file diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/preference/preferences.post.desc.xml b/config/alfresco/templates/webscripts/org/alfresco/repository/preference/preferences.post.desc.xml new file mode 100644 index 0000000000..ba39f4792a --- /dev/null +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/preference/preferences.post.desc.xml @@ -0,0 +1,8 @@ + + Preferences + Set a users preferences + /api/people/{userid}/preferences + + user + required + \ No newline at end of file diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/preference/preferences.post.js b/config/alfresco/templates/webscripts/org/alfresco/repository/preference/preferences.post.js new file mode 100644 index 0000000000..aea97e76c8 --- /dev/null +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/preference/preferences.post.js @@ -0,0 +1,20 @@ +function main() +{ + // Try and get the person + var userid = url.templateArgs.userid; + var person = people.getPerson(userid); + if (person == null) + { + // 404 since person resource could not be found + status.setCode(status.STATUS_NOT_FOUND, "The user " + userid + " could not be found"); + return; + } + + // Convert the passed json into a native JS object + var preferences = jsonUtils.toObject(json); + + // Set the preferences + preferenceService.setPreferences(userid, preferences); +} + +main(); \ No newline at end of file diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/preference/preferences.post.json.ftl b/config/alfresco/templates/webscripts/org/alfresco/repository/preference/preferences.post.json.ftl new file mode 100644 index 0000000000..e69de29bb2 diff --git a/config/alfresco/web-scripts-application-context.xml b/config/alfresco/web-scripts-application-context.xml index 1c6f9aa32d..25474e6088 100644 --- a/config/alfresco/web-scripts-application-context.xml +++ b/config/alfresco/web-scripts-application-context.xml @@ -272,4 +272,9 @@ + + + + + diff --git a/source/java/org/alfresco/repo/web/scripts/preference/PreferenceServiceTest.java b/source/java/org/alfresco/repo/web/scripts/preference/PreferenceServiceTest.java new file mode 100644 index 0000000000..161788dd0c --- /dev/null +++ b/source/java/org/alfresco/repo/web/scripts/preference/PreferenceServiceTest.java @@ -0,0 +1,190 @@ +/* + * Copyright (C) 2005-2007 Alfresco Software Limited. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + * As a special exception to the terms and conditions of version 2.0 of + * the GPL, you may redistribute this Program in connection with Free/Libre + * and Open Source Software ("FLOSS") applications as described in Alfresco's + * FLOSS exception. You should have recieved a copy of the text describing + * the FLOSS exception, and it is also available here: + * http://www.alfresco.com/legal/licensing" + */ +package org.alfresco.repo.web.scripts.preference; + +import java.util.ArrayList; +import java.util.List; + +import org.alfresco.model.ContentModel; +import org.alfresco.repo.security.authentication.AuthenticationComponent; +import org.alfresco.repo.site.SiteModel; +import org.alfresco.repo.web.scripts.BaseWebScriptTest; +import org.alfresco.service.cmr.security.AuthenticationService; +import org.alfresco.service.cmr.security.PersonService; +import org.alfresco.util.GUID; +import org.alfresco.util.PropertyMap; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; +import org.springframework.mock.web.MockHttpServletResponse; + +/** + * Unit test to test preference Web Script API + * + * @author Roy Wetherall + */ +public class PreferenceServiceTest extends BaseWebScriptTest +{ + private AuthenticationService authenticationService; + private AuthenticationComponent authenticationComponent; + private PersonService personService; + + private static final String USER_ONE = "PreferenceTestOne" + System.currentTimeMillis(); + private static final String USER_BAD = "PreferenceTestBad" + System.currentTimeMillis(); + + private static final String URL = "/api/people/" + USER_ONE + "/preferences";; + + @Override + protected void setUp() throws Exception + { + super.setUp(); + + this.authenticationService = (AuthenticationService)getServer().getApplicationContext().getBean("AuthenticationService"); + this.authenticationComponent = (AuthenticationComponent)getServer().getApplicationContext().getBean("authenticationComponent"); + this.personService = (PersonService)getServer().getApplicationContext().getBean("PersonService"); + + // Create users + createUser(USER_ONE); + createUser(USER_BAD); + + // Do tests as user one + this.authenticationComponent.setCurrentUser(USER_ONE); + } + + private void createUser(String userName) + { + if (this.authenticationService.authenticationExists(userName) == false) + { + this.authenticationService.createAuthentication(userName, "PWD".toCharArray()); + + PropertyMap ppOne = new PropertyMap(4); + ppOne.put(ContentModel.PROP_USERNAME, userName); + ppOne.put(ContentModel.PROP_FIRSTNAME, "firstName"); + ppOne.put(ContentModel.PROP_LASTNAME, "lastName"); + ppOne.put(ContentModel.PROP_EMAIL, "email@email.com"); + ppOne.put(ContentModel.PROP_JOBTITLE, "jobTitle"); + + this.personService.createPerson(ppOne); + } + } + + @Override + protected void tearDown() throws Exception + { + super.tearDown(); + this.authenticationComponent.setCurrentUser("admin"); + + } + + public void testPreferences() throws Exception + { + // Get the preferences before they have been set + + MockHttpServletResponse resp = getRequest(URL, 200); + JSONObject jsonResult = new JSONObject(resp.getContentAsString()); + + assertNotNull(jsonResult); + assertFalse(jsonResult.keys().hasNext()); + + // Set some preferences + + JSONObject jsonObject = getPreferenceObj(); + jsonObject.put("comp1", getPreferenceObj()); + + resp = postRequest(URL, 200, jsonObject.toString(), "application/json"); + assertEquals(0, resp.getContentLength()); + + // Get the preferences + + resp = getRequest(URL, 200); + jsonResult = new JSONObject(resp.getContentAsString()); + assertNotNull(jsonResult); + assertTrue(jsonResult.keys().hasNext()); + + checkJSONObject(jsonResult); + checkJSONObject(jsonResult.getJSONObject("comp1")); + + // Update some of the preferences + + jsonObject.put("stringValue", "updated"); + jsonObject.put("comp2", getPreferenceObj()); + + resp = postRequest(URL, 200, jsonObject.toString(), "application/json"); + assertEquals(0, resp.getContentLength()); + + // Get the preferences + + resp = getRequest(URL, 200); + jsonResult = new JSONObject(resp.getContentAsString()); + assertNotNull(jsonResult); + assertTrue(jsonResult.keys().hasNext()); + + jsonObject.put("stringValue", "updated"); + jsonObject.put("numberValue", 10); + jsonObject.put("numberValue2", 3.142); + checkJSONObject(jsonResult.getJSONObject("comp1")); + checkJSONObject(jsonResult.getJSONObject("comp2")); + + // Filter the preferences retrieved + + resp = getRequest(URL + "?pf=comp2", 200); + jsonResult = new JSONObject(resp.getContentAsString()); + assertNotNull(jsonResult); + assertTrue(jsonResult.keys().hasNext()); + + checkJSONObject(jsonResult.getJSONObject("comp2")); + assertFalse(jsonResult.has("comp1")); + assertFalse(jsonResult.has("stringValue")); + + // Clear some of the preferences + + // Clear all the preferences + + // Test trying to update another user's permissions + postRequest("/api/people/" + USER_BAD + "/preferences", 500, jsonObject.toString(), "application/json"); + + // Test error conditions + getRequest("/api/people/noExistUser/preferences", 404); + } + + private JSONObject getPreferenceObj() + throws JSONException + { + JSONObject jsonObject = new JSONObject(); + jsonObject.put("stringValue", "value"); + jsonObject.put("numberValue", 10); + jsonObject.put("numberValue2", 3.142); + return jsonObject; + } + + private void checkJSONObject(JSONObject jsonObject) + throws JSONException + { + assertEquals("value", jsonObject.get("stringValue")); + assertEquals(10, jsonObject.get("numberValue")); + assertEquals(3.142, jsonObject.get("numberValue2")); + } + +}