The mySiteMap.java class will get the navigation nodes for the current user and render them as links in HTML.
The mySiteMap.java class is an AbstractPortalComponent, which must implement doContent(), which writes HTML to the response (IPortalComponentResponse). In addition, the class will define several helper methods.
Create a Hashtable of environment variables that are required when looking up the navigation nodes.
Syntax
public Hashtable getEnvironment(IPortalComponentRequest request) { Hashtable environment = new Hashtable(); IUserContext userContext = request.getUser(); if (userContext != null) { environment.put("NavigationPrincipal", userContext); String user = userContext.getUniqueName(); if (user != null && !user.equals("")) { environment.put("User", user); } } return environment; } }
Define a method to retrieve the current user's initial (top-level) navigation nodes.
The method gets a reference to the navigation service and calls getInitialNodes() method, passing in the environment variables, as retrieved in Step 1.
Syntax
private NavigationNodes getNavNodes(IPortalComponentRequest request) { INavigationService service = (INavigationService) PortalRuntime.getRuntimeResources().getService(INavigationService.KEY); NavigationNodes initialNodes = null; try { initialNodes = service.getInitialNodes(getEnvironment(request)); } catch (NamingException ne) { ne.printStackTrace(); } return initialNodes; }
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.
Syntax
private void renderNavNodes(IPortalComponentRequest request, IPortalComponentResponse response, NavigationNodes navigationNodes, int level) { if (navigationNodes == null) { return; } NavigationNodes children; Iterator it = navigationNodes.iterator(); INavigationNode navigationNode; String name; // navigation node full path name String title; // navigation node title int showType; // 0: Same window, 1: New window, 2: New portal window String windowName; // Relevant when showType=1 int windowWidth; // Relevant when showType=1 int windowHeight; // Relevant when showType=1 String windowFeatures; // 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 (NoSuchAttributeException e) { windowFeatures = "toolbar,location,status,scrollbars,directories,menubar,resizable"; } try { windowName = navigationNode.getWindowName(); } catch (UnsupportedOperationException e) { windowName = "DefaultExternal"; } try { windowHeight = navigationNode.getExtWindowHeight(); } catch (UnsupportedOperationException e) { windowHeight = -1; } try { windowWidth = navigationNode.getExtWindowWidth(); } catch (UnsupportedOperationException e) { 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 (int i = 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 (NamingException ne) { ne.printStackTrace(); } } }
Define the doContent() method.
Syntax
public void doContent(IPortalComponentRequest request, IPortalComponentResponse response) { // Get current user’s nodes NavigationNodes initialNodes = getNavNodes(request); response.write("<table>"); // Render nodes renderNavNodes(request, response, initialNodes, 0); response.write("</table>"); }
The following are the imports for mySiteMap.java:
Syntax
import java.util.Hashtable; import java.util.Iterator; import javax.naming.NamingException; import javax.naming.directory.NoSuchAttributeException; import com.sapportals.portal.navigation.INavigationNode; import com.sapportals.portal.navigation.INavigationService; import com.sapportals.portal.navigation.NavigationNodes; import com.sapportals.portal.prt.component.AbstractPortalComponent; import com.sapportals.portal.prt.component.IPortalComponentRequest; import com.sapportals.portal.prt.component.IPortalComponentResponse; import com.sapportals.portal.prt.runtime.PortalRuntime; import com.sapportals.portal.prt.session.IUserContext; import com.sapportals.portal.prt.util.StringUtils;