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.Model

Imports DevExpress.ExpressApp.SystemModule
Imports DevExpress.Xpo
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.Data.Filtering

Public Class LockObjectBeforeeditVC
    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 TryCast(View, ListView) IsNot Nothing Then
            Dim _ListView As ListView = TryCast(View, ListView)
            Dim _IModelClass As IModelClass

            _IModelClass = _ListView.Model.ModelClass
            If TryCast(_IModelClass, IModelClassAllowSort).LockObjectBeforeEdit = True Then
                AddHandler Frame.GetController(Of ListViewProcessCurrentObjectController).CustomProcessSelectedItem, AddressOf ListView_CustomProcessSelectedItem

            End If
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If TryCast(View, DetailView) IsNot Nothing Then
            '// DetailView után a lock felszabadítása !
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _uow_lock As New UnitOfWork(_onew.Session.DataLayer)
            Dim _User As User = _onew.GetObject(TryCast(SecuritySystem.CurrentUser, User))
            Dim _IModelClass As IModelClass = TryCast(View, DetailView).Model.ModelClass
            If TryCast(_IModelClass, IModelClassAllowSort).LockObjectBeforeEdit = True Then
                If View.SelectedObjects.Count > 0 Then
                    Dim _ActBusinessObject As Object = TryCast(View.SelectedObjects(0), Object)
                    LockingModule.UnLockObject(_uow_lock, _IModelClass.TypeInfo.Type, _ActBusinessObject.Oid.ToString, _User)
                End If
            End If
        End If
        If TryCast(View, ListView) IsNot Nothing Then
            Dim _ListView As ListView = TryCast(View, ListView)
            Dim _IModelClass As IModelClass

            _IModelClass = _ListView.Model.ModelClass
            If TryCast(_IModelClass, IModelClassAllowSort).LockObjectBeforeEdit = True Then
                RemoveHandler Frame.GetController(Of ListViewProcessCurrentObjectController).CustomProcessSelectedItem, AddressOf ListView_CustomProcessSelectedItem
            End If
        End If
    End Sub

    Private Sub ListView_CustomProcessSelectedItem(sender As Object, e As CustomProcessListViewSelectedItemEventArgs)
        '// Objektum lockolása, ha nem megy akkor e.handled=True !
        Try
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _uow_lock As New UnitOfWork(_onew.Session.DataLayer)
            Dim _User As User = _onew.GetObject(TryCast(SecuritySystem.CurrentUser, User))
            Dim _IModelClass As IModelClass = TryCast(View, ListView).Model.ModelClass
            Dim _ActBusinessObject As Object = TryCast(View.SelectedObjects(0), Object)

            If Not LockingModule.LockObject(_uow_lock, _IModelClass.TypeInfo.Type, _ActBusinessObject.Oid.ToString, _User) Then
                e.Handled = True
                Dim _LockingObject As LockingObject = _onew.FindObject(Of LockingObject)(CriteriaOperator.Parse("ObjectKey=?", _ActBusinessObject.Oid.ToString))
                If _LockingObject IsNot Nothing Then
                    MsgBox(String.Format(TryCast(_IModelClass, IModelClassAllowSort).LockObjectBeforeEditErrorString, _LockingObject.User.FullName, _LockingObject.ObjectCreated), MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "Hiba!")
                Else

                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Hiba!")
        End Try
    End Sub

End Class
