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



Public Class OperationalLog_VC
    Inherits DevExpress.ExpressApp.ViewController

    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)
    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()

        If View.Id = "OperationalLog_ListView_Subconractor" Or
            View.Id = "OperationalLog_ListView_Subconractor_HFR" Then
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)

            Dim _User As User
            If _ocur IsNot Nothing Then
                _User = _ocur.GetObjectByKey(Of User)(_ocur.GetKeyValue(SecuritySystem.CurrentUser))
            End If
            Dim _Contacts As Contacts = _ocur.FindObject(Of Contacts)(CriteriaOperator.Parse("User=?", _User))
            Dim _Subcontractor As Subcontractor = _ocur.FindObject(Of Subcontractor)(CriteriaOperator.Parse("Customers=?", _Contacts.Customers))

            Dim _OperationalLog_CollectionSource As CollectionSource = TryCast(View, ListView).CollectionSource
            Dim _Criteria As String = "WorkPlace.Oid in('"
            If _Subcontractor.SubcontractorCustomers.Count > 0 Then
                For Each _SubcontractorCustomers As SubcontractorCustomers In _Subcontractor.SubcontractorCustomers
                    If _SubcontractorCustomers.Workplace IsNot Nothing Then
                        _Criteria += _SubcontractorCustomers.Workplace.Oid.ToString + "','"
                    End If
                Next
            Else
                _Criteria = "1=2"
            End If
            If _Criteria.Length > 18 Then
                _Criteria = _Criteria.Remove(_Criteria.Length - 2, 2)
                _Criteria += ")"
            Else
                _Criteria = "1=2"
            End If

            _OperationalLog_CollectionSource.BeginUpdateCriteria()
            _OperationalLog_CollectionSource.Criteria("First") = CriteriaOperator.Parse(_Criteria)
            _OperationalLog_CollectionSource.EndUpdateCriteria()

        End If
        If View.Id = "OperationalLog_ListView_Support" Then

            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)

            Dim _User As User
            If _ocur IsNot Nothing Then
                _User = _ocur.GetObjectByKey(Of User)(_ocur.GetKeyValue(SecuritySystem.CurrentUser))
            End If
            Dim _Contacts As Contacts = _ocur.FindObject(Of Contacts)(CriteriaOperator.Parse("User=?", _User))
            Dim _Workplace As WorkPlace = _ocur.FindObject(Of WorkPlace)(CriteriaOperator.Parse("Customers=? and OperationalLogType='General'", _Contacts.Customers))

            Dim _OperationalLog_CollectionSource As CollectionSource = TryCast(View, ListView).CollectionSource
            Dim _Criteria As String = ""
            If _Workplace IsNot Nothing Then
                _Criteria = "WorkPlace.Oid='" + _Workplace.Oid.ToString + "'"
            Else
                _Criteria = "1=2"
            End If

            _OperationalLog_CollectionSource.BeginUpdateCriteria()
            _OperationalLog_CollectionSource.Criteria("First") = CriteriaOperator.Parse(_Criteria)
            _OperationalLog_CollectionSource.EndUpdateCriteria()

        End If
        If View.Id = "OperationalLog_ListView_Support_HFR" Then
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)

            Dim _User As User
            If _ocur IsNot Nothing Then
                _User = _ocur.GetObjectByKey(Of User)(_ocur.GetKeyValue(SecuritySystem.CurrentUser))
            End If
            Dim _Contacts As Contacts = _ocur.FindObject(Of Contacts)(CriteriaOperator.Parse("User=?", _User))
            Dim _Workplace As WorkPlace = _ocur.FindObject(Of WorkPlace)(CriteriaOperator.Parse("Customers=? and OperationalLogType='HFR'", _Contacts.Customers))

            Dim _OperationalLog_CollectionSource As CollectionSource = TryCast(View, ListView).CollectionSource
            Dim _Criteria As String = ""
            If _Workplace IsNot Nothing Then
                _Criteria = "WorkPlace.Oid='" + _Workplace.Oid.ToString + "'"
            Else
                _Criteria = "1=2"
            End If

            _OperationalLog_CollectionSource.BeginUpdateCriteria()
            _OperationalLog_CollectionSource.Criteria("First") = CriteriaOperator.Parse(_Criteria)
            _OperationalLog_CollectionSource.EndUpdateCriteria()
        End If
        If View.Id = "OperationalLog_OperationalLogDocuments_ListView" Then
            Frame.GetController(Of CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of ListViewProcessCurrentObjectController)().Active.SetItemValue("", False)
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", False)
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()

        If View.Id = "OperationalLog_ListView_Subconractor" Then

        End If
        If View.Id = "OperationalLog_OperationalLogDocuments_ListView" Then
            Frame.GetController(Of CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of ListViewProcessCurrentObjectController)().Active.SetItemValue("", True)
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", True)
        End If
    End Sub

    Private Sub aOperationLog_CreateQuestion_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aOperationLog_CreateQuestion.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _User As DevExpress.Persistent.BaseImpl.User = _onew.GetObjectByKey(Of DevExpress.Persistent.BaseImpl.User)(_onew.Session.GetKeyValue(SecuritySystem.CurrentUser))

        Dim _Question As New Question(_onew.Session)
        With _Question
            .AliasUser = _User
        End With

        e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "Question_DetailView_User", True, _Question)
    End Sub
End Class
