Adding portlets to page on the fly

In the Creating portal pages on the fly, i talked about how you can create a portal page dynamically and redirect it to that page.

The DynamicUICtrl, has a addPortlet() methdo that you can use for adding portlet to page on the fly so i built this sample portlet to add com.webspherenotes.popup.portlet on the fly but it does not work


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 static final String TEMPLATE_PORTLET_UNIQUENAME = "com.webspherenotes.popup.portlet";

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 templatePortletObjectId = getObjectID(TEMPLATE_PORTLET_UNIQUENAME);
System.out.println("Template Portlet Object Id " + templatePortletObjectId);

ObjectID dynamicPortletObjectId = dynamicUIControl.addPortlet(templatePortletObjectId, new LocalizedImpl("Dynamic page", "Dynamic page demo"), null);

System.out.println("Lauch portlet object id " + dynamicPortletObjectId);
if(dynamicPortletObjectId == null)
return;

EngineURL dynamicPageURL = redirectURLGenerator.createPortletURL(dynamicPortletObjectId);
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;
}
}


The dynamicUIControl.addPortlet(templatePortletObjectId, new LocalizedImpl("Dynamic page", "Dynamic page demo"), null) method always returns null, i tried quite few combination but they do not help, i saw few question on ibm developerworks forum where other developers are also facing some problem but it seems that we will have to open a PMR with IBM and see how it goes