<%@ page import="com.plumtree.remote.prc.IRemoteSession,
                 com.plumtree.remote.prc.RemoteSessionFactory,
                 com.plumtree.remote.portlet.PortletContextFactory,
                 com.plumtree.remote.portlet.IPortletContext,
                 com.plumtree.remote.prc.collaboration.project.*,
                 com.plumtree.remote.prc.collaboration.document.*,
                 java.io.InputStream,
                 java.io.ByteArrayInputStream"%>
<%!
    public static final String SESSION_PROJECT_KEY = "edk_sample_project";
    public static final String PROJECT_NAME = "EDK Sample Project";

    public IProject createProject(IRemoteSession remoteSession) throws Exception
    {
        IProjectManager projectManager = remoteSession.getCollaborationFactory().getProjectManager();
        IProject project = projectManager.createProject(PROJECT_NAME, "Project created for EDK samples");
        project.store();

        //create sample objects in project
        IDocumentManager documentManager = remoteSession.getCollaborationFactory().getDocumentManager();

        IDocumentFolder rootFolder = documentManager.getTopLevelFolder(project);

        IDocumentFolder main1 = documentManager.createNewFolder("main folder 1", "");
        documentManager.insertNewFolder(rootFolder, main1, true);
        IDocumentFolder main2 = documentManager.createNewFolder("main folder 2", "");
        documentManager.insertNewFolder(rootFolder, main2, true);
        IDocumentFolder main3 = documentManager.createNewFolder("main folder 3", "");
        documentManager.insertNewFolder(rootFolder, main3, true);

        IDocumentFolder sub21 = documentManager.createNewFolder("sub folder 2.1", "");
        documentManager.insertNewFolder(main2, sub21, true);
        IDocumentFolder sub22 = documentManager.createNewFolder("sub folder 2.2", "");
        documentManager.insertNewFolder(main2, sub22, true);
        IDocumentFolder sub23 = documentManager.createNewFolder("sub folder 2.3", "");
        documentManager.insertNewFolder(main2, sub23, true);

        //insert some documents
        IDocument doc1 = documentManager.createNewDocument("document 1", "");
        documentManager.insertNewDocument(main1, doc1, "", getTemporaryInputStream(), null, true);
        IDocument doc2 = documentManager.createNewDocument("document 2", "");
        documentManager.insertNewDocument(main2, doc2, "", getTemporaryInputStream(), null, true);
        IDocument doc3 = documentManager.createNewDocument("document 3", "");
        documentManager.insertNewDocument(sub21, doc3, "", getTemporaryInputStream(), null, true);
        IDocument doc4 = documentManager.createNewDocument("document 4", "");
        documentManager.insertNewDocument(sub21, doc4, "", getTemporaryInputStream(), null, true);
        IDocument doc5 = documentManager.createNewDocument("document 5", "");
        documentManager.insertNewDocument(sub22, doc5, "", getTemporaryInputStream(), null, true);
        IDocument doc6 = documentManager.createNewDocument("document 6", "");
        documentManager.insertNewDocument(sub23, doc6, "", getTemporaryInputStream(), null, true);
        IDocument doc7 = documentManager.createNewDocument("document 7", "");
        documentManager.insertNewDocument(sub23, doc7, "", getTemporaryInputStream(), null, true);
        IDocument doc8 = documentManager.createNewDocument("document 8", "");
        documentManager.insertNewDocument(sub23, doc8, "", getTemporaryInputStream(), null, true);

        return project;
    }

    public InputStream getTemporaryInputStream()
    {
        InputStream rv = new ByteArrayInputStream("Sample document for EDK example code.".getBytes());
        return rv;
    }

    public void storeProject(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception
    {
        Object retrievedProject = session.getAttribute(SESSION_PROJECT_KEY);

        if(retrievedProject != null && retrievedProject instanceof IProject)
            return;

        IPortletContext portletContext = PortletContextFactory.createPortletContext(request, response);
        IRemoteSession remoteSession = portletContext.getRemotePortalSession();
        IProjectManager projectManager = remoteSession.getCollaborationFactory().getProjectManager();
        IProjectFilter projectFilter = projectManager.createProjectFilter();
        projectFilter.setNameSearchText(PROJECT_NAME);
        IProject[] sampleProjects = projectManager.queryProjects(projectFilter);
        IProject sampleProject = null;
        
        if(sampleProjects.length > 1)
        {
            throw new ServletException("Found two projects called "+PROJECT_NAME+". Please remove one.");
        }
        else if(sampleProjects.length == 0)
        {
            sampleProject = createProject(remoteSession);
        }
        else
        {
            sampleProject = sampleProjects[0];
        }

        //projectManager.removeProject(sampleProject);
        session.setAttribute(SESSION_PROJECT_KEY, sampleProject);
    }
%>
<%
  String source = request.getParameter("source");
  String create = request.getParameter("create");

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

  if(create != null)
  {
        if(create.equals("true"))
        {
          storeProject(request, response, session);
          portletContext.getResponse().returnToPortal();
        }
  }
%>
<html>
<head>
<title>Create Sample EDK Project</title>
</head>
<body>
<p>Create &quot;Sample EDK Project&quot;? <a href="ProjectCreator.jsp?create=true&source=<%= source %>">OK</a>
</p>
<p>You must create this project to run the sample JSP code. Note that if you receive an error upon creating the project, this is probably a timeout as creating the project takes a few seconds. Just refresh the page after the error.</p>
</body>
</html>