Imports Microsoft.VisualBasic
Imports System

Imports DevExpress.Xpo

Imports DevExpress.ExpressApp
Imports DevExpress.Persistent.Base
Imports DevExpress.Persistent.Validation
Imports DevExpress.Persistent.Base.General
Imports DevExpress.Xpo.Metadata
Imports System.ComponentModel
Imports System.Xml


<DefaultClassOptions(), NavigationItem(False), CreatableItem(False)> _
Public Class HRPersonEvent
    Inherits DevExpress.Persistent.BaseImpl.BaseObject
    Implements IEvent, IRecurrentEvent

#If MediumTrust Then
		<Persistent("ResourceIds"), Size(SizeAttribute.Unlimited), ObjectValidatorIgnoreIssue(GetType(ObjectValidatorLargeNonDelayedMember))> _
		Public resourceIds As String
#Else
    <Persistent("ResourceIds"), Size(SizeAttribute.Unlimited), ObjectValidatorIgnoreIssue(GetType(ObjectValidatorLargeNonDelayedMember))> _
    Private resourceIds As String
#End If
    Private _appointmentImpl As New EventImpl
    <Persistent("RecurrencePattern")> _
    Private recurrencePattern_Renamed As DevExpress.Persistent.BaseImpl.Event
    Private recurrenceInfoXml_Renamed As String
    Private Sub UpdateResources()
        Resources.SuspendChangedEvents()
        Try
            Do While Resources.Count > 0
                Resources.Remove(Resources(0))
            Loop
            If (Not String.IsNullOrEmpty(resourceIds)) Then
                Dim xmlDocument As New XmlDocument()
                xmlDocument.LoadXml(resourceIds)
                For Each xmlNode As XmlNode In xmlDocument.DocumentElement.ChildNodes
                    Dim resource As HRPerson = Session.GetObjectByKey(Of HRPerson)(New Guid(xmlNode.Attributes("Value").Value))
                    If resource IsNot Nothing Then
                        Resources.Add(resource)
                    End If
                Next xmlNode
            End If
        Finally
            Resources.ResumeChangedEvents()
        End Try
    End Sub
    Private Sub Resources_CollectionChanged(ByVal sender As Object, ByVal e As XPCollectionChangedEventArgs)
        If (e.CollectionChangedType = XPCollectionChangedType.AfterAdd) OrElse (e.CollectionChangedType = XPCollectionChangedType.AfterRemove) Then
            UpdateResourceIds()
            OnChanged("ResourceId")
        End If
    End Sub
    Private Sub session_ObjectSaving(ByVal sender As Object, ByVal e As ObjectManipulationEventArgs)
    End Sub
    Protected Overloads Overrides Function CreateCollection(ByVal [property] As XPMemberInfo) As XPCollection
        Dim result As XPCollection = MyBase.CreateCollection([property])
        If [property].Name = "Resources" Then
            AddHandler result.CollectionChanged, AddressOf Resources_CollectionChanged
        End If
        Return result
    End Function
    Public Sub New(ByVal session As Session)
        MyBase.New(session)

        AddHandler session.ObjectSaving, AddressOf session_ObjectSaving
    End Sub
    Public Overrides Sub AfterConstruction()
        MyBase.AfterConstruction()
        _appointmentImpl = New EventImpl
        _appointmentImpl.AfterConstruction()
    End Sub
    Public Sub UpdateResourceIds()
        resourceIds = "<ResourceIds>" & Constants.vbCrLf
        For Each resource As HRPerson In Resources
            resourceIds &= String.Format("<ResourceId Type=""{0}"" Value=""{1}"" />" & Constants.vbCrLf, resource.Id.GetType().FullName, resource.Id)
        Next resource
        resourceIds &= "</ResourceIds>"
    End Sub
    <NonPersistent(), Browsable(False)> _
    Public ReadOnly Property AppointmentId() As Object Implements IEvent.AppointmentId
        Get
            Return Oid
        End Get
    End Property
    <Size(250)> _
    Public Property Subject() As String Implements IEvent.Subject
        Get
            Return _appointmentImpl.Subject
        End Get
        Set(ByVal value As String)
            _appointmentImpl.Subject = value
            OnChanged("Subject", Nothing, value)
        End Set
    End Property
    <Size(SizeAttribute.Unlimited), ObjectValidatorIgnoreIssue(GetType(ObjectValidatorLargeNonDelayedMember))> _
    Public Property Description() As String Implements IEvent.Description
        Get
            Return _appointmentImpl.Description
        End Get
        Set(ByVal value As String)
            _appointmentImpl.Description = value
            OnChanged("Description")
        End Set
    End Property
    <Indexed()> _
    Public Property StartOn() As DateTime Implements IEvent.StartOn
        Get
            Return _appointmentImpl.StartOn
        End Get
        Set(ByVal value As DateTime)
            _appointmentImpl.StartOn = value
            OnChanged("StartOn")
        End Set
    End Property
    <Indexed()> _
    Public Property EndOn() As DateTime Implements IEvent.EndOn
        Get
            Return _appointmentImpl.EndOn
        End Get
        Set(ByVal value As DateTime)
            _appointmentImpl.EndOn = value
            OnChanged("EndOn")
        End Set
    End Property
    Public Property AllDay() As Boolean Implements IEvent.AllDay
        Get
            Return _appointmentImpl.AllDay
        End Get
        Set(ByVal value As Boolean)
            _appointmentImpl.AllDay = value
            OnChanged("AllDay")
        End Set
    End Property
    Public Property Location() As String Implements IEvent.Location
        Get
            Return _appointmentImpl.Location
        End Get
        Set(ByVal value As String)
            _appointmentImpl.Location = value
            OnChanged("Location")
        End Set
    End Property
    Public Property Label() As Integer Implements IEvent.Label
        Get
            Return _appointmentImpl.Label
        End Get
        Set(ByVal value As Integer)
            _appointmentImpl.Label = value
            OnChanged("Label")
        End Set
    End Property
    Public Property Status() As Integer Implements IEvent.Status
        Get
            Return _appointmentImpl.Status
        End Get
        Set(ByVal value As Integer)
            _appointmentImpl.Status = value
            OnChanged("Status")
        End Set
    End Property
    Public Property Type() As Integer Implements IEvent.Type
        Get
            Return _appointmentImpl.Type
        End Get
        Set(ByVal value As Integer)
            _appointmentImpl.Type = value
            OnChanged("Type")
        End Set
    End Property
    <Association("HRPersonEvent-HRPerson", GetType(HRPerson))> _
    Public ReadOnly Property Resources() As XPCollection(Of HRPerson)
        Get
            Return GetCollection(Of HRPerson)("Resources")
        End Get
    End Property
    <NonPersistent(), Browsable(False)> _
    Public Property ResourceId() As String Implements IEvent.ResourceId
        Get
            If resourceIds Is Nothing Then
                UpdateResourceIds()
            End If
            Return resourceIds
        End Get
        Set(ByVal value As String)
            If resourceIds <> value Then
                resourceIds = value
                UpdateResources()
            End If
        End Set
    End Property
    <DevExpress.Xpo.DisplayName("Recurrence"), Size(SizeAttribute.Unlimited), ObjectValidatorIgnoreIssue(GetType(ObjectValidatorLargeNonDelayedMember))> _
    Public Property RecurrenceInfoXml() As String Implements IRecurrentEvent.RecurrenceInfoXml
        Get
            Return recurrenceInfoXml_Renamed
        End Get
        Set(ByVal value As String)
            recurrenceInfoXml_Renamed = value
            OnChanged("RecurrenceInfoXml")
        End Set
    End Property
    Public Property RecurrencePattern() As IRecurrentEvent Implements IRecurrentEvent.RecurrencePattern
        Get
            Return recurrencePattern_Renamed
        End Get
        Set(ByVal value As IRecurrentEvent)
            recurrencePattern_Renamed = CType(value, DevExpress.Persistent.BaseImpl.Event)
            OnChanged("RecurrencePattern")
        End Set
    End Property
    <NonPersistent(), Browsable(False), RuleFromBoolProperty("MyEventIntervalValid", DefaultContexts.Save, "The start date must be less than the end date", UsedProperties:="StartOn, EndOn")> _
    Public ReadOnly Property IsIntervalValid() As Boolean
        Get
            Return StartOn < EndOn
        End Get
    End Property
End Class
