Creating portal pages on the fly

WebSphere Portal has concept of DynamicUIManagementFactoryService that you can use to create portal pages on the fly, add portlets to the pages,..etc. The pages that you create or portlets that you add with this method are available only in the current user session, so once user logs off these changes would be lost.

I followed the instructions in Exploiting the WebSphere Portal 5.1.0.1 programming model: Part 4: Making your portal dynamic and context sensitive article to create a sample portlet that will create a dynamic page and then display that page in dialog box. THis is how my sample portlet looks like


package com.webspherenotes.portlet;

import java.io.IOException;

import javax.naming.CompositeName;
import javax.naming.InitialContext;
import javax.naming.InvalidNameException;
import javax.naming.Name;
import javax.naming.NamingException;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import com.ibm.portal.ObjectID;
import com.ibm.portal.dynamicui.DynamicUICtrl;
import com.ibm.portal.dynamicui.DynamicUIManagementException;
import com.ibm.portal.portlet.service.PortletServiceHome;
import com.ibm.portal.portlet.service.dynamicui.DynamicUIManagementFactoryService;
import com.ibm.portal.portlet.service.state.RedirectURLGeneratorFactoryService;
import com.ibm.portal.state.EngineURL;
import com.ibm.portal.state.RedirectURLGenerator;
import com.ibm.portal.state.exceptions.StateException;

public class DynamicUIPortlet extends GenericPortlet {

public static final String EXTENSION_PAGE_UNIQUENAME="com.webspherenotes.popup.extension";
public static final String TEMPLATE_PAGE_UNIQUENAME="com.webspherenotes.popup.dynamic";

public void processAction(ActionRequest request, ActionResponse response)
throws PortletException, IOException {
System.out.println("Entering DynamicUIPortlet.processAction()");

try {
RedirectURLGenerator redirectURLGenerator = redirectURLGeneratorService.getURLGenerator(request, response);


DynamicUICtrl dynamicUIControl= dynamicUIManagerFactoryService.getDynamicUICtrl(request, response, EXTENSION_PAGE_UNIQUENAME);

ObjectID templatePageObjectId = getObjectID(TEMPLATE_PAGE_UNIQUENAME);
System.out.println("Template Page Object Id " + templatePageObjectId);
ObjectID dynamicPageObjectId = dynamicUIControl.addPage(templatePageObjectId, new LocalizedImpl("Dynamic page", "Dynamic page demo"), null);

System.out.println("Lauch page object id " + dynamicPageObjectId);

EngineURL dynamicPageURL = redirectURLGenerator.createPageURL(dynamicPageObjectId);
String dynamicPageURLStr = dynamicPageURL.toString();

System.out.println("Dynamic Page URL " + dynamicPageURLStr);
response.sendRedirect(dynamicPageURLStr);
} catch (DynamicUIManagementException e) {
e.printStackTrace(System.out);
} catch (StateException e) {
e.printStackTrace(System.out);
}
System.out.println("Exiting DynamicUIPortlet.processAction()");
}

protected void doView(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
System.out.println("Entering DynamicUIPortlet.doView()");
getPortletContext().getRequestDispatcher("/index.jsp").include(request, response);
System.out.println("Exiting DynamicUIPortlet.doView()");
}

RedirectURLGeneratorFactoryService redirectURLGeneratorService;
DynamicUIManagementFactoryService dynamicUIManagerFactoryService;

public void init() throws PortletException {
System.out.println("ENtering DynamicUIPortlet.init()");
try {
InitialContext ctx = new InitialContext();
final PortletServiceHome dynamicUIManagerFactoryServiceHome = (PortletServiceHome) ctx
.lookup("portletservice/com.ibm.portal.portlet.service.dynamicui.DynamicUIManagementFactoryService");
dynamicUIManagerFactoryService = (DynamicUIManagementFactoryService) dynamicUIManagerFactoryServiceHome
.getPortletService(DynamicUIManagementFactoryService.class);

final PortletServiceHome redirectServiceHome = (PortletServiceHome) ctx
.lookup("portletservice/com.ibm.portal.portlet.service.state.RedirectURLGeneratorFactoryService");
redirectURLGeneratorService = (RedirectURLGeneratorFactoryService) redirectServiceHome
.getPortletService(RedirectURLGeneratorFactoryService.class);
} catch (NamingException e) {
e.printStackTrace(System.out);
}
System.out.println("Exiting DynamicUIPortlet.init()");
}

private ObjectID getObjectID(String uniqueNameStr) {
try {
InitialContext ctx = new InitialContext();
final Name uniqueName = new CompositeName("portal:uniquename");
uniqueName.add(uniqueNameStr);
ObjectID oidForUniqueName = (ObjectID) ctx.lookup(uniqueName);
return oidForUniqueName;
} catch (InvalidNameException e) {
e.printStackTrace(System.out);
} catch (NamingException e) {
e.printStackTrace(System.out);
}
return null;
}
}


In the doView() method of the DynamicUIPortlet, i am forwarding control to index.jsp page, which looks like this

<%@page language="java" contentType="text/html; %>
<%@taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="/WEB-INF/tld/portal.tld" prefix="portal" %>

<portlet:defineObjects />

<script type="text/javascript">
<!--
function openPopup(popupUrl){
if (window.showModalDialog) {
window.showModalDialog(popupUrl,"name","dialogWidth:455px;dialogHeight:450px");
} else {
window.open(popupUrl,'name',
'height=500,width=500,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,
resizable=no ,modal=yes');
}
}
//-->
</script>



<input type="button" onclick="openPopup('<portlet:actionURL/>')" value="Dynamic Page" />
<br/>


When you click on the DYnamic Page button it creates a actionURL and opens it in the dialog box.

The DynamicUIService has restriction that you can call it only during the action phase of the portlet, so inside the processAction() method i am calling dynamicUIControl.addPage() method to add a dynamic page and then create redirectURL to the newly created page by calling redirectURLGenerator.createPageURL() method and then redirecting it to that URL. This is the UI that i get when i click on the Dynamic Page button



1 comment: