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 DevExpress.ExpressApp.SystemModule
Imports DevExpress.ExpressApp.Web.SystemModule

Public Class SubcontractorVC
    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 Like "SubcontractorRoles_ListView_Subcontractor" Then
            Frame.GetController(Of ListViewController).EditAction.Active.SetItemValue("", False)
        End If
        If View.Id = "WorkSheet_Header_ListView_Subcontractor" Or _
           View.Id = "WorkSheet_Header_ListView_Subcontractor_Closed" Or _
           View.Id = "WorkSheet_Header_ListView_Subcontractor_Ready" Then
            Frame.GetController(Of ListViewController).EditAction.Active.SetItemValue("", False)
        End If
        If View.Id = "CRM_Opportunities_ListView_Subcontractor" Then
            Frame.GetController(Of ListViewController).EditAction.Active.SetItemValue("", False)
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _Contacts As Contacts = _ocur.FindObject(Of Contacts)(CriteriaOperator.Parse("User = ?", _ocur.GetObject(SecuritySystem.CurrentUser)))

            If _Contacts IsNot Nothing Then
                If _Contacts.Customers IsNot Nothing Then
                    TryCast(View, ListView).CollectionSource.Criteria("ByPartner") = CriteriaOperator.Parse("Customers.Oid = ?", _Contacts.Customers.Oid)
                Else
                    TryCast(View, ListView).CollectionSource.Criteria("ByPartner") = CriteriaOperator.Parse("1=2")
                End If
            Else
                TryCast(View, ListView).CollectionSource.Criteria("ByPartner") = CriteriaOperator.Parse("1=2")
            End If
        End If
        If View.Id = "Contracts_ListView_Subcontractor" Then
            Frame.GetController(Of ListViewController).EditAction.Active.SetItemValue("", False)
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _Contacts As Contacts = _ocur.FindObject(Of Contacts)(CriteriaOperator.Parse("User = ?", _ocur.GetObject(SecuritySystem.CurrentUser)))

            If _Contacts IsNot Nothing Then
                If _Contacts.Customers IsNot Nothing Then
                    TryCast(View, ListView).CollectionSource.Criteria("ByPartner") = CriteriaOperator.Parse("Customers.Oid = ?", _Contacts.Customers.Oid)
                Else
                    TryCast(View, ListView).CollectionSource.Criteria("ByPartner") = CriteriaOperator.Parse("1=2")
                End If
            Else
                TryCast(View, ListView).CollectionSource.Criteria("ByPartner") = CriteriaOperator.Parse("1=2")
            End If
        End If
        If View.Id = "OfferInHeader_ListView_Subcontractor" Then
            Frame.GetController(Of ListViewController).EditAction.Active.SetItemValue("", False)
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _Contacts As Contacts = _ocur.FindObject(Of Contacts)(CriteriaOperator.Parse("User = ?", _ocur.GetObject(SecuritySystem.CurrentUser)))

            If _Contacts IsNot Nothing Then
                If _Contacts.Customers IsNot Nothing Then
                    TryCast(View, ListView).CollectionSource.Criteria("ByPartner") = CriteriaOperator.Parse("Customers.Oid = ?", _Contacts.Customers.Oid)
                Else
                    TryCast(View, ListView).CollectionSource.Criteria("ByPartner") = CriteriaOperator.Parse("1=2")
                End If
            Else
                TryCast(View, ListView).CollectionSource.Criteria("ByPartner") = CriteriaOperator.Parse("1=2")
            End If
        End If
        If View.Id = "RegistryHeader_ListView_Subcontractor" Then
            Frame.GetController(Of ListViewController).EditAction.Active.SetItemValue("", False)
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _Contacts As Contacts = _ocur.FindObject(Of Contacts)(CriteriaOperator.Parse("User = ?", _ocur.GetObject(SecuritySystem.CurrentUser)))

            If _Contacts IsNot Nothing Then
                If _Contacts.Customers IsNot Nothing Then
                    TryCast(View, ListView).CollectionSource.Criteria("ByPartner") = CriteriaOperator.Parse("Customers.Oid = ? AND RegistryType.RegistryCRMType = 2", _Contacts.Customers.Oid)
                Else
                    TryCast(View, ListView).CollectionSource.Criteria("ByPartner") = CriteriaOperator.Parse("1=2")
                End If
            Else
                TryCast(View, ListView).CollectionSource.Criteria("ByPartner") = CriteriaOperator.Parse("1=2")
            End If
        End If
        If View.Id = "SubcontractorDocuments_ListView" Then
            Frame.GetController(Of ListViewController).EditAction.Active.SetItemValue("", False)
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id Like "SubcontractorRoles_ListView_Subcontractor" Then
            Frame.GetController(Of ListViewController).EditAction.Active.SetItemValue("", True)
        End If
        If View.Id = "WorkSheet_Header_ListView_Subcontractor" Or _
          View.Id = "WorkSheet_Header_ListView_Subcontractor_Closed" Or _
          View.Id = "WorkSheet_Header_ListView_Subcontractor_Ready" Then
            Frame.GetController(Of ListViewController).EditAction.Active.SetItemValue("", True)
        End If
        If View.Id = "CRM_Opportunities_ListView_Subcontractor" Then
            Frame.GetController(Of ListViewController).EditAction.Active.SetItemValue("", True)
        End If
        If View.Id = "Contracts_ListView_Subcontractor" Then
            Frame.GetController(Of ListViewController).EditAction.Active.SetItemValue("", True)
        End If
        If View.Id = "OfferInHeader_ListView_Subcontractor" Then
            Frame.GetController(Of ListViewController).EditAction.Active.SetItemValue("", True)
        End If
        If View.Id = "SubcontractorDocuments_ListView" Then
            Frame.GetController(Of ListViewController).EditAction.Active.SetItemValue("", True)
        End If
    End Sub
    Private Sub aSubcontractorNavigationDisable_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aSubcontractorNavigationDisable.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _uow As New UnitOfWork(_onew.Session.DataLayer)
        Dim _MasterKey As Guid = SecuritySystem.CurrentUserId

        Dim _ModelNavigation_Collection As New XPCollection(Of ModelNavigation)(_uow, CriteriaOperator.Parse("MasterKey=?", _MasterKey))
        _uow.Delete(_ModelNavigation_Collection)
        _uow.CommitChanges()

        If View.SelectedObjects.Count = 1 Then
            Dim _SubcontractorRoles As SubcontractorRoles = TryCast(View.SelectedObjects(0), SubcontractorRoles)
            If _SubcontractorRoles IsNot Nothing Then
                Dim _NavigationItemRole_Collection As New XPCollection(Of NavigationItemRole)(_onew.Session, CriteriaOperator.Parse("SubcontractorRoles.Oid=?", _SubcontractorRoles.Oid))
                Dim _NavigationItemRole As NavigationItemRole

                Dim _ModelNavigation As ModelNavigation

                Dim _IModelNavigationItems As IModelNavigationItems
                Dim _IModelNavigationItem As IModelNavigationItem

                Dim _NavigationItems As IModelRootNavigationItems = DirectCast(Application.Model, IModelApplicationNavigationItems).NavigationItems
                _IModelNavigationItems = TryCast(_NavigationItems.Items, IModelNavigationItems)
                For Each _IModelNavigationItem In _IModelNavigationItems
                    RecursiveAddModelNavigation(_uow, _MasterKey, Nothing, _IModelNavigationItem)
                Next
                _uow.CommitChanges()

                Dim _CS As CollectionSource = New CollectionSource(_onew, GetType(ModelNavigation))

                _CS.BeginUpdateCriteria()
                _CS.Criteria.Add("Filter1", CriteriaOperator.Parse("MasterKey=?", _MasterKey))
                _CS.EndUpdateCriteria()
                _CS.Sorting.Add(New SortProperty("Index", DB.SortingDirection.Ascending))

                For Each _ModelNavigation In _CS.Collection
                    For Each _NavigationItemRole In _NavigationItemRole_Collection
                        If _NavigationItemRole.ChoiceActionItemId = _ModelNavigation.Name Then
                            RecursiveDeny(_ModelNavigation)
                        End If
                    Next
                Next

                e.View = Application.CreateListView("ModelNavigation_ListView", _CS, False)
                If _SubcontractorRoles IsNot Nothing Then
                    e.View.Caption += " (" & _SubcontractorRoles.ShortName & ")"
                End If
            End If
        End If
    End Sub
    Private Sub aSubcontractorNavigationDisable_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aSubcontractorNavigationDisable.Execute
        Dim _ModelNavigation As ModelNavigation
        Dim _NavigationItemRole As NavigationItemRole
        Dim _ocur As IObjectSpace = View.ObjectSpace

        Dim _SubcontractorRoles As SubcontractorRoles

        For Each _SubcontractorRoles In View.SelectedObjects
            For Each _ModelNavigation In e.PopupWindow.View.SelectedObjects
                _NavigationItemRole = _ocur.FindObject(Of NavigationItemRole)(CriteriaOperator.Parse("SubcontractorRoles.Oid=? and ChoiceActionItemId=?", _SubcontractorRoles.Oid, _ModelNavigation.Name))
                If _NavigationItemRole Is Nothing Then
                    _NavigationItemRole = _ocur.CreateObject(Of NavigationItemRole)()
                    _NavigationItemRole.SubcontractorRoles = _ocur.GetObject(_SubcontractorRoles)
                    _NavigationItemRole.ChoiceActionItemId = _ModelNavigation.Name
                End If
            Next
        Next
        _ocur.CommitChanges()
    End Sub

    Private Sub aSubcontractorNavigationEnable_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aSubcontractorNavigationEnable.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _uow As New UnitOfWork(_onew.Session.DataLayer)
        Dim _MasterKey As Guid = SecuritySystem.CurrentUserId

        Dim _ModelNavigation_Collection As New XPCollection(Of ModelNavigation)(_uow, CriteriaOperator.Parse("MasterKey=?", _MasterKey))
        _uow.Delete(_ModelNavigation_Collection)
        _uow.CommitChanges()

        If View.SelectedObjects.Count = 1 Then
            Dim _SubcontractorRoles As SubcontractorRoles = TryCast(View.SelectedObjects(0), SubcontractorRoles)
            If _SubcontractorRoles IsNot Nothing Then
                Dim _NavigationItemRole_Collection As New XPCollection(Of NavigationItemRole)(_onew.Session, CriteriaOperator.Parse("SubcontractorRoles.Oid=?", _SubcontractorRoles.Oid))
                Dim _NavigationItemRole As NavigationItemRole

                Dim _ModelNavigation As ModelNavigation

                Dim _IModelNavigationItems As IModelNavigationItems
                Dim _IModelNavigationItem As IModelNavigationItem

                Dim _NavigationItems As IModelRootNavigationItems = DirectCast(Application.Model, IModelApplicationNavigationItems).NavigationItems
                _IModelNavigationItems = TryCast(_NavigationItems.Items, IModelNavigationItems)
                For Each _IModelNavigationItem In _IModelNavigationItems
                    RecursiveAddModelNavigation(_uow, _MasterKey, Nothing, _IModelNavigationItem)
                Next
                _uow.CommitChanges()

                Dim _CS As CollectionSource = New CollectionSource(_onew, GetType(ModelNavigation))

                _CS.BeginUpdateCriteria()
                _CS.Criteria.Add("Filter1", CriteriaOperator.Parse("MasterKey=?", _MasterKey))
                _CS.EndUpdateCriteria()
                _CS.Sorting.Add(New SortProperty("Index", DB.SortingDirection.Ascending))

                For Each _ModelNavigation In _CS.Collection
                    For Each _NavigationItemRole In _NavigationItemRole_Collection
                        If _NavigationItemRole.ChoiceActionItemId = _ModelNavigation.Name Then
                            RecursiveDeny(_ModelNavigation)
                        End If
                    Next
                Next

                e.View = Application.CreateListView("ModelNavigation_ListView", _CS, False)
                If _SubcontractorRoles IsNot Nothing Then
                    e.View.Caption += " (" & _SubcontractorRoles.ShortName & ")"
                End If
            End If
        End If
    End Sub
    Private Sub aSubcontractorNavigationEnable_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aSubcontractorNavigationEnable.Execute
        Dim _ModelNavigation As ModelNavigation
        Dim _NavigationItemRole As NavigationItemRole
        Dim _ocur As IObjectSpace = View.ObjectSpace

        Dim _SubcontractorRoles As SubcontractorRoles

        For Each _SubcontractorRoles In View.SelectedObjects
            For Each _ModelNavigation In e.PopupWindow.View.SelectedObjects
                _NavigationItemRole = _ocur.FindObject(Of NavigationItemRole)(CriteriaOperator.Parse("SubcontractorRoles.Oid=? and ChoiceActionItemId=?", _SubcontractorRoles.Oid, _ModelNavigation.Name))
                If _NavigationItemRole IsNot Nothing Then
                    _ocur.Delete(_NavigationItemRole)
                End If
            Next
        Next
        _ocur.CommitChanges()
    End Sub

    Sub RecursiveAddModelNavigation(_uow As UnitOfWork, _MasterKey As Guid, _ModelNavigationParent As ModelNavigation, _IModelNavigationItem As IModelNavigationItem)
        Dim _ModelNavigation As ModelNavigation
        Dim _IModelNavigationItem_Child As IModelNavigationItem

        _ModelNavigation = New ModelNavigation(_uow)

        _ModelNavigation.Parent = _ModelNavigationParent
        _ModelNavigation.Name = _IModelNavigationItem.Id
        _ModelNavigation.Navigation_Caption = _IModelNavigationItem.Caption
        _ModelNavigation.Navigation_ImageName = _IModelNavigationItem.ImageName
        _ModelNavigation.MasterKey = _MasterKey
        'If _IModelNavigationItem.Index IsNot Nothing Then
        '    _ModelNavigation.Index = CLng(_IModelNavigationItem.Index)
        'Else

        'End If


        _uow.CommitChanges()

        If _IModelNavigationItem.Items.Count > 0 Then
            For Each _IModelNavigationItem_Child In _IModelNavigationItem.Items
                RecursiveAddModelNavigation(_uow, _MasterKey, _ModelNavigation, _IModelNavigationItem_Child)
            Next
        End If

    End Sub
    Sub RecursiveDeny(_ModelNavigation As ModelNavigation)
        Dim _ModelNavigation_Children As ModelNavigation
        _ModelNavigation.Navigation_Enabled = False
        If _ModelNavigation.Children.Count > 0 Then
            For Each _ModelNavigation_Children In _ModelNavigation.Children
                RecursiveDeny(_ModelNavigation_Children)
            Next
        End If
    End Sub

    Private Sub aSubcontractorDocuments_OpenDashboardWithViewer_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aSubcontractorDocuments_OpenDashboardWithViewer.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _SubcontractorDocuments As SubcontractorDocuments = TryCast(View.SelectedObjects(0), SubcontractorDocuments)

        If _SubcontractorDocuments IsNot Nothing Then
            Dim _FileName As String = System.IO.Path.Combine(System.IO.Path.GetTempPath(), System.IO.Path.GetTempPath, _SubcontractorDocuments.File.FileName)
            Dim _StreamFile As System.IO.Stream = New System.IO.FileStream(_FileName, System.IO.FileMode.Create, System.IO.FileAccess.Write)
            _SubcontractorDocuments.File.SaveToStream(_StreamFile)
            _StreamFile.Close()

            If IO.File.Exists(_FileName) Then
                GLOBAL_DASHBOARD_XML_DashboardXmlFile = _FileName
                GLOBAL_DASHBOARD_XML_UserName = _SubcontractorDocuments.XML_UserName
                GLOBAL_DASHBOARD_XML_Password = _SubcontractorDocuments.XML_Password
                GLOBAL_DASHBOARD_XML_ServerName = _SubcontractorDocuments.XML_ServerName
                GLOBAL_DASHBOARD_XML_DatabaseName = _SubcontractorDocuments.XML_DatabaseName
                Select Case _SubcontractorDocuments.XML_SqlType
                    Case eSQLType.MSSQL
                        GLOBAL_DASHBOARD_XML_SQLType = "MSSQL"
                    Case eSQLType.MySQL
                        GLOBAL_DASHBOARD_XML_SQLType = "MYSQL"
                    Case eSQLType.PostgreSQL
                        GLOBAL_DASHBOARD_XML_SQLType = "PSQL"
                End Select

                DevExpress.ExpressApp.Web.WebWindow.CurrentRequestWindow.RegisterStartupScript("ShowCustomFormScriptKey", String.Format("window.open('{0}');", "Controls/DashboardViewer.aspx"))
            End If
        End If
    End Sub
End Class
