Sunday, 9 October 2011

Sample Code for ContentModelProvider in Websphere portal server

The content model defines a tree structure for content elements (such as pages and labels); it is used to group these logically.

Each content node can provide the type of content node it represents; if this type is ContentNodeType.PAGE, a model exists that represents the layout of that page. It can be obtained with the getLayoutModel method of the content model. The layout model holds LayoutNode elements that describe the layout and contents of the page. 

Each layout node can return metrics in form of the LayoutMetrics interface. It can describe attributes of the node such as width or orientation (horizontal/vertical).

The elements of the layout model are either LayoutContainers that define rows and columns or LayoutControls, which represent portlets. This information is used when a page is rendered.

Sample Code:

package com.ibm.mycontentmodelprovider;
import java.io.*;
import java.util.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.portlet.*;
import com.ibm.portal.content.ContentModel;
import com.ibm.portal.content.ContentNode;
import com.ibm.portal.content.ContentPage;
import com.ibm.portal.content.LayoutMetrics;
import com.ibm.portal.content.LayoutModel;
import com.ibm.portal.content.LayoutNode;
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.ContentModelProvider;
import com.ibm.portal.portlet.service.model.NavigationSelectionModelProvider;
import com.ibm.wps.composition.elements.UnlayeredContainer;

/**
 * A sample portlet based on GenericPortlet
 */
public class MyContentModelProviderPortlet extends GenericPortlet {
    public static final String JSP_FOLDER    = "/_MyContentModelProvider/jsp/";    // JSP folder name
    public static final String VIEW_JSP      = "MyContentModelProviderPortletView";         // JSP file name to be rendered on the view mode
    /**
     * @see javax.portlet.Portlet#init()
     */
    PortletServiceHome psh;
    public void init() throws PortletException{
        super.init();
        Context context;
        try {
            context = new InitialContext();
            psh = (PortletServiceHome) context.lookup(ContentModelProvider.JNDI_NAME);
        }
        catch(Exception exany)
        {
            exany.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());

        try
        {
            ContentModelProvider provider = (ContentModelProvider) psh.getPortletService(ContentModelProvider.class);
            ContentModel model = provider.getContentModel(request, response);
            NavigationSelectionModelProvider navprovider = (NavigationSelectionModelProvider) psh.getPortletService(NavigationSelectionModelProvider.class);
            NavigationSelectionModel navmodel = navprovider.getNavigationSelectionModel(request, response);
            NavigationNode selectedNode = (NavigationNode) navmodel.getSelectedNode();
            ContentNode contentNode = selectedNode.getContentNode();
            ContentPage contentPage =(ContentPage) model.getLocator().findByID(contentNode.getObjectID());
            LayoutModel layoutModel = model.getLayoutModel(contentPage);
            LayoutNode navNode=(LayoutNode)layoutModel.getRoot();
            LayoutMetrics metrics = navNode.getLayoutMetrics();
            Object info = metrics.getValue(LayoutMetrics.ORIENTATION);
            if (layoutModel.hasChildren(navNode)) {
               for (Iterator it = layoutModel.getChildren(navNode); it.hasNext();) {
                   UnlayeredContainer un=(UnlayeredContainer)it.next();
                   System.out.println(un.getTemplateName());
                   System.out.println(un.getCreated());
                   System.out.println(un.getLastModified());
               }
            }
   
         
        }
        catch(Exception exany)
        {
            exany.printStackTrace();
        }

        // Invoke the JSP to render
        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 {
       
    }

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

}
 
Click here to download source code

No comments:

Post a Comment