Imports System
Imports System.Configuration
Imports System.Windows.Forms

Imports DevExpress.Persistent.Base
Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Security
Imports DevExpress.ExpressApp.Win

Imports Nuvolar.Module.Win
Imports DevExpress.Persistent.AuditTrail
Imports Microsoft.VisualBasic

Imports DevExpress.Xpo
Imports DevExpress.Data.Filtering
Imports DevExpress.Persistent.BaseImpl

Imports DevExpress.ExpressApp.Model.Core
Imports DevExpress.ExpressApp.Model
Imports Nuvolar.Module

Friend NotInheritable Class Program
    Private Sub New()
    End Sub
    <STAThread()> _
    Public Shared Sub Main(ByVal arguments() As String)
#If EASYTEST Then
        DevExpress.ExpressApp.EasyTest.WinAdapter.RemotingRegistration.Register(4100)
#End If

        Application.EnableVisualStyles()
        Application.SetCompatibleTextRenderingDefault(False)
        EditModelPermission.AlwaysGranted = System.Diagnostics.Debugger.IsAttached

        '-- Eredeti kód
        'Dim _application As NuvolarWindowsFormsApplication_DB = New NuvolarWindowsFormsApplication_DB()

        'If (Not ConfigurationManager.ConnectionStrings.Item("ConnectionString") Is Nothing) Then
        '    _application.ConnectionString = ConfigurationManager.ConnectionStrings.Item("ConnectionString").ConnectionString
        'End If
        'If System.Diagnostics.Debugger.IsAttached Then
        '    _application.DatabaseUpdateMode = DatabaseUpdateMode.UpdateDatabaseAlways
        'End If
        'Try
        '    Dim timeStampStrategy As IAuditTimestampStrategy = New Nuvolar.Module.TimeStamp
        '    AuditTrailService.Instance.TimestampStrategy = timeStampStrategy

        '    _application.Setup()
        '    _application.Start()
        'Catch e As Exception
        '    _application.HandleException(e)
        'End Try

        '-- Új kód
        Do
            Dim winApplication As NuvolarWindowsFormsApplication
            If LogOnDB.NewApplication Is Nothing Then
                winApplication = NuvolarWindowsFormsApplication.CreateApplication()
            Else
                winApplication = CType(LogOnDB.NewApplication, NuvolarWindowsFormsApplication)
                LogOnDB.NewApplication = Nothing
            End If
            Try
                AddHandler AuditTrailService.Instance.SaveAuditTrailData, AddressOf Instance_SaveAuditTrailData
                AddHandler winApplication.CreateCustomUserModelDifferenceStore, AddressOf winApplication_CreateCustomUserModelDifferenceStore
                AddHandler winApplication.LastLogonParametersWriting, AddressOf winApplication_LastLogonParametersWriting
                AddHandler winApplication.CustomHandleException, AddressOf winApplication_CustomHandleException
                winApplication.DelayedViewItemsInitialization = True
                winApplication.SplashScreen = New DevExpress.ExpressApp.Win.Utils.DXSplashScreen

                AuditTrailService.Instance.ObjectAuditingMode = ObjectAuditingMode.CreationOnly

                winApplication.Setup()
                winApplication.Start()
                If WinChangeDatabaseHelper.AuthenticatedUserLogonFailed Then
                    WinChangeDatabaseHelper.SkipLogonDialog = False
                    winApplication.Start()
                End If
            Catch e As Exception
                winApplication.HandleException(e)
            End Try
            winApplication.Dispose()
        Loop While LogOnDB.NewApplication IsNot Nothing
        '-- Új kód vége
    End Sub
    Shared Sub winApplication_CreateCustomUserModelDifferenceStore(ByVal sender As Object, ByVal e As CreateCustomModelDifferenceStoreEventArgs)
        Dim userDiffs As New UserStore(CType(sender, WinApplication))
        e.Store = userDiffs
        e.Handled = True
    End Sub

    Private Shared Sub Instance_SaveAuditTrailData(sender As Object, e As SaveAuditTrailDataEventArgs)
        If GLOBAL_HAS_Audit = False Then
            e.Handled = True
        End If
    End Sub
    Shared Sub winApplication_LastLogonParametersWriting(ByVal sender As Object, ByVal e As LastLogonParametersWritingEventArgs)
        'If (CType(e.LogonObject, AuthenticationStandardLogonParameters)).UserName <> "SysAdmin" Then
        e.SettingsStorage.SaveOption("", "UserName", (CType(e.LogonObject, AuthenticationStandardLogonParameters)).UserName)
        e.SettingsStorage.SaveOption("", "DBConnection", GLOBAL_SQLDatabase)
        e.SettingsStorage.SaveOption("", "SQLServer", GLOBAL_SQLServer)
        Select Case GLOBAL_SQLType
            Case eSQLType.MSSQL
                e.SettingsStorage.SaveOption("", "SQLType", "MSSQL")
            Case eSQLType.MySQL
                e.SettingsStorage.SaveOption("", "SQLType", "MySQL")
            Case eSQLType.PostgreSQL
                e.SettingsStorage.SaveOption("", "SQLType", "PostgreSQL")
        End Select

        e.Handled = True
    End Sub

    Private Shared Sub winApplication_CustomHandleException(sender As Object, e As CustomHandleExceptionEventArgs)
        '// Egyéni hiba kezelés !!!
    End Sub

End Class



