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.ExpressApp.SystemModule
Imports DevExpress.Xpo
Imports DevExpress.Data.Filtering


Public Class NavigationItemPermission
    Inherits DevExpress.ExpressApp.WindowController
    Private _navigationController As ShowNavigationItemController
    Private _ID As Long = 1
    Public Sub New()
        MyBase.New()

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

    End Sub
    Protected Overrides Sub OnFrameAssigned()

        MyBase.OnFrameAssigned()
        '_navigationController = Frame.GetController(Of ShowNavigationItemController)()
        'If _navigationController IsNot Nothing Then
        '    AddHandler _navigationController.ItemsInitialized, AddressOf ItemsInitialized
        'End If
    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
    End Sub

    Private Sub ItemsInitialized(sender As Object, e As EventArgs)
        Dim _ChoiceActionItem As ChoiceActionItem
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _uow As New UnitOfWork(_onew.Session.DataLayer)
        Dim _NavigationItemRole_Collection As XPCollection(Of NavigationItemRole) = New XPCollection(Of NavigationItemRole)(_onew.Session, CriteriaOperator.Parse("1=1"))


        _uow.Delete(New XPCollection(Of ModelNavigation)(_uow))
        _uow.CommitChanges()

        For Each _ChoiceActionItem In _navigationController.ShowNavigationItemAction.Items

            RecursiveSetPermission(_ChoiceActionItem, _NavigationItemRole_Collection)
            If SecuritySystem.CurrentUserName = "SysAdmin" Then
                RecursiveAddModelNavigation(_uow, Nothing, _ChoiceActionItem)
            End If
        Next
        _uow.CommitChanges()

    End Sub

    Sub RecursiveSetPermission(_ChoiceActionItem As ChoiceActionItem, _NavigationItemRole_Collection As XPCollection(Of NavigationItemRole))
        Dim _NavigationItemRole As NavigationItemRole
        Dim _ChoiceActionItem_Child As ChoiceActionItem

        If _ChoiceActionItem.Items.Count > 0 Then
            For Each _ChoiceActionItem_Child In _ChoiceActionItem.Items
                RecursiveSetPermission(_ChoiceActionItem_Child, _NavigationItemRole_Collection)
            Next
        End If
        If _ChoiceActionItem.Id = "@NavigationItemPermission" Then
            If SecuritySystem.CurrentUserName <> "SysAdmin" Then
                _ChoiceActionItem.Active.SetItemValue("", False)
            End If
        End If
        For Each _NavigationItemRole In _NavigationItemRole_Collection
            If _NavigationItemRole.ChoiceActionItemId = _ChoiceActionItem.Id Then
                If _NavigationItemRole.Role IsNot Nothing Then
                    If SecuritySystemHelper.IsUserInRoleCurrentUser(_NavigationItemRole.Role.Name) Then
                        _ChoiceActionItem.Active.SetItemValue("", False)
                    End If
                End If
                If _NavigationItemRole.SubcontractorRoles IsNot Nothing Then
                    Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
                    Dim _SubcontractorUserRoles_Collection As New XPCollection(Of SubcontractorUserRoles)(_onew.Session, CriteriaOperator.Parse("User.Oid=?", TryCast(SecuritySystem.CurrentUser, DevExpress.Persistent.BaseImpl.User).Oid))

                    For Each _SubcontractorUserRoles As SubcontractorUserRoles In _SubcontractorUserRoles_Collection
                        If _NavigationItemRole.SubcontractorRoles.Oid = _SubcontractorUserRoles.SubcontractorRoles.Oid Then
                            _ChoiceActionItem.Active.SetItemValue("", False)
                        End If
                    Next
                End If
                If _NavigationItemRole.SupportRoles IsNot Nothing Then
                    Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
                    Dim _SupportUserRoles_Collection As New XPCollection(Of SupportUserRoles)(_onew.Session, CriteriaOperator.Parse("User.Oid=?", TryCast(SecuritySystem.CurrentUser, DevExpress.Persistent.BaseImpl.User).Oid))

                    For Each _SupportUserRoles As SupportUserRoles In _SupportUserRoles_Collection
                        If _NavigationItemRole.SupportRoles.Oid = _SupportUserRoles.SupportRoles.Oid Then
                            _ChoiceActionItem.Active.SetItemValue("", False)
                        End If
                    Next
                End If
            End If
        Next

    End Sub
    Sub RecursiveAddModelNavigation(_uow As UnitOfWork, _ModelNavigationParent As ModelNavigation, _ChoiceActionItem As ChoiceActionItem)
        Dim _ModelNavigation As ModelNavigation
        Dim _ChoiceActionItem_Child As ChoiceActionItem

        _ID += 1
        _ModelNavigation = New ModelNavigation(_uow)

        _ModelNavigation.Parent = _ModelNavigationParent
        _ModelNavigation.Name = _ChoiceActionItem.Id
        _ModelNavigation.Navigation_Caption = _ChoiceActionItem.Caption
        _ModelNavigation.Navigation_ImageName = _ChoiceActionItem.ImageName

        If _ChoiceActionItem.Items.Count > 0 Then
            For Each _ChoiceActionItem_Child In _ChoiceActionItem.Items
                RecursiveAddModelNavigation(_uow, _ModelNavigation, _ChoiceActionItem_Child)
            Next
        End If

    End Sub
End Class

