Imports System
Imports System.ComponentModel

Imports DevExpress.Xpo
Imports DevExpress.Data.Filtering

Imports DevExpress.ExpressApp
Imports DevExpress.Persistent.Base
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.Persistent.Validation
Imports DevExpress.ExpressApp.ConditionalAppearance

Public Enum eEditMode
    eCentralXML = 0
    eUserXML = 1
End Enum
<Appearance("ListViewCustomization - Hide SQLServer when eEditMode = 0", AppearanceItemType.ViewItem, "eEditMode = 0", TargetItems:="SQLServer", visibility:=Editors.ViewItemVisibility.Hide)> _
<Appearance("ListViewCustomization - Hide SQLDatabase when eEditMode = 0", AppearanceItemType.ViewItem, "eEditMode = 0", TargetItems:="SQLDatabase", visibility:=Editors.ViewItemVisibility.Hide)> _
<Appearance("ListViewCustomization - Hide SQLUser when eEditMode = 0", AppearanceItemType.ViewItem, "eEditMode = 0", TargetItems:="SQLUser", visibility:=Editors.ViewItemVisibility.Hide)> _
<Appearance("ListViewCustomization - Hide SQLPassword when eEditMode = 0", AppearanceItemType.ViewItem, "eEditMode = 0", TargetItems:="SQLPassword", visibility:=Editors.ViewItemVisibility.Hide)> _
<Appearance("ListViewCustomization - Hide SQLType when eEditMode = 0", AppearanceItemType.ViewItem, "eEditMode = 0", TargetItems:="SQLType", visibility:=Editors.ViewItemVisibility.Hide)> _
<Appearance("ListViewCustomization - Hide User when eEditMode = 0", AppearanceItemType.ViewItem, "eEditMode = 0", TargetItems:="User", visibility:=Editors.ViewItemVisibility.Hide)> _
<Appearance("ListViewCustomization - Hide XMLFileName when eEditMode = 1", AppearanceItemType.ViewItem, "eEditMode = 1", TargetItems:="XMLFileName", visibility:=Editors.ViewItemVisibility.Hide)> _
<Appearance("ListViewCustomization - Hide aListViewModelManager_FileBrowser when  eEditMode = 1", AppearanceItemType.Action, " eEditMode = 1", TargetItems:="aListViewModelManager_FileBrowser", visibility:=Editors.ViewItemVisibility.Hide)> _
<DefaultClassOptions()> _
<NonPersistent()> _
<CreatableItem(False)> _
<NavigationItem(False)> _
Public Class ListViewCustomization
    Inherits BaseObject

    Private _eEditMode As eEditMode
    Private _SQLServer As String
    Private _SQLDatabase As String
    Private _SQLUser As String
    Private _SQLPassword As String
    Private _SQLType As eSQLType
    Private _XMLFileName As String
    Private _User As User

    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub
    Public Overrides Sub AfterConstruction()
        MyBase.AfterConstruction()
        XMLFileName = ""
        eEditMode = eEditMode.eCentralXML
        SQLType = eSQLType.MSSQL
    End Sub

    <ImmediatePostData()> _
    Property eEditMode As eEditMode
        Get
            Return _eEditMode
        End Get
        Set(value As eEditMode)
            SetPropertyValue("eEditMode", _eEditMode, value)
        End Set
    End Property
    Property SQLServer As String
        Get
            Return _SQLServer
        End Get
        Set(value As String)
            SetPropertyValue("SQLServer", _SQLServer, value)
        End Set
    End Property
    Property SQLDatabase As String
        Get
            Return _SQLDatabase
        End Get
        Set(value As String)
            SetPropertyValue("SQLDatabase", _SQLDatabase, value)
        End Set
    End Property
    Property SQLUser As String
        Get
            Return _SQLUser
        End Get
        Set(value As String)
            SetPropertyValue("SQLUser", _SQLUser, value)
        End Set
    End Property
    Property SQLPassword As String
        Get
            Return _SQLPassword
        End Get
        Set(value As String)
            SetPropertyValue("SQLPassword", _SQLPassword, value)
        End Set
    End Property
    Property SQLType As eSQLType
        Get
            Return _SQLType
        End Get
        Set(value As eSQLType)
            SetPropertyValue("SQLType", _SQLType, value)
        End Set
    End Property
    Property XMLFileName As String
        Get
            Return _XMLFileName
        End Get
        Set(value As String)
            SetPropertyValue("XMLFileName", _XMLFileName, value)
        End Set
    End Property
    <DataSourceProperty("XMLDatabaseUser", DataSourcePropertyIsNullMode.SelectNothing)> _
    Property User As User
        Get
            Return _User
        End Get
        Set(value As User)
            SetPropertyValue("User", _User, value)
        End Set
    End Property
    Private ReadOnly Property XMLDatabaseUser As XPCollection(Of User)
        Get
            Dim _UserCollection As XPCollection(Of User) = Nothing
            Dim _Session As New Session
            Try
                _Session.ConnectionString = GetConnectionString()
                _Session.Connect()
                If _Session.IsConnected Then
                    _UserCollection = New XPCollection(Of User)(_Session)
                End If
            Catch ex As Exception
            Finally
                If _Session.IsConnected Then _Session.Disconnect()
            End Try

            Return _UserCollection
        End Get
    End Property
    Private Function GetConnectionString()
        Dim _ConnectionString As String = ""
        Select Case SQLType
            Case eSQLType.MSSQL
                _ConnectionString = DevExpress.Xpo.DB.MSSqlConnectionProvider.GetConnectionString(SQLServer, SQLUser, SQLPassword, SQLDatabase)
            Case eSQLType.MySQL
                _ConnectionString = DevExpress.Xpo.DB.MySqlConnectionProvider.GetConnectionString(SQLServer, SQLUser, SQLPassword, SQLDatabase)
            Case eSQLType.PostgreSQL
                _ConnectionString = DevExpress.Xpo.DB.PostgreSqlConnectionProvider.GetConnectionString(SQLServer, SQLUser, SQLPassword, SQLDatabase)
        End Select

        Return _ConnectionString
    End Function
End Class
