I needed to develop an iView which shows role content in SAP NetWeaver Portal. I just wanted to share my approach with the community. Here is what you need to do:
<?xml version="1.0" encoding="utf-8"?>
<application>
<application-config>
<property name="PrivateSharingReference" value="com.sap.portal.htmlb"/>
</application-config>
<components>
<component name="RoleContent">
<component-config>
<property name="ClassName" value="com.saptr.portal.RoleContent"/>
</component-config>
<component-profile>
<property name="TargetRoleName" value="pcd:portal_content/administrator/super_admin/super_admin_role"/>
</component-profile>
</component>
</components>
<services/>
</application>
package com.saptr.portal;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NameClassPair;
import javax.naming.NamingEnumeration;
import com.sap.portal.directory.Constants;
import com.sap.security.api.IRole;
import com.sap.security.api.UMFactory;
import com.sapportals.htmlb.page.DynPage;
import com.sapportals.htmlb.page.PageException;
import com.sapportals.portal.htmlb.page.PageProcessorComponent;
import com.sapportals.portal.pcd.gl.IPcdAttribute;
import com.sapportals.portal.pcd.gl.IPcdContext;
import com.sapportals.portal.prt.component.IPortalComponentProfile;
import com.sapportals.portal.prt.component.IPortalComponentRequest;
import com.sapportals.portal.prt.component.IPortalComponentResponse;
public class RoleContent extends PageProcessorComponent {
public DynPage getPage() {
return new RoleContentDynPage();
}
public static class RoleContentDynPage extends DynPage {
private InitialContext ctx;
private IPortalComponentRequest request;
private IPortalComponentResponse response;
private String roleUniqueName;
private String imgPath;
/**
* Initialization code executed once per user.
*/
public void doInitialization() {
Hashtable env = new Hashtable();
try {
request = (IPortalComponentRequest)this.getRequest();
response = (IPortalComponentResponse)this.getResponse();
env.put(Context.INITIAL_CONTEXT_FACTORY, IPcdContext.PCD_INITIAL_CONTEXT_FACTORY);
env.put(Constants.REQUESTED_ASPECT, IPcdAttribute.PERSISTENCY_ASPECT);
env.put(Context.SECURITY_PRINCIPAL, request.getUser());
ctx = new InitialContext(env);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Input handling code. In general called the first time with the second page request from the user.
*/
public void doProcessAfterInput() throws PageException {
}
/**
* Create output. Called once per request.
*/
public void doProcessBeforeOutput() throws PageException {
try {
imgPath = request.getWebResourcePath() + "/images/";
IPortalComponentProfile profile = request.getComponentContext().getProfile();
roleUniqueName = profile.getProperty("TargetRoleName");
IRole role = UMFactory.getRoleFactory().getRoleByUniqueName(roleUniqueName);
String roleRoot = role.getUniqueName();
response.write("<table border=\"0\" cellspacing=\"5\" cellpadding=\"5\">");
response.write("<tr>");
response.write("<td><img src=\"" + imgPath + "Role.gif\"> Role</td>");
response.write("<td><img src=\"" + imgPath + "Workset.gif\"> Workset</td>");
response.write("<td><img src=\"" + imgPath + "Folder.gif\"> Folder</td>");
response.write("<td><img src=\"" + imgPath + "Page.gif\"> Page</td>");
response.write("<td><img src=\"" + imgPath + "iView.gif\"> iView</td>");
response.write("</tr>");
response.write("<tr>");
response.write("<td colspan=\"5\">");
try {
IPcdContext rootRole = (IPcdContext)ctx.lookup(roleRoot);
Object titleObj = rootRole.getAttributes("").get("com.sap.portal.pcm.Title");
if (titleObj instanceof IPcdAttribute) {
IPcdAttribute titleAtt = (IPcdAttribute) titleObj;
String title = titleAtt.get(request.getLocale());
response.write("<strong>'" + title + "' (" + rootRole.getAtomicName() + ") Role Content</strong>");
}
} catch (Exception e) {
response.write("<strong>'" + roleRoot + "' Role Content</strong>");
}
response.write("<ul>");
this.getChildren(request, response, roleRoot);
response.write("</ul>");
response.write("</ul>");
response.write("</td>");
response.write("</tr>");
response.write("</table>");
} catch (Exception e) {
response.write(e.getLocalizedMessage());
e.printStackTrace();
}
}
private void getChildren(IPortalComponentRequest request,IPortalComponentResponse response, String nodeName) {
try{
NamingEnumeration names = ctx.list(nodeName);
if (names.hasMoreElements()) {
NameClassPair nameClass;
while (names.hasMore()) {
nameClass = (NameClassPair) names.next();
String newRoot = nodeName + "/" + nameClass.getName();
IPcdContext pcdObj = (IPcdContext)ctx.lookup(newRoot);
if(nameClass.getClassName().contains("Role")){
response.write("<ul>");
try {
IPcdContext role = (IPcdContext)ctx.lookup(newRoot);
Object titleObj = role.getAttributes("").get("com.sap.portal.pcm.Title");
if (titleObj instanceof IPcdAttribute) {
IPcdAttribute titleAtt = (IPcdAttribute) titleObj;
String title = titleAtt.get(request.getLocale());
response.write("<li style=\"list-style-image: url(" + imgPath + "Role.gif);\">" + title + "</li>");
}
} catch (Exception e) {
response.write("<li style=\"list-style-image: url(" + imgPath + "Role.gif);\">" + pcdObj.getAtomicName() + "</li>");
}
response.write("<ul>");
this.getChildren(request, response, newRoot);
response.write("</ul>");
response.write("</ul>");
} else if(nameClass.getClassName().contains("Workset")){
response.write("<ul>");
try {
IPcdContext workset = (IPcdContext)ctx.lookup(newRoot);
Object titleObj = workset.getAttributes("").get("com.sap.portal.pcm.Title");
if (titleObj instanceof IPcdAttribute) {
IPcdAttribute titleAtt = (IPcdAttribute) titleObj;
String title = titleAtt.get(request.getLocale());
response.write("<li style=\"list-style-image: url(" + imgPath + "Workset.gif);\">" + title + "</li>");
}
} catch (Exception e) {
response.write("<li style=\"list-style-image: url(" + imgPath + "Workset.gif);\">" + pcdObj.getAtomicName() + "</li>");
}
response.write("<ul>");
this.getChildren(request, response, newRoot);
response.write("</ul>");
response.write("</ul>");
}else if(nameClass.getClassName().contains("Folder")){
response.write("<ul>");
try {
IPcdContext folder = (IPcdContext)ctx.lookup(newRoot);
Object titleObj = folder.getAttributes("").get("com.sap.portal.pcm.Title");
if (titleObj instanceof IPcdAttribute) {
IPcdAttribute titleAtt = (IPcdAttribute) titleObj;
String title = titleAtt.get(request.getLocale());
response.write("<li style=\"list-style-image: url(" + imgPath + "Folder.gif);\">" + title + "</li>");
}
} catch (Exception e) {
response.write("<li style=\"list-style-image: url(" + imgPath + "Folder.gif);\">" + pcdObj.getAtomicName() + "</li>");
}
response.write("<ul>");
this.getChildren(request, response, newRoot);
response.write("</ul>");
response.write("</ul>");
}else if(nameClass.getClassName().contains("Page")){
response.write("<ul>");
try {
IPcdContext page = (IPcdContext)ctx.lookup(newRoot);
Object titleObj = page.getAttributes("").get("com.sap.portal.pcm.Title");
if (titleObj instanceof IPcdAttribute) {
IPcdAttribute titleAtt = (IPcdAttribute) titleObj;
String title = titleAtt.get(request.getLocale());
response.write("<li style=\"list-style-image: url(" + imgPath + "Page.gif);\">" + title + "</li>");
}
} catch (Exception e) {
response.write("<li style=\"list-style-image: url(" + imgPath + "Page.gif);\">" + pcdObj.getAtomicName() + "</li>");
}
response.write("<ul>");
this.getChildren(request, response, newRoot);
response.write("</ul>");
response.write("</ul>");
}else if(nameClass.getClassName().contains("iView")){
try {
IPcdContext iview = (IPcdContext)ctx.lookup(newRoot);
Object titleObj = iview.getAttributes("").get("com.sap.portal.pcm.Title");
if (titleObj instanceof IPcdAttribute) {
IPcdAttribute titleAtt = (IPcdAttribute) titleObj;
String title = titleAtt.get(request.getLocale());
response.write("<li style=\"list-style-image: url(" + imgPath + "iView.gif);\">" + title + "</li>");
}
} catch (Exception e) {
response.write(e.getMessage() + "<br>");
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
17 | |
14 | |
12 | |
10 | |
9 | |
8 | |
7 | |
7 | |
6 | |
6 |