Creating a URL to different portlet mode and window state

In the Using PortletStateManagerService for creating URLs from within the portlet blog i talked about how to create a URL to portal page from inside portlet.

But what if you want to create a URL that will invoke the target portlet in say Edit mode and maximized window state, in that case you can use following code


private String getEditModeMaximizedURL(PortletRequest request, PortletResponse response){
PortletStateManager portletStateManager = null;
try {
portletStateManager = portletStateManagerService
.getPortletStateManager(request, response);
final URLFactory urlFct = portletStateManager.getURLFactory();
final EngineURL url = urlFct.newURL(null);

final SelectionAccessorFactory selectionAccessorFactory = portletStateManager
.getAccessorFactory(SelectionAccessorFactory.class);
SelectionAccessorController selectionAccessorCOntroller = selectionAccessorFactory
.getSelectionController(url.getState());
selectionAccessorCOntroller
.setSelection("com.webspherenotes.popup.static");

final PortletAccessorFactory portletFct =
(PortletAccessorFactory) portletStateManager
.getAccessorFactory(PortletAccessorFactory.class);
final PortletAccessorController portletCtrl =
portletFct
.getPortletController("com.webspherenotes.popup.control",
url.getState());
portletCtrl.getParameters().put("userName",
new String[] { "editModeSample" });

portletCtrl.setPortletMode(PortletMode.EDIT);
portletCtrl.setWindowState(WindowState.MAXIMIZED);


String finalURL = url.toString();
System.out.println("Portal Page URL " + finalURL);
return finalURL;
} catch (InvalidConstantException e) {
e.printStackTrace(System.out);
} catch (CannotInsertSelectionNodeException e) {
e.printStackTrace(System.out);
} catch (CannotCloneDocumentModelException e) {
e.printStackTrace(System.out);
} catch (CannotCreateDocumentException e) {
e.printStackTrace(System.out);
} catch (StateNotInRequestException e) {
e.printStackTrace(System.out);
} catch (MissingUniqueNameException e) {
e.printStackTrace(System.out);
} catch (UnknownUniqueNameException e) {
e.printStackTrace(System.out);
} catch (StateManagerException e) {
e.printStackTrace(System.out);
} catch (UnknownAccessorTypeException e) {
e.printStackTrace(System.out);
} catch (CannotInstantiateAccessorException e) {
e.printStackTrace(System.out);
} catch (CannotInstantiateURLFactoryException e) {
e.printStackTrace(System.out);
}
return null;
}



You can use the PortletAccessorController for setting a different portlet mode and window state for a portlet. After setting this is how my target page looks like in the edit mode

No comments: