Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling

Imports CharterDotCom.CommonEntities.Library

Partial Class FolderProperties
    Inherits System.Web.UI.UserControl

    Public Event FolderUpdated(ByVal folderID As Integer)

    Public WriteOnly Property FolderID() As Integer

        Set(ByVal value As Integer)

            ViewState(Me.ID & Constants.FOLDER_VIEWSTATE) = value

            Select Case value
                Case Constants.Folders.Unassigned
                Case 0

                    nameTextBox.Text = ""
                    parentFolderDropDownList.SelectedIndex = 0
                Case Else

                    For Each parentFolder As ListItem In parentFolderDropDownList.Items
                        parentFolder.Enabled = Not (parentFolder.Value = value)
                    Next

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

                    Dim folder As DMSAdmin.DocumentFolder = documentManager.GetFolder(value)
                    nameTextBox.Text = folder.Name
                    parentFolderDropDownList.SelectedValue = folder.ParentFolderID

                    Dim thisFolder As ListItem = parentFolderDropDownList.Items.FindByValue(value)
                    thisFolder.Enabled = False
            End Select

            Dim enabled As Boolean = (value <> Constants.Folders.Unassigned)
            parentFolderDropDownList.Enabled = enabled
            nameTextBox.Enabled = enabled
            submitButton.Enabled = enabled
            cancelButton.Enabled = enabled
        End Set

    End Property

    Private Sub RefreshParentFolderList()

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

        Dim folders() As DMSAdmin.DocumentFolder = documentManager.GetDocumentFolders()
        With parentFolderDropDownList
            .Items.Clear()
            .DataSource = folders
            .DataTextField = "Name"
            .DataValueField = "ID"
            .DataBind()

            Dim unassignedFolder As ListItem = .Items.FindByValue(CStr(Global.Constants.Folders.Unassigned))
            .Items.Remove(unassignedFolder)

            Dim rootFolder As ListItem = New ListItem("Root", "0")
            .Items.Insert(0, rootFolder)
        End With

    End Sub

    Protected Sub submitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles submitButton.Click

        Try
            Dim folderName As String = nameTextBox.Text.Trim()
            If folderName.Length > 0 Then

                Dim folderID As Integer = CInt(ViewState(Me.ID & Constants.FOLDER_VIEWSTATE))

                Dim folder As DMSAdmin.DocumentFolder = New DMSAdmin.DocumentFolder
                With folder
                    .ID = folderID
                    .Name = folderName
                    .ParentFolderID = parentFolderDropDownList.SelectedValue
                End With

                Dim documentManager As New DMSAdmin.DocumentManagerBL
                documentManager.Credentials = Net.CredentialCache.DefaultNetworkCredentials
                If folderID = Constants.Folders.NewFolder Then
                    Dim page As AdminPage = CType(Me.Page, AdminPage)
                    folderID = documentManager.AddFolder(folder, page.CurrentUserID)
                Else
                    Dim page As AdminPage = CType(Me.Page, AdminPage)
                    documentManager.UpdateFolder(folder, page.CurrentUserID)
                End If

                RefreshParentFolderList()

                RaiseEvent FolderUpdated(folderID)
            End If
        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

        Try
            If Not IsPostBack Then

                '#####################################################
                'THIS IS USED FOR THE SEARCH CAPABILITY BASED ON 
                'KEYWORDS/ID. -NAQIB 9/23/08  FOR ISSUE ID: 2362   
                '#####################################################
                If Len(Request.QueryString("DcoumentID")) > 0 And Len(Request.QueryString("FolderID")) > 0 Then
                    RefreshParentFolderList()
                    Dim folderID As Integer = CInt(Request.QueryString("FolderID"))
                    Me.FolderID = folderID
                    parentFolderDropDownList.Enabled = True
                    nameTextBox.Enabled = True
                    submitButton.Enabled = True
                    cancelButton.Enabled = True
                Else
                    RefreshParentFolderList()

                    parentFolderDropDownList.Enabled = False
                    nameTextBox.Enabled = False
                    submitButton.Enabled = False
                    cancelButton.Enabled = False
                End If
            End If
        Catch ex As Exception


      Throw
    End Try

  End Sub


  Protected Sub cancelButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cancelButton.Click

    Try
      Dim folderID As Integer = CInt(ViewState(Me.ID & Constants.FOLDER_VIEWSTATE))
      Me.FolderID = folderID
    Catch ex As Exception


      Throw
    End Try

  End Sub

End Class
