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;
}