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

Public Module LockingModule
    Public Function LockObject(_Paramuow As UnitOfWork, _ParamObjectType As Type, _ParamObjectKey As String, _ParamUser As User) As Boolean
        UnLockObject(_Paramuow, _ParamObjectType, _ParamObjectKey, _ParamUser)
        Dim _IsLocked As Boolean = False
        Dim _TableName As String
        Dim _LockingObject As LockingObject
        Try
            _TableName = _ParamObjectType.Name & "-" & _ParamObjectKey
            _LockingObject = New LockingObject(_Paramuow)
            _LockingObject.TableName = _TableName
            _LockingObject.User = _Paramuow.GetObjectByKey(Of User)(_ParamUser.Oid)
            _LockingObject.ObjectKey = _ParamObjectKey
            _LockingObject.ObjectType = _ParamObjectType
            _LockingObject.ObjectCreated = Now
            _LockingObject.SessionCreated = _Paramuow.ToString
            _Paramuow.CommitChanges()

            _LockingObject = _Paramuow.FindObject(Of LockingObject)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                                CriteriaOperator.Parse("TableName=? and User=? and SessionCreated=?", _TableName, _Paramuow.GetObjectByKey(Of User)(_ParamUser.Oid), _Paramuow.ToString))
            If _LockingObject IsNot Nothing Then
                _IsLocked = True
            End If

        Catch ex As Exception
            _IsLocked = False
        End Try
        Return _IsLocked
    End Function
    Public Function UnLockObject(_Paramuow As UnitOfWork, _ParamObjectType As Type, _ParamObjectKey As String, _ParamUser As User) As Boolean
        Dim _TableName As String = _ParamObjectType.Name & "-" & _ParamObjectKey
        Dim _LockingObject As LockingObject = _Paramuow.FindObject(Of LockingObject)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                   CriteriaOperator.Parse("TableName=? AND User=? AND ObjectKey=?", _TableName, _Paramuow.GetObjectByKey(Of DevExpress.Persistent.BaseImpl.User)(_ParamUser.Oid), _ParamObjectKey))
        If _LockingObject IsNot Nothing Then
            _Paramuow.Delete(_LockingObject)
            _Paramuow.CommitChanges()
        End If
    End Function
    Public Function ReleaseAllLocking(_Paramuow As UnitOfWork, _Paramuser As User) As Boolean
        Dim _LockingObject_Collection As New XPCollection(Of LockingObject)(_Paramuow, CriteriaOperator.Parse("User.Oid=?", _Paramuser.Oid))
        Dim _IsUnlocked As Boolean = False
        Try
            _Paramuow.Delete(_LockingObject_Collection)
            _Paramuow.CommitChanges()

            _LockingObject_Collection = New XPCollection(Of LockingObject)(_Paramuow, CriteriaOperator.Parse("User.Oid=?", _Paramuser.Oid))
            If _LockingObject_Collection.Count = 0 Then
                _IsUnlocked = True
            End If
        Catch ex As Exception
            _IsUnlocked = False
        End Try
        Return _IsUnlocked
    End Function
End Module
