I wanted to learn how the CC/PP works so i built this sample portlet, which queries list of all the profile attributes and then prints the attributes and there values in table format like this. You can download the sample code from here
This is source code of the portlet
package com.webspherenotes.misc;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import javax.ccpp.Attribute;
import javax.ccpp.Component;
import javax.ccpp.Profile;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletRequest;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
public class CPIPortlet extends GenericPortlet{
protected void doView(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
System.out.println("Entering CPIPortlet.doView()");
response.setContentType("text/html");
Profile clientProfile =
(Profile) request.getAttribute(PortletRequest.CCPP_PROFILE);
System.out.println("Profile Object " + clientProfile);
HashMap attributeMap = new HashMap();
if(clientProfile != null){
processProfile(clientProfile );
Iterator attributeIt = clientProfile.getAttributes().iterator();
while(attributeIt.hasNext()){
Attribute attribute = attributeIt.next();
if(attribute != null){
System.out.println(attribute.getName() + " = " + attribute.getValue());
attributeMap.put(attribute.getName(), attribute.getValue());
}
}
}
System.out.println("Attribute Map " + attributeMap);
request.setAttribute("clientProfileAttributeMap", attributeMap);
getPortletContext().getRequestDispatcher("/index.jsp").include(request, response);
System.out.println("Entering CPIPortlet.doView()");
}
public void processProfile(Profile profile) {
System.out.println("Entering processProfile");
Set comps = profile.getComponents();
for(Iterator i = comps.iterator(); i.hasNext(); ) {
Component comp = (Component) i.next();
System.out.println("Component: " + comp.getName());
Set attrs = comp.getAttributes();
for(Iterator j = attrs.iterator(); j.hasNext(); ) {
Attribute attr = (Attribute) j.next();
Object value = attr.getValue();
System.out.println("\tAttribute: " + attr.getName() +
" = " + attr.getValue());
}
}
System.out.println("Entering processProfile");
}
}
In the doView() method of the CPIPorltet i am reading the
PortletRequest.CCPP_PROFILE
attribute from the PortletRequest and then i am using the Profile attribute to first find out list of the attributes and then generating table of this data using JSP
Thanks for info....
ReplyDeleteSEO Company in Bangalore