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.ExpressApp.Win.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
            AddHandler Frame.GetController(Of OpenObjectController).CustomOpenObject, AddressOf ListView_CustomOpenObject
        End If
    End Sub
    Private Sub ListView_CustomOpenObject(sender As Object, e As CustomOpenObjectEventArgs)
        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 _ActBusinessObject As Object = e.ObjectToOpen

            If TryCast(Application.Model.BOModel.GetNode(e.ObjectToOpen.GetType().FullName), IModelClassAllowSort).LockObjectBeforeEdit = True Then
                If Not LockingModule.LockObject(_uow_lock, e.ObjectToOpen.GetType, _ActBusinessObject.Oid.ToString, _User) Then
                    e.Handled = True
                    Dim _LockingObject As LockingObject = _onew.FindObject(Of LockingObject)(CriteriaOperator.Parse("ObjectKey=?", _ActBusinessObject.Oid.ToString))
                    Throw New Exception(String.Format(TryCast(Application.Model.BOModel.GetNode(e.ObjectToOpen.GetType().FullName), IModelClassAllowSort).LockObjectBeforeEditErrorString, _
                                                      _LockingObject.User.FullName, _LockingObject.ObjectCreated))
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "Hiba!")
        End Try
    End Sub

End Class
