Author: [email protected]
Date: Tue Jan  3 09:01:53 2012
New Revision: 1882

Log:
[AMDATUOPENSOCIAL-171] Added a REST service to retrieve the user preferences 
from a gadget of which the values are overruled by values stored in AppData

Added:
   
trunk/amdatu-opensocial/opensocial-gadgetmanagement/src/main/java/org/amdatu/opensocial/gadgetmanagement/UserPrefBean.java
   
trunk/amdatu-opensocial/opensocial-gadgetmanagement/src/main/java/org/amdatu/opensocial/gadgetmanagement/UserPrefsBean.java
Modified:
   
trunk/amdatu-opensocial/opensocial-gadgetmanagement/src/main/java/org/amdatu/opensocial/gadgetmanagement/OpenSocialContainer.java
   
trunk/amdatu-opensocial/opensocial-gadgetmanagement/src/main/java/org/amdatu/opensocial/gadgetmanagement/service/BaseRESTServiceImpl.java
   
trunk/amdatu-opensocial/opensocial-gadgetmanagement/src/main/java/org/amdatu/opensocial/gadgetmanagement/service/WidgetRESTServiceImpl.java
   
trunk/amdatu-opensocial/opensocial-shindig/src/main/java/org/amdatu/opensocial/shindig/service/ShindigOpenSocialContainerImpl.java
   
trunk/amdatu-opensocial/opensocial-shindig/src/main/resources/features/shindig.container/appdatauserprefstore.js

Modified: 
trunk/amdatu-opensocial/opensocial-gadgetmanagement/src/main/java/org/amdatu/opensocial/gadgetmanagement/OpenSocialContainer.java
==============================================================================
--- 
trunk/amdatu-opensocial/opensocial-gadgetmanagement/src/main/java/org/amdatu/opensocial/gadgetmanagement/OpenSocialContainer.java
   (original)
+++ 
trunk/amdatu-opensocial/opensocial-gadgetmanagement/src/main/java/org/amdatu/opensocial/gadgetmanagement/OpenSocialContainer.java
   Tue Jan  3 09:01:53 2012
@@ -18,6 +18,7 @@
 import java.util.Map;
 
 import javax.servlet.http.HttpServletRequest;
+
 
 /**
  * This interface facilitates the decoupling of gadget management with the 
actual OpenSocial implementation.
@@ -85,5 +86,15 @@
      * @param userPrefs
      * @return
      */
-    Map<String, String> getGadgetSpec(HttpServletRequest request, String 
gadgetUrl, Map<String, String> userPrefs);
+    Map<String, String> getGadgetSpec(HttpServletRequest request, String 
gadgetUrl, Map<String, String> userPrefs);
+    
+    /**
+     * Returns the user preferences of the specified gadget of which the 
default values are
+     * overwritten with the provided user preferences.
+     * @param gadgetUrl The URL of the gadget to retrieve the user preferences 
for
+     * @param userPrefValues User preference values that will override the 
default values returned by
+     * this method. May be null.
+     * @return The user preferences stored in a UserPrefsBean
+     */
+    UserPrefsBean getUserPreferences(String gadgetUrl, Map<String, String> 
userPrefValues);
 }

Added: 
trunk/amdatu-opensocial/opensocial-gadgetmanagement/src/main/java/org/amdatu/opensocial/gadgetmanagement/UserPrefBean.java
==============================================================================
--- (empty file)
+++ 
trunk/amdatu-opensocial/opensocial-gadgetmanagement/src/main/java/org/amdatu/opensocial/gadgetmanagement/UserPrefBean.java
  Tue Jan  3 09:01:53 2012
@@ -0,0 +1,75 @@
+/*
+ * Copyright (c) 2010, 2011 The Amdatu Foundation
+ * 
+ * Licensed 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.
+ */
+package org.amdatu.opensocial.gadgetmanagement;
+
+/**
+ * This bean holds the properties of a UserPref according to the open social
+ * specification. See 
http://code.google.com/apis/gadgets/docs/reference.html#Userprefs_Ref
+ * 
+ * @author <a href=mailto:[email protected]>Amdatu Project Team</a>
+ */
+public class UserPrefBean {
+    private String m_name;
+    private String m_display_name;
+    private String m_urlparam;
+    private String m_datatype;
+    private boolean m_required;
+    private String m_value;
+    private String m_default_value;
+    
+    public String getName() {
+        return m_name;
+    }
+    public void setName(String name) {
+        m_name = name;
+    }
+    public String getDisplay_name() {
+        return m_display_name;
+    }
+    public void setDisplay_name(String display_name) {
+        m_display_name = display_name;
+    }
+    public String getUrlparam() {
+        return m_urlparam;
+    }
+    public void setUrlparam(String urlparam) {
+        m_urlparam = urlparam;
+    }
+    public String getDatatype() {
+        return m_datatype;
+    }
+    public void setDatatype(String datatype) {
+        m_datatype = datatype;
+    }
+    public boolean isRequired() {
+        return m_required;
+    }
+    public void setRequired(boolean required) {
+        m_required = required;
+    }
+    public String getValue() {
+        return m_value;
+    }
+    public void setValue(String value) {
+        m_value = value;
+    }
+    public String getDefault_value() {
+        return m_default_value;
+    }
+    public void setDefault_value(String default_value) {
+        m_default_value = default_value;
+    }
+}

Added: 
trunk/amdatu-opensocial/opensocial-gadgetmanagement/src/main/java/org/amdatu/opensocial/gadgetmanagement/UserPrefsBean.java
==============================================================================
--- (empty file)
+++ 
trunk/amdatu-opensocial/opensocial-gadgetmanagement/src/main/java/org/amdatu/opensocial/gadgetmanagement/UserPrefsBean.java
 Tue Jan  3 09:01:53 2012
@@ -0,0 +1,53 @@
+/*
+ * Copyright (c) 2010, 2011 The Amdatu Foundation
+ * 
+ * Licensed 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.
+ */
+package org.amdatu.opensocial.gadgetmanagement;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+
+/**
+ * This bean holds a collection of UserPref beans according to the open social
+ * specification. See 
http://code.google.com/apis/gadgets/docs/reference.html#Userprefs_Ref
+ * 
+ * @author <a href=mailto:[email protected]>Amdatu Project Team</a>
+ */
+@SuppressWarnings("restriction")
+@XmlRootElement(name = "UserPrefs")
+@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
+public class UserPrefsBean {
+    private List<UserPrefBean> m_userPrefs;
+
+    @XmlElement(name = "UserPref")
+    public List<UserPrefBean> getUserPrefs() {
+        return m_userPrefs;
+    }
+
+    public void setUserPrefs(final List<UserPrefBean> userPrefs) {
+        m_userPrefs = userPrefs;
+    }
+
+    public void addUserPref(UserPrefBean userPref) {
+        if (m_userPrefs == null) {
+            m_userPrefs = new ArrayList<UserPrefBean>();
+        }
+        m_userPrefs.add(userPref);
+    }
+}

Modified: 
trunk/amdatu-opensocial/opensocial-gadgetmanagement/src/main/java/org/amdatu/opensocial/gadgetmanagement/service/BaseRESTServiceImpl.java
==============================================================================
--- 
trunk/amdatu-opensocial/opensocial-gadgetmanagement/src/main/java/org/amdatu/opensocial/gadgetmanagement/service/BaseRESTServiceImpl.java
   (original)
+++ 
trunk/amdatu-opensocial/opensocial-gadgetmanagement/src/main/java/org/amdatu/opensocial/gadgetmanagement/service/BaseRESTServiceImpl.java
   Tue Jan  3 09:01:53 2012
@@ -24,6 +24,8 @@
 
 import javax.servlet.http.HttpServletRequest;
 import javax.ws.rs.core.CacheControl;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.Status;
 
 import org.amdatu.auth.tokenprovider.InvalidTokenException;
 import org.amdatu.auth.tokenprovider.TokenProvider;
@@ -320,4 +322,15 @@
            }
            return null;
        }
+       
+    // 400 - BAD REQUEST
+    protected Response get400(String msg) {
+        return 
Response.status(Status.BAD_REQUEST).entity(msg).cacheControl(NO_CACHE_CONTROL).build();
+    }
+       
+    // Returns a 500 - INTERNAL SERVER ERROR
+    protected Response get500(Exception e, String msg) {
+        m_logService.log(LogService.LOG_ERROR, msg, e);
+        return 
Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
+    }
 }

Modified: 
trunk/amdatu-opensocial/opensocial-gadgetmanagement/src/main/java/org/amdatu/opensocial/gadgetmanagement/service/WidgetRESTServiceImpl.java
==============================================================================
--- 
trunk/amdatu-opensocial/opensocial-gadgetmanagement/src/main/java/org/amdatu/opensocial/gadgetmanagement/service/WidgetRESTServiceImpl.java
 (original)
+++ 
trunk/amdatu-opensocial/opensocial-gadgetmanagement/src/main/java/org/amdatu/opensocial/gadgetmanagement/service/WidgetRESTServiceImpl.java
 Tue Jan  3 09:01:53 2012
@@ -21,7 +21,9 @@
 import javax.servlet.http.HttpServletRequest;
 import javax.ws.rs.GET;
 import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
 import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.core.Context;
 import javax.ws.rs.core.HttpHeaders;
@@ -29,6 +31,7 @@
 import javax.ws.rs.core.Response;
 
 import org.amdatu.opensocial.gadgetmanagement.GadgetDefinition;
+import org.amdatu.opensocial.gadgetmanagement.UserPrefsBean;
 import org.amdatu.opensocial.gadgetmanagement.bean.Metadata;
 import org.amdatu.opensocial.gadgetmanagement.bean.Widget;
 import org.amdatu.opensocial.gadgetmanagement.bean.Widgets;
@@ -37,6 +40,21 @@
 @Path("widgets")
 public class WidgetRESTServiceImpl extends BaseRESTServiceImpl {
     /**
+     * This method can be used to check the availability of the Widget Service.
+     * If the REST service is online, it returns a 200.
+     * 
+     * @return <ul>
+     *         <li>200 (OK) : Body contains the text "Widget service 
online".</li>
+     *         </ul>
+     */
+    @GET
+    @Produces({ MediaType.TEXT_PLAIN })
+    @Path("status")
+    public String status() {
+        return "Widget service online";
+    }
+    
+    /**
      * Returns widgets for the current user. URL to this resource:
      * /rest/widgets/mine
      * 
@@ -44,7 +62,7 @@
      */
     @GET
     @Path("/mine")
-    @Produces({MediaType.APPLICATION_JSON})
+    @Produces({ MediaType.APPLICATION_JSON })
     public Response getWidgets(@Context final HttpHeaders headers, @Context 
final HttpServletRequest request) {
         Widgets widgetsBean = new Widgets();
         try {
@@ -101,7 +119,7 @@
                 // The dashboard plugins loads the gadgetspec from the 
'gadgeturl' property in the metadata
                 metadata.setGadgetUrl(gadgetUrl);
 
-                //widgetsJson.add(widget);
+                // widgetsJson.add(widget);
                 widgetBean.setId(widgetId);
                 widgetBean.setUrl("");
                 widgetsBean.addWidget(widgetBean);
@@ -116,6 +134,41 @@
         return Response.ok(widgetsBean, 
MediaType.APPLICATION_JSON_TYPE).cacheControl(NO_CACHE_CONTROL).build();
     }
 
+    /**
+     * Returns the user preferences of a single widget. Both the id of the 
widget and the uri of the gadget
+     * must be provided.
+     * 
+     * @param id The id of the widget to load the user preferences for
+     * @param uri The URI of the gadget to retrieve the user preferences for
+     * @param request The http request
+     * @return The user preferences of this widget.
+     */
+    @GET
+    @Path("{id}/userpreferences")
+    @Produces({ MediaType.APPLICATION_JSON })
+    public Response getUserPreferences(@PathParam("id") final String id, 
@QueryParam("uri") final String uri, 
+        @Context final HttpServletRequest request) {
+        try {
+            if (uri == null || uri.isEmpty()) {
+                return get400("No query parameter 'uri' provided. You must 
provide the gadget URL.");
+            }
+            
+            // If the user is authenticated, retrieve the userprefs from 
AppData
+            User user = getUserFromRequest(request);
+            Map<String, String> persistedUserPrefs = null;
+            if (user != null) {     
+                persistedUserPrefs = getUserPreferences(request, id);
+            }
+            
+            UserPrefsBean userPrefs = 
getOpenSocialContainer().getUserPreferences(uri, persistedUserPrefs);
+           
+            return Response.ok(userPrefs, 
MediaType.APPLICATION_JSON_TYPE).cacheControl(NO_CACHE_CONTROL).build();
+        }
+        catch (Exception e) {
+            return get500(e, "Could not retrieve user preferences for widget 
'" + id + "', gadget uri '" + uri + "'");
+        }
+    }
+
     private Map<String, Map<String, String>> getDefaultWidgets(User user, 
final HttpServletRequest request) {
         // No widgets are associated with this user, by default we assign the 
default gadgets (duh)
         GadgetDefinition[] defaultGadgets = 
getGadgetManagement().getDefaultGadgets();
@@ -126,15 +179,15 @@
         for (int i = 0; i < defaultGadgets.length; i++) {
             // FIXME: disabled the authorization check for now as this change 
is far from complete
             // See AMDATUOPENSOCIAL-146
-               // if (canSeeGadgetCategory(user, 
defaultGadgets[i].getCategory())) {
-                   Map<String, String> gadget = new HashMap<String, String>();
-                   gadget.put("id", Integer.toString(i+1));
-                   gadget.put("url", defaultGadgets[i].getUrl());
-                   widgets.put(Integer.toString(i), gadget);
-               // }
+            // if (canSeeGadgetCategory(user, 
defaultGadgets[i].getCategory())) {
+            Map<String, String> gadget = new HashMap<String, String>();
+            gadget.put("id", Integer.toString(i + 1));
+            gadget.put("url", defaultGadgets[i].getUrl());
+            widgets.put(Integer.toString(i), gadget);
+            // }
         }
         return widgets;
     }
-
-  
+    
+    
 }

Modified: 
trunk/amdatu-opensocial/opensocial-shindig/src/main/java/org/amdatu/opensocial/shindig/service/ShindigOpenSocialContainerImpl.java
==============================================================================
--- 
trunk/amdatu-opensocial/opensocial-shindig/src/main/java/org/amdatu/opensocial/shindig/service/ShindigOpenSocialContainerImpl.java
  (original)
+++ 
trunk/amdatu-opensocial/opensocial-shindig/src/main/java/org/amdatu/opensocial/shindig/service/ShindigOpenSocialContainerImpl.java
  Tue Jan  3 09:01:53 2012
@@ -35,6 +35,8 @@
 import org.amdatu.auth.tokenprovider.TokenProviderException;
 import org.amdatu.core.tenant.Tenant;
 import org.amdatu.opensocial.gadgetmanagement.OpenSocialContainer;
+import org.amdatu.opensocial.gadgetmanagement.UserPrefBean;
+import org.amdatu.opensocial.gadgetmanagement.UserPrefsBean;
 import org.amdatu.opensocial.shindig.osgi.Activator;
 import org.apache.felix.dm.Component;
 import org.apache.felix.dm.DependencyManager;
@@ -56,6 +58,7 @@
 import org.apache.shindig.gadgets.spec.GadgetSpec;
 import org.apache.shindig.gadgets.spec.LocaleSpec;
 import org.apache.shindig.gadgets.spec.MessageBundle;
+import org.apache.shindig.gadgets.spec.SpecParserException;
 import org.apache.shindig.gadgets.spec.UserPref;
 import org.apache.shindig.gadgets.variables.Substitutions;
 import org.apache.shindig.gadgets.variables.UserPrefSubstituter;
@@ -68,6 +71,7 @@
  * 
  * @author ivol
  */
+
 public class ShindigOpenSocialContainerImpl implements OpenSocialContainer {
     // Timeout for retrieving gadget specs
     private static final int GADGETSPEC_READ_TIMEOUT = 5000;
@@ -155,52 +159,8 @@
         Map<String, String> gadgetSpec = new HashMap<String, String>();
         try {
             m_logService.log(LogService.LOG_DEBUG, "Retrieving gadgetspec for 
'" + gadgetUrl + "'");
-            String xml = loadXMLFromCache(gadgetUrl);
-            if (xml != null) {
-                GadgetSpec spec = new GadgetSpec(Uri.parse(gadgetUrl), xml);
-
-                // Perform message substitution if locales are available
-                Map<Locale, LocaleSpec> locales = 
spec.getModulePrefs().getLocales();
-                if (locales != null && locales.size() > 0) {
-                    // TODO: replace this with users locale
-                    Locale defaultLocale = Locale.getDefault();
-                    Locale locale = getPreferredLocale(defaultLocale, locales);
-
-                    // First replace messages with the preferred language 
labels
-                    String localeUrl = 
locales.get(locale).getMessages().toString();
-                    if (!localeUrl.isEmpty()) {
-                        String messageXML = 
cleanMessageXML(loadContentFromURL(localeUrl));
-                        MessageBundle bundle = new 
MessageBundle(locales.get(locale), messageXML);
-                        Substitutions substituter = new Substitutions();
-                        
substituter.addSubstitutions(Substitutions.Type.MESSAGE, bundle.getMessages());
-                        spec = spec.substitute(substituter);
-                    }
-
-                    // Replace the remaining messages with 'all' messages
-                    locale = getPreferredLocale(new Locale("all", "all"), 
locales);
-                    localeUrl = locales.get(locale).getMessages().toString();
-                    if (!localeUrl.isEmpty()) {
-                        String messageXML =
-                            
cleanMessageXML(loadContentFromURL(locales.get(locale).getMessages().toString()));
-                        MessageBundle bundle = new 
MessageBundle(locales.get(locale), messageXML);
-                        Substitutions substituter = new Substitutions();
-                        
substituter.addSubstitutions(Substitutions.Type.MESSAGE, bundle.getMessages());
-                        spec = spec.substitute(substituter);
-                    }
-                }
-
-                // Replace user preferences (syntax: __UP_<pref>__)
-                final UserPrefs prefs = loadUserPrefs(spec, userPrefs);
-                GadgetContext context = new GadgetContext() {
-                    public UserPrefs getUserPrefs() {
-                        return prefs;
-                    }
-                };
-                Substitutions substituter = new Substitutions();
-                UserPrefSubstituter userPrefSubs = new UserPrefSubstituter();
-                userPrefSubs.addSubstitutions(substituter, context, spec);
-                spec = spec.substitute(substituter);
-
+            GadgetSpec spec = loadGadgetSpec(gadgetUrl, userPrefs);
+            if (spec != null) {
                 // Copy attributes
                 gadgetSpec.put("title", spec.getModulePrefs().getTitle());
                 gadgetSpec.put("description", 
spec.getModulePrefs().getDescription());
@@ -230,6 +190,97 @@
         return null;
     }
 
+    private GadgetSpec loadGadgetSpec(final String gadgetUrl, Map<String, 
String> userPrefs)
+        throws ClientProtocolException, IOException, SpecParserException {
+        String xml = loadXMLFromCache(gadgetUrl);
+        if (xml != null) {
+            GadgetSpec spec = new GadgetSpec(Uri.parse(gadgetUrl), xml);
+
+            // Perform message substitution if locales are available
+            Map<Locale, LocaleSpec> locales = 
spec.getModulePrefs().getLocales();
+            if (locales != null && locales.size() > 0) {
+                // TODO: replace this with users locale
+                Locale defaultLocale = Locale.getDefault();
+                Locale locale = getPreferredLocale(defaultLocale, locales);
+
+                // First replace messages with the preferred language labels
+                String localeUrl = 
locales.get(locale).getMessages().toString();
+                if (!localeUrl.isEmpty()) {
+                    String messageXML = 
cleanMessageXML(loadContentFromURL(localeUrl));
+                    MessageBundle bundle = new 
MessageBundle(locales.get(locale), messageXML);
+                    Substitutions substituter = new Substitutions();
+                    substituter.addSubstitutions(Substitutions.Type.MESSAGE, 
bundle.getMessages());
+                    spec = spec.substitute(substituter);
+                }
+
+                // Replace the remaining messages with 'all' messages
+                locale = getPreferredLocale(new Locale("all", "all"), locales);
+                localeUrl = locales.get(locale).getMessages().toString();
+                if (!localeUrl.isEmpty()) {
+                    String messageXML =
+                        
cleanMessageXML(loadContentFromURL(locales.get(locale).getMessages().toString()));
+                    MessageBundle bundle = new 
MessageBundle(locales.get(locale), messageXML);
+                    Substitutions substituter = new Substitutions();
+                    substituter.addSubstitutions(Substitutions.Type.MESSAGE, 
bundle.getMessages());
+                    spec = spec.substitute(substituter);
+                }
+            }
+
+            // Replace user preferences (syntax: __UP_<pref>__)
+            final UserPrefs prefs = loadUserPrefs(spec, userPrefs);
+            GadgetContext context = new GadgetContext() {
+                public UserPrefs getUserPrefs() {
+                    return prefs;
+                }
+            };
+            Substitutions substituter = new Substitutions();
+            UserPrefSubstituter userPrefSubs = new UserPrefSubstituter();
+            userPrefSubs.addSubstitutions(substituter, context, spec);
+            spec = spec.substitute(substituter);
+
+            return spec;
+        }
+        return null;
+    }
+
+    public UserPrefsBean getUserPreferences(String gadgetUrl, Map<String, 
String> userPrefValues) {
+        try {
+            GadgetSpec spec = loadGadgetSpec(gadgetUrl, userPrefValues);
+            Map<String, UserPref> userPrefs = spec.getUserPrefs();
+            UserPrefsBean userPrefsBean = new UserPrefsBean();
+            for (String key : userPrefs.keySet()) {
+                UserPrefBean up = new UserPrefBean();
+                UserPref pref = userPrefs.get(key);
+                up.setDatatype(pref.getDataType().name());
+                up.setDefault_value(pref.getDefaultValue());
+                up.setDisplay_name(pref.getDisplayName());
+                up.setName(pref.getName());
+                up.setRequired(pref.getRequired());
+                if (userPrefValues != null && userPrefValues.containsKey(key)) 
{
+                    up.setValue(userPrefValues.get(key));
+                } else {
+                    up.setValue(pref.getDefaultValue());
+                }
+                userPrefsBean.addUserPref(up);
+            }
+            return userPrefsBean;
+        }
+        catch (ClientProtocolException e) {
+            m_logService.log(LogService.LOG_ERROR,
+                "Could not retrieve user preferences for gadget '" + gadgetUrl 
+ "'", e);
+        }
+        catch (SpecParserException e) {
+            m_logService.log(LogService.LOG_ERROR,
+                "Could not retrieve user preferences for gadget '" + gadgetUrl 
+ "'", e);
+        }
+        catch (IOException e) {
+            m_logService.log(LogService.LOG_ERROR,
+                "Could not retrieve user preferences for gadget '" + gadgetUrl 
+ "'", e);
+        }
+
+        return null;
+    }
+
     private String loadContentFromURL(String url) throws 
ClientProtocolException, IOException {
         if (url == null || url.isEmpty()) {
             return "";
@@ -399,4 +450,5 @@
     public String getOwner(final HttpServletRequest request) {
         return getViewer(request);
     }
+
 }

Modified: 
trunk/amdatu-opensocial/opensocial-shindig/src/main/resources/features/shindig.container/appdatauserprefstore.js
==============================================================================
--- 
trunk/amdatu-opensocial/opensocial-shindig/src/main/resources/features/shindig.container/appdatauserprefstore.js
    (original)
+++ 
trunk/amdatu-opensocial/opensocial-shindig/src/main/resources/features/shindig.container/appdatauserprefstore.js
    Tue Jan  3 09:01:53 2012
@@ -35,10 +35,10 @@
 shindig.AppDataBasedUserPrefStore.prototype.USER_PREFS_PREFIX =
     'gadgetUserPrefs-';
 
-shindig.AppDataBasedUserPrefStore.prototype.getPrefs = function(gadget) {
+// Loads the user specific preferences and sets them in the provide userPrefs
+shindig.AppDataBasedUserPrefStore.prototype.loadUserPrefs = function(gadget, 
userPrefs) {
   // 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;
@@ -65,9 +65,38 @@
     error: function(request, textStatus, errorThrown) {
     }
   });
-  return userPrefs;
 };
 
+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 = "/rest/widgets/" + gadget.widget.id + "/userpreferences";
+  url += "?st=" + gadget.secureToken;
+  url += "&uri=" + encodeURIComponent(gadget.specUrl);
+
+  jQuery.ajax({
+    url: url,
+    type: "GET",
+    contentType: "application/json",
+    dataType: "json",
+    async:false,
+    success: function(response) {
+       var result = response.UserPrefs;
+       $.each(result, function(UserPref, prefObj) {
+        $.each(prefObj, function (index, pref) {
+          userPrefs[pref.name] = pref.value;
+        });
+       });
+    },
+    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.
_______________________________________________
Amdatu-commits mailing list
[email protected]
http://lists.amdatu.org/mailman/listinfo/amdatu-commits

Reply via email to