Finding uniqueName from ObjectId of the portlet and other way round

When your working with Portal Model then you might need a way to figure out unique name of the portlet from its ObjectId and other way. I built this POC to do that. In


import java.io.IOException;
import java.util.Iterator;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletRequest;
import javax.portlet.PortletResponse;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ibm.portal.ModelException;
import com.ibm.portal.ObjectID;
import com.ibm.portal.model.PortletModelHome;
import com.ibm.portal.portletmodel.PortletDefinition;
import com.ibm.portal.portletmodel.admin.AdminPortletModel;
import com.ibm.portal.portletmodel.admin.PortletDefinitionList;

public class PortletUniqueNamePortlet extends GenericPortlet{
PortletModelHome portletModelHome;

public void init() throws PortletException {
System.out.println("Entering PortalPOCPortlet.init()");
try {
InitialContext context = new InitialContext();
portletModelHome =(PortletModelHome) context.lookup("portal:service/model/PortletModel");
System.out.println("PortletModelHome " + portletModelHome);
} catch (NamingException e) {
e.printStackTrace(System.out);
}
System.out.println("Entering PortalPOCPortlet.init()");
}

protected void doView(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
response.setContentType("text/html");
response.getWriter().println("Object Id of the sitemap portlet " + getObjectIdOfPortlet(request, response, "wps.p.Sitemap"));
response.getWriter().println("
Unique Name of the sitemap portlet " + getUniqueNameOfPortlet(request, response, getObjectIDStr(getObjectIdOfPortlet(request, response, "wps.p.Sitemap"))));

}

public static String getObjectIDStr(ObjectID objectID){
String temp = objectID.toString();
int firstInd = temp.indexOf("'");
String result = temp.substring(firstInd+1, temp.indexOf("'", firstInd+1 ));
return result;
}

public ObjectID getObjectIdOfPortlet(PortletRequest request, PortletResponse response, String portletUniqueName){
System.out.println("Entering PortalPOCPortlet.getObjectIdOfPortlet()");
try {
AdminPortletModel adminModel = portletModelHome.getPortletModelProvider().getAdminPortletModel((HttpServletRequest)request, (HttpServletResponse)response);
PortletDefinition portletDef = adminModel.getPortletDefinitionList().getLocator().findByUniqueName(portletUniqueName);
return portletDef.getObjectID();
} catch (ModelException e) {
e.printStackTrace();
}
System.out.println("Exiting PortalPOCPortlet.getObjectIdOfPortlet()");
return null;
}

public String getUniqueNameOfPortlet(PortletRequest request, PortletResponse response, String portletObjectId){
System.out.println("Entering PortalPOCPortlet.getUniqueNameOfPortlet()");
try {
AdminPortletModel adminModel = portletModelHome.getPortletModelProvider().getAdminPortletModel((HttpServletRequest)request, (HttpServletResponse)response);
PortletDefinitionList portletDefinitionList = adminModel.getPortletDefinitionList();
Iterator portletDefinitionIterator = portletDefinitionList.iterator();
while(portletDefinitionIterator.hasNext()){
PortletDefinition portletDefinition = portletDefinitionIterator.next();
String currentObjectIdStr = getObjectIDStr(portletDefinition.getObjectID());
if(currentObjectIdStr.equals(portletObjectId)){
return portletDefinition.getObjectID().getUniqueName();
}
}
} catch (ModelException e) {
e.printStackTrace();
}
System.out.println("Not able to find portlet for given portletObjectId");
return null;
}
}

2 comments:

  1. The blog was absolutely fantastic! Lot of great information which can be helpful in some or the other way. Keep updating the blog, looking forward for more contents...Great job, keep it up.. Web Design Company Bangalore | Ecommerce web development company

    ReplyDelete