Saturday, 8 October 2011

Sample Code for ContentMetaDataModelProvider in Websphere Portal Server


Sometimes you may need to access page parameter, ContentMetaDataModelProvider is used for that.
These parameters are referred as page Meta data. With help of ContentMetaDataModelProvider and NavigationModelProvider, we can access these parameters.

Set Page Parameter:
1.       Click on Advanced Option of page properties of any page
2.       Click   I want to set parameters link
3.       Set the parameter  name and value and Click “OK”   

In    In this example,I have set two parameter.



Check with Parameter is set in the page:
Export the page and in export xml, we will notice your parameter
<content-node action="update" active="true" allportletsallowed="true" content-parentref="Z6_CGAH47L0085810IAHU76SD20A3" create-type="explicit" domain="rel" objectid="Z6_IARJNKG10GDB70I97EUIBM20G5" ordinal="1100" themeref="ZJ_CGAH47L0085810IAHU76SD20H0" type="page">
   
            <parameter name="meta1" type="string" update="set"><![CDATA[sample1]]></parameter>
            <parameter name="meta2" type="string" update="set"><![CDATA[sample2]]></parameter>
        </content-node>

Sample code:  

package com.ibm.mycontentmetadatamodelprovider;

import java.io.*;
import java.util.*;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.portlet.*;

import com.ibm.portal.MetaData;
import com.ibm.portal.ModelException;
import com.ibm.portal.content.ContentMetaDataModel;
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.ContentMetaDataModelProvider;
import com.ibm.portal.portlet.service.model.NavigationSelectionModelProvider;

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

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

    public static final String VIEW_JSP      = "MyContentMetaDataModelProviderPortletView";         // 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(ContentMetaDataModelProvider.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 {
            ContentMetaDataModelProvider provider = (ContentMetaDataModelProvider) psh.getPortletService(ContentMetaDataModelProvider.class);
            ContentMetaDataModel model = provider.getContentMetaDataModel(request, response);
            NavigationSelectionModelProvider navprovider = (NavigationSelectionModelProvider) psh.getPortletService(NavigationSelectionModelProvider.class);
            NavigationSelectionModel navmodel = navprovider.getNavigationSelectionModel(request, response);
            NavigationNode selectedNode = (NavigationNode) navmodel.getSelectedNode();
            if (selectedNode != null) {
                MetaData md = model.getMetaData(selectedNode);
                HashMap map=new HashMap();
                Iterator itr = md.iterator();
                while(itr.hasNext()){
                    Map.Entry entry = (Map.Entry) itr.next();
                    map.put(entry.getKey(),entry.getValue());
                   
            }
                request.setAttribute("result", map);
            }

        } catch (ModelException 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);
    }

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

}

ScreenShot:

Click here to download Sample Code



No comments:

Post a Comment