Author: [email protected]
Date: Fri Aug 12 16:58:44 2011
New Revision: 1314

Log:
[AMDATUOPENSOCIAL-83] Patch with UserPref store that persists the UserPres in 
the AppData of the user

Added:
   
trunk/amdatu-opensocial/opensocial-shindig/src/main/resources/features/shindig.container/appdatauserprefstore.js
Modified:
   
trunk/amdatu-opensocial/opensocial-dashboard/src/main/resources/jsp/gadgets_appdata.js.jsp
   
trunk/amdatu-opensocial/opensocial-shindig/src/main/java/org/amdatu/opensocial/shindig/persistence/AppDataServiceImpl.java
   
trunk/amdatu-opensocial/opensocial-shindig/src/main/resources/features/shindig.container/feature.xml

Modified: 
trunk/amdatu-opensocial/opensocial-dashboard/src/main/resources/jsp/gadgets_appdata.js.jsp
==============================================================================
--- 
trunk/amdatu-opensocial/opensocial-dashboard/src/main/resources/jsp/gadgets_appdata.js.jsp
  (original)
+++ 
trunk/amdatu-opensocial/opensocial-dashboard/src/main/resources/jsp/gadgets_appdata.js.jsp
  Fri Aug 12 16:58:44 2011
@@ -45,7 +45,6 @@
 addWidgetToAppData = function(obj, startId, dashboard) {
   var currentGadgets = 
retrieveCurrentWidgetsInAppData(obj.metadata.securetoken);
   var gadgetUrl = obj.metadata.gadgeturl + "";
-  var gadgetId = encodeURIComponent(startId + '-' + gadgetUrl);
 
   // use appData opensocial call to add this gadget to the users appData
   var postdata = '{"registeredgadgets":"';
@@ -55,7 +54,7 @@
     postdata += currentGadgets + " " + gadgetUrl + '"}';
   }
 
-  var url = "${contextPath}/social/rest/appdata/@me/@self/" + gadgetId + 
"?fields=registeredgadgets&st=" + obj.metadata.securetoken;
+  var url = 
"${contextPath}/social/rest/appdata/@me/@self?fields=registeredgadgets&st=" + 
obj.metadata.securetoken;
 
   jQuery.ajax({
     url: url,

Modified: 
trunk/amdatu-opensocial/opensocial-shindig/src/main/java/org/amdatu/opensocial/shindig/persistence/AppDataServiceImpl.java
==============================================================================
--- 
trunk/amdatu-opensocial/opensocial-shindig/src/main/java/org/amdatu/opensocial/shindig/persistence/AppDataServiceImpl.java
  (original)
+++ 
trunk/amdatu-opensocial/opensocial-shindig/src/main/java/org/amdatu/opensocial/shindig/persistence/AppDataServiceImpl.java
  Fri Aug 12 16:58:44 2011
@@ -67,9 +67,13 @@
             if (user != null) {
                 // Convert the String Map to a byte array and write it to 
UserAdmin
                 try {
-                    Map<String, String> newValues = addValues(user, values);
-                    byte[] bytesValue = 
ConversionUtil.objectToByteArray(newValues);
-                    user.getProperties().put(APP_DATA, bytesValue);
+                    Map<String, String> newValues = addValues(user, values, 
appId);
+                    byte[] bytesValue = 
ConversionUtil.objectToByteArray(newValues);
+                    if (appId == null) {
+                        user.getProperties().put(APP_DATA, bytesValue);
+                    } else {
+                        user.getProperties().put(APP_DATA + "." + appId, 
bytesValue);
+                    }
                     return ImmediateFuture.newInstance(null);
                 }
                 catch (IOException e) {
@@ -92,8 +96,17 @@
     }
 
     // Append/overwrite values to the current APP_DATA properties of the user
-    private Map<String, String> addValues(User user, Map<String, String> 
values) throws ClassNotFoundException, IOException {
-        byte[] currentProperties = (byte[]) user.getProperties().get(APP_DATA);
+    @SuppressWarnings("unchecked")
+    private Map<String, String> addValues(User user, Map<String, String> 
values, String appId) throws ClassNotFoundException, IOException {
+        byte[] currentProperties;
+        if (appId == null) {
+            currentProperties = (byte[]) user.getProperties().get(APP_DATA);
+        } else {
+            currentProperties = (byte[]) user.getProperties().get(APP_DATA + 
"." + appId);
+        }
+        if (currentProperties == null) {
+            return values;
+        }
         Map<String, String> currentValues = (Map<String, String>) 
ConversionUtil.byteArrayToObject(currentProperties);
         for (String key : values.keySet()) {
             currentValues.put(key, values.get(key));
@@ -110,7 +123,12 @@
             for (UserId userId : userIds) {
                 User user = getUser(userId, token);
                 if (user != null) {
-                    byte[] bytesValue = (byte[]) 
user.getProperties().get(APP_DATA);
+                    byte[] bytesValue;
+                    if (appId == null) {
+                        bytesValue = (byte[]) 
user.getProperties().get(APP_DATA);
+                    } else {
+                        bytesValue = (byte[]) 
user.getProperties().get(APP_DATA + "." + appId);
+                    }
                     if (bytesValue != null) {
                         try {
                             Map<String, String> values =
@@ -146,12 +164,21 @@
             if (user != null) {
                 // Convert the String Map to a byte array and write it to 
UserAdmin
                 try {
-                    byte[] bytesValue = (byte[]) 
user.getProperties().get(APP_DATA);
+                    byte[] bytesValue;
+                    if (appId == null) {
+                        bytesValue = (byte[]) 
user.getProperties().get(APP_DATA);
+                    } else {
+                        bytesValue = (byte[]) 
user.getProperties().get(APP_DATA + "." + appId);
+                    }
                     Map<String, String> values = (Map<String, String>) 
ConversionUtil.byteArrayToObject(bytesValue);
                     for (String field : fields) {
                         values.remove(field);
+                    }
+                    if (appId == null) {
+                        user.getProperties().put(APP_DATA, 
ConversionUtil.objectToByteArray(values));
+                    } else {
+                        user.getProperties().put(APP_DATA + "." + appId, 
ConversionUtil.objectToByteArray(values));
                     }
-                    user.getProperties().put(APP_DATA, 
ConversionUtil.objectToByteArray(values));
                     return ImmediateFuture.newInstance(null);
                 }
                 catch (IOException e) {

Added: 
trunk/amdatu-opensocial/opensocial-shindig/src/main/resources/features/shindig.container/appdatauserprefstore.js
==============================================================================
--- (empty file)
+++ 
trunk/amdatu-opensocial/opensocial-shindig/src/main/resources/features/shindig.container/appdatauserprefstore.js
    Fri Aug 12 16:58:44 2011
@@ -0,0 +1,86 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ */
+
+/**
+ * @fileoverview Implements the gadgets.UserPrefStore interface using a cookies
+ * based implementation. Depends on cookies.js. This code should not be used in
+ * a production environment.
+ */
+
+/**
+ * Cookie-based user preference store.
+ * @constructor
+ */
+shindig.AppDataBasedUserPrefStore = function() {
+  shindig.UserPrefStore.call(this);
+};
+
+shindig.AppDataBasedUserPrefStore.inherits(shindig.UserPrefStore);
+
+shindig.AppDataBasedUserPrefStore.prototype.USER_PREFS_PREFIX =
+    'gadgetUserPrefs-';
+
+shindig.AppDataBasedUserPrefStore.prototype.getPrefs = function(gadget) {
+  // We cannot use the OpenSocial API to retrieve appdata, since we must do 
synchronous
+  // calls here (by the definition of Shindigs UserPrefStore API).
+  var userPrefs = {};
+
+  // TODO: this URL should be prefixed by the contextpath
+  var url = "/rpc?st=" + gadget.secureToken;
+
+  var postdata = '{"method":"appdata.get","id":"appdata.get","params":{';
+  postdata += '"appId":"userprefs.' + gadget.id + '"';
+  postdata += ',"userId":"@me","fields":"@all","groupId":"@self"}}';
+
+  jQuery.ajax({
+    url: url,
+    type: "POST",
+    contentType: "application/json",
+    data: postdata,
+    dataType: "json",
+    async:false,
+    success: function(response) {
+       var result = response.result;
+       $.each(result, function(username, user) {
+        $.each(user, function(prefname, pref) {
+          var userPref = new Object();
+          userPref.value = pref.value;
+          userPrefs[prefname] = userPref;
+         });
+       });
+    },
+    error: function(request, textStatus, errorThrown) {
+    }
+  });
+  return userPrefs;
+};
+
+
+shindig.AppDataBasedUserPrefStore.prototype.savePrefs = function(gadget) {
+  // Set the security token on the auth context before making the call.
+  // Decode it first, since it is already URL encoded and the 
updateSecurityToken
+  // does it again.
+  shindig.auth.updateSecurityToken(decodeURIComponent(gadget.secureToken));
+  osapi.appdata.update({
+    appId: "userprefs." + gadget.id,
+    userId: "@me",
+    data: gadget.getUserPrefs() }).execute();
+};
+
+shindig.Container.prototype.userPrefStore =
+    new shindig.AppDataBasedUserPrefStore();

Modified: 
trunk/amdatu-opensocial/opensocial-shindig/src/main/resources/features/shindig.container/feature.xml
==============================================================================
--- 
trunk/amdatu-opensocial/opensocial-shindig/src/main/resources/features/shindig.container/feature.xml
        (original)
+++ 
trunk/amdatu-opensocial/opensocial-shindig/src/main/resources/features/shindig.container/feature.xml
        Fri Aug 12 16:58:44 2011
@@ -32,7 +32,7 @@
     <script src="util.js"/>
     <script src="cookies.js"/>
     <script src="shindig-container.js"/>
-    <script src="cookiebaseduserprefstore.js"/>
+    <script src="appdatauserprefstore.js"/>
     <script src="osapi.js"/>
   </container>
 </feature>
_______________________________________________
Amdatu-commits mailing list
[email protected]
http://lists.amdatu.org/mailman/listinfo/amdatu-commits

Reply via email to