Imports System.Data.SqlClient
Imports System.IO

Partial Class Documents
    Inherits System.Web.UI.UserControl

    Public Event AddDocument(ByVal folderID As Integer)
    Public Event DocumentDeleted(ByVal folderID As Integer)
    Public Event DocumentSelected(ByVal folderID As Integer, ByVal documentID As Integer)

    Public Property SelectedDocument() As Integer

        Get
            Return documentsListBox.SelectedValue
        End Get

        Set(ByVal value As Integer)
            documentsListBox.SelectedValue = value
            deleteButton.Enabled = True
            moveButton.Enabled = True
            folderDropDownList.Enabled = True
            Me.btnUploadUpdate.Visible = True
        End Set

    End Property

    Public Sub LoadDocuments(ByVal folderID As Integer)

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

        Dim documentManager As New DMSAdmin.DocumentManagerBL
        documentManager.Credentials = Net.CredentialCache.DefaultNetworkCredentials
        Dim obj As Object = documentManager.GetFolderDocuments(folderID)

        Dim documents() As DMSAdmin.BLHTMLDocument = documentManager.GetFolderDocuments(folderID)

        With documentsListBox
            .Items.Clear()
            .DataSource = documents
            .DataTextField = "Title"
            .DataValueField = "ID"
            .DataBind()
        End With

        newButton.Enabled = True
        deleteButton.Enabled = False
        moveButton.Enabled = False
        folderDropDownList.Enabled = False

    End Sub

    Public Sub LoadFolders()

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

        Dim folders() As DMSAdmin.DocumentFolder = documentManager.GetDocumentFolders()

        With folderDropDownList
            .Items.Clear()
            .DataSource = folders
            .DataTextField = "Name"
            .DataValueField = "ID"
            .DataBind()
        End With

    End Sub

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

        Try
            Dim documentID As Integer = documentsListBox.SelectedValue
            Dim folderID As Integer = CInt(ViewState(Me.ID & Constants.FOLDER_VIEWSTATE))

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

            RaiseEvent DocumentDeleted(folderID)
        Catch ex As Exception

      Throw
    End Try

  End Sub

  Protected Sub documentsListBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles documentsListBox.SelectedIndexChanged

    Try
            Dim folderID As Integer = CInt(ViewState(Me.ID & Constants.FOLDER_VIEWSTATE))
            Dim documentID As Integer = CInt(documentsListBox.SelectedValue)

            RaiseEvent DocumentSelected(folderID, documentID)

            deleteButton.Enabled = True
            moveButton.Enabled = True
            folderDropDownList.Enabled = True
            Me.btnUploadUpdate.Visible = True

    Catch ex As Exception


      Throw
    End Try

  End Sub

  Protected Sub moveButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles moveButton.Click

    Try
      Dim documentID As Integer = documentsListBox.SelectedValue
      Dim newFolderID As Integer = folderDropDownList.SelectedValue

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

      Dim folderID As Integer = CInt(ViewState(Me.ID & Constants.FOLDER_VIEWSTATE))
      Me.LoadDocuments(folderID)
    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
      Dim folderID As Integer = CInt(ViewState(Me.ID & Constants.FOLDER_VIEWSTATE))
      RaiseEvent AddDocument(folderID)
    Catch ex As Exception


      Throw
    End Try

  End Sub

    Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click

        Dim documentManager As New DMSAdmin.DocumentManagerBL
        documentManager.Credentials = Net.CredentialCache.DefaultNetworkCredentials
        Dim _buffer(FileUpload1.PostedFile.ContentLength) As Byte
        FileUpload1.PostedFile.InputStream.Read(_buffer, 0, _buffer.Length)

        ' DB - readding this, don't know why it was removed between versions 4 and 6
        If documentsListBox.SelectedValue Is Nothing Or documentsListBox.SelectedValue = "" Then
            Dim folderID As Integer = CInt(ViewState(Me.ID & Constants.FOLDER_VIEWSTATE))

            Dim IsCheckBoxChecked As Boolean = False
            If chkFileType.Checked Then
                IsCheckBoxChecked = True
            Else
                IsCheckBoxChecked = False
            End If
            Dim title As String = Path.GetFileNameWithoutExtension(FileUpload1.PostedFile.FileName)

            Dim page As AdminPage = CType(Me.Page, AdminPage)
            documentManager.UploadFile(_buffer, folderID, title, IsCheckBoxChecked, page.CurrentUserID)
            'documentManager.UploadFile(_buffer, folderID, title, IsCheckBoxChecked, ".pdf", "application/pdf", page.CurrentUserID)
            LoadDocuments(folderID)
        Else
            Dim documentid As Int32 = Int32.Parse(documentsListBox.SelectedValue)
            Dim page As AdminPage = CType(Me.Page, AdminPage)
            documentManager.UpdateUploadedFile(documentid, _buffer, page.CurrentUserID)
        End If
        ' end DB add...

    End Sub

    Protected Sub btnClearSelection_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClearSelection.Click
        Me.btnUploadUpdate.Visible = False
        Me.documentsListBox.SelectedIndex = -1
    End Sub

    Protected Sub btnUploadUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUploadUpdate.Click
        If Not documentsListBox.SelectedItem Is Nothing Then
            Dim documentManager As New DMSAdmin.DocumentManagerBL
            documentManager.Credentials = Net.CredentialCache.DefaultNetworkCredentials
            Dim _buffer(FileUpload1.PostedFile.ContentLength) As Byte
            FileUpload1.PostedFile.InputStream.Read(_buffer, 0, _buffer.Length)

            Dim documentid As Int32 = Int32.Parse(documentsListBox.SelectedValue)
            Dim page As AdminPage = CType(Me.Page, AdminPage)
            documentManager.UpdateUploadedFile(documentid, _buffer, page.CurrentUserID)
        Else
            Me.lblErrorMessage.Visible = True
            Me.lblErrorMessage.Text = "Select a document to update first"
        End If

        
    End Sub




    '#####################################################
    'THIS IS USED FOR THE SEARCH CAPABILITY BASED ON 
    'KEYWORDS. Issue: 2362
    'NAQIB 10/9/08    
    '#####################################################
    Public Sub PopulateControl(ByVal folderID As Integer, ByVal documentID As Integer)

        ViewState(Me.ID & Constants.FOLDER_VIEWSTATE) = folderID
        ViewState(Me.ID & Constants.DOCUMENT_VIEWSTATE) = documentID
        LoadFolders()

        folderDropDownList.Items.FindByValue(folderID.ToString()).Selected = True
        LoadDocuments(folderID)
        documentsListBox.Items.FindByValue(documentID.ToString()).Selected = True
        documentsListBox.Enabled = True

        RaiseEvent DocumentSelected(folderID, documentID)
        deleteButton.Enabled = True
        moveButton.Enabled = True
        folderDropDownList.Enabled = True
        Me.btnUploadUpdate.Visible = True

    End Sub
End Class
