mySiteMap.java
ThemySiteMap.javaclass will get the navigation nodes for the current user and render them as links in HTML.
ThemySiteMap.javaclass is anAbstractPortalComponent, which must implementdoContent(), which writes HTML to the response (IPortalComponentResponse). In addition, the class will define several helper methods.
Procedure
Create aHashtableof environment variables that are required when looking up the navigation nodes.
publicHashtablegetEnvironment(IPortalComponentRequestrequest){
Hashtableenvironment=newHashtable();
IUserContextuserContext=request.getUser();
if(userContext!=null){
environment.put("NavigationPrincipal"userContext);
Stringuser=userContext.getUniqueName();
if(user!=null&&!user.equals("")){
environment.put("User"user);
}
}
returnenvironment;
}
}
Define a method to retrieve the current user’s initial (top-level) navigation nodes.
The method gets a reference to the navigation service and callsgetInitialNodes()method, passing in the environment variables, as retrieved in Step 1.
privateNavigationNodesgetNavNodes(IPortalComponentRequestrequest){
INavigationServiceservice=(INavigationService)PortalRuntime.getRuntimeResources().getService(INavigationService.KEY);
NavigationNodesinitialNodes=null
try{
initialNodes=service.getInitialNodes(getEnvironment(request));
}catch(NamingExceptionne){
ne.printStackTrace();
}
returninitialNodes;
}
Define a method for rendering the nodes in HTML.
At the end of the method, the children nodes of the current node are retrieved, and the rendering method is called, recursively.
privatevoidrenderNavNodes(IPortalComponentRequestrequest, IPortalComponentResponseresponse,
NavigationNodesnavigationNodes,intlevel){
if(navigationNodes==null){
return
}
NavigationNodeschildren;
Iteratorit=navigationNodes.iterator();
INavigationNodenavigationNode;
Stringname;// navigation node full path name
Stringtitle; // navigation node title
intshowType; // 0: Same window, 1: New window, 2: New portal window
StringwindowName; // Relevant when showType=1
intwindowWidth; // Relevant when showType=1
intwindowHeight; // Relevant when showType=1
StringwindowFeatures; // Relevant when showType=1
while(it.hasNext()){
response.write("<tr>");
navigationNode=(INavigationNode)it.next();
title=navigationNode.getTitle(request.getLocale());
name=StringUtils.escapeToJS(navigationNode.getName());
showType=navigationNode.getShowType();
// Getting the navigation node properties:
try{
windowFeatures=(String)navigationNode.getAttributeValue("com.sapportals.portal.navigation.WinFeatures");
}catch(NoSuchAttributeExceptione){
windowFeatures="toolbar,location,status,scrollbars,directories,menubar,resizable"
}
try{
windowName=navigationNode.getWindowName();
}catch(UnsupportedOperationExceptione){
windowName="DefaultExternal"
}
try{
windowHeight=navigationNode.getExtWindowHeight();
}catch(UnsupportedOperationExceptione){
windowHeight=-1;
}
try{
windowWidth=navigationNode.getExtWindowWidth();
}catch(UnsupportedOperationExceptione){
windowWidth=-1;
}
response.write("<td id='"+title+"' ");
// Set up EPCM.doNavigate() link
response.write("onclick=\"EPCM.doNavigate('"+name+"',"+showType+",'height= "+windowHeight+
",width="+windowWidth+
","+windowFeatures+"','"+
windowName+"')\" >");
if(level==0){
response.write("<br><br><strong><b><i>");
}
if(level==1){
response.write("<b>");
}
for(inti=0;i<level;i++){
response.write("     ");
}
response.write("<a href=\"#"+title+"\">");
response.write(title);
response.write("</a>");
if(level==0){
response.write("</strong></b></i>");
}
if(level==1){
response.write("</b>");
}
response.write("</td></tr>");
// Get children nodes and call renderNavNodes recursively
try{
children=navigationNode.getChildren();
renderNavNodes(request,response,children,level+1);
}catch(NamingExceptionne){
ne.printStackTrace();
}
}
}
Define thedoContent()method.
publicvoiddoContent(IPortalComponentRequestrequest, IPortalComponentResponseresponse){
// Get current user’s nodes
NavigationNodesinitialNodes=getNavNodes(request);
response.write("<table>");
// Render nodes
renderNavNodes(request,response,initialNodes,0);
response.write("</table>");
}
Imports
The following are the imports formySiteMap.java
importjava.util.Hashtable;
importjava.util.Iterator;
importjavax.naming.NamingException;
importjavax.naming.directory.NoSuchAttributeException;
importcom.sapportals.portal.navigation.INavigationNode;
importcom.sapportals.portal.navigation.INavigationService;
importcom.sapportals.portal.navigation.NavigationNodes;
importcom.sapportals.portal.prt.component.AbstractPortalComponent;
importcom.sapportals.portal.prt.component.IPortalComponentRequest;
importcom.sapportals.portal.prt.component.IPortalComponentResponse;
importcom.sapportals.portal.prt.runtime.PortalRuntime;
importcom.sapportals.portal.prt.session.IUserContext;
importcom.sapportals.portal.prt.util.StringUtils;