Thursday, 16 October 2014

Exploring NavigationModelProvider and identification provider in websphere portal

Navigation model provider :-  Using this navigation model provider we can get full navigation structure for portal right from root. We can navigate and display full navigation structure.

Usecase scenario1 :- We can take input as objectId and then construct page navigation from that node. Below code describes displaying all nodes at level one.

Lookup code for services :-   

public PortletServiceHome psh;
public Identification idf;


public boolean serviceAvailable = false;

try {
javax.naming.Context ctx = new javax.naming.InitialContext();
  psh = (PortletServiceHome) 
      ctx.lookup("portletservice/com.ibm.portal.portlet.service.model.NavigationModelProvider");
      serviceAvailable = true;
      idf = (Identification)ctx.lookup(Identification.JNDI_NAME);
}
catch(Exception ex) 
{
    ex.printStackTrace();

}


Overall portlet code :- 

package com.ibm.modelspiportlet;

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

import javax.portlet.*;

import java.util.logging.Logger;

import com.ibm.portal.ModelException;
import com.ibm.portal.ObjectID;
import com.ibm.portal.content.ContentNode;
import com.ibm.portal.content.ContentNodeType;
import com.ibm.portal.identification.Identification;
import com.ibm.portal.navigation.NavigationModel;
import com.ibm.portal.navigation.NavigationNode;
import com.ibm.portal.portlet.service.PortletServiceHome;
import com.ibm.portal.portlet.service.model.NavigationModelProvider;
import com.ibm.portal.serialize.SerializationException;

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

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

public static final String VIEW_JSP      = "ModelSpiPortletView";         // JSP file name to be rendered on the view mode

public PortletServiceHome psh;
public Identification idf;
public boolean serviceAvailable = false;
public static final Logger logger = Logger
.getLogger(GenericPortlet.class.getName());

 
/**
* @see javax.portlet.Portlet#init()
*/
public void init() throws PortletException{
super.init();
try {
javax.naming.Context ctx = new javax.naming.InitialContext();
  psh = (PortletServiceHome) 
      ctx.lookup("portletservice/com.ibm.portal.portlet.service.model.NavigationModelProvider");
      serviceAvailable = true;
      idf = (Identification)ctx.lookup(Identification.JNDI_NAME);
}
catch(Exception ex) 
{
    ex.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());
if (serviceAvailable) {
  NavigationModelProvider provider = (NavigationModelProvider)
                          psh.getPortletService(NavigationModelProvider.class);
  try {
//Retrieve current navigation model
NavigationModel model = provider.getNavigationModel(request, response);
//Get root and their children
Object navigationRoot = model.getRoot();
Iterator rootChildren = model.getChildren(navigationRoot);
if(rootChildren!=null){
while(rootChildren.hasNext()){
//select current navigation node and retrieve content node of it
NavigationNode navigationNode = (NavigationNode)rootChildren.next();
ContentNode contentNode = navigationNode.getContentNode();
ObjectID id = contentNode.getObjectID();
logger.info("***start***");
logger.info("Title of page in navigation:-"+contentNode.getTitle(Locale.ENGLISH));
logger.info("Description :-"+contentNode.getDescription(Locale.ENGLISH));
//serialize it to string to get objectId of string format
String oid = idf.serialize(id);
logger.info("objectId :-"+oid);
ContentNodeType contentNodeType = contentNode.getContentNodeType();
logger.info("Type of node"+contentNodeType.toString());
logger.info("***end***");
}
}
} catch (ModelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SerializationException 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";
}

}

No comments:

Post a Comment

Custom single threaded java server

 package com.diffengine.csv; import java.io.*; import java.net.*; import java.util.Date; public class Server { public static void main(Str...