Showing posts with label spi. Show all posts
Showing posts with label spi. Show all posts

Thursday, May 28, 2009

Using Model SPI to get Page title

IBM WebSphere Portal uses a concept of Models for content aggregation, to track user's pages etc. The Model SPI provides access to these models. The attached code provides an example to illustrate on how to get a title of a page using model SPI in JSR 286 portlet. Here is the code snippet to get title of page

public String getTitle(PortletRequest request, PortletResponse response)
{
Context context=null;
PortletServiceHome homeObject =null;
NavigationSelectionModelProvider provider=null;
NavigationSelectionModel navSelectionModel = null;

try
{
context = new InitialContext();
homeObject = (PortletServiceHome) context.lookup("portletservice/com.ibm.portal.portlet.service.model.NavigationSelectionModelProvider");
provider = (NavigationSelectionModelProvider) homeObject.getPortletService (NavigationSelectionModelProvider.class);
navSelectionModel = provider.getNavigationSelectionModel(request, response);
}
catch (Exception exception)
{
exception.printStackTrace();
}
NavigationNode selectedNode = (NavigationNode) navSelectionModel.getSelectedNode();
//Following is commented since there is no title for page if user's locale is US English
//String pageTitle = selectedNode.getTitle(request.getLocale());
String pageTitle = selectedNode.getTitle(Locale.ENGLISH);
return pageTitle;

}

Portlet shows English title of page. For locale of US English there is no title for page and you will see null value. You may comment this line of code and uncomment previous line of code to get title of page in your locale. Please login to the site to download war file.

Friday, April 10, 2009

Working with navigation model spi

Get the Current Selected Page
public static ObjectID getCurrentPage(PortletRequest request,
PortletResponse response) throws StateException, NamingException, IOException {
ObjectID oId = null;
try {
NavigationSelectionModelProvider provider = getNavigationSelectionModelProvider();

NavigationSelectionModel model = provider.getNavigationSelectionModel(request, response);
NavigationNode node = (NavigationNode)model.getSelectedNode();
oId node.getObjectID();
} catch (ModelException e) {
System.err.println("The current page could not be located = " + e);
}

return oId;
}

protected static NavigationSelectionModelProvider getNavigationSelectionModelProvider() {
NavigationSelectionModelProvider provider = null;
try {
if(navSelHome == null) {
Context ctx = new InitialContext();
navSelHome = (PortletServiceHome)ctx.lookup("portletservice/com.ibm.portal.portlet.service.model.NavigationSelectionModelProvider");
}
provider =(NavigationSelectionModelProvider) navSelHome.getPortletService(NavigationSelectionModelProvider.class);
} catch (Exception e) {
System.err.println("There was an error getting the navigation selection model provider = " + e);
}

return provider;
}


Create a Navigation Change URL
public static String generateUrl(
String pageName,
String portletName,
HashMap params,
boolean saveState,
PortletRequest request,
PortletResponse response)
throws StateException, NamingException, IOException {

final PortletStateManager mgr = getPortletStateManager(request, response);

// Get the URL factory
URLFactory urlFactory = mgr.getURLFactory();
String finalUrl = "";

try {
final EngineURL url ;

if(saveState) {
url = urlFactory.newURL(Constants.SMART_COPY);
} else {
url = urlFactory.newURL(Constants.EMPTY_COPY);
}
// Set the page this URL should point to
final com.ibm.portal.state.accessors.selection.SelectionAccessorFactory selectionFactory = (com.ibm.portal.state.accessors.selection.SelectionAccessorFactory) mgr.getAccessorFactory(com.ibm.portal.state.accessors.selection.SelectionAccessorFactory.class);
// Request the selection controller to set the page; pass in the state associated with the created URL
final SelectionAccessorController selectionCtrl = selectionFactory.getSelectionAccessorController(url.getState());
// Set the page; you need the unique name (String) or the ObjectID of that page
selectionCtrl.setSelection(pageName);
// Dispose the accessor (avoids memory leak)
selectionCtrl.dispose();
if(portletName != null) {
// Set portlet render parameters
final PortletAccessorFactory portletAccessorFactory = (PortletAccessorFactory) mgr.getAccessorFactory(PortletAccessorFactory.class);
// Get the portlet controller to set render parameters; pass in the state associated with rge created URL
final PortletAccessorController portletCtrl = portletAccessorFactory.getPortletAccessorController(portletName, url.getState());
// Set the render parameter
if(params != null) {
portletCtrl.getParameters().putAll(params);
}
// Dispose the accessor (avoids memory leak)
portletCtrl.dispose();
}
finalUrl = url.writeDispose(new StringWriter()).toString();
} finally {
urlFactory.dispose();
}
return finalUrl;
}