Imports CharterDotCom.CommonEntities.Library

Partial Class FoldersTree
    Inherits System.Web.UI.UserControl

    Public Event AddFolder()
    Public Event FolderDeleted()
    Public Event FolderSelected(ByVal folderID As Integer)

    Public Property SelectedFolder() As Integer

        Get
            Return CInt(foldersTreeView.SelectedValue)
        End Get

        Set(ByVal value As Integer)
            Dim nodeToSelect As TreeNode = foldersTreeView.FindNode(value.ToString())
            If Not nodeToSelect Is Nothing Then
                nodeToSelect.Select()
            End If
        End Set

    End Property

    Public Sub Refresh()

        CreateRootNode()

    End Sub

    Private Sub CreateRootNode()

        foldersTreeView.Nodes.Clear()

        Dim rootNode As New TreeNode()
        With rootNode
            .SelectAction = TreeNodeSelectAction.Expand
            .Text = "Root"
            .Value = 0
        End With

        foldersTreeView.Nodes.Add(rootNode)

        CreateChildNodes(rootNode)

        foldersTreeView.ExpandAll()

    End Sub

    Private Sub CreateChildNodes(ByVal parentNode As TreeNode)

        Dim documentManager As New DMSAdmin.DocumentManagerBL
        documentManager.Credentials = Net.CredentialCache.DefaultNetworkCredentials

        Dim parentFolderID As Integer = CInt(parentNode.Value)
        Dim childFolders() As DMSAdmin.DocumentFolder = documentManager.GetDocumentFoldersByParentFolderID(parentFolderID)

        For Each childFolder As DMSAdmin.DocumentFolder In childFolders

            Dim childHasChildren As Boolean = (childFolder.NumberOfChildren > 0)

            Dim childNode As New TreeNode()
            With childNode
                .Text = HttpUtility.HtmlDecode(childFolder.Name).Trim()
                .Value = childFolder.ID
                If childHasChildren Then
                    .SelectAction = TreeNodeSelectAction.SelectExpand
                Else
                    .SelectAction = TreeNodeSelectAction.Select
                End If
            End With

            parentNode.ChildNodes.Add(childNode)

            If childHasChildren Then
                CreateChildNodes(childNode)
            End If
        Next

    End Sub

    Protected Sub deleteButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles deleteButton.Click

        Try
            Dim folderID As Integer = CInt(foldersTreeView.SelectedValue)

            If folderID <> Global.Constants.Folders.Unassigned Then

                Dim documentManager As New DMSAdmin.DocumentManagerBL
                documentManager.Credentials = Net.CredentialCache.DefaultNetworkCredentials
                Dim page As AdminPage = CType(Me.Page, AdminPage)
                documentManager.DeleteFolder(folderID, page.CurrentUserID)

                Refresh()

                RaiseEvent FolderDeleted()
            End If
        Catch ex As Exception
      Throw
    End Try

    End Sub

    Protected Sub foldersTreeView_SelectedNodeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles foldersTreeView.SelectedNodeChanged

        Try
            Dim selectedValue As Integer = CInt(foldersTreeView.SelectedValue)
            RaiseEvent FolderSelected(selectedValue)

            deleteButton.Enabled = (selectedValue <> Constants.Folders.Unassigned)
        Catch ex As Exception
      Throw
    End Try

    End Sub

    Protected Sub newButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles newButton.Click

        Try
            RaiseEvent AddFolder()
        Catch ex As Exception
      Throw
    End Try

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        '#####################################################
        'THIS IS USED FOR THE SEARCH CAPABILITY BASED ON 
        'KEYWORDS/ID. -NAQIB 9/23/08  FOR ISSUE ID: 2362   
        '#####################################################
        Try
            If Not Page.IsPostBack Then
                If Len(Request.QueryString("DcoumentID")) > 0 And Len(Request.QueryString("FolderID")) > 0 Then
                    Dim documentID As Integer = CInt(Request.QueryString("DcoumentID"))
                    Dim folderID As Integer = CInt(Request.QueryString("FolderID"))

                    CreateRootNode(folderID.ToString())
                Else
                    CreateRootNode()
                    Refresh()
                End If
            End If
        Catch ex As Exception
            Throw
        End Try

    End Sub


    Private Sub CreateRootNode(ByVal selectNode As String)
        '#####################################################
        'THIS IS USED FOR THE SEARCH CAPABILITY BASED ON 
        'KEYWORDS/ID. -NAQIB 9/23/08  FOR ISSUE ID: 2362   
        '#####################################################
        foldersTreeView.Nodes.Clear()

        Dim rootNode As New TreeNode()
        With rootNode
            .SelectAction = TreeNodeSelectAction.Expand
            .Text = "Root"
            .Value = 0
        End With

        foldersTreeView.Nodes.Add(rootNode)

        CreateChildNodes(rootNode, selectNode)

        foldersTreeView.ExpandAll()

    End Sub

    Private Sub CreateChildNodes(ByVal parentNode As TreeNode, ByVal selectNode As String)
        '#####################################################
        'THIS IS USED FOR THE SEARCH CAPABILITY BASED ON 
        'KEYWORDS/ID. -NAQIB 9/23/08  FOR ISSUE ID: 2362   
        '#####################################################
        Dim documentManager As New DMSAdmin.DocumentManagerBL
        documentManager.Credentials = Net.CredentialCache.DefaultNetworkCredentials

        Dim parentFolderID As Integer = CInt(parentNode.Value)
        Dim childFolders() As DMSAdmin.DocumentFolder = documentManager.GetDocumentFoldersByParentFolderID(parentFolderID)

        For Each childFolder As DMSAdmin.DocumentFolder In childFolders

            Dim childHasChildren As Boolean = (childFolder.NumberOfChildren > 0)

            Dim childNode As New TreeNode()
            With childNode
                .Text = HttpUtility.HtmlDecode(childFolder.Name).Trim()
                .Value = childFolder.ID
                If childHasChildren Then
                    .SelectAction = TreeNodeSelectAction.SelectExpand
                Else
                    .SelectAction = TreeNodeSelectAction.Select
                End If
            End With

            If childNode.Value = selectNode Then
                childNode.Selected = True
                childNode.Select()
            End If

            parentNode.ChildNodes.Add(childNode)

            If childHasChildren Then
                CreateChildNodes(childNode, selectNode)
            End If
        Next

    End Sub

End Class
