Example usage for org.w3c.dom Attr setNodeValue

List of usage examples for org.w3c.dom Attr setNodeValue

Introduction

In this page you can find the example usage for org.w3c.dom Attr setNodeValue.

Prototype

public void setNodeValue(String nodeValue) throws DOMException;

Source Link

Document

The value of this node, depending on its type; see the table above.

Usage

From source file:org.sakaiproject.webservices.SakaiScript.java

/**
 * Return XML document listing all pages and tools in those pages for a given site.
 * The session id must be of a valid, active user in that site, or a super user, or it will throw an exception.
 * If a page is hidden in a site, the page and all tools in that page will be skipped from the returned document, as they are in the portal.
 * Super user's can request any site to retrieve the full list.
 *
 * @param sessionid the session id of a user in a site, or a super user
 * @param siteid    the site to retrieve the information for
 * @return xml or an empty list <site/>. The return XML format is below:
 * <site id="9ec48d9e-b690-4090-a300-10a44ed7656e">
 * <pages>//from  w  w  w  . ja  va  2s . c om
 * <page id="ec1b0ab8-90e8-4d4d-bf64-1e586035f08f">
 * <page-title>Home</page-title>
 * <tools>
 * <tool id="dafd2a4d-8d3f-4f4c-8e12-171968b259cd">
 * <tool-id>sakai.iframe.site</tool-id>
 * <tool-title>Site Information Display</tool-title>
 * </tool>
 * ...
 * </tools>
 * </page>
 * <page>
 * ...
 * </page>
 * ...
 * </pages>
 * </site>
 * @throws RuntimeException if not a super user and the user attached to the session is not in the site, if site does not exist
 */
@WebMethod
@Path("/getPagesAndToolsForSiteForCurrentUser")
@Produces("text/plain")
@GET
public String getPagesAndToolsForSiteForCurrentUser(
        @WebParam(name = "sessionid", partName = "sessionid") @QueryParam("sessionid") String sessionid,
        @WebParam(name = "siteid", partName = "siteid") @QueryParam("siteid") String siteid) {
    Session session = establishSession(sessionid);

    //check if site exists
    Site site;
    try {
        site = siteService.getSite(siteid);
    } catch (Exception e) {
        LOG.warn("WS getPagesAndToolsForSiteForCurrentUser(): Error looking up site: " + siteid + ":"
                + e.getClass().getName() + " : " + e.getMessage());
        throw new RuntimeException("WS getPagesAndToolsForSiteForCurrentUser(): Error looking up site: "
                + siteid + ":" + e.getClass().getName() + " : " + e.getMessage());
    }

    String userId = session.getUserId();

    //check if super user
    boolean isSuperUser = false;
    if (securityService.isSuperUser(userId)) {
        isSuperUser = true;
    }

    //if not super user, check user is a member of the site, and get their Role
    Role role;
    if (!isSuperUser) {
        Member member = site.getMember(userId);
        if (member == null || !member.isActive()) {
            LOG.warn("WS getPagesAndToolsForSiteForCurrentUser(): User: " + userId
                    + " does not exist in site : " + siteid);
            throw new RuntimeException("WS getPagesAndToolsForSiteForCurrentUser(): User: " + userId
                    + " does not exist in site : " + siteid);
        }
        role = member.getRole();
    }

    //get list of pages in the site, if none, return empty list
    List<SitePage> pages = site.getPages();
    if (pages.isEmpty()) {
        return "<site id=\"" + site.getId() + "\"/>";
    }

    //site node
    Document dom = Xml.createDocument();
    Element siteNode = dom.createElement("site");
    Attr siteIdAttr = dom.createAttribute("id");
    siteIdAttr.setNodeValue(site.getId());
    siteNode.setAttributeNode(siteIdAttr);

    //pages node
    Element pagesNode = dom.createElement("pages");

    for (SitePage page : pages) {

        //page node
        Element pageNode = dom.createElement("page");
        Attr pageIdAttr = dom.createAttribute("id");
        pageIdAttr.setNodeValue(page.getId());
        pageNode.setAttributeNode(pageIdAttr);

        //pageTitle
        Element pageTitleNode = dom.createElement("page-title");
        pageTitleNode.appendChild(dom.createTextNode(page.getTitle()));

        //get tools in page
        List<ToolConfiguration> tools = page.getTools();

        Element toolsNode = dom.createElement("tools");

        boolean includePage = true;
        for (ToolConfiguration toolConfig : tools) {
            //if we not a superAdmin, check the page properties
            //if any tool on this page is hidden, skip the rest of the tools and exclude this page from the output
            //this makes the behaviour consistent with the portal

            //if not superUser, process  tool function requirements
            if (!isSuperUser) {

                //skip processing tool if we've skipped tools previously on this page
                if (!includePage) {
                    continue;
                }

                //skip this tool if not visible, ultimately hiding the whole page
                if (!toolManager.isVisible(site, toolConfig)) {
                    includePage = false;
                    break;
                }
            }

            //if we got this far, add the details about the tool to the document
            Element toolNode = dom.createElement("tool");

            //tool uuid
            Attr toolIdAttr = dom.createAttribute("id");
            toolIdAttr.setNodeValue(toolConfig.getId());
            toolNode.setAttributeNode(toolIdAttr);

            //registration (eg sakai.profile2)
            Element toolIdNode = dom.createElement("tool-id");
            toolIdNode.appendChild(dom.createTextNode(toolConfig.getToolId()));
            toolNode.appendChild(toolIdNode);

            Element toolTitleNode = dom.createElement("tool-title");
            toolTitleNode.appendChild(dom.createTextNode(toolConfig.getTitle()));
            toolNode.appendChild(toolTitleNode);

            toolsNode.appendChild(toolNode);

        }

        //if the page is not hidden, add the elements
        if (includePage) {
            pageNode.appendChild(pageTitleNode);
            pageNode.appendChild(toolsNode);
            pagesNode.appendChild(pageNode);
        }
    }

    //add the main nodes
    siteNode.appendChild(pagesNode);
    dom.appendChild(siteNode);

    return Xml.writeDocumentToString(dom);
}