Show TOC

Procedure documentationmySiteMap.java Locate this document in the navigation structure

 

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.

Procedure

  1. Create a Hashtable of environment variables that are required when looking up the navigation nodes.

    Syntax Syntax

    1. 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;
        }
      }
      
    End of the code.
  2. 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 Syntax

    1. 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;
      }
      
    End of the code.
  3. 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 Syntax

    1. 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("&nbsp &nbsp &nbsp");
             }
             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();
             }
          }
      }
    End of the code.
  4. Define the doContent() method.

    Syntax Syntax

    1. 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>");
      }
    End of the code.
Imports

The following are the imports for mySiteMap.java:

Syntax Syntax

  1. 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;
    
End of the code.