Thursday, 13 October 2011

Sample Code for NavigationSelectionModelProvider in Websphere Portal Server

When a user is navigating through the portal, the currently selected navigation node is important to render the current page. The NavigationSelectionModel reflects the current selection. It represents a list that defines a path through the navigation model.

The last node of this path is always the currently selected navigation node, and its referenced content node represents what is aggregated for the user to see (normally a page represented through a ContentPage object).

The following example shows how to render a breadcrumb trail that shows the current selection path.


Sample Code:

package com.ibm.mynavigationselectionmodelprovider;
import java.io.*;
import java.util.ArrayList;
import java.util.Locale;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NameNotFoundException;
import javax.naming.NamingException;
import javax.portlet.*;
import com.ibm.portal.ModelException;
import com.ibm.portal.content.ContentNode;
import com.ibm.portal.navigation.NavigationNode;
import com.ibm.portal.navigation.NavigationSelectionModel;
import com.ibm.portal.portlet.service.PortletServiceHome;
import com.ibm.portal.portlet.service.model.NavigationSelectionModelProvider;
import com.ibm.portal.state.EngineURL;
import com.ibm.portal.state.PortletStateManager;
import com.ibm.portal.state.URLFactory;
import com.ibm.portal.state.accessors.selection.SelectionAccessorController;
import com.ibm.portal.state.accessors.selection.SelectionAccessorFactory;
import com.ibm.portal.state.service.PortletStateManagerService;


/**
 * A sample portlet based on GenericPortlet
 */
public class MyNavigationSelectionModelProviderPortlet extends GenericPortlet {

    public static final String JSP_FOLDER    = "/_MyNavigationSelectionModelProvider/jsp/";    // JSP folder name

    public static final String VIEW_JSP      = "MyNavigationSelectionModelProviderPortletView";         // JSP file name to be rendered on the view mode
   
    PortletServiceHome psh;
    protected PortletStateManagerService service;
    private static final String JNDI_NAME =
        "portletservice/com.ibm.portal.state.service.PortletStateManagerService";
    /**
     * @see javax.portlet.Portlet#init()
     */
    public void init() throws PortletException{
        super.init();
        try {
            Context ctx = new InitialContext();
            psh = (PortletServiceHome) ctx.lookup(NavigationSelectionModelProvider.JNDI_NAME);
             PortletServiceHome serviceHome = (PortletServiceHome)ctx.lookup(JNDI_NAME);
            service = (PortletStateManagerService)
            serviceHome.getPortletService(PortletStateManagerService.class);
            } catch(NameNotFoundException ex) {
                ex.printStackTrace();
            } catch (NamingException e) {
                // TODO Auto-generated catch block
                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());
        NavigationSelectionModelProvider provider = (NavigationSelectionModelProvider) psh.getPortletService(NavigationSelectionModelProvider.class);
        try {
            NavigationSelectionModel model = provider.getNavigationSelectionModel(request, response);
            NavigationNode currentNavNode = (NavigationNode) model.getSelectedNode();
            ContentNode currentContentNode = currentNavNode.getContentNode();
            ArrayList<Item> pages=new ArrayList<Item>();
             pages.clear();
             for (java.util.Iterator i = model.iterator(); i.hasNext(); )
                {
                    NavigationNode node = (NavigationNode) i.next();
                    if (i.hasNext()) {
                        String title=node.getTitle(Locale.ENGLISH);
                        String uniqueName=node.getObjectID().getUniqueName();
                        String url=createPageURL(uniqueName, request,  response);
                        pages.add(new Item(url, title));
                    }
                    else
                    {
                        request.setAttribute("currentnode",node.getTitle(Locale.ENGLISH));
                    }
                }
             request.setAttribute("result", pages);
           
        } catch (ModelException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // Invoke the JSP to render
        PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher(getJspFilePath(request, VIEW_JSP));
        rd.include(request,response);
    }

    /**
     * SelectionAccessorFactory:
     * Provides accessors to read and write portal page selection information.
     * Use the SelectionAccessorController to create a URL to another portal page.
     */
    public String createPageURL(final String uniquepagename,PortletRequest request, PortletResponse response) throws Exception {
            // get the needed factories
        URLFactory urlFactory=null;
        SelectionAccessorController selCtrl=null;
        try{
         PortletStateManager manager = service.getPortletStateManager(request, response);
         urlFactory = manager.getURLFactory();
         EngineURL url=urlFactory.newURL(null);
         SelectionAccessorFactory selFct = (SelectionAccessorFactory)manager.getAccessorFactory(SelectionAccessorFactory.class);
         selCtrl = selFct.getSelectionAccessorController(url.getState());
         selCtrl.setSelection(uniquepagename);
         return url.writeDispose(new StringWriter()).toString();
          }
        catch(Exception e)
        {
            e.printStackTrace();
            throw e;
        }
        finally
        {
                    if(null !=selCtrl)selCtrl.dispose();
                    if(null !=urlFactory)urlFactory.dispose();
          }
        }
    /**
     * 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 {
    }

    /**
     * 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";
    }

}

ScreenShots:


Click here to download the sample code













No comments:

Post a Comment