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

3 comments:

srjwebsolutions said...


We are leading responsive website designing and development company in Noida.
We are offering mobile friendly responsive website designing, website development, e-commerce website, seo service and sem services in Noida.

Responsive Website Designing Company in Noida
Website Designing Company in Noida
SEO Services in Noida
SMO Services in Noida

Vikas Chaudhary said...

Battery Mantra is Authorized exide car battery dealer in Noida and Greater Noida. We are providing our service in Indirapuram, Delhi, Ashok Nagar.

Exide Battery Dealer in Noida
Battery Dealer in Noida
Authorized Battery Dealer in Noida
Car Battery Dealer in Noida
Car Battery Dealer
Exide Battery Dealer

EG MEDI said...

Egmedi.com is online medical store pharmacy in laxmi nagar Delhi. You can Order prescription/OTC medicines online.
Cash on Delivery available. Free Home Delivery


Online Pharmacy in Delhi
Buy Online medicine in Delhi
Online Pharmacy in laxmi nagar
Buy Online medicine in laxmi nagar
Onine Medical Store in Delhi
Online Medical store in laxmi nagar
Online medicine store in delhi
online medicine store in laxmi nagar
Purchase Medicine Online
Online Pharmacy India
Online Medical Store