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 DocumentExampleVB
    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 ErrorLabel As System.Web.UI.WebControls.Label
    Protected WithEvents EnterProjectIDLabel As System.Web.UI.WebControls.Label
    Protected WithEvents ProjectIDBox As System.Web.UI.WebControls.TextBox
    Protected WithEvents ProjectSelectButton As System.Web.UI.WebControls.Button
    Protected WithEvents OrLabel As System.Web.UI.WebControls.Label
    Protected WithEvents SampleProjectLink As System.Web.UI.WebControls.LinkButton
    Protected WithEvents TreeView As System.Web.UI.WebControls.Literal

    '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 sampleProject As IProject = Nothing

    Dim documentManager As IDocumentManager
    Dim remoteSession As Plumtree.Remote.PRC.IRemoteSession

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim portletContext As IPortletContext = PortletContextFactory.CreatePortletContext(Request, Response)

        remoteSession = portletContext.GetRemotePortalSession()
        documentManager = remoteSession.GetCollaborationFactory().GetDocumentManager()

        sampleProject = DirectCast(Session.Item("edk_sample_project"), IProject)

        If TreeView.Text Is "" And Not sampleProject Is Nothing Then
            CreateTree(sampleProject)
        End If
    End Sub

    Private Sub ProjectSelectButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProjectSelectButton.Click
        Try
            Dim projectID As Integer = Int32.Parse(ProjectIDBox.Text)

            sampleProject = remoteSession.GetCollaborationFactory().GetProjectManager().GetProject(projectID)
            If sampleProject Is Nothing Then
                ErrorLabel.Text = "Could not find project: " & projectID
            End If

        Catch fe As FormatException
            ErrorLabel.Text = "Cannot parse projectID: [" & ProjectIDBox.Text & "]"
        End Try

        CreateTree(sampleProject)
    End Sub

    Private Sub SampleProjectLink_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SampleProjectLink.Click
        sampleProject = DirectCast(Session.Item("edk_sample_project"), IProject)

        If sampleProject Is Nothing Then
            Response.Redirect("../project/ProjectCreatorVB.aspx?source=../document/DocumentExampleVB.aspx")
        Else
            CreateTree(sampleProject)
        End If
    End Sub

    Private Sub CreateTree(ByVal project As IProject)
        Dim rootFolder As IDocumentFolder = documentManager.GetTopLevelFolder(project)
        TreeView.Text = AppendFolder(rootFolder, 0).Text
    End Sub

    Public Function AppendFolder(ByVal folder As IDocumentFolder, ByVal depth As Integer) As LiteralControl

        If folder Is Nothing Then
            Return Nothing
        End If

        Dim html As StringBuilder = New StringBuilder

        Dim i As Integer

        For i = 0 To depth - 1
            html.Append("&nbsp;&nbsp;&nbsp;")
        Next i

        html.Append(folder.Name)

        html.Append(" [<a href=""" & folder.DetailsURL & """>")
        html.Append("details")
        html.Append("</a>]")
        html.Append("<br/>")

        Dim rv As LiteralControl = New LiteralControl(html.ToString())

        Dim childDocuments() As IDocument = GetChildDocuments(folder)

        For i = 0 To UBound(childDocuments)
            rv.Text += AppendDocument(childDocuments(i), depth + 1).Text
        Next i

        Dim childFolders() As IDocumentFolder = GetChildFolders(folder)

        For i = 0 To UBound(childFolders)
            rv.Text += AppendFolder(childFolders(i), depth + 1).Text
        Next i

        Return rv
    End Function

    Public Function AppendDocument(ByVal document As IDocument, ByVal depth As Integer) As LiteralControl

        Dim html As StringBuilder = New StringBuilder

        For i As Integer = 0 To depth - 1
            html.Append("&nbsp;&nbsp;&nbsp;")
            html.Append(document.Name)
            html.Append(" [<a href=""" & document.DetailsURL & """>")
            html.Append("details")
            html.Append("</a>]")
            html.Append(" [<a href=""" & document.ContentURL & """>")
            html.Append("download")
            html.Append("</a>]")
            html.Append("<br/>")
        Next i

        Dim rv As LiteralControl = New LiteralControl(html.ToString())

        Return rv
    End Function

    Public Function GetChildFolders(ByVal folder As IDocumentFolder) As IDocumentFolder()
        Dim filter As IDocumentFolderFilter = documentManager.CreateDocumentFolderFilter()
        Dim children() As IDocumentFolder = documentManager.QueryFolders(folder, filter)
        Return children
    End Function

    Public Function GetChildDocuments(ByVal folder As IDocumentFolder) As IDocument()
        Dim filter As IDocumentFilter = documentManager.CreateDocumentFilter()
        Dim children() As IDocument = documentManager.QueryDocuments(folder, filter)
        Return children
    End Function
End Class
