Author: [email protected]
Date: Tue Aug 23 17:52:02 2011
New Revision: 1340
Log:
[AMDATUOPENSOCIAL-92] Finalized implementation, using Shindigs user preferences
dialog.
Modified:
trunk/amdatu-opensocial/opensocial-dashboard/src/main/resources/jsp/gadgets_appdata.js.jsp
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/GadgetManagementRESTServiceImpl.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
trunk/amdatu-opensocial/opensocial-shindig/src/main/resources/features/shindig.container/shindig-container.js
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
Tue Aug 23 17:52:02 2011
@@ -45,6 +45,9 @@
// Adds a gadget to the AppData of the current user
addWidgetToAppData = function(obj, dashboard) {
var widgets = retrieveWidgetsFromAppData(obj.metadata.secureToken);
+ if (typeof(widgets) == 'undefined') {
+ widgets = new Object();
+ }
// Append our widget
var widget = new Object();
@@ -114,6 +117,7 @@
// Adds a gadget to the gadget repository and return the widget wrapping the
gadget
addGadgetToRepository = function(uri, catId) {
var widget;
+ uri = trim(uri);
var url = "${contextPath}/rest/gadgetstore/gadgets?uri="+
encodeURIComponent(uri) + "&category=" + encodeURIComponent(catId);
jQuery.ajax({
url: url,
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 Aug 23 17:52:02 2011
@@ -76,7 +76,14 @@
* @return The OpenSocial owner associated with the specified HTTP Request.
*/
String getOwner(HttpServletRequest request);
-
- Map<String, String> getGadgetSpec(HttpServletRequest request, String
gadgetUrl);
-
+
+ /**
+ * Load the gadget specification for the given gadget URL. Optionally user
preferences can be passed,
+ * which will replace the __UP_..._ placeholders in the gadgetspec.
+ * @param request
+ * @param gadgetUrl
+ * @param userPrefs
+ * @return
+ */
+ Map<String, String> getGadgetSpec(HttpServletRequest request, String
gadgetUrl, Map<String, String> userPrefs);
}
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 Aug 23 17:52:02 2011
@@ -15,6 +15,9 @@
*/
package org.amdatu.opensocial.gadgetmanagement.service;
+import static
org.amdatu.opensocial.gadgetmanagement.OpenSocialContainer.APP_DATA;
+
+import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
@@ -24,6 +27,7 @@
import org.amdatu.auth.tokenprovider.TokenProvider;
import org.amdatu.auth.tokenprovider.TokenProviderException;
import org.amdatu.core.tenant.Tenant;
+import org.amdatu.libraries.utilities.ConversionUtil;
import org.amdatu.opensocial.gadgetmanagement.GadgetManagement;
import org.amdatu.opensocial.gadgetmanagement.OpenSocialContainer;
import org.amdatu.opensocial.gadgetmanagement.bean.Metadata;
@@ -100,8 +104,16 @@
return m_userAdmin;
}
- protected Widget getWidget(final HttpServletRequest request, final String
gadgetUrl) {
- Map<String, String> gadgetSpec =
getOpenSocialContainer().getGadgetSpec(request, gadgetUrl);
+ protected Widget getWidget(final HttpServletRequest request, final String
gadgetUrl, final String widgetId)
+ throws TokenProviderException, InvalidTokenException,
ClassNotFoundException, IOException {
+ // Load the user preferences for this widget
+ Map<String, String> userPrefs = null;
+ if (widgetId != null) {
+ userPrefs = getUserPreferences(request, widgetId);
+ }
+
+ Map<String, String> gadgetSpec =
getOpenSocialContainer().getGadgetSpec(request, gadgetUrl, userPrefs);
+
Widget widget = new Widget();
// Set metadata
@@ -148,7 +160,7 @@
return widget;
}
-
+
protected String toAbsoluteUrl(final String url, final HttpServletRequest
request) {
if (url.startsWith("/")) {
// This is a relative URL, convert to absolute URL
@@ -188,6 +200,24 @@
return null;
}
+
+ @SuppressWarnings("unchecked")
+ protected Map<String, String> getUserPreferences(final HttpServletRequest
request, final String widgetId)
+ throws ClassNotFoundException, IOException, TokenProviderException,
InvalidTokenException {
+ User user = getCurrentUser(request);
+ if (user != null) {
+ byte[] userPrefsBytes = null;
+ if (widgetId != null) {
+ userPrefsBytes = (byte[]) user.getProperties().get(APP_DATA +
".userprefs." + widgetId);
+ if (userPrefsBytes != null) {
+ Map<String, String> userPrefs =
+ (Map<String, String>)
ConversionUtil.byteArrayToObject(userPrefsBytes);
+ return userPrefs;
+ }
+ }
+ }
+ return null;
+ }
protected User getUserFromViewer(final HttpServletRequest request) {
String viewer = getOpenSocialContainer().getViewer(request);
Modified:
trunk/amdatu-opensocial/opensocial-gadgetmanagement/src/main/java/org/amdatu/opensocial/gadgetmanagement/service/GadgetManagementRESTServiceImpl.java
==============================================================================
---
trunk/amdatu-opensocial/opensocial-gadgetmanagement/src/main/java/org/amdatu/opensocial/gadgetmanagement/service/GadgetManagementRESTServiceImpl.java
(original)
+++
trunk/amdatu-opensocial/opensocial-gadgetmanagement/src/main/java/org/amdatu/opensocial/gadgetmanagement/service/GadgetManagementRESTServiceImpl.java
Tue Aug 23 17:52:02 2011
@@ -103,7 +103,7 @@
// Return all gadgets
for (GadgetDefinition gadgetDefinition :
getGadgetManagement().getGadgets()) {
boolean skip = false;
- Widget widget = getWidget(request,
toAbsoluteUrl(gadgetDefinition.getUrl(), request));
+ Widget widget = getWidget(request,
toAbsoluteUrl(gadgetDefinition.getUrl(), request), null);
if (widget != null) {
if (!"".equals(categoryId)) {
String category =
gadgetDefinition.getCategory().getId();
@@ -161,7 +161,7 @@
@QueryParam("uri") final String uri) {
try {
// Now generate the JSON expression to return
- Widget widget = getWidget(request, toAbsoluteUrl(uri, request));
+ Widget widget = getWidget(request, toAbsoluteUrl(uri, request),
null);
if (widget != null) {
// Be sure to add the security token
String st = getOpenSocialContainer().getSecurityToken(uri,
request);
@@ -253,6 +253,7 @@
if (!isAuthorized(request)) {
return
Response.status(Response.Status.UNAUTHORIZED).cacheControl(NO_CACHE_CONTROL).build();
}
+
if (uri == null || "".equals(uri) || categoryId == null ||
"".equals(categoryId)) {
return
Response.status(Response.Status.BAD_REQUEST).cacheControl(NO_CACHE_CONTROL).build();
}
@@ -278,7 +279,7 @@
}
// Now generate the JSON expression to return
- Widget widget = getWidget(request, toAbsoluteUrl(uri, request));
+ Widget widget = getWidget(request, toAbsoluteUrl(uri, request),
null);
if (widget != null) {
// Be sure to add the security token
String st = getOpenSocialContainer().getSecurityToken(uri,
request);
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 Aug 23 17:52:02 2011
@@ -74,7 +74,7 @@
gadgetUrl = toAbsoluteUrl(gadgetUrl, request);
}
- Widget widgetBean = getWidget(request, gadgetUrl);
+ Widget widgetBean = getWidget(request, gadgetUrl, widgetId);
// Set widget column properties
// TODO: setting proper layout still needs to be implemented
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 Aug 23 17:52:02 2011
@@ -56,8 +56,6 @@
import org.apache.shindig.gadgets.variables.UserPrefSubstituter;
import org.osgi.framework.Constants;
import org.osgi.service.log.LogService;
-import org.osgi.service.useradmin.Role;
-import org.osgi.service.useradmin.User;
import org.osgi.service.useradmin.UserAdmin;
/**
@@ -92,13 +90,13 @@
// Create a service dependency on the token provider for 'our' tenant
String tenantFilter =
"(&(" + Tenant.TENANT_ID_SERVICEPROPERTY + "=" + m_tenant.getId()
+ ")(" + Constants.OBJECTCLASS
- + "=" + TokenProvider.class.getName() + "))";
+ + "=" + TokenProvider.class.getName() + "))";
m_component.add(m_dependencyManager.createServiceDependency()
- .setService(TokenProvider.class,
tenantFilter).setRequired(true).setInstanceBound(true));
-
+ .setService(TokenProvider.class,
tenantFilter).setRequired(true).setInstanceBound(true));
+
String tenantFilter2 =
"(&(" + Tenant.TENANT_ID_SERVICEPROPERTY + "=" + m_tenant.getId()
+ ")(" + Constants.OBJECTCLASS
- + "=" + UserAdmin.class.getName() + "))";
+ + "=" + UserAdmin.class.getName() + "))";
m_component.add(m_dependencyManager.createServiceDependency()
.setService(UserAdmin.class,
tenantFilter2).setRequired(true).setInstanceBound(true));
@@ -142,29 +140,29 @@
}
return null;
}
-
- public Map<String, String> getGadgetSpec(final HttpServletRequest request,
final String gadgetUrl) {
+
+ public Map<String, String> getGadgetSpec(final HttpServletRequest request,
final String gadgetUrl, Map<String, String> userPrefs) {
Map<String, String> gadgetSpec = new HashMap<String, String>();
try {
m_logService.log(LogService.LOG_DEBUG, "Retrieving gadgetspec for
'" + gadgetUrl + "'");
String xml = loadXMLFromCache(new URL(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 messageXML = internalLoadXML(new
URL(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 the remaining messages with 'all' messages
locale = getPreferredLocale(new Locale("all", "all"),
locales);
messageXML = internalLoadXML(new
URL(locales.get(locale).getMessages().toString()));
@@ -173,9 +171,9 @@
substituter.addSubstitutions(Substitutions.Type.MESSAGE,
bundle.getMessages());
spec = spec.substitute(substituter);
}
-
+
// Replace user preferences (syntax: __UP_<pref>__)
- final UserPrefs prefs = loadUserPrefs(spec, getUser(request));
+ final UserPrefs prefs = loadUserPrefs(spec, userPrefs);
GadgetContext context = new GadgetContext() {
public UserPrefs getUserPrefs() {
return prefs;
@@ -184,23 +182,23 @@
Substitutions substituter = new Substitutions();
UserPrefSubstituter userPrefSubs = new UserPrefSubstituter();
userPrefSubs.addSubstitutions(substituter, context, spec);
- spec = spec.substitute(substituter);
-
- // Copy attributes __UP_title__
+ spec = spec.substitute(substituter);
+
+ // Copy attributes
gadgetSpec.put("title", spec.getModulePrefs().getTitle());
gadgetSpec.put("description",
spec.getModulePrefs().getDescription());
gadgetSpec.put("author", spec.getModulePrefs().getAuthor());
gadgetSpec.put("title_url",
spec.getModulePrefs().getTitleUrl().toString());
-
+
if (spec.getModulePrefs().getScreenshot() != null
- &&
!spec.getModulePrefs().getScreenshot().toString().isEmpty()) {
+ &&
!spec.getModulePrefs().getScreenshot().toString().isEmpty()) {
gadgetSpec.put("screenshot",
spec.getModulePrefs().getScreenshot().toString());
}
else {
gadgetSpec.put("screenshot", request.getContextPath() +
Activator.ALIAS +
- "/static/images/defaultgadget.png");
+ "/static/images/defaultgadget.png");
}
-
+
return gadgetSpec;
}
else {
@@ -212,39 +210,27 @@
m_logService.log(LogService.LOG_ERROR, "Could not retrieve gadget
spec for url '" + gadgetUrl + "'", e);
}
return null;
- }
-
- private User getUser(final HttpServletRequest request) {
- String viewer = getViewer(request);
- if (viewer != null && !"anonymous".equals(viewer)) {
- Role userRole = m_userAdmin.getRole(viewer);
- if (userRole.getType() == Role.USER) {
- return (User) userRole;
- }
- }
- return null;
}
-
- private UserPrefs loadUserPrefs(GadgetSpec spec, final User user) {
+
+ private UserPrefs loadUserPrefs(GadgetSpec spec, final Map<String, String>
userPrefs) {
// Default userpref values, when there are no user specific userprefs
stored (yet)
Map<String, UserPref> defaultUserPrefs = spec.getUserPrefs();
-
+
// First build the userprefs with the default values
Map<String, String> userPrefMap = new HashMap<String, String>();
for (String name : defaultUserPrefs.keySet()) {
userPrefMap.put(name,
defaultUserPrefs.get(name).getDefaultValue());
}
-
- // TODO: overrule default values with user specific user preferences
- if (user != null) {
- Object userPrefs = user.getProperties().get(USER_PREFS_PREFIX +
"." + spec.getUrl().toString());
- if (userPrefs != null) {
-
+
+ // Now overrule default values with user specific user preferences.
+ if (userPrefs != null) {
+ for (String name : userPrefs.keySet()) {
+ userPrefMap.put(name, userPrefs.get(name));
}
}
return new UserPrefs(userPrefMap);
}
-
+
// Returns the preferred locale in this order:
// 1 Locale that matches country & language of preferred locale
// 2 Locale that matches language of preferred locale
@@ -362,7 +348,7 @@
}
catch (IOException e) {
m_logService
- .log(LogService.LOG_ERROR, "Could not retrieve gadget XML from
url '" + url.toString() + "'", e);
+ .log(LogService.LOG_ERROR, "Could not retrieve gadget XML from url
'" + url.toString() + "'", e);
}
m_logService.log(LogService.LOG_DEBUG, "Retrieving gadgetspec '" + url
+ "' failed");
return null;
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 Aug 23 17:52:02 2011
@@ -44,7 +44,7 @@
var url = "/rpc?st=" + gadget.secureToken;
var postdata = '{"method":"appdata.get","id":"appdata.get","params":{';
- postdata += '"appId":"userprefs.' + gadget.id + '"';
+ postdata += '"appId":"userprefs.' + gadget.serviceName + '"';
postdata += ',"userId":"@me","fields":"@all","groupId":"@self"}}';
jQuery.ajax({
@@ -58,9 +58,7 @@
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;
+ userPrefs[prefname] = pref;
});
});
},
@@ -77,7 +75,7 @@
// does it again.
shindig.auth.updateSecurityToken(decodeURIComponent(gadget.secureToken));
osapi.appdata.update({
- appId: "userprefs." + gadget.id,
+ appId: "userprefs." + gadget.serviceName,
userId: "@me",
data: gadget.getUserPrefs() }).execute();
};
Modified:
trunk/amdatu-opensocial/opensocial-shindig/src/main/resources/features/shindig.container/shindig-container.js
==============================================================================
---
trunk/amdatu-opensocial/opensocial-shindig/src/main/resources/features/shindig.container/shindig-container.js
(original)
+++
trunk/amdatu-opensocial/opensocial-shindig/src/main/resources/features/shindig.container/shindig-container.js
Tue Aug 23 17:52:02 2011
@@ -191,9 +191,7 @@
var gadget = shindig.container.getGadget(id);
var prefs = gadget.getUserPrefs();
for (var i = 1, j = arguments.length; i < j; i += 2) {
- var prefValue = new Object();
- prefValue.value = arguments[i + 1];
- prefs[arguments[i]] = prefValue;
+ prefs[arguments[i]] = arguments[i + 1];
}
gadget.saveUserPrefs();
};
@@ -399,8 +397,7 @@
shindig.Gadget.prototype.getUserPrefValue = function(name) {
var pref = this.userPrefs[name];
- return typeof(pref.value) != 'undefined' && pref.value != null ?
- pref.value : pref['default'];
+ return typeof(pref) != 'undefined' && pref != null ? pref : '';
};
shindig.Gadget.prototype.render = function(chrome) {
@@ -555,13 +552,7 @@
shindig.BaseIfrGadget.prototype.hasViewablePrefs_ = function() {
- for (var name in this.getUserPrefs()) {
- var pref = this.userPrefs[name];
- if (pref.type != 'hidden') {
- return true;
- }
- }
- return false;
+ return this.getUserPrefs() != {};
};
@@ -614,11 +605,19 @@
var userPrefNamePrefix = 'm_' + this.id + '_up_';
var userPrefName = input.name.substring(userPrefNamePrefix.length);
var userPrefValue = input.value;
- this.userPrefs[userPrefName].value = userPrefValue;
+ this.userPrefs[userPrefName] = userPrefValue;
}
this.saveUserPrefs();
this.refresh();
+
+ // Title in the titlebar is an exception
+ if (typeof(this.userPrefs['title']) != 'undefined') {
+ var iframe = $(document.getElementById(this.serviceName));
+ var parent = iframe.parent().parent().parent();
+ var titlebar = parent.find('.widgettitle');
+ titlebar.html(this.userPrefs['title']);
+ }
};
shindig.BaseIfrGadget.prototype.handleCancelUserPrefs = function() {
@@ -955,16 +954,16 @@
// TODO: implement
shindig.Container.prototype.getGadgetFromId = function(serviceName) {
- for (var i = 0; i < this.gadgets_.length; i++) {
- if (this.gadgets_[i].serviceName = serviceName) {
- return this.gadgets_[i];
+ var gadget = null;
+ $.each(this.gadgets_, function(i, item) {
+ if (item.serviceName == serviceName) {
+ gadget = item;
}
- }
- return null;
+ });
+ return gadget;
};
shindig.IfrContainer.prototype.handleOpenUserPrefsDialog = function(id) {
- //var gadget = shindig.container.getGadgetFromId(id);
- //gadget.handleOpenUserPrefsDialog();
- alert("this function is not yet implemented");
+ var gadget = shindig.container.getGadgetFromId(id);
+ gadget.handleOpenUserPrefsDialog();
};
_______________________________________________
Amdatu-commits mailing list
[email protected]
http://lists.amdatu.org/mailman/listinfo/amdatu-commits