Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports System.Xml
Imports System.Data
Imports System.Collections.Generic

Partial Class SiteSettings
    Inherits AdminPage

    Protected Sub btnSettings_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSettings.Click
        Dim lConfigProvider As New Service
        Dim setting As String = ConfigurationManager.AppSettings("ConfigLocation")
        Dim dt As DataTable = lConfigProvider.GetProviderSettings(setting)
        Dim ds As New Data.DataSet
        Dim strOut As String = ""

        Try
            strOut = strOut & "<table>" & vbNewLine
            strOut = strOut & "<tr><td><b>CONFIGLOCATION</b></td><td><b>KEY Value</b></td><td><b>XMLDATA Value</b></td></tr>" & vbNewLine

            ds = lConfigProvider.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()
                strOut = strOut & "<tr><td>" & row.Item(0) & " </td><td>" & row.Item("Key") & " </td><td> " & "" & row.Item("XMLData") & " </td></tr>" & vbNewLine
            Next
            strOut = strOut & "</table>" & vbNewLine
            lblDisplay.Text = strOut








            '#################################################################################################
            '####################   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()

                strOut = strOut & "<table>" & vbNewLine
                strOut = strOut & "<tr><td><b>KEY Value</b></td><td><b>XMLDATA Value</b></td></tr>" & vbNewLine

                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
                    strOut = strOut & "<tr><td>" & dr.Item(0) & " </td><td> " & "" & dr.Item(1) & " </td></tr>" & vbNewLine
                Next
                strOut = strOut & "</table>" & vbNewLine
                lblDisplay.Text = strOut

                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

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub btnViewApplicationObj_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnViewApplicationObj.Click
        Dim strTempAppValue As String

        Response.Write("<h1>All values from the application object on Machine " & Environment.MachineName.ToLower & "</h1>")
        Response.Write("<table>")
        For Each Key As String In Application.AllKeys
            strTempAppValue = ""
            Try
                If Not (Key Is Nothing) Then
                    Response.Write("<tr><td><b>KEY:</b><i> " & Key & "</i></td></tr>" & vbNewLine)
                    Try
                        strTempAppValue = Application(Key)
                        Response.Write("<tr><td><b>VALUE:</b><i>" & strTempAppValue & "</i></td></tr>" & vbNewLine & vbNewLine)
                    Catch ex As Exception
                        Response.Write("<tr><td><h3>An Error has occured ..." & ex.Message.ToString() & "</h3></td></tr>" & vbNewLine)
                    End Try
                    Response.Write("<tr><td><br></td></tr>" & vbNewLine & vbNewLine)
                End If
            Catch ex As Exception
                '#############################################
                'CONTINUE AND KEEP WRITING THE VALUES OUT.
                '#############################################
            End Try
        Next
        Response.Write("</table>")

    End Sub

    Protected Sub btnTestEncryption_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnTestEncryption.Click
        Dim ccomEncrypt As New CCOMEncryption

        ''#################################################################
        ''TEST STRING ENCRYPTION
        ''#################################################################
        'Dim testString As String = ""
        'testString = ccomEncrypt.EncryptTripleDESAndEncode(testString)
        'testString = ccomEncrypt.DecryptTripleDESAndDecode(testString)
        ''#################################################################

        Dim strTempAppValue As String

        Response.Write("<h1>All Provider password values using TripDES Encryption on Machine " & Environment.MachineName.ToLower & "</h1>")
        Response.Write("<table>")
        For Each Key As String In Application.AllKeys
            If Key.ToLower().Contains(".password") Then
                strTempAppValue = ""
                Try
                    If Not (Key Is Nothing) Then
                        Response.Write("<tr><td><b>KEY:</b><i> " & Key & "</i></td></tr>" & vbNewLine)
                        Try
                            strTempAppValue = Application(Key)
                            Response.Write("<tr><td><b>Encrypted VALUE:</b><i>" & ccomEncrypt.EncryptTripleDESAndEncode(strTempAppValue) & "</i></td></tr>" & vbNewLine & vbNewLine)
                        Catch ex As Exception
                            Response.Write("<tr><td><h3>An Error has occured ..." & ex.Message.ToString() & "</h3></td></tr>" & vbNewLine)
                        End Try
                        Response.Write("<tr><td><br></td></tr>" & vbNewLine & vbNewLine)
                    End If
                Catch ex As Exception
                    '#############################################
                    'CONTINUE AND KEEP WRITING THE VALUES OUT.
                    '#############################################
                End Try
            End If
        Next
        Response.Write("</table>")

    End Sub

End Class
