<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.plumtree.remote.prc.IRemoteSession,
                 com.plumtree.remote.prc.RemoteSessionFactory,
                 java.net.URL,
                 com.plumtree.remote.prc.collaboration.project.*,
                 java.io.PrintWriter,
                 com.plumtree.remote.prc.collaboration.document.*,
                 com.plumtree.remote.portlet.*"%>
<%
    PrintWriter jspOut = response.getWriter();
    String projectIDStr = request.getParameter("projectID");

    IPortletContext portletContext = PortletContextFactory.createPortletContext(request, response);

    if(projectIDStr != null)
    {
        portletContext.getResponse().setSettingValue(SettingType.Portlet, "projectID", projectIDStr);
        portletContext.getResponse().returnToPortal();
        return;
    }

    projectIDStr = portletContext.getRequest().getSettingValue(SettingType.Portlet, "projectID");

    int projectID = 0;

    if(projectIDStr != null)
    {
        try
        {
            projectID = Integer.parseInt(projectIDStr);
        }
        catch (NumberFormatException e)
        {
            jspOut.println("Cannot parse projectID: [" + projectIDStr + "]");
        }
    }

    IRemoteSession remoteSession = portletContext.getRemotePortalSession();
    IDocumentManager documentManager = remoteSession.getCollaborationFactory().getDocumentManager();

    IProject sampleProject = null;

    if(projectID > 0)
    {
        sampleProject = remoteSession.getCollaborationFactory().getProjectManager().getProject(projectID);
        if(sampleProject == null)
            jspOut.println("Could not find project: " + projectID);
    }

    if(projectID == -1)
    {
       sampleProject = (IProject)session.getAttribute("edk_sample_project");

        if(sampleProject == null)
            response.sendRedirect("../project/ProjectCreator.jsp");
    }

    IDocumentFolder rootFolder = null;
    if(sampleProject != null)
         rootFolder = documentManager.getTopLevelFolder(sampleProject);

%>
<%!
    public IDocumentFolder[] getChildFolders(IDocumentFolder folder, IDocumentManager manager) throws Exception
    {
        IDocumentFolderFilter filter = manager.createDocumentFolderFilter();
        IDocumentFolder[] children = manager.queryFolders(folder, filter);
        return children;
    }

    public IDocument[] getChildDocuments(IDocumentFolder folder, IDocumentManager manager) throws Exception
    {
        IDocumentFilter filter = manager.createDocumentFilter();
        IDocument[] children = manager.queryDocuments(folder, filter);
        return children;
    }

    public void renderDocument(IDocument document, IDocumentManager manager, PrintWriter out, int depth) throws Exception
    {
        for(int i = 0; i < depth; i++)
            out.write("&nbsp;&nbsp;&nbsp;");
        out.write(document.getName());
        out.write(" [<a href=\"" + document.getDetailsURL() + "\">");
        out.write("details");
        out.write("</a>]");
        out.write(" [<a href=\"" + document.getContentURL() + "\">");
        out.write("download");
        out.write("</a>]");
        out.write("<br/>");
    }

    public void renderFolder(IDocumentFolder folder, IDocumentManager manager, PrintWriter out, int depth) throws Exception
    {
        if(folder == null)
            return;

        for(int i = 0; i < depth; i++)
            out.write("&nbsp;&nbsp;&nbsp;");
        out.write(folder.getName());

        out.write(" [<a href=\"" + folder.getDetailsURL() + "\">");
        out.write("details");
        out.write("</a>]");
        out.write("<br/>");

        IDocument[] childDocuments = getChildDocuments(folder, manager);

        for(int i = 0; i < childDocuments.length; i++)
            renderDocument(childDocuments[i], manager, out, depth + 1);

        IDocumentFolder[] childFolders = getChildFolders(folder, manager);

        for(int i = 0; i < childFolders.length; i++)
            renderFolder(childFolders[i], manager, out, depth + 1);
    }
%>
<form action="DocumentExample.jsp">
<p>Enter Project ID: <input type="text" name="projectID"/> <input type="submit" value="Go" /> Or <a href="DocumentExample.jsp?projectID=-1">Use Sample Project</a></p>
</form>
<% renderFolder(rootFolder, documentManager, jspOut, 0); %>