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

Public Class Technologies_VC
    Inherits DevExpress.ExpressApp.ViewController

    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)
    End Sub
#Region "Overrides"
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        If View.Id = "Technologies_Products_ListView" Then
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
        End If
        If View.Id = "Technologies_Services_ListView" Then
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
        End If
        If View.Id = "Technologies_Customers_ListView" Then
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
        End If
        If View.Id = "Technologies_Contacts_ListView" Then
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", False)
        End If
        If View.Id = "Technologies_Technologies_BusinessDirectory_ListView" Then
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController)().Active.SetItemValue("", False)
            ' Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", False)
            'Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "Technologies_Products_ListView" Then
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
        End If
        If View.Id = "Technologies_Services_ListView" Then
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
        End If
        If View.Id = "Technologies_Customers_ListView" Then
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
        End If
        If View.Id = "Technologies_Contacts_ListView" Then
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", True)
        End If
        If View.Id = "Technologies_Technologies_BusinessDirectory_ListView" Then
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController)().Active.SetItemValue("", True)
            'Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", True)
            'Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
        End If
    End Sub
#End Region
    Private Sub aTechnologies_AddCustomers_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aTechnologies_AddCustomers.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _Customers_CollectionSource As New CollectionSource(_onew, GetType(Customers))

        e.View = Application.CreateListView("Customers_ListView", _Customers_CollectionSource, True)
    End Sub
    Private Sub aTechnologies_AddCustomers_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aTechnologies_AddCustomers.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)

        Dim _Technologies As Technologies = TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, Technologies)
        For Each _Customers As Customers In e.PopupWindow.View.SelectedObjects
            Dim _TechnologiesCustomers As New TechnologiesCustomers(_ocur.Session)
            With _TechnologiesCustomers
                .Customers = _ocur.GetObject(_Customers)
                .Technologies = _Technologies
            End With
        Next
        _ocur.CommitChanges()

        Dim nestedFrame As NestedFrame = TryCast(Frame, NestedFrame)
        If nestedFrame IsNot Nothing Then
            Dim parentView As View = nestedFrame.View
            parentView.ObjectSpace.Refresh()
        End If
    End Sub
    Private Sub aTechnologies_AddProducts_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aTechnologies_AddProducts.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _Products_CollectionSource As New CollectionSource(_onew, GetType(Products))

        e.View = Application.CreateListView("Products_ListView_Find", _Products_CollectionSource, True)
    End Sub
    Private Sub aTechnologies_AddProducts_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aTechnologies_AddProducts.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _Technologies As Technologies = TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, Technologies)
        For Each _Products As Products In e.PopupWindow.View.SelectedObjects
            Dim _TechnologiesProducts As New TechnologiesProducts(_ocur.Session)
            With _TechnologiesProducts
                .Products = _ocur.GetObject(_Products)
                .Technologies = _Technologies
            End With
        Next
        _ocur.CommitChanges()

        Dim nestedFrame As NestedFrame = TryCast(Frame, NestedFrame)
        If nestedFrame IsNot Nothing Then
            Dim parentView As View = nestedFrame.View
            parentView.ObjectSpace.Refresh()
        End If
    End Sub
    Private Sub aTechnologies_AddServices_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aTechnologies_AddServices.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _Services_CollectionSource As New CollectionSource(_onew, GetType(Services))

        e.View = Application.CreateListView("Services_ListView", _Services_CollectionSource, True)
    End Sub
    Private Sub aTechnologies_AddServices_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aTechnologies_AddServices.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)

        Dim _Technologies As Technologies = TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, Technologies)
        For Each _Services As Services In e.PopupWindow.View.SelectedObjects
            Dim _TechnologiesServices As New TechnologiesServices(_ocur.Session)
            With _TechnologiesServices
                .Services = _ocur.GetObject(_Services)
                .Technologies = _Technologies
            End With
        Next
        _ocur.CommitChanges()

        Dim nestedFrame As NestedFrame = TryCast(Frame, NestedFrame)
        If nestedFrame IsNot Nothing Then
            Dim parentView As View = nestedFrame.View
            parentView.ObjectSpace.Refresh()
        End If
    End Sub
    Private Sub aTechnologies_RemoveServices_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aTechnologies_RemoveServices.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _Technologies As Technologies = TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, Technologies)
        Dim _Del_Collection As New ArrayList

        For Each _Services As Services In View.SelectedObjects
            Dim _TechnologiesServices As TechnologiesServices = _ocur.FindObject(Of TechnologiesServices)(CriteriaOperator.Parse("Technologies=? AND Services=?", _Technologies, _Services))

            If _TechnologiesServices IsNot Nothing Then _Del_Collection.Add(_TechnologiesServices)
        Next

        _ocur.Delete(_Del_Collection)
        _ocur.CommitChanges()

        Dim nestedFrame As NestedFrame = TryCast(Frame, NestedFrame)
        If nestedFrame IsNot Nothing Then
            Dim parentView As View = nestedFrame.View
            parentView.ObjectSpace.Refresh()
        End If
    End Sub
    Private Sub aTechnologies_RemoveCustomers_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aTechnologies_RemoveCustomers.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _Technologies As Technologies = TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, Technologies)
        Dim _Del_Collection As New ArrayList

        For Each _Customers As Customers In View.SelectedObjects
            Dim _TechnologiesCustomers As TechnologiesCustomers = _ocur.FindObject(Of TechnologiesCustomers)(CriteriaOperator.Parse("Technologies=? AND Customers=?", _Technologies, _Customers))

            If _TechnologiesCustomers IsNot Nothing Then _Del_Collection.Add(_TechnologiesCustomers)

            Dim _TechnologiesContacts_Collection As New DevExpress.Xpo.XPCollection(Of TechnologiesContacts)(_ocur.Session, CriteriaOperator.Parse("Technologies=? AND Contacts.Customers=?", _Technologies, _Customers))

            If _TechnologiesContacts_Collection.Count > 0 Then _Del_Collection.AddRange(_TechnologiesContacts_Collection)
        Next

        _ocur.Delete(_Del_Collection)
        _ocur.CommitChanges()

        Dim nestedFrame As NestedFrame = TryCast(Frame, NestedFrame)
        If nestedFrame IsNot Nothing Then
            Dim parentView As View = nestedFrame.View
            parentView.ObjectSpace.Refresh()
        End If
    End Sub
    Private Sub aTechnologies_RemoveProducts_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aTechnologies_RemoveProducts.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _Technologies As Technologies = TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, Technologies)
        Dim _Del_Collection As New ArrayList

        For Each _Products As Products In View.SelectedObjects
            Dim _TechnologiesProducts As TechnologiesProducts = _ocur.FindObject(Of TechnologiesProducts)(CriteriaOperator.Parse("Technologies=? AND Products=?", _Technologies, _Products))

            If _TechnologiesProducts IsNot Nothing Then _Del_Collection.Add(_TechnologiesProducts)
        Next

        _ocur.Delete(_Del_Collection)
        _ocur.CommitChanges()

        Dim nestedFrame As NestedFrame = TryCast(Frame, NestedFrame)
        If nestedFrame IsNot Nothing Then
            Dim parentView As View = nestedFrame.View
            parentView.ObjectSpace.Refresh()
        End If
    End Sub

    Private Sub aTechnologies_AddContacts_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aTechnologies_AddContacts.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _Technologies As Technologies = TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, Technologies)

        If _Technologies IsNot Nothing Then
            Dim _Contacts_CollectionSource As New CollectionSource(_onew, GetType(Contacts))

            Dim _Criteria As String = "Customers.Oid in('"

            For Each _Customers As Customers In _Technologies.Customers
                _Criteria += _Customers.Oid.ToString + "','"
            Next

            If _Criteria.Length < 20 Then
                _Criteria = "1=2"
            Else
                _Criteria = _Criteria.Remove(_Criteria.Length - 2, 2)
                _Criteria += ")"
            End If

            _Contacts_CollectionSource.BeginUpdateCriteria()
            _Contacts_CollectionSource.Criteria.Add("First", CriteriaOperator.Parse(_Criteria))
            _Contacts_CollectionSource.EndUpdateCriteria()

            e.View = Application.CreateListView("Contacts_ListView", _Contacts_CollectionSource, True)
        End If
    End Sub

    Private Sub aTechnologies_AddContacts_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aTechnologies_AddContacts.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)

        Dim _Technologies As Technologies = TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, Technologies)
        For Each _Contacts As Contacts In e.PopupWindow.View.SelectedObjects
            Dim _TechnologiesContacts As New TechnologiesContacts(_ocur.Session)
            With _TechnologiesContacts
                .Contacts = _ocur.GetObject(_Contacts)
                .Technologies = _Technologies
            End With
        Next
        _ocur.CommitChanges()

        Dim nestedFrame As NestedFrame = TryCast(Frame, NestedFrame)
        If nestedFrame IsNot Nothing Then
            Dim parentView As View = nestedFrame.View
            parentView.ObjectSpace.Refresh()
        End If
    End Sub
End Class
