14 November 2007

Best Practices: WLP10.0: Read Portlet Preferences

The Background

This post addresses the following portal setup:

  • WebLogic Portal 10.0
  • Portal is used in streaming mode (virtual portals, desktops, book/page/portlet instances
The requirements I had to fulfill were the following:
  • From anywhere in the whole portal, the business logic had to be able to read portlet preferences from any portlet instance (even from desktops different than the current desktop)

The Problem
I thought the implementation would be easy by using the PresentationContext API:
PortletPresentationContext px = PortletPresentationContext.getPortletPresentationContext(request);

this worked fine as long as the portlet I was reading the preferences from belongs to the desktop the current request belongs to.
In case the portlet instances was belonging to another desktop, that did not work.

The Workarounds
There are no workaronds...

The Solution
... but a solution! The following code shows
  • How you can get all the preferences of any portlet (getPortletPreferences())
  • How you should retrieve an EJB Home interface

/**
* @return null if portlet has no preferences or if portlet can not be retrieved. otherwise each key represents a preference, the value is Preference through which you retrieve the name, value or anything else from the preference.
*/
public static final Map getPortletPreferences(HttpServletRequest request, PortletView portletView) {
request = getRealRequest(request);
Map map = null;
try {
// init
CustomizationContext customizationContext = new CustomizationContext(Locale.ENGLISH);
getPortalCustomizationManager();
getPreferencePersistenceManager();

// read preferences
PortletInstance preferencesPortletInstance = portletView.getPortletInstance();
PortletPreferencesId portletPreferencesId = new PortletPreferencesId(preferencesPortletInstance.getPortletInstanceId().getId());
map = preferencePersistenceManager.getPreferences(customizationContext, portletPreferencesId);

if (map != null && map.isEmpty()) {
map = null;
}
} catch (Exception e) {
e.printStackTrace();
}
return map;
}

private static PortalCustomizationManager getPortalCustomizationManager() {
if (portalCustomizationManager == null) {
try {
PortalCustomizationManagerHome portalCustomizationManagerHome =
(PortalCustomizationManagerHome)getHome(new InitialContext(),PortalCustomizationManagerHome.LOOKUP_NAME);
portalCustomizationManager = portalCustomizationManagerHome.create();
} catch (Exception e) {
e.printStackTrace();
}
}
return portalCustomizationManager;
}

private static PreferencePersistenceManager getPreferencePersistenceManager() {
if (preferencePersistenceManager == null) {
try {
PreferencePersistenceManagerHome preferencePersistenceManagerHome =
(PreferencePersistenceManagerHome)getHome(new InitialContext(),PreferencePersistenceManagerHome.LOOKUP_NAME);
preferencePersistenceManager = preferencePersistenceManagerHome.create();
} catch (Exception e) {
e.printStackTrace();
}
}
return preferencePersistenceManager;
}

private static Object getHome(InitialContext context, String jndiName) throws NamingException
{
String fullJndiName = ApplicationHelper.getNonVersionedAppName()+ "." + jndiName;
Object obj = context.lookup(fullJndiName);
return obj;
}

/**
* @param request
* @return returns the original Request Object, not the wrapped pageflow request object.
*/
public static HttpServletRequest getRealRequest(HttpServletRequest request) {
if (request instanceof ScopedRequest) {
request = ((ScopedRequest)request).getOuterRequest();
}
return request;
}




Your Feedback
I also posted this in the official portal forum, so feedback is welcome at both places! Thanks.
http://forums.bea.com/thread.jspa?threadID=300003850

No comments: