Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports DevExpress.ExpressApp.Win
Imports DevExpress.ExpressApp.Updating
Imports DevExpress.ExpressApp
Imports System.Configuration
Imports DevExpress.ExpressApp.Security
Imports SISBusiness.Module
Imports DevExpress.Persistent.BaseImpl
Imports SISBusiness.Module.Win
Imports DevExpress.Xpo
Imports DevExpress.Xpo.DB
Partial Public Class SISBusinessWindowsFormsApplication
    Inherits WinApplication
    Implements IApplicationFactory
    Friend WithEvents SystemModule1 As DevExpress.ExpressApp.SystemModule.SystemModule
    Protected Overrides Sub ReadLastLogonParametersCore(ByVal storage As DevExpress.ExpressApp.Utils.SettingsStorage, ByVal logonObject As Object)
        Dim standardLogonParameters As AuthenticationStandardLogonParameters = TryCast(logonObject, AuthenticationStandardLogonParameters)
        If (standardLogonParameters IsNot Nothing) AndAlso String.IsNullOrEmpty(standardLogonParameters.UserName) Then
            MyBase.ReadLastLogonParametersCore(storage, logonObject)
            Try
                TryCast(logonObject, ChangeLogon).DBConnection = TryCast(storage, DevExpress.ExpressApp.Utils.SettingsStorageOnString).Values("\DBConnection")
                TryCast(logonObject, ChangeLogon).SQLServer = TryCast(storage, DevExpress.ExpressApp.Utils.SettingsStorageOnString).Values("\SQLServer")
                Select Case TryCast(storage, DevExpress.ExpressApp.Utils.SettingsStorageOnString).Values("\SQLType")
                    Case "MSSQL"
                        TryCast(logonObject, ChangeLogon).SQLType = eSQLType.MSSQL
                    Case "PostgreSQL"
                        TryCast(logonObject, ChangeLogon).SQLType = eSQLType.PostgreSQL
                    Case "MySQL"
                        TryCast(logonObject, ChangeLogon).SQLType = eSQLType.MySQL
                End Select
            Catch
            End Try
        End If
    End Sub
    Protected Overrides Sub OnLoggingOn(ByVal args As LogonEventArgs)
        MyBase.OnLoggingOn(args)

        Dim _connectionString As String
        Dim _ChangeLogon As ChangeLogon
        Dim _plattform As String = ""

        _ChangeLogon = TryCast(args.LogonParameters, ChangeLogon)

        GLOBAL_SQLType = _ChangeLogon.SQLType
        GLOBAL_SQLServer = _ChangeLogon.SQLServer
        GLOBAL_SQLDatabase = _ChangeLogon.DBConnection


        Select Case _ChangeLogon.SQLType
            Case eSQLType.MSSQL
                _connectionString = " User ID=sa; "
                _connectionString &= " Application Name=SIS Business Framework (" & UCase(_ChangeLogon.UserName) & ");"
                _connectionString &= " Password=pcl718;"
                _connectionString &= " Pooling=false;"
                _connectionString &= " Data Source=" & _ChangeLogon.SQLServer & ";"
                _connectionString &= " Initial Catalog=" & _ChangeLogon.DBConnection
                _plattform = "MS SQL"
                GLOBAL_SQLUser = "sa"
                GLOBAL_SQLPassword = "pcl718"
            Case eSQLType.MySQL
                Select Case _ChangeLogon.SQLServer
                    Case "84.1.95.74"
                        _connectionString = DevExpress.Xpo.DB.MySqlConnectionProvider.GetConnectionString(_ChangeLogon.SQLServer, "root", "malomko10", _ChangeLogon.DBConnection)
                    Case Else
                        _connectionString = DevExpress.Xpo.DB.MySqlConnectionProvider.GetConnectionString(_ChangeLogon.SQLServer, "root", "pcl718", _ChangeLogon.DBConnection)
                End Select
                _plattform = "MySQL"
                GLOBAL_SQLUser = "root"
                GLOBAL_SQLPassword = "pcl718"
            Case eSQLType.PostgreSQL
                _connectionString = DevExpress.Xpo.DB.PostgreSqlConnectionProvider.GetConnectionString(_ChangeLogon.SQLServer, "root", "pcl718", _ChangeLogon.DBConnection)
                _plattform = "postgreSQL"
            Case Else
                _connectionString = " User ID=sa; "
                _connectionString &= " Password=pcl718;"
                _connectionString &= " Pooling=false;"
                _connectionString &= " Data Source=" & _ChangeLogon.SQLServer & ";"
                _connectionString &= " Initial Catalog=" & _ChangeLogon.DBConnection
                GLOBAL_SQLUser = "sa"
                GLOBAL_SQLPassword = "pcl718"
        End Select

        Dim anm As System.Reflection.AssemblyName = System.Reflection.AssemblyName.GetAssemblyName(Application.StartupPath & "\SISBusiness.Module.dll")

        Me.ConnectionString = _connectionString
        GLOBAL_ConnectionString = _connectionString
        GLOBAL_WINFormCaption = "SISBusiness Framework version: " & Application.ProductVersion & " [" & anm.Version.ToString & "]"
        GLOBAL_WINFormCaption += ", (" & _plattform & ") Server: " & UCase(_ChangeLogon.SQLServer) & ", Database: " & UCase(_ChangeLogon.DBConnection)
        GLOBAL_ApplicationStartupPath = Application.StartupPath & "\"
    End Sub
    Protected Overrides Function OnLogonFailed(ByVal logonParameters As Object, ByVal e As Exception) As Boolean
        If WinChangeDatabaseHelper.SkipLogonDialog Then
            Return True
        End If
        Return MyBase.OnLogonFailed(logonParameters, e)
    End Function
    Public Shared Function CreateApplication() As SISBusinessWindowsFormsApplication
        Dim winApplication As New SISBusinessWindowsFormsApplication

        CType(winApplication.Security, SecurityBase).Authentication = New WinChangeDatabaseStandardAuthentication()
        Return winApplication
    End Function
    Private Function IApplicationFactory_CreateApplication() As WinApplication Implements IApplicationFactory.CreateApplication
        Return CreateApplication()
    End Function
    Protected Overrides Function GetModelAssemblyFilePath() As String
        Return Nothing
    End Function
End Class

