Imports System
Imports System.ComponentModel
Imports Microsoft.VisualBasic
Imports DevExpress.Xpo
Imports DevExpress.ExpressApp
Imports DevExpress.Persistent.Base
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.Persistent.Validation
Imports DevExpress.Persistent.Base.General
Imports DevExpress.ExpressApp.Utils
Imports DevExpress.ExpressApp.ConditionalAppearance

<NavigationItem(False)> _
<CreatableItem(False)> _
<DefaultClassOptions()> _
<Appearance("Hide Dashboard UserName and Password, when IsDashBoard=False", AppearanceItemType.ViewItem, "IsDashBoard = False", TargetItems:="DashBoard_Database;DashBoard_Server;DashBoard_UserName;DashBoard_Password;SQLType", visibility:=Editors.ViewItemVisibility.Hide)> _
Public Class BusinessDirectory
    Inherits BaseObject
    Implements ITreeNode, ITreeNodeImageProvider

    Private _BusinessDirectory_Parent As BusinessDirectory
    Private _ShortName As String
    Private _IsFolder As Boolean
    Private _FileData As FileData
    Private _FileCreated As Date
    Private _FileModified As Date
    Private _Completed As Boolean 'További tennivaló van -e?
    Private _TaskDescription As String
    Private _EMail_Sender As String
    Private _Email_Copy As String
    Private _IsDashBoard As Boolean
    Private _DashBoard_Server As String
    Private _DashBoard_Database As String
    Private _DashBoard_UserName As String
    Private _DashBoard_Password As String
    Private _SQLType As eSQLType

    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub
    Public Overrides Sub AfterConstruction()
        MyBase.AfterConstruction()
        IsFolder = True
        IsDashBoard = False
    End Sub
    Protected Overrides Sub OnSaving()
        MyBase.OnSaving()
        If FileData IsNot Nothing Then
            IsFolder = False
            If String.IsNullOrEmpty(ShortName) Then ShortName = FileData.FileName
        Else
            IsFolder = True
        End If
    End Sub
    <Association("BusinessDirectory-BusinessDirectory")> _
    Property BusinessDirectory_Parent As BusinessDirectory
        Get
            Return _BusinessDirectory_Parent
        End Get
        Set(value As BusinessDirectory)
            SetPropertyValue("BusinessDirectory_Parent", _BusinessDirectory_Parent, value)
        End Set
    End Property
    <RuleRequiredField("BusinessDirectory - ShortName", DefaultContexts.Save)> _
    Property ShortName As String
        Get
            Return _ShortName
        End Get
        Set(value As String)
            SetPropertyValue("ShortName", _ShortName, value)
        End Set
    End Property
    <VisibleInListView(False)> _
    <VisibleInDetailView(False)> _
    Property IsFolder As Boolean
        Get
            Return _IsFolder
        End Get
        Set(value As Boolean)
            SetPropertyValue("IsFolder", _IsFolder, value)
            If Not Session.IsObjectsSaving And Not Session.IsObjectsLoading Then
                If value And Session.IsNewObject(Me) Then
                    Completed = True
                Else
                    Completed = False
                End If
            End If
        End Set
    End Property
    Property FileData As FileData
        Get
            Return _FileData
        End Get
        Set(value As FileData)
            SetPropertyValue("FileData", _FileData, value)
        End Set
    End Property
    Property FileCreated As Date
        Get
            Return _FileCreated
        End Get
        Set(value As Date)
            SetPropertyValue("FileCreated", _FileCreated, value)
        End Set
    End Property
    Property FileModified As Date
        Get
            Return _FileModified
        End Get
        Set(value As Date)
            SetPropertyValue("FileModified", _FileModified, value)
        End Set
    End Property
    Property Completed As Boolean
        Get
            Return _Completed
        End Get
        Set(value As Boolean)
            SetPropertyValue("Completed", _Completed, value)
        End Set
    End Property
    <Size(-1)> _
    Property TaskDescription As String
        Get
            Return _TaskDescription
        End Get
        Set(value As String)
            SetPropertyValue("TaskDescription", _TaskDescription, value)
        End Set
    End Property
    <Size(255)> _
    Property EMail_Sender As String
        Get
            Return _EMail_Sender
        End Get
        Set(value As String)
            SetPropertyValue("EMail_Sender", _EMail_Sender, value)
        End Set
    End Property
    <Size(900)> _
    Property Email_Copy As String
        Get
            Return _Email_Copy
        End Get
        Set(value As String)
            SetPropertyValue("Email_Copy", _Email_Copy, value)
        End Set
    End Property
    <ImmediatePostData()> _
    Property IsDashBoard As Boolean
        Get
            Return _IsDashBoard
        End Get
        Set(value As Boolean)
            SetPropertyValue("IsDashBoard", _IsDashBoard, value)
        End Set
    End Property
    <RuleRequiredField("BusinessDirectory - DashBoard_Server", DefaultContexts.Save, TargetCriteria:="IsDashBoard=True")> _
    Property DashBoard_Server As String
        Get
            Return _DashBoard_Server
        End Get
        Set(value As String)
            SetPropertyValue("DashBoard_Server", _DashBoard_Server, value)
        End Set
    End Property
    <RuleRequiredField("BusinessDirectory - DashBoard_Database", DefaultContexts.Save, TargetCriteria:="IsDashBoard=True")> _
    Property DashBoard_Database As String
        Get
            Return _DashBoard_Database
        End Get
        Set(value As String)
            SetPropertyValue("DashBoard_Database", _DashBoard_Database, value)
        End Set
    End Property
    <RuleRequiredField("BusinessDirectory - DashBoard_UserName", DefaultContexts.Save, TargetCriteria:="IsDashBoard=True")> _
    Property DashBoard_UserName As String
        Get
            Return _DashBoard_UserName
        End Get
        Set(value As String)
            SetPropertyValue("DashBoard_UserName", _DashBoard_UserName, value)
        End Set
    End Property
    <RuleRequiredField("BusinessDirectory - DashBoard_Password", DefaultContexts.Save, TargetCriteria:="IsDashBoard=True")> _
    <PasswordPropertyText(True)> _
    <ValueConverter(GetType(Encryption))> _
    Property DashBoard_Password As String
        Get
            Return _DashBoard_Password
        End Get
        Set(value As String)
            SetPropertyValue("DashBoard_Password", _DashBoard_Password, value)
        End Set
    End Property
    <RuleRequiredField("BusinessDirectory - SQLType", DefaultContexts.Save, TargetCriteria:="IsDashBoard=True")> _
    Property SQLType As eSQLType
        Get
            Return _SQLType
        End Get
        Set(value As eSQLType)
            SetPropertyValue("SQLType", _SQLType, value)
        End Set
    End Property

    <Association("BusinessDirectory-BusinessDirectory"), Aggregated()> _
    Public ReadOnly Property NestedBusinessDirectory() As XPCollection(Of BusinessDirectory)
        Get
            Return GetCollection(Of BusinessDirectory)("NestedBusinessDirectory")
        End Get
    End Property

#Region "ITreeNode Members"
    Public ReadOnly Property Children() As IBindingList Implements ITreeNode.Children
        Get
            Return NestedBusinessDirectory
        End Get
    End Property
    Private ReadOnly Property Name() As String Implements ITreeNode.Name
        Get
            Return ShortName
        End Get
    End Property
    Public ReadOnly Property Parent() As ITreeNode Implements ITreeNode.Parent
        Get
            Return BusinessDirectory_Parent
        End Get
    End Property
#End Region

#Region "ITreeNodeImageProvider Members"
    Public Function GetImage(ByRef imageName As String) As System.Drawing.Image Implements DevExpress.Persistent.Base.General.ITreeNodeImageProvider.GetImage

        If IsFolder Then
            If NestedBusinessDirectory IsNot Nothing AndAlso NestedBusinessDirectory.Count > 0 Then
                imageName = "Folder_document"
            Else
                imageName = "Folder"
            End If
        Else
            If FileData Is Nothing Then
                imageName = "window_warning"
            Else
                Select Case System.IO.Path.GetExtension(FileData.FileName)
                    Case ".msg"
                        imageName = "mail2"
                    Case ".pdf"
                        imageName = ""
                    Case ".rtf", ".docx", ".doc"
                        imageName = "word_icon"
                    Case ".xlsx", ".xls"
                        imageName = "excel_icon"
                    Case ".xml"
                        imageName = "Navigation_Item_Dashboard"
                    Case Else
                        imageName = "BO_FileAttachment"
                End Select
            End If
        End If
        Return ImageLoader.Instance.GetImageInfo(imageName).Image
    End Function
#End Region
End Class
