Imports System.Xml
Imports System.Xml.Serialization
Imports System.Configuration
Imports CharterDotCom.CommonEntities.Library
Imports CharterDotCom.CommonEntities.Library.Enumerations
Imports CharterDotCom.CommonEntities.Library.EntityMemberServices
Imports CharterDotCom.CommonEntities.Library.EntityEBSServices
Imports CharterDotCom.CommonEntities.Library.EntitySupport
Imports CharterDotCom.CommonEntities.Library.EntitySearch
Imports CharterDotCom.CommonEntities.Library.EntityWebConfig
Imports CharterDotCom.CommonEntities.Library.EntityG2BServices
Imports CharterDotCom.WebSite.Library.BLCommon
Imports System.Web

Public Class BLContentManager
  Inherits ContentClient

  Private contentProvider As IContentProvider
  Private site_versions As Dictionary(Of String, String)
  Private _contentID As Integer
  Private _versionid As Integer
  Private _usercontrol As String
  Private _ownerid As String
  Private _ownertypeid As Integer
  Private configdata_content As XmlNode


  Public Sub New()

  End Sub
  Public Sub New(ByVal DictionaryOfVariables As Dictionary(Of String, Object))
    'If Not (DictionaryOfVariables("contentProvider") Is Nothing) Then
    '    Me.contentProvider = CType(DictionaryOfVariables("contentProvider"), IContentProvider)
        'End If
        If DictionaryOfVariables IsNot Nothing Then
            If DictionaryOfVariables.ContainsKey("site_versions") AndAlso Not (DictionaryOfVariables("site_versions") Is Nothing) Then
                Me.site_versions = CType(DictionaryOfVariables("site_versions"), Dictionary(Of String, String))
            End If
            If DictionaryOfVariables.ContainsKey("_contentID") AndAlso Not (DictionaryOfVariables("_contentID") Is Nothing) Then
                Me._contentID = CType(DictionaryOfVariables("_contentID"), Integer)
            End If
            If DictionaryOfVariables.ContainsKey("_versionid") AndAlso Not (DictionaryOfVariables("_versionid") Is Nothing) Then
                Me._versionid = CType(DictionaryOfVariables("_versionid"), Integer)
            End If
            If DictionaryOfVariables.ContainsKey("_usercontrol") AndAlso Not (DictionaryOfVariables("_usercontrol") Is Nothing) Then
                Me._usercontrol = CType(DictionaryOfVariables("_usercontrol"), String)
            End If
            If DictionaryOfVariables.ContainsKey("_ownerid") AndAlso Not (DictionaryOfVariables("_ownerid") Is Nothing) Then
                Me._ownerid = CType(DictionaryOfVariables("_ownerid"), String)
            End If
            If DictionaryOfVariables.ContainsKey("_ownertypeid") AndAlso Not (DictionaryOfVariables("_ownertypeid") Is Nothing) Then
                Me._ownertypeid = CType(DictionaryOfVariables("_ownertypeid"), Integer)
            End If
        End If
    End Sub

  Public ReadOnly Property ValuesForSetSession() As Dictionary(Of String, Object)
    Get
      Try
        Dim VarList As New Dictionary(Of String, Object)
        'VarList.Add("contentProvider", Me.contentProvider)
        VarList.Add("site_versions", Me.site_versions)
        VarList.Add("_contentID", Me._contentID)
        VarList.Add("_versionid", Me._versionid)
        VarList.Add("_usercontrol", Me._usercontrol)
        VarList.Add("_ownerid", Me._ownerid)
        VarList.Add("_ownertypeid", Me._ownertypeid)
        Return VarList
      Catch ex As Exception
        Throw ex
      End Try
    End Get
  End Property

  Protected Overrides Sub PopulateLocalVariables(ByVal trackingID As String)
        Try
            If Not (trackingID Is Nothing) Then
                Dim objSession As Object
                objSession = Session(BLCommon.ContentManagerKey)
                MyBase.TrackingID = trackingID
                If Not objSession Is Nothing Then
                    Dim lManager As New BLContentManager(CType(objSession, Dictionary(Of String, Object)))
                    contentProvider = lManager.contentProvider
                    Me.site_versions = lManager.site_versions
                    Me._contentID = lManager._contentID
                    Me._versionid = lManager._versionid
                    Me._usercontrol = lManager._usercontrol
                    Me._ownerid = lManager._ownerid
                    Me._ownertypeid = lManager._ownertypeid
                End If
            End If
            contentProvider = CType(Activator.GetObject(GetType(IContentProvider), CStr(HttpContext.Current.Application("RemoteURL_ContentProvider"))), IContentProvider)


        Catch ex As Exception
            Throw ex
        End Try

    End Sub

    'Retrieves all template header information for the site.  This method should be called on application start
    Public Sub LoadTemplateHeaderInfo()
        contentProvider = CType(Activator.GetObject(GetType(IContentProvider), CStr(HttpContext.Current.Application("RemoteURL_ContentProvider"))), IContentProvider)
        Dim headerInfo As String = contentProvider.GetTemplateHeaderInfo()
        Dim xdoc As XmlDocument = New XmlDocument()
        xdoc.LoadXml(headerInfo)
        HttpContext.Current.Application("templateHeaderInfo") = xdoc.DocumentElement
    End Sub


#Region "Version Maintenance Functions"

    Public Overrides Function GetContentID(ByVal trackingID As String, ByVal UserControl As String, ByVal OwnerID As String, ByVal Ownertype As String) As Integer
        Try
            PopulateLocalVariables(trackingID)
            Dim lOwnerType As OwnerType = BLCommon.ConvertToOwnerType(Ownertype)
            Dim intReturn As Integer
            intReturn = GetContentID_Business(UserControl, OwnerID, lOwnerType)
            If intReturn = Nothing Then
                Return Nothing
            Else
                Return intReturn
            End If
        Catch ex As Exception
            BLCommon.LogException(ex, ExceptionSeverity.High)
            Throw ex
        End Try
    End Function

    Private Function GetContentID_Business(ByVal UserControl As String, ByVal OwnerID As String, ByVal Ownertype As OwnerType) As Integer
        Try
            Dim intLocaleID As Integer
            Dim intRegionID As Integer
            Dim intKMAId As Integer
            Dim intBillingStationID As Integer
            Dim strFTAID As String
            Dim intTemp As Integer

            Dim objAccountmanager As New BLAccountManager(CType(Session(BLCommon.AccountManagerKey), Dictionary(Of String, Object)))

            intLocaleID = objAccountmanager.LocaleID
            intRegionID = objAccountmanager.account.RegionID
            intKMAId = objAccountmanager.account.KMAID
            intBillingStationID = objAccountmanager.account.BillingCode
            strFTAID = objAccountmanager.account.FTA

            _usercontrol = UserControl
            _ownerid = OwnerID
            _ownertypeid = Ownertype

            SetSession(BLCommon.ContentManagerKey, Me.ValuesForSetSession)

            If intLocaleID = 0 Then
                BLCommon.LogException(New Exception("Content Manager : Invalid LocaleID of Zero or Locale ID not set"), ExceptionSeverity.High)
                Return Nothing
            End If

            'Currently test parameters are used, needs to change to refer to actual parameters
            If Not objAccountmanager.account.UserType = UserType.Author Then
                Select Case Ownertype
                    Case Ownertype.Bundle, Ownertype.Feature, Ownertype.ServicePlan
                        If Len(CStr(OwnerID)) > 1 Then
                            OwnerID = Right(OwnerID, Len(OwnerID) - 1)
                        End If
                End Select
            Else

                Select Case Ownertype
                    Case Ownertype.Bundle, Ownertype.Feature, Ownertype.ServicePlan
                        If Len(CStr(OwnerID)) > 1 Then
                            OwnerID = Right(OwnerID, Len(OwnerID) - 1)
                        End If
                End Select

                Select Case Ownertype
                    Case Ownertype.Bundle, Ownertype.ServicePlan
                        Dim objOwnerList As OWNERLIST
                        objOwnerList = GetEPCIDList(OwnerID)
                        If Not objOwnerList.m_OWNERDATA Is Nothing Then
                            For Each item As OWNERDATA In objOwnerList.m_OWNERDATA
                                OwnerID = item.ID
                                Exit For
                            Next
                        End If
                End Select


            End If



            'To check if only Billing code and FTA are available, then display content at the corresponding level
            If intRegionID = 0 And intKMAId = 0 And intBillingStationID <> 0 And strFTAID <> String.Empty Then
                intTemp = contentProvider.GetContentID(UserControl, intLocaleID, OwnerID, Ownertype, Nothing, Nothing, intBillingStationID, strFTAID)
            Else
                'To check if none of the Organization details are available, then display National level content.
                If intRegionID = 0 And intKMAId = 0 And intBillingStationID = 0 And strFTAID = String.Empty Then
                    intTemp = contentProvider.GetContentID(UserControl, intLocaleID, OwnerID, Ownertype, Nothing, Nothing, Nothing, Nothing)
                Else
                    'To check if localized until region, then display the Region level content
                    If intRegionID <> 0 And intKMAId = 0 And intBillingStationID = 0 And strFTAID = String.Empty Then
                        intTemp = contentProvider.GetContentID(UserControl, intLocaleID, OwnerID, Ownertype, intRegionID, Nothing, Nothing, Nothing)
                    End If
                    'To check if localized until KMA, then display the KMA level content
                    If intRegionID <> 0 And intKMAId <> 0 And intBillingStationID = 0 And strFTAID = String.Empty Then
                        intTemp = contentProvider.GetContentID(UserControl, intLocaleID, OwnerID, Ownertype, intRegionID, intKMAId, Nothing, Nothing)
                    End If
                    'To check if localized until Billing station, then display the Billing station level content
                    If intRegionID <> 0 And intKMAId <> 0 And intBillingStationID <> 0 And strFTAID = String.Empty Then
                        intTemp = contentProvider.GetContentID(UserControl, intLocaleID, OwnerID, Ownertype, intRegionID, intKMAId, intBillingStationID, Nothing)
                    End If
                    'To check if localized until FTA, then display the FTA level content
                    If intRegionID <> 0 And intKMAId <> 0 And intBillingStationID <> 0 And strFTAID <> String.Empty Then
                        intTemp = contentProvider.GetContentID(UserControl, intLocaleID, OwnerID, Ownertype, intRegionID, intKMAId, intBillingStationID, strFTAID)
                    End If

                End If
            End If

            Return intTemp


        Catch ex As Exception
            BLCommon.LogException(ex, ExceptionSeverity.High)
            Throw ex
        Finally

        End Try

        Return Nothing

    End Function

    Public Overrides Function GetVersions(ByVal TrackingId As String, ByVal ContentID As Integer) As List(Of ContentSchema.VERSION)
        PopulateLocalVariables(TrackingId)
        'Function level variables which are destroyed in the Finally block
        Dim objRoot As New XmlRootAttribute("CONTENT")
        Dim versionlist As New List(Of ContentSchema.VERSION)
        'Get object to be returned to UI layer
        Dim objContentData As New ContentSchema.CONTENT
        'Get serializer for De-serializing content xml to Content object
        Dim objXMLSerializer_Content As XmlSerializer

        Try
            'Currently test parameters are used, needs to change to refer to actual parameters

            Dim strtemp As String = contentProvider.GetContentVersionsByContentID(ContentID) ' GetContentNormalized("Test2", 0, 0, 12, 0) 'ContentData.GetContentNormalized("WEBCONTROL_00", EntityType, Right(ItemID, Len(ItemID) - 1), Session("FTAID"), Session("LocaleID"))

            'Load the string reader with the XML string returned by Data Access layer
            Dim objReader As New System.IO.StringReader(strtemp)

            'Instantiate XML serializer of the corresponding content object type
            objXMLSerializer_Content = New XmlSerializer(GetType(ContentSchema.CONTENT), objRoot)

            'De-serialize the XML data onto an instance
            objContentData = CType(objXMLSerializer_Content.Deserialize(objReader), ContentSchema.CONTENT)

            'return the object to UI layer
            If Not objContentData.m_version Is Nothing Then
                versionlist.AddRange(objContentData.m_version)
                Return versionlist
            Else
                Return Nothing
            End If


        Catch ex As Exception
            BLCommon.LogException(ex, ExceptionSeverity.High)
            Throw ex
        Finally
            versionlist = Nothing
            objRoot = Nothing
        End Try
    End Function

    Public Overrides Sub UpdateVersionText(ByVal TrackingId As String, ByVal VersionId As Integer, ByVal VersionText As String, ByVal VersionName As String, ByVal SearchResultDisplayable As Boolean)
        Try
            PopulateLocalVariables(TrackingId)
            contentProvider.UpdateVersion(VersionId, VersionText, VersionName, SearchResultDisplayable)
        Catch ex As Exception
            BLCommon.LogException(ex, ExceptionSeverity.High)
            Throw ex
        Finally
            'objDA = Nothing
        End Try
    End Sub

    Public Overrides Sub DeleteVersion(ByVal TrackingId As String, ByVal versionID As Integer)
        'Dim objDA As New ContentData
        Try
            PopulateLocalVariables(TrackingId)
            contentProvider.DeleteVersion(versionID)
        Catch ex As Exception
            BLCommon.LogException(ex, ExceptionSeverity.High)
            Throw ex
        Finally
        End Try
    End Sub

    Public Overrides Function AddVersion(ByVal TrackingId As String, ByVal ContentID As Integer, ByVal VersionName As String, ByVal VersionText As String) As Boolean

        PopulateLocalVariables(TrackingId)

        Dim lContentID As Integer
        Dim intLocaleID As Integer
        Dim intRegionID As Integer
        Dim intKMAId As Integer
        Dim intBillingStationID As Integer
        Dim strFTAID As String
        Dim strTemp As String

        Dim objAccountmanager As New BLAccountManager(CType(Session(BLCommon.AccountManagerKey), Dictionary(Of String, Object)))

        intLocaleID = objAccountmanager.LocaleID
        intRegionID = objAccountmanager.account.RegionID
        intKMAId = objAccountmanager.account.KMAID
        intBillingStationID = objAccountmanager.account.BillingCode
        strFTAID = objAccountmanager.account.FTA

        If intLocaleID = 0 Then
            BLCommon.LogException(New Exception("Content Manager : Invalid LocaleID of Zero or Locale ID not set"), ExceptionSeverity.High)
            Return False
        End If

        'Currently test parameters are used, needs to change to refer to actual parameters

        Try
            If ContentID = 0 Then
                Select Case CType(_ownertypeid, OwnerType)
                    Case OwnerType.Bundle, OwnerType.ServicePlan


                        Dim objOwnerList As OWNERLIST
                        objOwnerList = GetEPCIDList(_ownerid)
                        If Not objOwnerList.m_OWNERDATA Is Nothing Then
                            Dim intcount As Integer = 0
                            For Each item As OWNERDATA In objOwnerList.m_OWNERDATA
                                If intcount = 0 Then

                                    'To check if only Billing code and FTA are available, then display content at the corresponding level
                                    'To check if none of the Organization details are available, then display National level content.
                                    If intRegionID = 0 And intKMAId = 0 And intBillingStationID = 0 And strFTAID = String.Empty Then
                                        lContentID = CInt(contentProvider.AddContentAndContentVersion(_usercontrol, intLocaleID, item.ID, _ownertypeid, Nothing, Nothing, Nothing, Nothing, ContentID, 1, VersionText, VersionName, Now()))
                                    Else
                                        'To check if localized until region, then display the Region level content
                                        If intRegionID <> 0 And intKMAId = 0 And intBillingStationID = 0 And strFTAID = String.Empty Then
                                            lContentID = CInt(contentProvider.AddContentAndContentVersion(_usercontrol, intLocaleID, item.ID, _ownertypeid, intRegionID, Nothing, Nothing, Nothing, ContentID, 1, VersionText, VersionName, Now()))
                                        End If
                                        'To check if localized until KMA, then display the KMA level content
                                        If intRegionID <> 0 And intKMAId <> 0 And intBillingStationID = 0 And strFTAID = String.Empty Then
                                            lContentID = CInt(contentProvider.AddContentAndContentVersion(_usercontrol, intLocaleID, item.ID, _ownertypeid, intRegionID, intKMAId, Nothing, Nothing, ContentID, 1, VersionText, VersionName, Now()))
                                        End If
                                        'To check if localized until Billing station, then display the Billing station level content
                                        If intRegionID <> 0 And intKMAId <> 0 And intBillingStationID <> 0 And strFTAID = String.Empty Then
                                            lContentID = CInt(contentProvider.AddContentAndContentVersion(_usercontrol, intLocaleID, item.ID, _ownertypeid, intRegionID, intKMAId, intBillingStationID, Nothing, ContentID, 1, VersionText, VersionName, Now()))
                                        End If
                                        'To check if localized until FTA, then display the FTA level content
                                        If intRegionID <> 0 And intKMAId <> 0 And intBillingStationID <> 0 And strFTAID <> String.Empty Then
                                            lContentID = CInt(contentProvider.AddContentAndContentVersion(_usercontrol, intLocaleID, item.ID, _ownertypeid, intRegionID, intKMAId, intBillingStationID, strFTAID, ContentID, 1, VersionText, VersionName, Now()))
                                        End If
                                    End If

                                Else

                                    'To check if only Billing code and FTA are available, then display content at the corresponding level
                                    'To check if none of the Organization details are available, then display National level content.
                                    If intRegionID = 0 And intKMAId = 0 And intBillingStationID = 0 And strFTAID = String.Empty Then
                                        contentProvider.AddContent(_usercontrol, intLocaleID, item.ID, _ownertypeid, Nothing, Nothing, Nothing, Nothing, lContentID)
                                    Else
                                        'To check if localized until region, then display the Region level content
                                        If intRegionID <> 0 And intKMAId = 0 And intBillingStationID = 0 And strFTAID = String.Empty Then
                                            contentProvider.AddContent(_usercontrol, intLocaleID, item.ID, _ownertypeid, intRegionID, Nothing, Nothing, Nothing, lContentID)
                                        End If
                                        'To check if localized until KMA, then display the KMA level content
                                        If intRegionID <> 0 And intKMAId <> 0 And intBillingStationID = 0 And strFTAID = String.Empty Then
                                            contentProvider.AddContent(_usercontrol, intLocaleID, item.ID, _ownertypeid, intRegionID, intKMAId, Nothing, Nothing, lContentID)
                                        End If
                                        'To check if localized until Billing station, then display the Billing station level content
                                        If intRegionID <> 0 And intKMAId <> 0 And intBillingStationID <> 0 And strFTAID = String.Empty Then
                                            contentProvider.AddContent(_usercontrol, intLocaleID, item.ID, _ownertypeid, intRegionID, intKMAId, intBillingStationID, Nothing, lContentID)
                                        End If
                                        'To check if localized until FTA, then display the FTA level content
                                        If intRegionID <> 0 And intKMAId <> 0 And intBillingStationID <> 0 And strFTAID <> String.Empty Then
                                            contentProvider.AddContent(_usercontrol, intLocaleID, item.ID, _ownertypeid, intRegionID, intKMAId, intBillingStationID, strFTAID, lContentID)
                                        End If
                                    End If

                                End If
                                intcount = 1
                            Next
                        End If


                    Case Else
                        Dim itemid As String
                        Select Case CType(_ownertypeid, OwnerType)
                            Case OwnerType.Feature
                                If Len(CStr(_ownerid)) > 1 Then
                                    itemid = Right(_ownerid, Len(_ownerid) - 1)
                                End If
                            Case Else
                                itemid = _ownerid
                        End Select
                        lContentID = CInt(contentProvider.AddContentAndContentVersion(_usercontrol, intLocaleID, itemid, _ownertypeid, Nothing, Nothing, Nothing, Nothing, ContentID, 1, VersionText, VersionName, Now()))

                End Select


            Else
                contentProvider.AddVersion(ContentID, 1, Now(), VersionText, VersionName)
            End If

        Catch ex As Exception
            BLCommon.LogException(ex, ExceptionSeverity.High)
            Throw ex
        Finally
        End Try
    End Function

    Public Overrides Function GetCharterMessages(ByVal TrackingId As String, ByVal UserControl As String) As List(Of String)

        PopulateLocalVariables(TrackingId)

        ' Function level variables which are destroyed in the Finally block
        Dim objRoot As New XmlRootAttribute("CONTENT")
        'Get object to be returned to UI layer
        Dim objContentData As ContentSchema.CONTENT
        'Get serializer for De-serializing content xml to Content object
        Dim objXMLSerializer_Content As XmlSerializer
        Dim versionlist As List(Of String)

        Try
            Dim intLocaleID As Integer
            Dim intRegionID As Integer
            Dim intKMAId As Integer
            Dim intBillingStationID As Integer
            Dim strFTAID As String
            Dim strTemp As String

            Dim objAccountmanager As New BLAccountManager(CType(Session(BLCommon.AccountManagerKey), Dictionary(Of String, Object)))


            intLocaleID = objAccountmanager.LocaleID
            intRegionID = objAccountmanager.account.RegionID
            intKMAId = objAccountmanager.account.KMAID
            intBillingStationID = objAccountmanager.account.BillingCode
            strFTAID = objAccountmanager.account.FTA


            If intLocaleID = 0 Then
                BLCommon.LogException(New Exception("Content Manager : Invalid LocaleID of Zero or Locale ID not set"), ExceptionSeverity.High)
                Return Nothing
            End If

            'To check if only Billing code and FTA are available, then display content at the corresponding level
            If intRegionID = 0 And intKMAId = 0 And intBillingStationID <> 0 And strFTAID <> String.Empty Then
                strTemp = contentProvider.GetCharterMessage(UserControl, intLocaleID, Nothing, Nothing, intBillingStationID, strFTAID)
            Else
                'To check if none of the Organization details are available, then display National level content.
                If intRegionID = 0 And intKMAId = 0 And intBillingStationID = 0 And strFTAID = String.Empty Then
                    strTemp = contentProvider.GetCharterMessage(UserControl, intLocaleID, Nothing, Nothing, Nothing, Nothing)
                Else
                    'To check if localized until region, then display the Region level content
                    If intRegionID <> 0 And intKMAId = 0 And intBillingStationID = 0 And strFTAID = String.Empty Then
                        strTemp = contentProvider.GetCharterMessage(UserControl, intLocaleID, intRegionID, Nothing, Nothing, Nothing)
                    End If
                    'To check if localized until KMA, then display the KMA level content
                    If intRegionID <> 0 And intKMAId <> 0 And intBillingStationID = 0 And strFTAID = String.Empty Then
                        strTemp = contentProvider.GetCharterMessage(UserControl, intLocaleID, intRegionID, intKMAId, Nothing, Nothing)
                    End If
                    'To check if localized until Billing station, then display the Billing station level content
                    If intRegionID <> 0 And intKMAId <> 0 And intBillingStationID <> 0 And strFTAID = String.Empty Then
                        strTemp = contentProvider.GetCharterMessage(UserControl, intLocaleID, intRegionID, intKMAId, intBillingStationID, Nothing)
                    End If
                    'To check if localized until FTA, then display the FTA level content
                    If intRegionID <> 0 And intKMAId <> 0 And intBillingStationID <> 0 And strFTAID <> String.Empty Then
                        strTemp = contentProvider.GetCharterMessage(UserControl, intLocaleID, intRegionID, intKMAId, intBillingStationID, strFTAID)
                    End If

                End If
            End If


            'Load the string reader with the XML string returned by Data Access layer
            Dim objReader As New System.IO.StringReader(strTemp)

            ' Instantiate XML serializer of the corresponding content object type
            objXMLSerializer_Content = New XmlSerializer(GetType(ContentSchema.CONTENT), objRoot)

            ' De-serialize the XML data onto an instance
            objContentData = CType(objXMLSerializer_Content.Deserialize(objReader), ContentSchema.CONTENT)

            ' return the object to UI layer
            If Not objContentData.m_version Is Nothing Then
                If site_versions Is Nothing Then site_versions = New Dictionary(Of String, String)
                If Not site_versions.ContainsKey(UserControl & "#" & OwnerType.CharterMessages & "#" & 1) Then
                    site_versions.Add(UserControl & "#" & OwnerType.CharterMessages & "#" & 1, objContentData.m_version(0).ContentID & "." & objContentData.m_version(0).VersionID)
                Else
                    site_versions(UserControl & "#" & OwnerType.CharterMessages & "#" & 1) = objContentData.m_version(0).ContentID & "." & objContentData.m_version(0).VersionID
                End If
                SetSession(BLCommon.ContentManagerKey, Me.ValuesForSetSession)
            End If

            'return the object to UI layer
            If Not objContentData.m_version Is Nothing Then
                versionlist = New List(Of String)
                For Each i As ContentSchema.VERSION In objContentData.m_version
                    versionlist.Add(i.ContentText)
                Next
                Return versionlist
            Else
                Return Nothing
            End If


        Catch ex As Exception
            BLCommon.LogException(ex, ExceptionSeverity.High)
            Throw ex
        Finally
            versionlist = Nothing
            objRoot = Nothing
        End Try
    End Function

    Private Function GetVersionByVersionID(ByVal VersionID As Integer) As String
        'Function level variables which are destroyed in the Finally block
        Dim objRoot As New XmlRootAttribute("CONTENT")
        'Get object to be returned to UI layer
        Dim objContentData As ContentSchema.CONTENT
        'Get serializer for De-serializing content xml to Content object
        Dim objXMLSerializer_Content As XmlSerializer
        Dim context As System.Web.HttpContext = System.Web.HttpContext.Current

        Try
            'Currently test parameters are used, needs to change to refer to actual parameters
            ''If System.Web.HttpContext.Current.Cache("Content" & VersionID) IsNot Nothing Then
            ''  Return CStr(context.Cache("Content" & VersionID))
            ''End If
            Dim strtemp As String = contentProvider.GetContentVersionRecord(VersionID) ' GetContentNormalized("Test2", 0, 0, 12, 0) 'ContentData.GetContentNormalized("WEBCONTROL_00", EntityType, Right(ItemID, Len(ItemID) - 1), Session("FTAID"), Session("LocaleID"))

            'Load the string reader with the XML string returned by Data Access layer
            Dim objReader As New System.IO.StringReader(strtemp)

            'Instantiate XML serializer of the corresponding content object type
            objXMLSerializer_Content = New XmlSerializer(GetType(ContentSchema.CONTENT), objRoot)

            'De-serialize the XML data onto an instance
            objContentData = CType(objXMLSerializer_Content.Deserialize(objReader), ContentSchema.CONTENT)

            'return the object to UI layer
            If Not objContentData.m_version Is Nothing AndAlso objContentData.m_version.Length > 0 Then
                '#############################################################################
                'MODIFICATION   MAR 6, 2009     ROD COPELAND
                '#############################################################################
                'NORMAL OPERATION: DUMP/EXPIRE CACHE AT 3AM TODAY UNLESS IT'S AFTER 3AM, THEN JUST 
                'DUMP/EXPIRE CACHE 3AM TOMORROW MORNING.
                '#############################################################################
                'THE CONTENT CACHE IS ABLE TO BE DUMPED BASED ON AN ADMIN USER MANUALLY 
                'CLICKING THE RESET CACHE BUTTON IN THE ADMIN UI ON THE APP TIER.  THERE IS 
                'ANOTHER NOT SO WE'LL KNOWN FEATURE OF THE SITE THAT DUMPS THE CACHE 
                'AUTOMATICALLY AT 3AM BY SETTING THE CONTENT EXPIRATION DATE WHEN ADDING TO THE 
                'HTTP CACHE COLLECTION. THE EXPIRATION DATE OF THE CONTENT IS INTENDED TO BE A 
                'CONFIGURABLE VALUE TO BE CHANGED AT RUNTIME FOR NEW ENTRIES TO THE CACHE ONLY.  
                'THIS VALUE SHOULD EXIST IN THE ADMIN.WEBCONFIG TABLE WITH A KEY OF 
                '"NUM_OF_DAYS_TO_CACHE" AND "NUM_OF_HOURS_TO_CACHE".  DETAILS OF THE USE OF 
                'THESE VALUES CAN BE FOUND IN THE 
                'commonEntities.Library.dll\virtualManagers\GenericClient.vb\GenerateContentCacheExpirationDate() 
                'method  ANY EXCEPTION IN THE FUNCTION CALL TO GET THE 
                'EXPIRATION DATE WILL SHOULD RESULT IN A RETURNED DATE OF 2 DAYS FROM NOW AT 3AM.
                '#############################################################################
                'Dim lManager As New BLContentManager
                'Dim expiredate As DateTime = lManager.GenerateContentCacheExpirationDate()
                '#############################################################################


                ''If objContentData._version(0).ContentText Is Nothing Then
                ''  context.Cache.Add("Content" & VersionID, "", Nothing, expiredate, System.Web.Caching.Cache.NoSlidingExpiration, Web.Caching.CacheItemPriority.Default, Nothing)
                ''Else
                ''  context.Cache.Add("Content" & VersionID, objContentData._version(0).ContentText, Nothing, expiredate, System.Web.Caching.Cache.NoSlidingExpiration, Web.Caching.CacheItemPriority.Default, Nothing)
                ''End If
                Return objContentData.m_version(0).ContentText
            Else
                Return Nothing
            End If

        Catch ex As Exception
            Throw ex
        Finally
            objRoot = Nothing
        End Try
    End Function

#End Region

#Region "Schedule Maintenance Functions"

    Public Overrides Function GetSchedules(ByVal TrackingId As String, ByVal VersionID As Integer) As List(Of ContentSchema.SCHEDULE)
        'Function level variables which are destroyed in the Finally block
        'Dim objDA As New ContentData
        PopulateLocalVariables(TrackingId)

        Dim objRoot As New XmlRootAttribute("CONTENT")
        Dim schedulelist As New List(Of ContentSchema.SCHEDULE)
        'Get object to be returned to UI layer
        Dim objContentData As ContentSchema.CONTENT
        'Get serializer for De-serializing content xml to Content object
        Dim objXMLSerializer_Content As XmlSerializer

        Try
            'Currently test parameters are used, needs to change to refer to actual parameters

            Dim strtemp As String = contentProvider.GetScheduleRecordsByVersionID(VersionID) ' GetContentNormalized("Test2", 0, 0, 12, 0) 'ContentData.GetContentNormalized("WEBCONTROL_00", EntityType, Right(ItemID, Len(ItemID) - 1), Session("FTAID"), Session("LocaleID"))

            'Load the string reader with the XML string returned by Data Access layer
            Dim objReader As New System.IO.StringReader(strtemp)

            'Instantiate XML serializer of the corresponding content object type
            objXMLSerializer_Content = New XmlSerializer(GetType(ContentSchema.CONTENT), objRoot)

            'De-serialize the XML data onto an instance
            objContentData = CType(objXMLSerializer_Content.Deserialize(objReader), ContentSchema.CONTENT)

            'return the object to UI layer
            'Return objContentData._schedule
            If Not objContentData.m_version Is Nothing Then
                If Not objContentData.m_version(0).m_SCHEDULE Is Nothing Then
                    schedulelist.AddRange(objContentData.m_version(0).m_SCHEDULE)
                    Return schedulelist
                Else
                    Return Nothing
                End If
            Else
                Return Nothing
            End If

        Catch ex As Exception
            BLCommon.LogException(ex, ExceptionSeverity.High)
            Throw ex
        Finally
            schedulelist = Nothing
            objRoot = Nothing
        End Try
    End Function

    Public Overrides Sub UpdateSchedule(ByVal TrackingId As String, ByVal ScheduleID As Integer, ByVal Startdate As Date, ByVal EndDate As Date)
        Try
            PopulateLocalVariables(TrackingId)
            contentProvider.UpdateSchedule(ScheduleID, Startdate, EndDate)
        Catch ex As Exception
            BLCommon.LogException(ex, ExceptionSeverity.High)
            Throw ex
        Finally
            'objDA = Nothing
        End Try
    End Sub

    Public Overrides Sub AddSchedule(ByVal TrackingId As String, ByVal VersionID As Integer, ByVal Startdate As Date, ByVal EndDate As Date)
        'Dim objDA As New ContentData
        Try
            PopulateLocalVariables(TrackingId)
            contentProvider.AddSchedule(VersionID, Startdate, EndDate, 1, Now())
        Catch ex As Exception
            BLCommon.LogException(ex, ExceptionSeverity.High)
            Throw ex
        Finally
            'objDA = Nothing
        End Try
    End Sub

    Public Overrides Sub DeleteSchedule(ByVal TrackingId As String, ByVal ScheduleID As Integer)
        'Dim objDA As New ContentData
        Try
            PopulateLocalVariables(TrackingId)
            contentProvider.DeleteSchedule(ScheduleID)
        Catch ex As Exception
            BLCommon.LogException(ex, ExceptionSeverity.High)
            Throw ex
        Finally
        End Try
    End Sub

#End Region

#Region "Version Image Maintenance Functions"

    Public Overrides Function GetImageForVersion(ByVal TrackingId As String, ByVal VersionID As Integer) As ContentSchema.IMAGE()
        'Function level variables which are destroyed in the Finally block
        'Dim objDA As New ContentData
        PopulateLocalVariables(TrackingId)

        Dim objRoot As New XmlRootAttribute("CONTENT")

        'Get object to be returned to UI layer
        Dim objContentData As ContentSchema.CONTENT
        'Get serializer for De-serializing content xml to Content object
        Dim objXMLSerializer_Content As XmlSerializer

        Try
            'Currently test parameters are used, needs to change to refer to actual parameters

            Dim strtemp As String '= contentProvider.GetImageRecordsByVersionID(VersionID) ' GetContentNormalized("Test2", 0, 0, 12, 0) 'ContentData.GetContentNormalized("WEBCONTROL_00", EntityType, Right(ItemID, Len(ItemID) - 1), Session("FTAID"), Session("LocaleID"))

            'Load the string reader with the XML string returned by Data Access layer
            Dim objReader As New System.IO.StringReader(strtemp)

            'Instantiate XML serializer of the corresponding content object type
            objXMLSerializer_Content = New XmlSerializer(GetType(ContentSchema.CONTENT), objRoot)

            'De-serialize the XML data onto an instance
            objContentData = CType(objXMLSerializer_Content.Deserialize(objReader), ContentSchema.CONTENT)

            'return the object to UI layer
            If Not objContentData.m_version Is Nothing Then
                If Not objContentData.m_version(0).m_Image Is Nothing Then
                    Return objContentData.m_version(0).m_Image
                Else
                    Return Nothing
                End If
            Else
                Return Nothing
            End If


        Catch ex As Exception
            Throw ex
        Finally
            'objDA = Nothing
            objRoot = Nothing
        End Try
    End Function

    Public Overrides Sub UpdateVersionImage(ByVal TrackingId As String, ByVal objVersion As ContentSchema.IMAGE)
        'Dim objDA As New ContentData
        Try
            PopulateLocalVariables(TrackingId)
            contentProvider.UpdateImage(objVersion.ImageID, 1, Now(), Nothing, Nothing, objVersion.Caption, 1, Now(), 1, Now())
            'ContentData.UpdateImage( (objVersion._VersionID, objVersion._ContentText)
        Catch ex As Exception
            Throw ex
        Finally
            'objDA = Nothing
        End Try
    End Sub

    Public Overrides Sub DeleteVersionImage(ByVal TrackingId As String, ByVal ImageID As Integer, ByVal versionID As Integer)
        'Dim objDA As New ContentData
        Try
            PopulateLocalVariables(TrackingId)
            contentProvider.DeleteImageFromContent(ImageID, versionID)
            'ContentData.DeleteVersion(versionID)
        Catch ex As Exception
            Throw ex
        Finally
            'objDA = Nothing
        End Try
    End Sub

    Public Overrides Sub AddVersionImage(ByVal TrackingId As String, ByVal objImage As ContentSchema.IMAGE)
        'Dim objDA As New ContentData
        Try
            PopulateLocalVariables(TrackingId)
            contentProvider.AddImageToContent(objImage.ImageID, objImage.VersionID)
            'ContentData.AddVersion(objVersion._ContentID, 1, Now(), objVersion._ContentText)
        Catch ex As Exception
            Throw ex
        Finally
            'objDA = Nothing
        End Try
    End Sub

#End Region

#Region "Image Maintenance Functions"

    Public Overrides Function GetImages(ByVal TrackingId As String) As List(Of String)
        'Function level variables which are destroyed in the Finally block
        'Dim objDA As New ContentData
        PopulateLocalVariables(TrackingId)
        Dim objRoot As New XmlRootAttribute("CONTENT")
        Dim imagelist As New List(Of String)
        'Get object to be returned to UI layer
        Dim objContentData As ContentSchema.CONTENT
        'Get serializer for De-serializing content xml to Content object
        Dim objXMLSerializer_Content As XmlSerializer

        Try
            'Currently test parameters are used, needs to change to refer to actual parameters

            Dim strtemp As String = contentProvider.GetAllImageIDList() ' GetContentNormalized("Test2", 0, 0, 12, 0) 'ContentData.GetContentNormalized("WEBCONTROL_00", EntityType, Right(ItemID, Len(ItemID) - 1), Session("FTAID"), Session("LocaleID"))

            'Load the string reader with the XML string returned by Data Access layer
            Dim objReader As New System.IO.StringReader(strtemp)

            'Instantiate XML serializer of the corresponding content object type
            objXMLSerializer_Content = New XmlSerializer(GetType(ContentSchema.CONTENT), objRoot)

            'De-serialize the XML data onto an instance
            objContentData = CType(objXMLSerializer_Content.Deserialize(objReader), ContentSchema.CONTENT)

            'return the object to UI layer
            If Not objContentData.m_Image Is Nothing Then
                For Each i As ContentSchema.IMAGE In objContentData.m_Image
                    imagelist.Add(i.ImageName)
                Next
                Return imagelist
            End If
            Return Nothing
        Catch ex As Exception
            BLCommon.LogException(ex, ExceptionSeverity.High)
            Throw ex
        Finally
            imagelist = Nothing
            objRoot = Nothing
        End Try
    End Function

    Public Overrides Function AddImage(ByVal TrackingId As String, ByVal Name As String, ByVal ImageData() As Byte) As Boolean

        Try
            PopulateLocalVariables(TrackingId)
            contentProvider.AddImage(1, Now(), ImageData, Nothing, Name, Name)
        Catch ex As Exception
            BLCommon.LogException(ex, ExceptionSeverity.High)
            Throw ex
        End Try
    End Function

    Public Overrides Function GetImage(ByVal TrackingId As String, ByVal ImageName As String) As ContentSchema.IMAGE

        'PopulateLocalVariables(TrackingId)
        'Function level variables which are destroyed in the Finally block
        Dim objRoot As New XmlRootAttribute("CONTENT")

        'Get object to be returned to UI layer
        Dim objContentData As ContentSchema.CONTENT
        'Get serializer for De-serializing content xml to Content object
        Dim objXMLSerializer_Content As XmlSerializer

        Try
            'Currently test parameters are used, needs to change to refer to actual parameters
            contentProvider = CType(Activator.GetObject(GetType(IContentProvider), CStr(HttpContext.Current.Application("RemoteURL_ContentProvider"))), IContentProvider)
            Dim strtemp As String = contentProvider.GetImageDataRecordByImageName(ImageName) ' GetContentNormalized("Test2", 0, 0, 12, 0) 'ContentData.GetContentNormalized("WEBCONTROL_00", EntityType, Right(ItemID, Len(ItemID) - 1), Session("FTAID"), Session("LocaleID"))

            'Load the string reader with the XML string returned by Data Access layer
            Dim objReader As New System.IO.StringReader(strtemp)

            'Instantiate XML serializer of the corresponding content object type
            objXMLSerializer_Content = New XmlSerializer(GetType(ContentSchema.CONTENT), objRoot)

            'De-serialize the XML data onto an instance
            objContentData = CType(objXMLSerializer_Content.Deserialize(objReader), ContentSchema.CONTENT)
            If Not objContentData.m_Image Is Nothing Then
                'return the object to UI layer
                Return objContentData.m_Image(0)
            Else
                Return Nothing
            End If
        Catch ex As Exception
            BLCommon.LogException(ex, ExceptionSeverity.High)
            Throw ex
        Finally
            contentProvider = Nothing
            objRoot = Nothing
        End Try
    End Function

    Public Overrides Sub DeleteImage(ByVal trackingID As String, ByVal Name As String)

        Try

        Catch ex As Exception
            BLCommon.LogException(ex, ExceptionSeverity.High)
            Throw ex
        End Try
    End Sub

#End Region

#Region "Binary File Maintenance Functions"

    Public Overrides Function GetBinaryFile(ByVal TrackingId As String, ByVal BinaryFileName As String, ByVal BinaryFileExtension As String) As ContentSchema.BINARYFILE

        'PopulateLocalVariables(TrackingId)
        'Function level variables which are destroyed in the Finally block
        Dim objRoot As New XmlRootAttribute("CONTENT")

        'Get object to be returned to UI layer
        Dim objContentData As ContentSchema.CONTENT
        'Get serializer for De-serializing content xml to Content object
        Dim objXMLSerializer_Content As XmlSerializer

        Try
            'Currently test parameters are used, needs to change to refer to actual parameters
            contentProvider = CType(Activator.GetObject(GetType(IContentProvider), CStr(HttpContext.Current.Application("RemoteURL_ContentProvider"))), IContentProvider)
            Dim strtemp As String = contentProvider.GetBinaryFileByBinaryFileName(BinaryFileName, BinaryFileExtension) ' GetContentNormalized("Test2", 0, 0, 12, 0) 'ContentData.GetContentNormalized("WEBCONTROL_00", EntityType, Right(ItemID, Len(ItemID) - 1), Session("FTAID"), Session("LocaleID"))

            'Load the string reader with the XML string returned by Data Access layer
            Dim objReader As New System.IO.StringReader(strtemp)

            'Instantiate XML serializer of the corresponding content object type
            objXMLSerializer_Content = New XmlSerializer(GetType(ContentSchema.CONTENT), objRoot)

            'De-serialize the XML data onto an instance
            objContentData = CType(objXMLSerializer_Content.Deserialize(objReader), ContentSchema.CONTENT)
            If Not objContentData.m_BinaryFile Is Nothing Then
                'return the object to UI layer
                Return objContentData.m_BinaryFile(0)
            Else
                Return Nothing
            End If
        Catch ex As Exception
            BLCommon.LogException(ex, ExceptionSeverity.High)
            Throw ex
        Finally
            contentProvider = Nothing
            objRoot = Nothing
        End Try
    End Function

#End Region

#Region "Product catalog Content display Functions"

    Public Overrides Function GetContent(ByVal trackingID As String, ByVal UserControl As String, ByVal OwnerID As String, ByVal Ownertype As String) As String

        PopulateLocalVariables(trackingID)
        ' Function level variables which are destroyed in the Finally block
        Dim objRoot As New XmlRootAttribute("CONTENT")
        'Get object to be returned to UI layer
        Dim lOwnerType As OwnerType
        Try
            Dim intLocaleID As Integer
            Dim intRegionID As Integer
            Dim intKMAId As Integer
            Dim intBillingStationID As Integer
            Dim strFTAID As String
            Dim strTemp As String
            Dim objAccountmanager As BLAccountManager
            Dim objSession As Object
            Dim lContentID As Integer
            Dim lVersionID As Integer
            Dim arrContent() As String
            Dim arrKeyOwner() As String
            Dim arrKeyOwnerType() As String

            Dim charContentSeparator() As Char = {"*"c, "@"c, "*"c}

            objSession = Session(BLCommon.AccountManagerKey)
            If objSession Is Nothing Then
                Dim VarList As New Dictionary(Of String, Object)
                VarList.Add("account", New BLAccount())
                VarList.Add("gwyShoppingCart", Nothing)
                VarList.Add("LocaleID", 1)
                VarList.Add("CookieID", Nothing)
                VarList.Add("addresses", Nothing)
                VarList.Add("serviceabilityResults", Nothing)
                VarList.Add("entityid", Nothing)
                VarList.Add("houseref", Nothing)
                VarList.Add("_sysPrin", Nothing)
                objAccountmanager = New BLAccountManager(VarList)
                SetSession(BLCommon.AccountManagerKey, VarList)
            Else
                objAccountmanager = New BLAccountManager(CType(objSession, Dictionary(Of String, Object)))
            End If
            Dim strTempIDs As String

            intLocaleID = 1
            intRegionID = objAccountmanager.account.RegionID
            intKMAId = objAccountmanager.account.KMAID
            intBillingStationID = objAccountmanager.account.BillingCode
            strFTAID = objAccountmanager.account.FTA
            If intLocaleID = 0 Then
                BLCommon.LogException(New Exception("Content Manager : Invalid LocaleID of Zero or Locale ID not set"), ExceptionSeverity.High)
                Return Nothing
            End If


            '##########################################################################
            'ROD  COPELAND  MODIFICATION    FEB 4, 2009 CREATIVE LIFT REDESIGN
            '##########################################################################
            'AT THE TIME THIS WAS WRITTEN, A SIMULAR BLOCK OF CODE EXISTS IN THE CONTROLS/CUSTOMCONTENT.ASCX.VB/Page_PreRender()
            'ENTERPRISE SECURITY HAS INDICATED THAT WE SHOULD NOT ALLOW PEOPLE TO PASS 
            'UNVALIDATED INPUT INTO THE DB CALLS FOR CONTENT RECORDS, THIS CHANGE STRIPS 
            'VALUES THAT CURRENT DONT MAKE SENSE FOR VALID CONTENT RECORD NAMES.  NORMAL NAMES 
            'AT THIS POINT ARE LIKE    
            'HOME:PROMO:CABLE:OFFER    OR CABLESUPPORTPAGERECORD
            '##########################################################################
            If Not (UserControl Is Nothing) Then
                UserControl = UserControl.Replace("~", "")
                UserControl = UserControl.Replace("`", "")
                UserControl = UserControl.Replace("!", "")
                'UserControl = UserControl.Replace("@", "")
                'UserControl = UserControl.Replace("#", "")
                UserControl = UserControl.Replace("$", "")
                UserControl = UserControl.Replace("%", "")

                UserControl = UserControl.Replace("^", "")
                UserControl = UserControl.Replace("&", "")
                'UserControl = UserControl.Replace("*", "")
                UserControl = UserControl.Replace("(", "")
                UserControl = UserControl.Replace(")", "")
                UserControl = UserControl.Replace("--", "")
                UserControl = UserControl.Replace("__", "")

                UserControl = UserControl.Replace("+", "")
                UserControl = UserControl.Replace("=", "")
                UserControl = UserControl.Replace("<", "")
                UserControl = UserControl.Replace(">", "")
                UserControl = UserControl.Replace(";", "")
                UserControl = UserControl.Replace("""", "")
                UserControl = UserControl.Replace("'", "")
                UserControl = UserControl.Replace(",", "")
                UserControl = UserControl.Replace(".", "")

                UserControl = UserControl.Replace("?", "")
                UserControl = UserControl.Replace("/", "")
                UserControl = UserControl.Replace("{", "")
                UserControl = UserControl.Replace("[", "")

                UserControl = UserControl.Replace("}", "")
                UserControl = UserControl.Replace("]", "")
                UserControl = UserControl.Replace("|", "")
                UserControl = UserControl.Replace("\", "")
            End If
            '##########################################################################




            If InStr(UserControl, "*@*") > 0 Then
                arrContent = UserControl.Split(charContentSeparator, StringSplitOptions.RemoveEmptyEntries)
                For intLength As Integer = 0 To arrContent.Length - 1
                    arrKeyOwner = OwnerID.Split(charContentSeparator, StringSplitOptions.RemoveEmptyEntries)
                    arrKeyOwnerType = Ownertype.Split(charContentSeparator, StringSplitOptions.RemoveEmptyEntries)

                    OwnerID = arrKeyOwner(intLength)
                    Ownertype = arrKeyOwnerType(intLength)
                    UserControl = arrContent(intLength)
                    lOwnerType = BLCommon.ConvertToOwnerType(Ownertype)

                    If Not objAccountmanager.account.UserType = UserType.Author Then
                        Select Case lOwnerType
                            Case CharterDotCom.CommonEntities.Library.Enumerations.OwnerType.Bundle, CharterDotCom.CommonEntities.Library.Enumerations.OwnerType.Feature, CharterDotCom.CommonEntities.Library.Enumerations.OwnerType.ServicePlan
                                If Len(CStr(OwnerID)) > 1 Then
                                    OwnerID = Right(OwnerID, Len(OwnerID) - 1)
                                End If
                        End Select
                    Else
                        Select Case lOwnerType
                            Case CharterDotCom.CommonEntities.Library.Enumerations.OwnerType.Bundle, CharterDotCom.CommonEntities.Library.Enumerations.OwnerType.Feature, CharterDotCom.CommonEntities.Library.Enumerations.OwnerType.ServicePlan
                                If Len(CStr(OwnerID)) > 1 Then
                                    OwnerID = Right(OwnerID, Len(OwnerID) - 1)
                                End If
                        End Select

                        Select Case lOwnerType
                            Case CharterDotCom.CommonEntities.Library.Enumerations.OwnerType.Bundle, CharterDotCom.CommonEntities.Library.Enumerations.OwnerType.ServicePlan
                                Dim objOwnerList As OWNERLIST
                                objOwnerList = GetEPCIDList(OwnerID)
                                If Not objOwnerList.m_OWNERDATA Is Nothing Then
                                    For Each item As OWNERDATA In objOwnerList.m_OWNERDATA
                                        OwnerID = item.ID
                                        Exit For
                                    Next
                                End If
                        End Select
                    End If

                    If Not site_versions Is Nothing Then
                        If site_versions.TryGetValue(UserControl & "#" & Ownertype & "#" & OwnerID, strTempIDs) Then
                            Dim arrTemp() As String
                            Dim charSeparator(1) As Char
                            charSeparator(0) = CChar(".")
                            arrTemp = strTempIDs.Split(charSeparator, StringSplitOptions.RemoveEmptyEntries)
                            strTemp = GetVersionByVersionID(CInt(arrTemp(1)))
                            If strTemp Is Nothing Then
                                strTemp = " *@*"
                            Else
                                strTemp = strTemp & "*@*"
                            End If
                        End If
                    End If

                    'To check if only Billing code and FTA are available, then display content at the corresponding level
                    If intRegionID = 0 And intKMAId = 0 And intBillingStationID <> 0 And strFTAID <> String.Empty Then
                        strTemp = contentProvider.GetContentNormalized(UserControl, intLocaleID, OwnerID, lOwnerType, Nothing, Nothing, intBillingStationID, strFTAID, lContentID, lVersionID)
                    Else
                        'To check if none of the Organization details are available, then display National level content.
                        If intRegionID = 0 And intKMAId = 0 And intBillingStationID = 0 And strFTAID = String.Empty Then
                            strTemp = contentProvider.GetContentNormalized(UserControl, intLocaleID, OwnerID, lOwnerType, Nothing, Nothing, Nothing, Nothing, lContentID, lVersionID)
                        Else
                            'To check if localized until region, then display the Region level content
                            If intRegionID <> 0 And intKMAId = 0 And intBillingStationID = 0 And strFTAID = String.Empty Then
                                strTemp = contentProvider.GetContentNormalized(UserControl, intLocaleID, OwnerID, lOwnerType, intRegionID, Nothing, Nothing, Nothing, lContentID, lVersionID)
                            End If
                            'To check if localized until KMA, then display the KMA level content
                            If intRegionID <> 0 And intKMAId <> 0 And intBillingStationID = 0 And strFTAID = String.Empty Then
                                strTemp = contentProvider.GetContentNormalized(UserControl, intLocaleID, OwnerID, lOwnerType, intRegionID, intKMAId, Nothing, Nothing, lContentID, lVersionID)
                            End If
                            'To check if localized until Billing station, then display the Billing station level content
                            If intRegionID <> 0 And intKMAId <> 0 And intBillingStationID <> 0 And strFTAID = String.Empty Then
                                strTemp = contentProvider.GetContentNormalized(UserControl, intLocaleID, OwnerID, lOwnerType, intRegionID, intKMAId, intBillingStationID, Nothing, lContentID, lVersionID)
                            End If
                            'To check if localized until FTA, then display the FTA level content
                            If intRegionID <> 0 And intKMAId <> 0 And intBillingStationID <> 0 And strFTAID <> String.Empty Then
                                strTemp = contentProvider.GetContentNormalized(UserControl, intLocaleID, OwnerID, lOwnerType, intRegionID, intKMAId, intBillingStationID, strFTAID, lContentID, lVersionID)
                            End If

                        End If
                    End If

                    ' return the object to UI layer
                    If Not strTemp Is Nothing Then
                        If site_versions Is Nothing Then site_versions = New Dictionary(Of String, String)
                        If Not site_versions.ContainsKey(UserControl & "#" & Ownertype & "#" & OwnerID) Then
                            site_versions.Add(UserControl & "#" & Ownertype & "#" & OwnerID, lContentID & "." & lVersionID)
                        Else
                            site_versions(UserControl & "#" & Ownertype & "#" & OwnerID) = lContentID & "." & lVersionID
                        End If
                        SetSession(BLCommon.ContentManagerKey, Me.ValuesForSetSession)
                        strTemp = strTemp & "*@*"
                    Else
                        strTemp = " *@*"
                    End If
                Next


            Else
                lOwnerType = BLCommon.ConvertToOwnerType(Ownertype)

                If Not objAccountmanager.account.UserType = UserType.Author Then
                    Select Case lOwnerType
                        Case CharterDotCom.CommonEntities.Library.Enumerations.OwnerType.Bundle, CharterDotCom.CommonEntities.Library.Enumerations.OwnerType.Feature, CharterDotCom.CommonEntities.Library.Enumerations.OwnerType.ServicePlan
                            If Len(CStr(OwnerID)) > 1 Then
                                OwnerID = Right(OwnerID, Len(OwnerID) - 1)
                            End If
                    End Select
                Else
                    Select Case lOwnerType
                        Case CharterDotCom.CommonEntities.Library.Enumerations.OwnerType.Bundle, CharterDotCom.CommonEntities.Library.Enumerations.OwnerType.Feature, CharterDotCom.CommonEntities.Library.Enumerations.OwnerType.ServicePlan
                            If Len(CStr(OwnerID)) > 1 Then
                                OwnerID = Right(OwnerID, Len(OwnerID) - 1)
                            End If
                    End Select

                    Select Case lOwnerType
                        Case CharterDotCom.CommonEntities.Library.Enumerations.OwnerType.Bundle, CharterDotCom.CommonEntities.Library.Enumerations.OwnerType.ServicePlan
                            Dim objOwnerList As OWNERLIST
                            objOwnerList = GetEPCIDList(OwnerID)
                            If Not objOwnerList.m_OWNERDATA Is Nothing Then
                                For Each item As OWNERDATA In objOwnerList.m_OWNERDATA
                                    OwnerID = item.ID
                                    Exit For
                                Next
                            End If
                    End Select
                End If

                If Not site_versions Is Nothing Then
                    If site_versions.TryGetValue(UserControl & "#" & Ownertype & "#" & OwnerID, strTempIDs) Then
                        Dim arrTemp() As String
                        Dim charSeparator(1) As Char
                        charSeparator(0) = CChar(".")
                        arrTemp = strTempIDs.Split(charSeparator, StringSplitOptions.RemoveEmptyEntries)
                        SetSession(BLCommon.ContentManagerKey, Me.ValuesForSetSession)
                        Return (GetVersionByVersionID(CInt(arrTemp(1))))
                    End If
                End If

                'To check if only Billing code and FTA are available, then display content at the corresponding level
                If intRegionID = 0 And intKMAId = 0 And intBillingStationID <> 0 And strFTAID <> String.Empty Then
                    strTemp = contentProvider.GetContentNormalized(UserControl, intLocaleID, OwnerID, lOwnerType, Nothing, Nothing, intBillingStationID, strFTAID, lContentID, lVersionID)
                Else
                    'To check if none of the Organization details are available, then display National level content.
                    If intRegionID = 0 And intKMAId = 0 And intBillingStationID = 0 And strFTAID = String.Empty Then
                        strTemp = contentProvider.GetContentNormalized(UserControl, intLocaleID, OwnerID, lOwnerType, Nothing, Nothing, Nothing, Nothing, lContentID, lVersionID)
                    Else
                        'To check if localized until region, then display the Region level content
                        If intRegionID <> 0 And intKMAId = 0 And intBillingStationID = 0 And strFTAID = String.Empty Then
                            strTemp = contentProvider.GetContentNormalized(UserControl, intLocaleID, OwnerID, lOwnerType, intRegionID, Nothing, Nothing, Nothing, lContentID, lVersionID)
                        End If
                        'To check if localized until KMA, then display the KMA level content
                        If intRegionID <> 0 And intKMAId <> 0 And intBillingStationID = 0 And strFTAID = String.Empty Then
                            strTemp = contentProvider.GetContentNormalized(UserControl, intLocaleID, OwnerID, lOwnerType, intRegionID, intKMAId, Nothing, Nothing, lContentID, lVersionID)
                        End If
                        'To check if localized until Billing station, then display the Billing station level content
                        If intRegionID <> 0 And intKMAId <> 0 And intBillingStationID <> 0 And strFTAID = String.Empty Then
                            strTemp = contentProvider.GetContentNormalized(UserControl, intLocaleID, OwnerID, lOwnerType, intRegionID, intKMAId, intBillingStationID, Nothing, lContentID, lVersionID)
                        End If
                        'To check if localized until FTA, then display the FTA level content
                        If intRegionID <> 0 And intKMAId <> 0 And intBillingStationID <> 0 And strFTAID <> String.Empty Then
                            strTemp = contentProvider.GetContentNormalized(UserControl, intLocaleID, OwnerID, lOwnerType, intRegionID, intKMAId, intBillingStationID, strFTAID, lContentID, lVersionID)
                        End If

                    End If
                End If

                ' return the object to UI layer
                If Not strTemp Is Nothing Then
                    If site_versions Is Nothing Then site_versions = New Dictionary(Of String, String)
                    If Not site_versions.ContainsKey(UserControl & "#" & Ownertype & "#" & OwnerID) Then
                        site_versions.Add(UserControl & "#" & Ownertype & "#" & OwnerID, lContentID & "." & lVersionID)
                    Else
                        site_versions(UserControl & "#" & Ownertype & "#" & OwnerID) = lContentID & "." & lVersionID
                    End If
                    SetSession(BLCommon.ContentManagerKey, Me.ValuesForSetSession)
                    Return strTemp
                Else
                    Return Nothing
                End If
            End If

            SetSession(BLCommon.ContentManagerKey, Me.ValuesForSetSession)

        Catch ex As Exception
            BLCommon.LogException(ex, ExceptionSeverity.High)
            Throw ex
        Finally
            objRoot = Nothing
        End Try

    End Function


    Public Overrides Function GetContentByTemplateID(ByVal TrackingID As String, ByVal TemplateID As Integer) As DataTable
        Dim dt As DataTable
        Try
            PopulateLocalVariables(TrackingID)
            Dim objSession As Object
            Dim objAccountmanager As BLAccountManager
            objSession = Session(BLCommon.AccountManagerKey)
            If objSession Is Nothing Then
                Dim VarList As New Dictionary(Of String, Object)
                VarList.Add("account", New BLAccount())
                VarList.Add("gwyShoppingCart", Nothing)
                VarList.Add("LocaleID", 1)
                VarList.Add("CookieID", Nothing)
                VarList.Add("addresses", Nothing)
                VarList.Add("serviceabilityResults", Nothing)
                VarList.Add("entityid", Nothing)
                VarList.Add("houseref", Nothing)
                VarList.Add("_sysPrin", Nothing)
                objAccountmanager = New BLAccountManager(VarList)
                SetSession(BLCommon.AccountManagerKey, VarList)
            Else
                objAccountmanager = New BLAccountManager(CType(objSession, Dictionary(Of String, Object)))
            End If
            dt = contentProvider.GetContentByTemplateID(TemplateID, 0, 0, objAccountmanager.account.BillingCode, objAccountmanager.account.FTA)
            If Not dt Is Nothing Then
                Dim keys(1) As DataColumn
                keys(0) = dt.Columns(0)
                dt.PrimaryKey = keys
                Return dt
            End If
            Return Nothing
        Catch ex As Exception
            BLCommon.LogException(ex, ExceptionSeverity.High)
            Throw ex
        Finally
        End Try
    End Function

#End Region

#Region "Organization structure Functions"
    'This sub-routine is called on changing Organization level 
    Public Overrides Sub SetOrganization(ByVal TrackingId As String, ByVal OrganizationID As String)
        Try
            PopulateLocalVariables(TrackingId)

            Dim objAccountmanager As New BLAccountManager(CType(Session(BLCommon.AccountManagerKey), Dictionary(Of String, Object)))

            Dim strTemp() As String
            Dim charSeparator(1) As Char
            charSeparator(0) = CChar(".")
            strTemp = OrganizationID.Split(charSeparator, StringSplitOptions.RemoveEmptyEntries)
            With objAccountmanager.account
                Select Case UBound(strTemp)
                    Case 0
                        If strTemp(0) = "0" Then
                            .RegionID = 0
                            .KMAID = 0
                            .BillingCode = 0
                            .FTA = String.Empty
                        Else
                            .RegionID = CInt(strTemp(0))
                            .KMAID = 0
                            .BillingCode = 0
                            .FTA = String.Empty
                        End If
                    Case 1
                        .RegionID = CInt(strTemp(0))
                        .KMAID = CInt(strTemp(1))
                        .BillingCode = 0
                        .FTA = String.Empty
                    Case 2
                        .RegionID = CInt(strTemp(0))
                        .KMAID = CInt(strTemp(1))
                        .BillingCode = CInt(strTemp(2))
                        .FTA = String.Empty
                    Case 3
                        .RegionID = CInt(strTemp(0))
                        .KMAID = CInt(strTemp(1))
                        .BillingCode = CInt(strTemp(2))
                        .FTA = strTemp(3)
                End Select
            End With
            SetSession(BLCommon.AccountManagerKey, objAccountmanager.ValuesForSetSession)
        Catch ex As Exception
            BLCommon.LogException(ex, ExceptionSeverity.High)
            Throw ex
        End Try
    End Sub

    Public Overrides Function GetOrganization(ByVal TrackingId As String) As String
        Try
            PopulateLocalVariables(TrackingId)
            Dim strTemp As String = contentProvider.GetOrgStructure()
            If strTemp = "" Then
                BLCommon.LogException(New Exception("Content Manager : Organization structure data not found"), ExceptionSeverity.High)
                Return Nothing
            Else
                Return strTemp
            End If
        Catch ex As Exception
            BLCommon.LogException(ex, ExceptionSeverity.High)
            Throw ex
        End Try

    End Function

    Public Overrides Function GetOwnerTypes(ByVal TrackingId As String) As Dictionary(Of Integer, String)
        'Function level variables which are destroyed in the Finally block
        'Dim objDA As New ContentData
        PopulateLocalVariables(TrackingId)
        Dim objRoot As New XmlRootAttribute("OWNERLIST")
        Dim ownerlist As Dictionary(Of Integer, String)
        'Get object to be returned to UI layer
        Dim objOwnerList As OWNERLIST
        'Get serializer for De-serializing content xml to Content object
        Dim objXMLSerializer_Content As XmlSerializer
        Dim objSession As Object

        Try

            ''TO DO:THIS block of code needs to be moved to content admin sign-in page
            ''SET USER TYPE TO CONTENT AUTHOR
            Dim lAccountmanager As BLAccountManager
            objSession = Session(BLCommon.AccountManagerKey)
            If objSession Is Nothing Then
                Dim VarList As New Dictionary(Of String, Object)
                VarList.Add("account", New BLAccount())
                VarList.Add("gwyShoppingCart", Nothing)
                VarList.Add("LocaleID", 1)
                VarList.Add("CookieID", Nothing)
                VarList.Add("addresses", Nothing)
                VarList.Add("serviceabilityResults", Nothing)
                VarList.Add("entityid", Nothing)
                VarList.Add("houseref", Nothing)
                VarList.Add("_sysPrin", Nothing)
                lAccountmanager = New BLAccountManager(VarList)
            Else
                lAccountmanager = New BLAccountManager(CType(objSession, Dictionary(Of String, Object)))
            End If

            lAccountmanager.account.UserType = UserType.Author

            SetSession(BLCommon.AccountManagerKey, lAccountmanager.ValuesForSetSession)
            Dim strtemp As String = contentProvider.GetOwnerTypeList

            'Load the string reader with the XML string returned by Data Access layer
            Dim objReader As New System.IO.StringReader(strtemp)

            'Instantiate XML serializer of the corresponding content object type
            objXMLSerializer_Content = New XmlSerializer(GetType(OWNERLIST), objRoot)

            'De-serialize the XML data onto an instance
            objOwnerList = CType(objXMLSerializer_Content.Deserialize(objReader), OWNERLIST)

            'return the object to UI layer
            If Not objOwnerList.m_OWNERDATA Is Nothing Then
                ownerlist = New Dictionary(Of Integer, String)
                For Each item As OWNERDATA In objOwnerList.m_OWNERDATA
                    ownerlist.Add(CInt(item.ID), item.DESCRIPTION)
                Next
                Return ownerlist
            Else
                Return Nothing
            End If

        Catch ex As Exception
            BLCommon.LogException(ex, ExceptionSeverity.High)
            Throw ex
        Finally
            ownerlist = Nothing
            objRoot = Nothing
        End Try
    End Function

    Public Overrides Function GetOwners(ByVal TrackingId As String, ByVal OwnerTypeID As Integer) As Dictionary(Of Integer, String)
        'Function level variables which are destroyed in the Finally block
        'Dim objDA As New ContentData
        PopulateLocalVariables(TrackingId)
        Dim objRoot As New XmlRootAttribute("OWNERLIST")
        Dim ownerlist As Dictionary(Of Integer, String)
        'Get object to be returned to UI layer
        Dim objOwnerList As OWNERLIST
        'Get serializer for De-serializing content xml to Content object
        Dim objXMLSerializer_Content As XmlSerializer

        Try
            'Currently test parameters are used, needs to change to refer to actual parameters

            Dim strtemp As String = contentProvider.GetOwnerIDList(OwnerTypeID)

            'Load the string reader with the XML string returned by Data Access layer
            Dim objReader As New System.IO.StringReader(strtemp)

            'Instantiate XML serializer of the corresponding content object type
            objXMLSerializer_Content = New XmlSerializer(GetType(OWNERLIST), objRoot)

            'De-serialize the XML data onto an instance
            objOwnerList = CType(objXMLSerializer_Content.Deserialize(objReader), OWNERLIST)

            'return the object to UI layer
            If Not objOwnerList.m_OWNERDATA Is Nothing Then
                ownerlist = New Dictionary(Of Integer, String)
                For Each item As OWNERDATA In objOwnerList.m_OWNERDATA
                    ownerlist.Add(CInt(item.ID), item.DESCRIPTION)
                Next
                Return ownerlist
            Else
                Return Nothing
            End If

        Catch ex As Exception
            BLCommon.LogException(ex, ExceptionSeverity.High)
            Throw ex
        Finally
            ownerlist = Nothing
            objRoot = Nothing
        End Try
    End Function

    Private Function GetEPCIDList(ByVal PlayBookID As String) As OWNERLIST
        'Function level variables which are destroyed in the Finally block
        'Dim objDA As New ContentData
        Dim objRoot As New XmlRootAttribute("OWNERLIST")
        Dim ownerlist As Dictionary(Of Integer, String)
        'Get object to be returned to UI layer
        Dim objOwnerList As OWNERLIST
        'Get serializer for De-serializing content xml to Content object
        Dim objXMLSerializer_Content As XmlSerializer

        Try
            Dim strtemp As String
            'Currently test parameters are used, needs to change to refer to actual parameters

            strtemp = contentProvider.GetEPCIDList(PlayBookID)

            'Load the string reader with the XML string returned by Data Access layer
            Dim objReader As New System.IO.StringReader(strtemp)

            'Instantiate XML serializer of the corresponding content object type
            objXMLSerializer_Content = New XmlSerializer(GetType(OWNERLIST), objRoot)

            'De-serialize the XML data onto an instance
            objOwnerList = CType(objXMLSerializer_Content.Deserialize(objReader), OWNERLIST)

            'return the object to UI layer
            Return objOwnerList

        Catch ex As Exception
            Throw ex
        Finally
            ownerlist = Nothing
            objRoot = Nothing
        End Try
    End Function
#End Region

End Class
