Imports CommonBLEntities
Imports CommonECGEntities
Imports System.Configuration
Imports CommonProviderInterfaces
Imports CommonECGEntities.EntityEBSServices
Imports CommonECGEntities.EntitySupport
Imports CommonECGEntities.EntitySearch
Imports CommonECGEntities.EntityWebConfig
Imports System.Xml
Imports System.Text
Imports System.Web
Imports System.Collections.Generic

Public Class AdminSiteManager

  Private _ChannelLineUp As BLChannelLineUp
  Private configdata_ebs As XmlNode
  Private configdata_support As XmlNode
  Private billinsystemcode As String
  Private FTACode As String
  Private findServiceability As Boolean = True
  Private SiteKey As String
  Public Sub New()

  End Sub


  Public Shared Function Instance() As AdminSiteManager
    Try
      Return New AdminSiteManager
    Catch ex As Exception
      Throw ex
    End Try
  End Function

#Region "SUPPORT METHODS AND FUNCTIONS V2"


  Public Sub AddDocumentComments(ByVal Comments As String, ByVal CreatedBy As String, ByVal DocumentID As Integer, ByVal Helpful As Boolean, ByVal Rating As Integer)
    '#################################################
    'THIS IS USED TO MAINTAIN STATE BETWEEN CALLS.
    'EACH TIME A METHOD IS CALLED THE STATE NEEDS 
    'TO BE RESTORED TO THIS OBJECT BY CALLING 
    'PopulateLocalVariables()
    '#################################################


    Try
      Dim iProvider As ISupportProvider = New SupportProvider.ProviderManager
      Dim objRequest As New EntitySupport.AddDocumentCommentsRequest

      objRequest.Comments = Comments
      objRequest.CreatedBy = CreatedBy
      objRequest.DocumentID = DocumentID
      objRequest.Helpful = Helpful
      objRequest.Rating = Rating


      '##########
      'EXECUTE
      '##########
      iProvider.AddDocumentComments(objRequest)


    Catch ex As Exception
      Throw ex
    End Try
  End Sub
    Public Function GetRelatedDocuments(ByVal ID As Integer) As DocumentFolder
        Try
            '#################################################
            'THIS IS USED TO MAINTAIN STATE BETWEEN CALLS.
            'EACH TIME A METHOD IS CALLED THE STATE NEEDS 
            'TO BE RESTORED TO THIS OBJECT BY CALLING 
            'PopulateLocalVariables()
            '#################################################


            Dim iProvider As ISupportProvider = New SupportProvider.ProviderManager
            Try
                Dim result As Dictionary(Of String, Object) = iProvider.GetRelatedDocuments(ID)

                'Dim lOutList As New Dictionary(Of String, Object)
                'For Each item As Dictionary(Of String, Object) In result
                '    Dim lList As New BLHTMLDocument
                '    lList.ID = CInt(item(0))
                '    lList.Title = item(1).ToString()
                '    lOutList.Add(lList)
                'Next

                Dim lFolder As New DocumentFolder
                Dim DocList As New List(Of BLHTMLDocument)
                Dim lCnt As Integer = 0
                For Each item As Dictionary(Of String, Object) In result.Values
                    Dim lList As New BLHTMLDocument
                    lList.ID = CType(item("HTMLDOcumentID"), Integer)
                    lList.Title = item("Title").ToString()

                

                    DocList.Add(lList)
                    lCnt = lCnt + 1
                Next

                If DocList IsNot Nothing Then
                    lFolder.ChildDocuments = DocList.ToArray
                End If

                'Return lOutList
                Return lFolder


                'Return SiteManagerProxy.GetRelatedDocuments(ID)

            Catch ex As Exception
                Throw ex
            End Try


        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Sub RemoveDocumentRelation(ByVal ID As Integer, ByVal IDtoRemove As Integer)
        Try
            '#################################################
            'THIS IS USED TO MAINTAIN STATE BETWEEN CALLS.
            'EACH TIME A METHOD IS CALLED THE STATE NEEDS 
            'TO BE RESTORED TO THIS OBJECT BY CALLING 
            'PopulateLocalVariables()
            '#################################################


            Dim iProvider As ISupportProvider = New SupportProvider.ProviderManager
            iProvider.RemoveDocumentRelation(ID, IDtoRemove)

        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Public Function GetAllDocuments() As DocumentFolder
        Try
            '#################################################
            'THIS IS USED TO MAINTAIN STATE BETWEEN CALLS.
            'EACH TIME A METHOD IS CALLED THE STATE NEEDS 
            'TO BE RESTORED TO THIS OBJECT BY CALLING 
            'PopulateLocalVariables()
            '#################################################


            Dim iProvider As ISupportProvider = New SupportProvider.ProviderManager
            Dim result As Dictionary(Of String, Object) = iProvider.GetAllDocuments()

            'Dim lOutList As New Dictionary(Of String, Object)
            'For Each item As Dictionary(Of String, Object) In result
            '    Dim lList As New BLHTMLDocument
            '    lList.ID = CInt(item(0))
            '    lList.Title = item(1).ToString()
            '    lOutList.Add(lList)
            'Next

            'Return lOutList

            Dim lFolder As New DocumentFolder
            Dim docList As New List(Of BLHTMLDocument)
            Dim lCnt As Integer = 0
            For Each item As Dictionary(Of String, Object) In result.Values
                Dim lList As New BLHTMLDocument
                lList.ID = CType(item("HTMLDOcumentID"), Integer)
                lList.Title = item("Title").ToString()



                docList.Add(lList)
                lCnt = lCnt + 1
            Next

            If docList IsNot Nothing Then
                lFolder.ChildDocuments = docList.ToArray
            End If

            Return lFolder

        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Sub RelateDocument(ByVal ID As Integer, ByVal IDtoRelate As Integer)
        Try
            '#################################################
            'THIS IS USED TO MAINTAIN STATE BETWEEN CALLS.
            'EACH TIME A METHOD IS CALLED THE STATE NEEDS 
            'TO BE RESTORED TO THIS OBJECT BY CALLING 
            'PopulateLocalVariables()
            '#################################################


            Dim iProvider As ISupportProvider = New SupportProvider.ProviderManager
            iProvider.RelateDocument(ID, IDtoRelate)

        Catch ex As Exception

            Throw ex
        End Try
    End Sub
  Public Function AddDocument(ByVal folderID As Integer, ByVal HTML As String, ByVal ID As Integer, ByVal KeyWords As String, ByVal Summary As String, ByVal Title As String) As Integer
    Try
      '#################################################
      'THIS IS USED TO MAINTAIN STATE BETWEEN CALLS.
      'EACH TIME A METHOD IS CALLED THE STATE NEEDS 
      'TO BE RESTORED TO THIS OBJECT BY CALLING 
      'PopulateLocalVariables()
      '#################################################


      Dim iProvider As ISupportProvider = New SupportProvider.ProviderManager
      Return iProvider.AddDocument(folderID, HTML, ID, KeyWords, Summary, Title)


    Catch ex As Exception

      Throw ex
    End Try
  End Function
  Public Function AddFolder(ByVal ID As Integer, ByVal Name As String, ByVal NumberOfChildren As Integer, ByVal ParentFolderID As Integer) As Integer
    Try
      '#################################################
      'THIS IS USED TO MAINTAIN STATE BETWEEN CALLS.
      'EACH TIME A METHOD IS CALLED THE STATE NEEDS 
      'TO BE RESTORED TO THIS OBJECT BY CALLING 
      'PopulateLocalVariables()
      '#################################################


      Dim iProvider As ISupportProvider = New SupportProvider.ProviderManager
      Return iProvider.AddFolder(ID, Name, NumberOfChildren, ParentFolderID)


    Catch ex As Exception

      Throw ex
    End Try
  End Function
  Public Sub DeleteDocument(ByVal documentID As Integer)
    Try
      '#################################################
      'THIS IS USED TO MAINTAIN STATE BETWEEN CALLS.
      'EACH TIME A METHOD IS CALLED THE STATE NEEDS 
      'TO BE RESTORED TO THIS OBJECT BY CALLING 
      'PopulateLocalVariables()
      '#################################################


      Dim iProvider As ISupportProvider = New SupportProvider.ProviderManager
      iProvider.DeleteDocument(documentID)


    Catch ex As Exception

      Throw ex
    End Try
  End Sub
  Public Sub DeleteFolder(ByVal FolderID As Integer)
    Try
      '#################################################
      'THIS IS USED TO MAINTAIN STATE BETWEEN CALLS.
      'EACH TIME A METHOD IS CALLED THE STATE NEEDS 
      'TO BE RESTORED TO THIS OBJECT BY CALLING 
      'PopulateLocalVariables()
      '#################################################


      Dim iProvider As ISupportProvider = New SupportProvider.ProviderManager
      iProvider.DeleteFolder(FolderID)


    Catch ex As Exception

      Throw ex
    End Try
  End Sub
  Public Function GetDistinctKeyWords() As Dictionary(Of String, Object)
    Try
      '#################################################
      'THIS IS USED TO MAINTAIN STATE BETWEEN CALLS.
      'EACH TIME A METHOD IS CALLED THE STATE NEEDS 
      'TO BE RESTORED TO THIS OBJECT BY CALLING 
      'PopulateLocalVariables()
      '#################################################


      Dim iProvider As ISupportProvider = New SupportProvider.ProviderManager
      Return iProvider.GetDistinctKeyWords()


    Catch ex As Exception

      Throw ex
    End Try
  End Function
  Public Function GetDocument(ByVal documentID As Integer) As IHTMLDocument
    Try
      '#################################################
      'THIS IS USED TO MAINTAIN STATE BETWEEN CALLS.
      'EACH TIME A METHOD IS CALLED THE STATE NEEDS 
      'TO BE RESTORED TO THIS OBJECT BY CALLING 
      'PopulateLocalVariables()
      '#################################################


      Dim iProvider As ISupportProvider = New SupportProvider.ProviderManager
      Dim result As Dictionary(Of String, Object) = iProvider.GetDocument(documentID)

      Dim document As New BLHTMLDocument

      document.HTML = result("HTML").ToString()
      document.ID = CInt(result("HTMLDocumentID"))
      document.KeyWords = result("Keywords").ToString()
      document.Summary = result("Summary").ToString()
      document.Title = result("Title").ToString()

      Return document


    Catch ex As Exception

      Throw ex
    End Try
  End Function

  Public Function GetAllFolders() As List(Of CachedFolder)
    Try
      Dim iProvider As ISupportProvider = New SupportProvider.ProviderManager
      Dim folderTable As Data.DataTable
      Dim list As New List(Of CachedFolder)
      Dim folder As CachedFolder
      folderTable = iProvider.GetAllFolders
      If folderTable IsNot Nothing Then
        For Each row As Data.DataRow In folderTable.Rows
          folder.ID = CInt(row("DocumentFolderID"))
          folder.Name = CStr(row("DocumentFolderName"))
          folder.ParentID = CInt(row("ParentFolderID"))
          list.Add(folder)
        Next
      End If
      Return list

    Catch ex As Exception

      Throw ex
    End Try

  End Function

    Public Function GetDocumentFolders() As DocumentFolder
        Try
            '#################################################
            'THIS IS USED TO MAINTAIN STATE BETWEEN CALLS.
            'EACH TIME A METHOD IS CALLED THE STATE NEEDS 
            'TO BE RESTORED TO THIS OBJECT BY CALLING 
            'PopulateLocalVariables()
            '#################################################


            Dim iProvider As ISupportProvider = New SupportProvider.ProviderManager
            Dim Folders As Dictionary(Of String, Object) = iProvider.GetDocumentFolders()
            Dim folderslist As New List(Of DocumentFolder)


            Dim outList As New DocumentFolder
            Dim lCnt As Integer = 0
            For Each obj As Dictionary(Of String, Object) In Folders.Values
                Dim folder As New DocumentFolder
                folder.ID = CInt(obj("DocumentFolderID"))
                folder.Name = obj("DocumentFolderName").ToString()

                folderslist.Add(folder)
                lCnt = lCnt + 1
            Next

            If Not folderslist Is Nothing Then
                outList.ChildFolders = folderslist.ToArray()
            End If

            Return outList
        Catch ex As Exception

            Throw ex
        End Try
    End Function
    Public Function GetDocumentFolders(ByVal parentFolderID As Integer) As DocumentFolder
        Try
            '#################################################
            'THIS IS USED TO MAINTAIN STATE BETWEEN CALLS.
            'EACH TIME A METHOD IS CALLED THE STATE NEEDS 
            'TO BE RESTORED TO THIS OBJECT BY CALLING 
            'PopulateLocalVariables()
            '#################################################


            Dim iProvider As ISupportProvider = New SupportProvider.ProviderManager
            Dim Folders As Dictionary(Of String, Object)
            Dim outList As New DocumentFolder
            Dim folderslist As New List(Of DocumentFolder)


            Folders = iProvider.GetDocumentFolders(parentFolderID)

            Dim lCnt As Integer = 0
            For Each obj As Dictionary(Of String, Object) In Folders.Values
                Dim folder As New DocumentFolder
                folder.ID = CInt(obj("DocumentFolderID"))
                folder.Name = obj("DocumentFolderName").ToString()
                folder.NumberOfChildren = CInt(obj("NumberOfChildren"))

                folderslist.Add(folder)
                lCnt = lCnt + 1
            Next


            If Not folderslist Is Nothing Then
                outList.ChildFolders = folderslist.ToArray()
            End If

            Return outList

        Catch ex As Exception

            Throw ex
        End Try
    End Function
    Public Function GetFolder(ByVal folderID As Integer) As DocumentFolder
        Try
            '#################################################
            'THIS IS USED TO MAINTAIN STATE BETWEEN CALLS.
            'EACH TIME A METHOD IS CALLED THE STATE NEEDS 
            'TO BE RESTORED TO THIS OBJECT BY CALLING 
            'PopulateLocalVariables()
            '#################################################


            Dim iProvider As ISupportProvider = New SupportProvider.ProviderManager
            Dim result As Dictionary(Of String, Object)
            Dim folder As New DocumentFolder
            Dim cacheEntry As Object
            cacheEntry = HttpContext.Current.Cache(CommonBLEntities.Common.SUPPORT_CACHE)
            Dim supportCache As List(Of CachedFolder)

            If cacheEntry Is Nothing Then
                result = iProvider.GetFolder(folderID)

                folder.ID = CInt(result("DocumentFolderID"))
                folder.Name = result("DocumentFolderName").ToString()
                folder.ParentFolderID = CInt(result("ParentFolderID"))
                Return folder
            Else
                ' get from cache
                supportCache = CType(cacheEntry, List(Of CachedFolder))
                For Each cf As CachedFolder In supportCache
                    If cf.ID = folderID Then
                        folder.ID = cf.ID
                        folder.Name = cf.Name
                        folder.ParentFolderID = cf.ParentID
                        Return folder
                    End If
                Next
            End If

            Return Nothing

        Catch ex As Exception

            Throw ex
        End Try
    End Function
    Public Function GetFolderDocuments(ByVal folderID As Integer) As DocumentFolder
        Try
            '#################################################
            'THIS IS USED TO MAINTAIN STATE BETWEEN CALLS.
            'EACH TIME A METHOD IS CALLED THE STATE NEEDS 
            'TO BE RESTORED TO THIS OBJECT BY CALLING 
            'PopulateLocalVariables()
            '#################################################


            Dim iProvider As ISupportProvider = New SupportProvider.ProviderManager
            Dim Documents As Dictionary(Of String, Object)
            Dim docslist As New List(Of BLHTMLDocument)

            Dim outList As New DocumentFolder

            Documents = iProvider.GetFolderDocuments(folderID)

            Dim lCnt As Integer = 0
            For Each obj As Dictionary(Of String, Object) In Documents.Values
                Dim document As New BLHTMLDocument
                document.ID = CInt(obj("HTMLDOcumentID"))
                document.Title = obj("Title").ToString()
                document.Summary = obj("Summary").ToString()
                document.HTML = obj("HTML").ToString()
                document.KeyWords = obj("KEYWORDS").ToString()

                

                docslist.Add(document)
                lCnt = lCnt + 1
            Next

            If docslist IsNot Nothing Then
                outList.ChildDocuments = docslist.ToArray
            End If

            Return outList

        Catch ex As Exception

            Throw ex
        End Try
    End Function
  Public Sub MoveDocument(ByVal documentID As Integer, ByVal newFolderID As Integer)
    Try
      '#################################################
      'THIS IS USED TO MAINTAIN STATE BETWEEN CALLS.
      'EACH TIME A METHOD IS CALLED THE STATE NEEDS 
      'TO BE RESTORED TO THIS OBJECT BY CALLING 
      'PopulateLocalVariables()
      '#################################################


      Dim iProvider As ISupportProvider = New SupportProvider.ProviderManager
      iProvider.MoveDocument(documentID, newFolderID)


    Catch ex As Exception

      Throw ex
    End Try
  End Sub
  Public Sub UpdateDocument(ByVal HTML As String, ByVal ID As Integer, ByVal KeyWords As String, ByVal Summary As String, ByVal Title As String)
    Try
      '#################################################
      'THIS IS USED TO MAINTAIN STATE BETWEEN CALLS.
      'EACH TIME A METHOD IS CALLED THE STATE NEEDS 
      'TO BE RESTORED TO THIS OBJECT BY CALLING 
      'PopulateLocalVariables()
      '#################################################


      Dim iProvider As ISupportProvider = New SupportProvider.ProviderManager
      iProvider.UpdateDocument(HTML, ID, KeyWords, Summary, Title)


    Catch ex As Exception

      Throw ex
    End Try
  End Sub
  Public Sub UpdateFolder(ByVal ID As Integer, ByVal Name As String, ByVal NumberOfChildren As Integer, ByVal ParentFolderID As Integer)
    Try
      '#################################################
      'THIS IS USED TO MAINTAIN STATE BETWEEN CALLS.
      'EACH TIME A METHOD IS CALLED THE STATE NEEDS 
      'TO BE RESTORED TO THIS OBJECT BY CALLING 
      'PopulateLocalVariables()
      '#################################################


      Dim iProvider As ISupportProvider = New SupportProvider.ProviderManager
      iProvider.UpdateFolder(ID, Name, NumberOfChildren, ParentFolderID)


    Catch ex As Exception

      Throw ex
    End Try
  End Sub
  'Public  Sub DeleteKeyWords(ByVal documentID As Integer)
  '    Try
  '        '#################################################
  '        'THIS IS USED TO MAINTAIN STATE BETWEEN CALLS.
  '        'EACH TIME A METHOD IS CALLED THE STATE NEEDS 
  '        'TO BE RESTORED TO THIS OBJECT BY CALLING 
  '        'PopulateLocalVariables()
  '        '#################################################
  '       

  '        Dim iProvider As ISupportProvider = New SupportProvider.ProviderManager(configdata_support)
  '        iProvider.DeleteKeyWords(documentID)


  '    Catch ex As Exception
  '
  'Throw ex
  '    End Try
  'End Sub
  'Public  Sub AddKeyWords(ByVal documentID As Integer, ByRef keyWords As String)
  '    Try
  '        '#################################################
  '        'THIS IS USED TO MAINTAIN STATE BETWEEN CALLS.
  '        'EACH TIME A METHOD IS CALLED THE STATE NEEDS 
  '        'TO BE RESTORED TO THIS OBJECT BY CALLING 
  '        'PopulateLocalVariables()
  '        '#################################################
  '       

  '        Dim iProvider As ISupportProvider = New SupportProvider.ProviderManager(configdata_support)
  '        iProvider.AddKeyWords(documentID, keyWords)


  '    Catch ex As Exception
  '
  'Throw ex
  '    End Try
  'End Sub

  Public Function GetBinaryDocument(ByVal ID As Integer) As KeyValuePair(Of String, Byte())
    Try


      Dim iProvider As ISupportProvider = New SupportProvider.ProviderManager
      Return iProvider.GetBinaryDocument(ID)

    Catch ex As Exception
      Throw ex
    End Try
  End Function


#End Region

    Public Function AddFolder(ByVal obj As DocumentFolder) As Integer
        Try
            Return AddFolder(obj.ID, obj.Name, obj.NumberOfChildren, obj.ParentFolderID)
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Public Function AddDocument(ByVal folderID As Integer, ByVal obj As BLHTMLDocument) As Integer
        Try
            Return AddDocument(folderID, obj.HTML, obj.ID, obj.KeyWords, obj.Summary, obj.Title)

        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Public Sub UpdateDocument(ByVal obj As BLHTMLDocument)
        Try
            UpdateDocument(obj.HTML, obj.ID, obj.KeyWords, obj.Summary, obj.Title)

        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Public Sub UpdateFolder(ByRef obj As DocumentFolder)
        Try
            UpdateFolder(obj.ID, obj.Name, obj.NumberOfChildren, obj.ParentFolderID)

        Catch ex As Exception
            Throw ex
        End Try
    End Sub

End Class
