Sunday 9 October 2011

Sample Code for DynamicUIManagementFactoryService in Websphere Portal Server


Dynamic user interfaces, or dynamic UIs, are portlets or pages that are dynamically created based on the definition of an existing page or portlet definition.Because of its dynamic nature, the interface is not persisted in the portal database and has a maximum lifetime of the user's session with the portal. The interface can also be closed prior to the end of the session either programmatically or by the user. When a portlet launches a dynamic page, the new page instance appears in the navigation under a node created just for containing dynamic pages. This node is called an extension node.

In this example,My current page uniquename is lanuchpage.

i Created a page (acts as dynamic page)  and uniquename for the page  is poppage.

To make poppage as extension node(dynamic page).

ConfigEngine.bat  action-enable-page-as-extension-node-wp.dynamicui.config
-DPageUniqueName=poppage

Sample Code:

package com.ibm.mydynamicuimanagementfactoryservice;
import java.io.*;
import java.util.*;
import javax.naming.CompositeName;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.Name;
import javax.portlet.*;

import util.LocalizedImpl;

import com.ibm.portal.ObjectID;
import com.ibm.portal.dynamicui.DynamicUICtrl;
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.wps.engine.localized.LocalizedContextHomeImpl;
import com.ibm.wps.engine.localized.LocalizedContextImpl;

/**
 * A sample portlet based on GenericPortlet
 */
public class MyDynamicUIManagementFactoryServicePortlet extends GenericPortlet {
    public static final String JSP_FOLDER    = "/_MyDynamicUIManagementFactoryService/jsp/";    // JSP folder name
    public static final String VIEW_JSP      = "MyDynamicUIManagementFactoryServicePortletView";         // JSP file name to be rendered on the view mode
    /**
     * @see javax.portlet.Portlet#init()
     */
    private DynamicUIManagementFactoryService dynamicUIManagerFactoryService;
    private RedirectURLGeneratorFactoryService redirectService;
    PortletServiceHome psh;
    public void init() throws PortletException{
        super.init();
        Context context;
        try {
            context = new InitialContext();
        // Obtain a reference to the Dynamic UI Management Factory service
            psh = (PortletServiceHome) context.lookup("portletservice/com.ibm.portal.portlet.service.dynamicui.DynamicUIManagementFactoryService");
            dynamicUIManagerFactoryService = (DynamicUIManagementFactoryService) psh.getPortletService(DynamicUIManagementFactoryService.class);
            psh = (PortletServiceHome)context.lookup("portletservice/com.ibm.portal.portlet.service.state.RedirectURLGeneratorFactoryService");
           redirectService = (RedirectURLGeneratorFactoryService)psh.getPortletService(RedirectURLGeneratorFactoryService.class);
    }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    /**
     * Serve up the <code>view</code> mode.
     *
     * @see javax.portlet.GenericPortlet#doView(javax.portlet.RenderRequest, javax.portlet.RenderResponse)
     */
    public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
        // Set the MIME type for the render response
        response.setContentType(request.getResponseContentType());
        PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher(getJspFilePath(request, VIEW_JSP));
        rd.include(request,response);
    }

    /**
     * Process an action request.
     *
     * @see javax.portlet.Portlet#processAction(javax.portlet.ActionRequest, javax.portlet.ActionResponse)
     */
     public void processAction(ActionRequest request, ActionResponse response) throws PortletException, java.io.IOException
        {
            try
            {
                    // Lookup ID of the page to be launched
                    Context ctx = new InitialContext();
                    Name uniqueName = new CompositeName("portal:uniquename");
                    uniqueName.add("lanuchpage");//Current page uniquename
                    ObjectID oidForUniqueName = (ObjectID) ctx.lookup(uniqueName);
                    // Launch dynamic page
                    DynamicUICtrl DynamicUICtrl = dynamicUIManagerFactoryService.getDynamicUICtrl(request, response,"poppage");
                    ObjectID launchedPageID = DynamicUICtrl.addPage(oidForUniqueName, new LocalizedImpl("Book flight", "Dynamic page to book a flight"), null);
                    // Redirect to launched page
                    RedirectURLGenerator redirector = redirectService.getURLGenerator(request, response);
                    EngineURL redirectURL = redirector.createPageURL(launchedPageID);
                    response.sendRedirect(redirectURL.toString());
                   
                }
            catch (Exception e)
            {
                e.printStackTrace();      
            }
        }

    /**
     * Returns JSP file path.
     *
     * @param request Render request
     * @param jspFile JSP file name
     * @return JSP file path
     */
    private static String getJspFilePath(RenderRequest request, String jspFile) {
        String markup = request.getProperty("wps.markup");
        if( markup == null )
            markup = getMarkup(request.getResponseContentType());
        return JSP_FOLDER + markup + "/" + jspFile + "." + getJspExtension(markup);
    }

    /**
     * Convert MIME type to markup name.
     *
     * @param contentType MIME type
     * @return Markup name
     */
    private static String getMarkup(String contentType) {
        if( "text/vnd.wap.wml".equals(contentType) )
            return "wml";
        else
            return "html";
    }

    /**
     * Returns the file extension for the JSP file
     *
     * @param markupName Markup name
     * @return JSP extension
     */
    private static String getJspExtension(String markupName) {
        return "jsp";
    }

}

Screen Shot


Click here to download source code


For more details









 

No comments:

Post a Comment