Imports System
Imports System.IO
Imports System.Text
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports Plumtree.Remote.Portlet
Imports Plumtree.Remote.PRC.Collaboration.Document
Imports Plumtree.Remote.PRC.Collaboration.Project

Public Class ProjectCreatorVB
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents OKButton As System.Web.UI.WebControls.LinkButton
    Protected WithEvents sourcePage As System.Web.UI.HtmlControls.HtmlInputHidden

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Dim portletContext As IPortletContext

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        sourcePage.Value = Request.Item("source")
        portletContext = PortletContextFactory.CreatePortletContext(Request, Response)

        Dim sampleProject As IProject
        sampleProject = QueryForSampleProject(portletContext)
        If Not sampleProject Is Nothing Then

            Session.Item(SESSION_PROJECT_KEY) = sampleProject

            If Not sourcePage.Value Is Nothing Then
                Response.Redirect(sourcePage.Value)
            Else
                portletContext.GetResponse().ReturnToPortal()
            End If
        End If
    End Sub

    Private Sub OKButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OKButton.Click
        StoreProject(portletContext)

        If Not sourcePage.Value = Nothing Then
            Response.Redirect(sourcePage.Value)
        Else
            portletContext.GetResponse().ReturnToPortal()
        End If
    End Sub

    Dim SESSION_PROJECT_KEY As String = "edk_sample_project"
    Dim PROJECT_NAME As String = "EDK Sample Project"

    Public Function CreateProject(ByVal remoteSession As Plumtree.Remote.PRC.IRemoteSession) As IProject

        Dim projectManager As IProjectManager = remoteSession.GetCollaborationFactory().GetProjectManager()
        Dim project As IProject = projectManager.CreateProject(PROJECT_NAME, "Project created for EDK samples")
        project.Store()

        'create sample objects in project
        Dim documentManager As IDocumentManager = remoteSession.GetCollaborationFactory().GetDocumentManager()

        Dim rootFolder As IDocumentFolder = documentManager.GetTopLevelFolder(project)

        Dim main1 As IDocumentFolder = documentManager.CreateNewFolder("main folder 1", "")
        documentManager.InsertNewFolder(rootFolder, main1, True)
        Dim main2 As IDocumentFolder = documentManager.CreateNewFolder("main folder 2", "")
        documentManager.InsertNewFolder(rootFolder, main2, True)
        Dim main3 As IDocumentFolder = documentManager.CreateNewFolder("main folder 3", "")
        documentManager.InsertNewFolder(rootFolder, main3, True)

        Dim sub21 As IDocumentFolder = documentManager.CreateNewFolder("sub folder 2.1", "")
        documentManager.InsertNewFolder(main2, sub21, True)
        Dim sub22 As IDocumentFolder = documentManager.CreateNewFolder("sub folder 2.2", "")
        documentManager.InsertNewFolder(main2, sub22, True)
        Dim sub23 As IDocumentFolder = documentManager.CreateNewFolder("sub folder 2.3", "")
        documentManager.InsertNewFolder(main2, sub23, True)

        'insert some documents
        Dim doc1 As IDocument = documentManager.CreateNewDocument("document 1", "")
        documentManager.InsertNewDocument(main1, doc1, "", GetTemporaryInputStream(), Nothing, True)
        Dim doc2 As IDocument = documentManager.CreateNewDocument("document 2", "")
        documentManager.InsertNewDocument(main2, doc2, "", GetTemporaryInputStream(), Nothing, True)
        Dim doc3 As IDocument = documentManager.CreateNewDocument("document 3", "")
        documentManager.InsertNewDocument(sub21, doc3, "", GetTemporaryInputStream(), Nothing, True)
        Dim doc4 As IDocument = documentManager.CreateNewDocument("document 4", "")
        documentManager.InsertNewDocument(sub21, doc4, "", GetTemporaryInputStream(), Nothing, True)
        Dim doc5 As IDocument = documentManager.CreateNewDocument("document 5", "")
        documentManager.InsertNewDocument(sub22, doc5, "", GetTemporaryInputStream(), Nothing, True)
        Dim doc6 As IDocument = documentManager.CreateNewDocument("document 6", "")
        documentManager.InsertNewDocument(sub23, doc6, "", GetTemporaryInputStream(), Nothing, True)
        Dim doc7 As IDocument = documentManager.CreateNewDocument("document 7", "")
        documentManager.InsertNewDocument(sub23, doc7, "", GetTemporaryInputStream(), Nothing, True)
        Dim doc8 As IDocument = documentManager.CreateNewDocument("document 8", "")
        documentManager.InsertNewDocument(sub23, doc8, "", GetTemporaryInputStream(), Nothing, True)

        Return project
    End Function

    Public Function GetTemporaryInputStream() As Stream
        Dim rv As Stream = New MemoryStream(Encoding.Default.GetBytes("Sample document for EDK example code."))
        Return rv
    End Function

    Public Function QueryForSampleProject(ByVal portletContext As IPortletContext) As IProject

        Dim remoteSession As Plumtree.Remote.PRC.IRemoteSession = portletContext.GetRemotePortalSession()
        Dim projectManager As IProjectManager = remoteSession.GetCollaborationFactory().GetProjectManager()
        Dim projectFilter As IProjectFilter = projectManager.CreateProjectFilter()
        projectFilter.NameSearchText = PROJECT_NAME
        Dim sampleProjects() As IProject = projectManager.QueryProjects(projectFilter)
        Dim sampleProject As IProject = Nothing

        If sampleProjects.Length > 1 Then
            Throw New ApplicationException("Found two projects called " & PROJECT_NAME & ". Please remove one.")
        ElseIf sampleProjects.Length = 0 Then
            Return Nothing
        Else
            sampleProject = sampleProjects(0)
        End If

        Return sampleProject
    End Function


    Public Sub StoreProject(ByVal portletContext As IPortletContext)

        Dim retrievedProject As Object = Session.Item(SESSION_PROJECT_KEY)

        If Not retrievedProject = Nothing And TypeOf retrievedProject Is IProject Then
            Return
        End If

        If QueryForSampleProject(portletContext) Is Nothing Then
            Session.Item(SESSION_PROJECT_KEY) = CreateProject(portletContext.GetRemotePortalSession())
        End If
    End Sub
End Class
