Imports System.Data.Common
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports System.Web.HttpContext 
Imports System.Web
Imports CharterDotCom.CommonEntities.Library
Imports CharterDotCom.CommonEntities.Library.EntityWebConfig
Imports System.Xml


Public Class ProviderManager
    Inherits IWebConfigProvider



    Private _cmd As DbCommand
    Private _db As Database

  Public Sub New()
    Me.DB = DatabaseFactory.CreateDatabase("Charter.Website.WS.CommerceEngineConnectionString")

   

  End Sub
  'Public Sub New(ByVal args As XmlNode)
  '  ''If (Not args Is Nothing) AndAlso (Not args.ChildNodes(0) Is Nothing) AndAlso _
  '  ''                (args.ChildNodes(0).LocalName = Constant.CONFIG_PROVIDERDATA) Then
  '  ''  For Each providerData As XmlNode In args.ChildNodes(0).ChildNodes
  '  ''    Select Case providerData.LocalName.ToUpper
  '  ''      Case Constant.CONFIG_CONNECTIONSTRING
  '  ''        Me.DB = DatabaseFactory.CreateDatabase(providerData.InnerText)
  '  ''      Case Else
  '  ''    End Select
  '  ''  Next
  '  ''Else
  '  ''  Throw New Exception(Constant.EXCEPTION_CONFIGURATION)
  '  ''End If
  'End Sub
    Public Shared Function Instance() As ProviderManager
    'Try
    '    If Current.Session("ContentData") Is Nothing Then
    '        'If user instance does not exist, create one
    '        Current.Session("ContentData") = New ProviderManager()
    '        Return CType(Current.Session("ProviderManager"), ProviderManager)
    '    Else
    '        'If user instance already exists, return that
    '        Return CType(Current.Session("ProviderManager"), ProviderManager)
    '    End If
    'Catch ex As Exception
    '    Throw ex
    'End Try
    Return Nothing
    End Function
    Private Property DB() As Database
        Get
            Return _db
        End Get
        Set(ByVal value As Database)
            _db = value
        End Set
    End Property
    Private Property CMD() As DbCommand
        Get
            Return _cmd
        End Get
        Set(ByVal value As DbCommand)
            _cmd = value
        End Set
    End Property


 
    Public Overrides Function GetWebConfigSettings(ByVal RequestLocation As String) As CharterDotCom.CommonEntities.Library.IProviderResults
        '############################################## 
        Dim objResponse As New GetWebConfigSettingsResponse
        '##############################################

        Try
            Dim strSQL As String = "SELECT [ID],[Key],XMLdata FROM SessionState.WebConfig WHERE CONFIGLOCATION = " & RequestLocation
            Me.CMD = DB.GetSqlStringCommand(strSQL)

            Dim retCode As DataSet = Me.DB.ExecuteDataSet(CMD)

            If Not retCode Is Nothing Then
                If retCode.Tables.Count = 1 Then
                    objResponse.BLWebConfigSettingsCollection = New WebConfigSettingsCollection
                    For Each row As Data.DataRow In retCode.Tables(0).Rows
                        Dim lWebConfigSetting As New WebConfigSetting
                        lWebConfigSetting.ID = row.Item("ID")
                        lWebConfigSetting.key = row.Item("Key")
                        lWebConfigSetting.XMLData = row.Item("XMLData")
                        objResponse.BLWebConfigSettingsCollection.WebConfigSetting.Add(lWebConfigSetting)
                    Next
                End If
            End If
            Return objResponse
        Catch ex As Exception
            objResponse.Exception = True
            objResponse.ExceptionMessage = ex.Message
            objResponse.ExceptionStackTrace = ex.StackTrace
            Return objResponse
        Finally
            If Not CMD Is Nothing Then
                CMD.Dispose()
            End If
        End Try
    End Function
End Class
