Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports System.Xml
Imports System.Data
Imports System.Collections.Generic
' issue 28289
Partial Class Configurations_SiteMessage
    Inherits AdminPage

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Dim roleName As String = "MessageReset"
        If Not (Me.IsPostBack) Then
            If Me.IsInRole(roleName) Then
                GetCurrentState()
            Else
                lblDisplay.Text = "You do not have permission to view this page.  You must have the " + _
                    roleName + " role to use this page."
                lblStatus.Text = ""
                btnChange.Visible = False
                Return
            End If
        End If
    End Sub

    Protected Sub btnChange_Click(sender As Object, e As System.EventArgs) Handles btnChange.Click
        Dim lConfigProvider As New Service
        Dim value As String = lConfigProvider.GetSiteMessageSetting()

        If (String.Compare(value, "F", True) = 0) Then
            lConfigProvider.SetSiteMessageSetting("T")
        Else
            lConfigProvider.SetSiteMessageSetting("F")
        End If
        ' Get updated values to display and reset site settings!
        GetCurrentState()
        ResetSiteSettings(lConfigProvider)
    End Sub

    Private Sub GetCurrentState()
        ' get the current status and update the btnChange caption
        Dim lConfigProvider As New Service
        Dim value As String = lConfigProvider.GetSiteMessageSetting()

        lblStatus.Text = value

        If (String.Compare(value, "F", True) = 0) Then
            btnChange.Text = "Turn Site Message On"
            lblDisplay.Text = "Site Message is OFF"
        Else
            btnChange.Text = "Turn Site Message Off"
            lblDisplay.Text = "Site Message is ON"
        End If

        value = lConfigProvider.GetCMSContent()
        lblCMS.Text = value
    End Sub

    Private Sub ResetSiteSettings(pConfigProvider As Service)
        Dim setting As String = ConfigurationManager.AppSettings("ConfigLocation")
        Dim dt As DataTable = pConfigProvider.GetProviderSettings(setting)
        Dim ds As New Data.DataSet

        Try

            ds = pConfigProvider.GetWebConfig(setting)
            For Each row As Data.DataRow In ds.Tables(0).Rows
                Application.Lock()
                Application.Remove(row.Item("Key"))
                Application.Add(row.Item("Key"), row.Item("XMLData"))
                Application.UnLock()
            Next

            '#################################################################################################
            '####################   M O D I F I C A T I O N        4/30/2007    ROD COPELAND #################
            '#################################################################################################
            'THIS FUNCTIONALITY IS USED FOR ERROR DISPLAYING. AN ERROR CODE GETS RETURNED FROM A WEBSERVICE
            'OR AN ERROR OCCURS IN THE APPLICAITON, THEN THE ERROR CODE IS SEARCHED FOR IN THE DICTIONARY 
            'AND THE CCOM MESSAGE ASSOCISTED WITH THAT IS DISPLAYED TO THE USER.
            'JUST BECAUSE THE ERROR MESSAGES DONT LOAD BECAUSE OF A PROBLEM, DOESN'T MEAN ITS A SHOW STOPPER.  
            'TRAP THE EXCEPTION AND CONTINUE TO LOAD THE APPLICATION.
            '#################################################################################################
            Try
                If ds.Tables.Count > 1 Then
                    Dim lErrorCodesCollection As New Dictionary(Of String, CharterDotCom.CommonEntities.Library.ErrorCodes.ErrorMapRecord)

                    For Each row As Data.DataRow In ds.Tables(1).Rows
                        Dim lErrorMapRecord As New CharterDotCom.CommonEntities.Library.ErrorCodes.ErrorMapRecord
                        If Not (row.Item("ID") Is System.DBNull.Value) Then
                            If Not (row.Item("ID") Is Nothing) Then
                                lErrorMapRecord.DB_ID = CType(row.Item("ID"), Integer)
                            End If
                        End If
                        If Not (row.Item("DATA_SOURCE_NAME") Is System.DBNull.Value) Then
                            If Not (row.Item("DATA_SOURCE_NAME") Is Nothing) Then
                                lErrorMapRecord.dataSourceName = CType(row.Item("DATA_SOURCE_NAME"), String)
                            End If
                        End If
                        If Not (row.Item("DATA_SOURCE_LOCATION") Is System.DBNull.Value) Then
                            If Not (row.Item("DATA_SOURCE_LOCATION") Is Nothing) Then
                                lErrorMapRecord.dataSourceLocation = CType(row.Item("DATA_SOURCE_LOCATION"), String)
                            End If
                        End If
                        If Not (row.Item("DATA_SOURCE_ERROR_CODE") Is System.DBNull.Value) Then
                            If Not (row.Item("DATA_SOURCE_ERROR_CODE") Is Nothing) Then
                                lErrorMapRecord.dataSourceErrorCode = CType(row.Item("DATA_SOURCE_ERROR_CODE"), String)
                            End If
                        End If
                        If Not (row.Item("DATA_SOURCE_ERROR_MESSAGE") Is System.DBNull.Value) Then
                            If Not (row.Item("DATA_SOURCE_ERROR_MESSAGE") Is Nothing) Then
                                lErrorMapRecord.dataSourceErrorMessage = CType(row.Item("DATA_SOURCE_ERROR_MESSAGE"), String)
                            End If
                        End If
                        If Not (row.Item("CCOM_RELATED_ERROR_CODE") Is System.DBNull.Value) Then
                            If Not (row.Item("CCOM_RELATED_ERROR_CODE") Is Nothing) Then
                                lErrorMapRecord.ccomRelatedErrorCode = CType(row.Item("CCOM_RELATED_ERROR_CODE"), String)
                            End If
                        End If
                        If Not (row.Item("CCOM_DISPLAY_MESSAGE") Is System.DBNull.Value) Then
                            If Not (row.Item("CCOM_DISPLAY_MESSAGE") Is Nothing) Then
                                lErrorMapRecord.ccomDisplayMessage = CType(row.Item("CCOM_DISPLAY_MESSAGE"), String)
                            End If
                        End If
                        If Not (row.Item("CREATE_DATE") Is System.DBNull.Value) Then
                            If Not (row.Item("CREATE_DATE") Is Nothing) Then
                                lErrorMapRecord.createDate = CType(row.Item("CREATE_DATE"), Date)
                            End If
                        End If

                        lErrorCodesCollection.Add(lErrorMapRecord.dataSourceName & lErrorMapRecord.dataSourceErrorCode, lErrorMapRecord)
                    Next

                    Application.Lock()
                    Application.Remove("ERROR_CODE_MAPPING_COLLECTION")
                    Application.Add("ERROR_CODE_MAPPING_COLLECTION", lErrorCodesCollection)
                    Application.UnLock()

                End If
            Catch ex As Exception
                Throw New Exception("the exception is occuring while trying to work with the Error Code Collection. - " & ex.Message)
            End Try
            '#################################################################################################
            '####################   M O D I F I C A T I O N        4/30/2007    ROD COPELAND #################
            '#################################################################################################

            If dt IsNot Nothing Then
                Application.Lock()

                Dim ccomEncrypt As New CCOMEncryption
                For Each dr As DataRow In dt.Rows
                    Try
                        Application.Remove(dr.Item(0))

                        If dr.Item(0).tolower().contains(".password") Then
                            If Not (dr.Item(1) Is Nothing) Then
                                If dr.Item(1).ToString().Trim() <> "" Then
                                    '######################################################################
                                    'MODIFICATION   ROD COPELAND    OCT 2, 2008     ISSUE ID 4710
                                    '######################################################################
                                    'DECRYPT THE PASSWORD VALUES PRIOR TO ADDING TO APPLICATION OBJECT.  
                                    'WHEN ADDING NEW VALUES, CALL THE EncryptTripleDESAndEncode METHOD 
                                    'OF THE CCOMENCRYPTION CLASS AND ENTER THE ENCRYPTED OUTPUT VALUE 
                                    'IN THE TABLE.  IN THE EVENT AN UNENCRYPTED VALUE IS PASSED IN AS 
                                    'INPUT, IT WILL CAUSE A BASE64 ENCODING ERROR, THE FORMATING.
                                    'THE EXCEPTION WILL BE CAUGHT BELOW AND WE'LL ATTEMPT TO ADD THE 
                                    'KEY WITHOUT DECODING IT. 
                                    '######################################################################
                                    Application.Add(dr.Item(0), ccomEncrypt.DecryptTripleDESAndDecode(dr.Item(1)))
                                Else
                                    '#######################################
                                    'DO THE NORMAL ADD WITHOUT DECRYPTING
                                    '#######################################
                                    Application.Add(dr.Item(0), dr.Item(1))
                                End If
                            Else
                                '#######################################
                                'DO THE NORMAL ADD WITHOUT DECRYPTING
                                '#######################################
                                Application.Add(dr.Item(0), dr.Item(1))
                            End If
                        Else
                            '#######################################
                            'DO THE NORMAL ADD WITHOUT DECRYPTING
                            '#######################################
                            Application.Add(dr.Item(0), dr.Item(1))
                        End If
                    Catch ex As Exception
                        '##########################################
                        'ATTEMPT THE OTHER ADDS IF JUST ONE FAILS.
                        '##########################################
                        Application.Add(dr.Item(0), dr.Item(1))
                    End Try
                Next

                Application.UnLock()
            End If

            Dim WebServerServices As New CharterDotComServices.CharterDotComServices

            Dim obj As Object = Application(ApplicationKey.WebServers)
            Dim ls As List(Of String) = obj
            If ls Is Nothing Then
                Dim ServerConfigService As New Service()
                ServerConfigService.GetServerConfig()
            End If
            For Each str As String In ls
                Try
                    Dim lURL As String = "http://" & str & "/_SITESETTINGS_WS/CharterDotComServices.asmx"
                    WebServerServices.Url = lURL
                    'WebServerServices.Url = "http://localhost/ui/_SITESETTINGS_WS/CharterDotComServices.asmx"
                    If WebServerServices.ChangeSiteSettings(ConfigurationManager.AppSettings("SiteSettings")) Then
                        Response.Write("Reset Cache on Webserver:" & str & " - succeeded" & "</BR>")
                    Else
                        Response.Write("Reset Cache on Web server:" & str & " - failed</BR>")
                    End If
                Catch ex As Exception
                    Response.Write("Reset Cache on Web server:" & str & " - failed" & ex.Message.ToString() & "</BR>")
                End Try

            Next

        Catch exDB As Exception
            Throw exDB
        Finally
        End Try

    End Sub
End Class