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.Xpo
Imports DevExpress.Data.Filtering
Imports System.IO
Imports System.Security
Imports System.Xml

Imports System.Security.Principal

Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.Persistent.Base.Security

Imports DevExpress.ExpressApp.Security
Imports DevExpress.ExpressApp.Updating
Imports DevExpress.ExpressApp.Win.Editors
Imports Nuvolar.Module


Public Class CustomRoleEditorVC
    Inherits DevExpress.ExpressApp.ViewController(Of ListView)
    Private _GridListEditor As GridListEditor
    Protected _selection As ArrayList
    Protected _noselect As Boolean = False

    Public Sub New()
        MyBase.New()
        _selection = New ArrayList()
        InitializeComponent()
        RegisterActions(components)
    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        ''____________Továbbgondolásig letíltva!__________________________>>>>
        'Frame.GetController(Of CustomRoleEditorVC).aCustomRoleSave.Active.SetItemValue("", False)
        ''____________Továbbgondolásig letíltva!__________________________<<<

        If View.Id = "PermissionMatrix_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.Win.SystemModule.OpenObjectController)().Active.SetItemValue("", False)
            AddHandler View.SelectionChanged, AddressOf PermissionMatrix_selection_changed
        End If

        '// Permission matrix beállítása Modelből

    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "PermissionMatrix_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.Win.SystemModule.OpenObjectController)().Active.SetItemValue("", True)

            RemoveHandler View.SelectionChanged, AddressOf PermissionMatrix_selection_changed
        End If
    End Sub

    Protected Overrides Sub OnViewControlsCreated()
        MyBase.OnViewControlsCreated()
        'If View.Id = "PermissionMatrix_ListView" Then
        '    Dim _ListView As ListView = CType(View, ListView)
        '    _GridListEditor = TryCast(View.Editor, GridListEditor)
        '    If _GridListEditor IsNot Nothing Then
        '        _GridListEditor.AllowEdit = True
        '        For i As Integer = 0 To _GridListEditor.Columns.Count - 1
        '            If _GridListEditor.GridView.Columns(i).FieldName <> "IsCreate" And _
        '                _GridListEditor.GridView.Columns(i).FieldName <> "IsRead" And _
        '                _GridListEditor.GridView.Columns(i).FieldName <> "IsWrite" And _
        '                _GridListEditor.GridView.Columns(i).FieldName <> "IsNavigate" And _
        '                _GridListEditor.GridView.Columns(i).FieldName <> "IsDelete" Then
        '                _GridListEditor.GridView.Columns(i).OptionsColumn.ReadOnly = True
        '                _GridListEditor.GridView.Columns(i).OptionsColumn.AllowEdit = False
        '                _GridListEditor.GridView.Columns(i).OptionsColumn.TabStop = False
        '            End If
        '        Next i
        '    End If
        'End If
    End Sub
    Sub PermissionMatrix_selection_changed(ByVal sender As Object, ByVal e As System.EventArgs)
        If View Is Nothing Then Exit Sub
        If View.Id = "PermissionMatrix_ListView" Then
            If _noselect = False Then
                _selection.Clear()
                For Each _PermissionMatrix As PermissionMatrix In View.SelectedObjects
                    _selection.Add(_PermissionMatrix)
                Next
            End If
        End If
    End Sub
    Sub CreatePermissionMatrix(ByVal ObjectSpace As Xpo.XPObjectSpace)
        Dim _PermissionMatrix_Collection As XPCollection(Of PermissionMatrix) = New XPCollection(Of PermissionMatrix)(ObjectSpace.Session, CriteriaOperator.Parse("Oid=Oid"))
        Dim _Role_Collection As XPCollection(Of Role) = New XPCollection(Of Role)(ObjectSpace.Session, CriteriaOperator.Parse("Oid=Oid"))

        Dim _Type As Type
        Dim _Role As Role
        Dim _Permission As IPermission
        Dim _XMLDocument As New XmlDocument
        Dim _XmlNodes As Xml.XmlNodeList
        Dim _XmlNode As Xml.XmlNode
        Dim _XmlNode1 As Xml.XmlNode
        Dim i As Long
        Dim _PermissionMatrix As PermissionMatrix
        Dim _OT As String
        ObjectSpace.Delete(_PermissionMatrix_Collection)

        For Each _Role In _Role_Collection


            For Each _Permission In _Role.Permissions
                _XMLDocument.LoadXml(_Permission.ToXml.ToString)
                _XmlNodes = _XMLDocument.SelectNodes("IPermission")
                For Each _XmlNode In _XmlNodes
                    i = 0
                    For Each _XmlNode1 In _XmlNode.SelectNodes("ParticularAccessItem")
                        If i = 0 Then

                            _PermissionMatrix = ObjectSpace.CreateObject(Of PermissionMatrix)()
                            _PermissionMatrix.Role = _Role

                            _OT = Replace(_XmlNode1.Attributes("objectType").Value.ToString, "Nuvolar.Module.", "")
                            _PermissionMatrix.ObjectType = _OT
                        End If
                        If _XmlNode1.Attributes("access").Value.ToString = "Create" Then
                            If _XmlNode1.Attributes("modifier").Value.ToString = "Allow" Then _PermissionMatrix.IsCreate = ePermissionType.Allow
                            If _XmlNode1.Attributes("modifier").Value.ToString = "Deny" Then _PermissionMatrix.IsCreate = ePermissionType.Deny
                        End If
                        If _XmlNode1.Attributes("access").Value.ToString = "Read" Then
                            If _XmlNode1.Attributes("modifier").Value.ToString = "Allow" Then _PermissionMatrix.IsRead = ePermissionType.Allow
                            If _XmlNode1.Attributes("modifier").Value.ToString = "Deny" Then _PermissionMatrix.IsRead = ePermissionType.Deny
                        End If
                        If _XmlNode1.Attributes("access").Value.ToString = "Write" Then
                            If _XmlNode1.Attributes("modifier").Value.ToString = "Allow" Then _PermissionMatrix.IsWrite = ePermissionType.Allow
                            If _XmlNode1.Attributes("modifier").Value.ToString = "Deny" Then _PermissionMatrix.IsWrite = ePermissionType.Deny
                        End If
                        If _XmlNode1.Attributes("access").Value.ToString = "Delete" Then
                            If _XmlNode1.Attributes("modifier").Value.ToString = "Allow" Then _PermissionMatrix.IsDelete = ePermissionType.Allow
                            If _XmlNode1.Attributes("modifier").Value.ToString = "Deny" Then _PermissionMatrix.IsDelete = ePermissionType.Deny
                        End If
                        If _XmlNode1.Attributes("access").Value.ToString = "Navigate" Then
                            If _XmlNode1.Attributes("modifier").Value.ToString = "Allow" Then _PermissionMatrix.IsNavigate = ePermissionType.Allow
                            If _XmlNode1.Attributes("modifier").Value.ToString = "Deny" Then _PermissionMatrix.IsNavigate = ePermissionType.Deny
                        End If
                        i += 1
                    Next
                    _PermissionMatrix.Save()
                Next
            Next
        Next
        ObjectSpace.CommitChanges()
        View.Refresh()
    End Sub
    Private Sub aCustomRoleLoad_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aCustomRoleLoad.Execute
        Me.CreatePermissionMatrix(View.ObjectSpace)
    End Sub
    Private Sub aCustomRoleSave_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aCustomRoleSave.Execute
        Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
        ' Dim _PermissionMatrix_Collection As XPCollection(Of PermissionMatrix) = New XPCollection(Of PermissionMatrix)(_ocur.Session, CriteriaOperator.Parse("IsCreate=? AND IsDelete=? AND IsNavigate=? AND IsRead=? AND IsWrite=?", _
        '                                                                                                             ePermissionType.NotSet, ePermissionType.NotSet, ePermissionType.NotSet, ePermissionType.NotSet, ePermissionType.NotSet))
        Dim _ObjectName As String = ""
        Dim _PermissionMatrix_Collection As XPCollection(Of PermissionMatrix) = New XPCollection(Of PermissionMatrix)(_ocur.Session, CriteriaOperator.Parse("1=1"))
        Dim _Role_Collection As XPCollection(Of Role) = New XPCollection(Of Role)(_ocur.Session, CriteriaOperator.Parse("Oid=Oid"))
        Dim _PersistentPermission_Collection As XPCollection = _ocur.CreateCollection(GetType(PersistentPermission), CriteriaOperator.Parse("1=1"))

        For Each _Role As Role In _Role_Collection
            For Each _PermissionMatrix As PermissionMatrix In _PermissionMatrix_Collection
                If _PermissionMatrix.Role Is _Role Then
                    Dim _PersistentPermissionXPColl As XPCollection = _ocur.CreateCollection(GetType(PersistentPermission), CriteriaOperator.Parse("Role=?", _Role))
                    For Each _PersistentPermission As PersistentPermission In _PersistentPermissionXPColl
                        If TryCast(_PersistentPermission.Permission, EditModelPermission) IsNot Nothing Then
                            _ObjectName = TryCast(_PersistentPermission.Permission, EditModelPermission).GetType.ToString
                        End If
                        If TryCast(_PersistentPermission.Permission, ObjectAccessPermission) IsNot Nothing Then
                            _ObjectName = TryCast(_PersistentPermission.Permission, ObjectAccessPermission).AccessItemList(0).ObjectTypeInfo.FullName
                        End If

                        Dim _ObjectNameArry As String() = _ObjectName.Split(New [Char]() {"."c})
                        If _ObjectNameArry(_ObjectNameArry.Length - 2) = "Win" Then
                            _ObjectName = _ObjectNameArry(_ObjectNameArry.Length - 2) & "." & _ObjectNameArry(_ObjectNameArry.Length - 1)
                        End If

                        If _ObjectName <> _PermissionMatrix.ObjectType And _ObjectNameArry(_ObjectNameArry.Length - 1) <> _PermissionMatrix.ObjectType Then
                            If _PermissionMatrix.IsCreate <> ePermissionType.NotSet And _
                               _PermissionMatrix.IsDelete <> ePermissionType.NotSet And _
                               _PermissionMatrix.IsNavigate <> ePermissionType.NotSet And _
                               _PermissionMatrix.IsRead <> ePermissionType.NotSet And _
                               _PermissionMatrix.IsWrite <> ePermissionType.NotSet Then

                                Dim _ActRole As Role = _PermissionMatrix.Role
                                Dim _ActPersistentPermission As XPCollection = _ocur.CreateCollection(GetType(PersistentPermission), CriteriaOperator.Parse("Role=?", _PermissionMatrix.Role))
                                Dim _ObjectAccessPermission As New ObjectAccessPermission
                                _ObjectAccessPermission.ObjectType = ReflectionHelper.FindType(_PermissionMatrix.ObjectType)
                                Select Case _PermissionMatrix.IsRead
                                    Case ePermissionType.Allow
                                        _ObjectAccessPermission.ReadAccess = ObjectAccessModifier.Allow
                                    Case ePermissionType.Deny
                                        _ObjectAccessPermission.ReadAccess = ObjectAccessModifier.Deny
                                End Select
                                Select Case _PermissionMatrix.IsWrite
                                    Case ePermissionType.Allow
                                        _ObjectAccessPermission.WriteAccess = ObjectAccessModifier.Allow
                                    Case ePermissionType.Deny
                                        _ObjectAccessPermission.WriteAccess = ObjectAccessModifier.Deny
                                End Select
                                Select Case _PermissionMatrix.IsNavigate
                                    Case ePermissionType.Allow
                                        _ObjectAccessPermission.NavigateAccess = ObjectAccessModifier.Allow
                                    Case ePermissionType.Deny
                                        _ObjectAccessPermission.NavigateAccess = ObjectAccessModifier.Deny
                                End Select
                                Select Case _PermissionMatrix.IsDelete
                                    Case ePermissionType.Allow
                                        _ObjectAccessPermission.DeleteAccess = ObjectAccessModifier.Allow
                                    Case ePermissionType.Deny
                                        _ObjectAccessPermission.DeleteAccess = ObjectAccessModifier.Deny
                                End Select
                                Select Case _PermissionMatrix.IsCreate
                                    Case ePermissionType.Allow
                                        _ObjectAccessPermission.CreateAccess = ObjectAccessModifier.Allow
                                    Case ePermissionType.Deny
                                        _ObjectAccessPermission.CreateAccess = ObjectAccessModifier.Deny
                                End Select

                                _ActRole.AddPermission(_ObjectAccessPermission)
                            End If
                        End If
                    Next
                End If
            Next
        Next
        'Dim _ObjectName As String = ""
        'Dim _Needpermission As Boolean = False
        'For Each _ActRole As Role In _Role_Collection
        '    For Each _MatrixElement As PermissionMatrix In _PermissionMatrix_Collection
        '        If Equals(_ActRole, _MatrixElement.Role) Then

        '            If _MatrixElement.IsCreate <> ePermissionType.NotSet And _
        '                _MatrixElement.IsDelete <> ePermissionType.NotSet And _
        '                _MatrixElement.IsNavigate <> ePermissionType.NotSet And _
        '                _MatrixElement.IsRead <> ePermissionType.NotSet And _
        '                _MatrixElement.IsWrite <> ePermissionType.NotSet Then
        '                Dim _ObjectAccessPermission As New ObjectAccessPermission

        '                Dim _ActPersistentPermission As XPCollection = _ocur.CreateCollection(GetType(PersistentPermission), CriteriaOperator.Parse("Role=?", _MatrixElement.Role))
        '                If _ActPersistentPermission.Count > 0 Then
        '                    For Each _PersistentPermission As PersistentPermission In _ActPersistentPermission
        '                        If TryCast(_PersistentPermission.Permission, EditModelPermission) IsNot Nothing Then
        '                            _ObjectName = TryCast(_PersistentPermission.Permission, EditModelPermission).GetType.ToString
        '                        End If
        '                        If TryCast(_PersistentPermission.Permission, ObjectAccessPermission) IsNot Nothing Then
        '                            _ObjectName = TryCast(_PersistentPermission.Permission, ObjectAccessPermission).AccessItemList(0).ObjectTypeInfo.FullName
        '                        End If
        '                        If _ObjectName <> _MatrixElement.ObjectType Then
        '                            _Needpermission = True
        '                        End If
        '                    Next
        '                End If
        '                If _Needpermission Then
        '                    _ObjectAccessPermission.ObjectType = ReflectionHelper.FindType(_MatrixElement.ObjectType)
        '                    Select Case _MatrixElement.IsRead
        '                        Case ePermissionType.Allow
        '                            _ObjectAccessPermission.ReadAccess = ObjectAccessModifier.Allow
        '                        Case ePermissionType.Deny
        '                            _ObjectAccessPermission.ReadAccess = ObjectAccessModifier.Deny
        '                    End Select
        '                    Select Case _MatrixElement.IsWrite
        '                        Case ePermissionType.Allow
        '                            _ObjectAccessPermission.WriteAccess = ObjectAccessModifier.Allow
        '                        Case ePermissionType.Deny
        '                            _ObjectAccessPermission.WriteAccess = ObjectAccessModifier.Deny
        '                    End Select
        '                    Select Case _MatrixElement.IsNavigate
        '                        Case ePermissionType.Allow
        '                            _ObjectAccessPermission.NavigateAccess = ObjectAccessModifier.Allow
        '                        Case ePermissionType.Deny
        '                            _ObjectAccessPermission.NavigateAccess = ObjectAccessModifier.Deny
        '                    End Select
        '                    Select Case _MatrixElement.IsDelete
        '                        Case ePermissionType.Allow
        '                            _ObjectAccessPermission.DeleteAccess = ObjectAccessModifier.Allow
        '                        Case ePermissionType.Deny
        '                            _ObjectAccessPermission.DeleteAccess = ObjectAccessModifier.Deny
        '                    End Select
        '                    Select Case _MatrixElement.IsCreate
        '                        Case ePermissionType.Allow
        '                            _ObjectAccessPermission.CreateAccess = ObjectAccessModifier.Allow
        '                        Case ePermissionType.Deny
        '                            _ObjectAccessPermission.CreateAccess = ObjectAccessModifier.Deny
        '                    End Select

        '                    _ActRole.AddPermission(_ObjectAccessPermission)
        '                    _Needpermission = False
        '                End If
        '            End If
        '        End If
        '    Next

        'Next


        'Dim _NotExistPerimissons As New XPCollection
        'For Each _PermissionMatrix As PermissionMatrix In _PermissionMatrix_Collection
        '    For Each _PersistentPermission As PersistentPermission In _PersistentPermission_Collection
        '        If TryCast(_PersistentPermission.Permission, EditModelPermission) IsNot Nothing Then
        '            _ObjectName = TryCast(_PersistentPermission.Permission, EditModelPermission).GetType.ToString
        '        End If
        '        If TryCast(_PersistentPermission.Permission, ObjectAccessPermission) IsNot Nothing Then
        '            _ObjectName = TryCast(_PersistentPermission.Permission, ObjectAccessPermission).AccessItemList(0).ObjectTypeInfo.FullName
        '        End If

        '    Next
        'Next


        'For Each _PermissionMatrix As PermissionMatrix In _PermissionMatrix_Collection
        '    For Each _PersistentPermission As PersistentPermission In _PersistentPermission_Collection
        '        If TryCast(_PersistentPermission.Permission, EditModelPermission) IsNot Nothing Then
        '            _ObjectName = TryCast(_PersistentPermission.Permission, EditModelPermission).GetType.ToString
        '        End If
        '        If TryCast(_PersistentPermission.Permission, ObjectAccessPermission) IsNot Nothing Then
        '            _ObjectName = TryCast(_PersistentPermission.Permission, ObjectAccessPermission).AccessItemList(0).ObjectTypeInfo.FullName
        '        End If

        '        If Not Equals(_PersistentPermission.Role, _PermissionMatrix.Role) Then

        '            If _PermissionMatrix.IsCreate <> ePermissionType.NotSet And _
        '                _PermissionMatrix.IsDelete <> ePermissionType.NotSet And _
        '                _PermissionMatrix.IsNavigate <> ePermissionType.NotSet And _
        '                _PermissionMatrix.IsRead <> ePermissionType.NotSet And _
        '                _PermissionMatrix.IsWrite <> ePermissionType.NotSet Then

        '                Dim _ObjectAccessPermission As New ObjectAccessPermission
        '                Dim _Role As Role = _PermissionMatrix.Role
        '                _ObjectAccessPermission.ObjectType = ReflectionHelper.FindType(_PermissionMatrix.ObjectType)
        '                Select Case _PermissionMatrix.IsRead
        '                    Case ePermissionType.Allow
        '                        _ObjectAccessPermission.ReadAccess = ObjectAccessModifier.Allow
        '                    Case ePermissionType.Deny
        '                        _ObjectAccessPermission.ReadAccess = ObjectAccessModifier.Deny
        '                End Select
        '                Select Case _PermissionMatrix.IsWrite
        '                    Case ePermissionType.Allow
        '                        _ObjectAccessPermission.WriteAccess = ObjectAccessModifier.Allow
        '                    Case ePermissionType.Deny
        '                        _ObjectAccessPermission.WriteAccess = ObjectAccessModifier.Deny
        '                End Select
        '                Select Case _PermissionMatrix.IsNavigate
        '                    Case ePermissionType.Allow
        '                        _ObjectAccessPermission.NavigateAccess = ObjectAccessModifier.Allow
        '                    Case ePermissionType.Deny
        '                        _ObjectAccessPermission.NavigateAccess = ObjectAccessModifier.Deny
        '                End Select
        '                Select Case _PermissionMatrix.IsDelete
        '                    Case ePermissionType.Allow
        '                        _ObjectAccessPermission.DeleteAccess = ObjectAccessModifier.Allow
        '                    Case ePermissionType.Deny
        '                        _ObjectAccessPermission.DeleteAccess = ObjectAccessModifier.Deny
        '                End Select
        '                Select Case _PermissionMatrix.IsCreate
        '                    Case ePermissionType.Allow
        '                        _ObjectAccessPermission.CreateAccess = ObjectAccessModifier.Allow
        '                    Case ePermissionType.Deny
        '                        _ObjectAccessPermission.CreateAccess = ObjectAccessModifier.Deny
        '                End Select

        '                '_Role.AddPermission(_ObjectAccessPermission)
        '            End If
        '        End If
        '    Next
        'Next

    End Sub
    Private Sub aCustomRoleGroupSetUp_Cancel(sender As Object, e As System.EventArgs) Handles aCustomRoleGroupSetUp.Cancel
        _noselect = False
    End Sub
    Private Sub aCustomRoleGroupSetUp_CustomizePopupWindowParams(sender As System.Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aCustomRoleGroupSetUp.CustomizePopupWindowParams
        _noselect = True
        If _selection.Count > 0 Then
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _CustomRoleGroupSetUpPopUp As CustomRoleGroupSetUpPopUp = _onew.CreateObject(Of CustomRoleGroupSetUpPopUp)()

            e.View = Application.CreateDetailView(_onew, "CustomRoleGroupSetUpPopUp_DetailView", False, _CustomRoleGroupSetUpPopUp)
        Else
            _noselect = False
            '_______________________________________TESZT MIATT____________________>>>>
            Throw New Exception("*Nincs kiválasztott sor(ok)!")
            '_______________________________________TESZT MIATT____________________<<<<

        End If
    End Sub
    Private Sub aCustomRoleGroupSetUp_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aCustomRoleGroupSetUp.Execute
        Dim _ocur As Xpo.XPObjectSpace = e.PopupWindow.View.ObjectSpace
        Dim _CustomRoleGroupSetUpPopUp As CustomRoleGroupSetUpPopUp = TryCast(e.PopupWindow.View.SelectedObjects(0), CustomRoleGroupSetUpPopUp)
        Dim _IsCreate As ePermissionType = _CustomRoleGroupSetUpPopUp.IsCreate
        Dim _IsDelete As ePermissionType = _CustomRoleGroupSetUpPopUp.IsDelete
        Dim _IsWrite As ePermissionType = _CustomRoleGroupSetUpPopUp.IsWrite
        Dim _IsRead As ePermissionType = _CustomRoleGroupSetUpPopUp.IsRead
        Dim _IsNavigate As ePermissionType = _CustomRoleGroupSetUpPopUp.IsNavigate

        For Each _PermissionMatrix As PermissionMatrix In _selection
            _PermissionMatrix.IsCreate = _IsCreate
            _PermissionMatrix.IsDelete = _IsDelete
            _PermissionMatrix.IsWrite = _IsWrite
            _PermissionMatrix.IsRead = _IsRead
            _PermissionMatrix.IsNavigate = _IsNavigate

            _PermissionMatrix.Save()
        Next
        _ocur.CommitChanges()
        _noselect = False
    End Sub
    Private Sub aCreateCustomRoleObjectTypes_CustomizePopupWindowParams(sender As System.Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aCreateCustomRoleObjectTypes.CustomizePopupWindowParams
        If View.SelectedObjects.Count > 0 Then
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _PermissionMatrix As PermissionMatrix = TryCast(View.SelectedObjects(0), PermissionMatrix)
            Dim _CreateCustomRoleObjectTypes As CreateCustomRoleObjectTypes
            Dim _FullObjectList As New ArrayList
            Dim _ActobjectList As New ArrayList
            Dim _CS As New CollectionSource(_onew, GetType(CreateCustomRoleObjectTypes))
            ' _CreateCustomRoleObjectTypes.Role = _onew.GetObject(_PermissionMatrix.Role)
            Dim _PersistentPermission As XPCollection = _onew.CreateCollection(GetType(PersistentPermission), CriteriaOperator.Parse("1=1"))


            For Each _Permission As PersistentPermission In _PersistentPermission
                If TryCast(_Permission.Permission, ObjectAccessPermission) IsNot Nothing Then
                    _FullObjectList.Add(TryCast(_Permission.Permission, ObjectAccessPermission).AccessItemList(0).ObjectTypeInfo.FullName)
                End If
                If TryCast(_Permission.Permission, EditModelPermission) IsNot Nothing Then
                    _FullObjectList.Add(TryCast(_Permission.Permission, EditModelPermission).GetType.ToString)
                End If
            Next

            For Each _Permissions As IPermission In _PermissionMatrix.Role.Permissions
                If TryCast(_Permissions, ObjectAccessPermission) IsNot Nothing Then
                    _ActobjectList.Add(_onew.GetObject(TryCast(_Permissions, ObjectAccessPermission).AccessItemList(0).ObjectTypeInfo.FullName))
                End If
                If TryCast(_Permissions, EditModelPermission) IsNot Nothing Then
                    _ActobjectList.Add(_onew.GetObject(TryCast(_Permissions, EditModelPermission).GetType.ToString))
                End If
            Next

            Dim _Exist As Boolean = False
            For Each _s In _FullObjectList
                For Each _sa In _ActobjectList
                    If _s = _sa Then
                        _Exist = True
                        Exit For
                    End If
                Next
                If Not _Exist Then
                    _CreateCustomRoleObjectTypes = _onew.CreateObject(Of CreateCustomRoleObjectTypes)()
                    _CreateCustomRoleObjectTypes.Role = _onew.GetObject(_PermissionMatrix.Role)
                    _CreateCustomRoleObjectTypes.MissingObjectType = _s

                    _cs.add(_CreateCustomRoleObjectTypes)
                End If
                _Exist = False
            Next
            e.View = Application.CreateListView("CreateCustomRoleObjectTypes_ListView", _CS, False)
        Else
            '_______________________________________TESZT MIATT____________________>>>>
            Throw New Exception("*Nincs kiválasztott sor!")
            '_______________________________________TESZT MIATT____________________<<<<
        End If
    End Sub
    Private Sub aCreateCustomRoleObjectTypes_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aCreateCustomRoleObjectTypes.Execute
        If e.PopupWindow.View.SelectedObjects.Count > 0 Then
            Dim _Role As Role = TryCast(View.SelectedObjects(0), PermissionMatrix).Role
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            For Each _CreateCustomRoleObjectTypes As CreateCustomRoleObjectTypes In e.PopupWindow.View.SelectedObjects
                Dim _PermissionMatrix As PermissionMatrix = _ocur.CreateObject(Of PermissionMatrix)()
                _PermissionMatrix.Role = _Role
                _PermissionMatrix.ObjectType = _CreateCustomRoleObjectTypes.MissingObjectType
                _PermissionMatrix.IsCreate = ePermissionType.NotSet
                _PermissionMatrix.IsDelete = ePermissionType.NotSet
                _PermissionMatrix.IsNavigate = ePermissionType.NotSet
                _PermissionMatrix.IsRead = ePermissionType.NotSet
                _PermissionMatrix.IsWrite = ePermissionType.NotSet

                _PermissionMatrix.Save()
            Next
            _ocur.CommitChanges()
        Else
            '_______________________________________TESZT MIATT____________________>>>>
            Throw New Exception("*Legalább egy sort ki kell választani!")
            '_______________________________________TESZT MIATT____________________<<<<
        End If

    End Sub
End Class
