<%@ Application Language="VB" %>
<%@ Import Namespace ="Microsoft.Practices.EnterpriseLibrary.Data" %>
<%@ Import Namespace ="System.Xml" %>
<%@ Import Namespace ="System.Data" %>
<%@ Import Namespace ="System.Data" %>
<%@ Import Namespace="System.Security.Principal" %>
<%@ Import Namespace="CharterDotCom.CommonEntities.Library.MessageQComponent" %>
<%@ Import Namespace="CharterDotCom.CommonEntities.Library.ErrorCodeMapping" %>
<%@ Import Namespace="System.Collections.Generic" %>

<%@ Import Namespace="CharterDotCom.Common.Logging" %>
<%@ Import Namespace="Quartz" %>
<%@ Import Namespace="Quartz.Impl" %>


<script RunAt="server">    
    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        Dim lConfigProvider As New Service
        Dim ds As New Data.DataSet
        Dim i As Integer
        i = 0
        Dim setting As String = ConfigurationManager.AppSettings("ConfigLocation")
        ds = lConfigProvider.GetWebConfig(setting)
        
        System.Diagnostics.Debug.WriteLine("Setting for ConfigLocation " + setting)
        For Each row As Data.DataRow In ds.Tables(0).Rows
            Application.Lock()
            Application.Remove(CType(row.Item("Key"), String))
            Application.Add(CType(row.Item("Key"), String), row.Item("XMLData"))
            Application.UnLock()
        Next
        
        Logger.ServiceLog("Initialize Server")
        Logger.IdTheftLog("Initialize Quartz Scheduler...")

        Dim _scheduler As IScheduler
        Dim factory As ISchedulerFactory = New StdSchedulerFactory

        _scheduler = factory.GetScheduler()
        _scheduler.Start()

        Logger.IdTheftLog("Quartz Scheduler Started!")

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

        Channels.RegisterChannels()
        Dim dt As DataTable = lConfigProvider.GetProviderSettings(setting)
        If dt IsNot Nothing Then
            Application.Lock()
            Dim ccomEncrypt As New CCOMEncryption
        
            ''#################################################################
            ''TEST STRING ENCRYPTION
            ''#################################################################
            'Dim testString As String = ""
            'testString = ccomEncrypt.EncryptTripleDESAndEncode(testString)
            'testString = ccomEncrypt.DecryptTripleDESAndDecode(testString)
            ''#################################################################
            
            
            For Each dr As DataRow In dt.Rows
                Try
                    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 ServerConfigService As New Service()
        ServerConfigService.GetServerConfig()
        
        
        
        
        
        
       
        
    End Sub    
   
    
    Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs on application shutdown
    End Sub
        
    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs when an unhandled error occurs
        Dim _error As Exception = Server.GetLastError()
	
        Try
            Try
                '###########################################
                'LOGG THE ERROR MESSAGE TO THE ERROR QUEUE
                '###########################################
                Dim tempErrorQLoc As String = ConfigurationManager.AppSettings("ErrorQueueLocation")
                Dim ErrorQueueLocation As String = Environment.MachineName & tempErrorQLoc
                Dim lMessageQComponent As New CharterDotCom.CommonEntities.Library.MessageQComponent
                lMessageQComponent.AddAMessage(ErrorQueueLocation, _error.Message)
            Catch ex As Exception
                '##########################################
                'IF THIS FAILS, JUST SKIP THE LOGGING PART
                '##########################################
            End Try
            
                                    
        Catch ex As Exception
	
        End Try
    End Sub
 
    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        
    End Sub

    Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs when a session ends. 
        ' Note: The Session_End event is raised only when the sessionstate mode
        ' is set to InProc in the Web.config file. If session mode is set to StateServer 
        ' or SQLServer, the event is not raised.
    End Sub
    
    
</script>


