Imports System
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Text

Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Actions
Imports DevExpress.Persistent.Base
Imports DevExpress.Data.Filtering
Imports System.Reflection

Public Class UniqueObjects_VC
    Inherits DevExpress.ExpressApp.ViewController

    Public Sub New()
        MyBase.New()

        'This call is required by the Component Designer.
        InitializeComponent()
        RegisterActions(components)

    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        If View.Id = "UniqueObjects_ListView_Deleted" Then
            If TryCast(View, ListView) IsNot Nothing Then
                If TryCast(View, ListView).CollectionSource IsNot Nothing Then
                    If TryCast(View, ListView).CollectionSource.Collection IsNot Nothing Then
                        If TryCast(TryCast(View, ListView).CollectionSource.Collection, ProxyCollection) IsNot Nothing Then
                            If TryCast(TryCast(View, ListView).CollectionSource.Collection, ProxyCollection).OriginalCollection IsNot Nothing Then
                                If TryCast(TryCast(TryCast(View, ListView).CollectionSource.Collection, ProxyCollection).OriginalCollection, DevExpress.Xpo.XPCollection) IsNot Nothing Then
                                    TryCast(TryCast(TryCast(View, ListView).CollectionSource.Collection, ProxyCollection).OriginalCollection, DevExpress.Xpo.XPCollection).SelectDeleted = True
                                End If
                            End If
                        End If
                    End If
                End If
            End If
        End If
    End Sub

    Private Sub aMergeUniqueObjects_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aMergeUniqueObjects.Execute
        Try
            Frame.GetController(Of Validation.PersistenceValidationController).Active.SetItemValue("", False)
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _Object As Object
            Dim _UniqueObjects As UniqueObjects
            Dim _UniqueObjectsActive As UniqueObjects

            For Each _UniqueObjects In View.SelectedObjects

                _UniqueObjectsActive = _onew.FindObject(Of UniqueObjects)(CriteriaOperator.Parse("Name=?", _UniqueObjects.Name))
                If _UniqueObjectsActive IsNot Nothing Then
                    For Each _Object In _onew.Session.CollectReferencingObjects(_onew.GetObjectByKey(Of UniqueObjects)(_UniqueObjects.Oid))
                        Try
                            For Each _PropertyInfo As PropertyInfo In _Object.GetType.GetProperties
                                If _PropertyInfo.PropertyType.Name = "UniqueObjects" Then
                                    _PropertyInfo.SetValue(_Object, _onew.GetObjectByKey(Of UniqueObjects)(_UniqueObjectsActive.Oid), Nothing)
                                End If
                            Next
                        Catch ex As Exception
                            Throw New Exception(ex.Message)
                        End Try
                    Next
                End If
            Next
            _onew.CommitChanges()
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            Frame.GetController(Of Validation.PersistenceValidationController).Active.SetItemValue("", True)
        End Try
    End Sub
End Class
