16Nov/056
Domino Document Locking Class
Updated version of Document Locking Class is now available.
To easy the pain of working with document locking I created a class that makes life a litlle easier. Here is the list of methods:
Public Sub new() Public Function LockingEnabled() As Boolean Public Function IsDocumentLockedByCurrentUser (doc As NotesDocument ) As Boolean Public Function IsUIDocumentLockedByCurrentUser (uiDoc As NotesUIDocument ) As Boolean Public Function LockUIDocument (uiDoc As NotesUIDocument ) As Boolean Public Function LockDocument (doc As NotesDocument ) As Boolean Public Function UnLockUIDocument (uiDoc As NotesUIDocument ) As Boolean Public Function UnLockDocument (doc As NotesDocument ) As Boolean
So why the class? Easy it did not want have to worry about the if the documents was new, if locking was enabled, and if the document was already locked by the user and I try to lock it again. There is not much else to the code.
So check it out.
Domino Document Locking
%INCLUDE "LSCONST.LSS" '/** ' * A rapper around the built-in document locking that is available in Domino 6. ' * The purpose is to provide a better more consistant way of locking and unlocking documents. ' * Will not try and lock a new document since it can not be locked. Will only show warnings once ' * ' * @author Chad Schelfhout http://www.chadsmiley.com/ ' * @version 1.0 ' */ Class DominoDocumentLocking Private DATABASE_LOCKING_NOT_ENABLED As String Private DATABASE_LOCKING_NOT_ENABLED_TITLE As String Private DOCUMENT_LOCKED As String Private DOCUMENT_LOCKED_TITLE As String Private Enabled As Boolean Private AlreadyLocked As Boolean 'Flag used so user is only informed once that database locking is not enabled Private DatabasePrompted As Boolean Public Sub new() DATABASE_LOCKING_NOT_ENABLED = "Database locking has not been enabled, please call Support Center" DATABASE_LOCKING_NOT_ENABLED_TITLE = "Locking Not Enabled" DOCUMENT_LOCKED = "Unable to lock, currently locked by: " DOCUMENT_LOCKED_TITLE = "Document Locked" DatabasePrompted = False AlreadyLocked = False Enabled = False End Sub '/** ' * Determines if database locking is enabled. ' */ Public Function LockingEnabled() As Boolean Dim Session As New NotesSession If session.CurrentDatabase.IsDocumentLockingEnabled Then Enabled = True Else Enabled = False If Not DatabasePrompted Then Msgbox DATABASE_LOCKING_NOT_ENABLED , MB_ICONSTOP , DATABASE_LOCKING_NOT_ENABLED_TITLE DatabasePrompted = True End If End If LockingEnabled = Enabled End Function '/** ' * Determines if the document is locked ' */ Private Function HasLock( doc As NotesDocument ) As Boolean 'if there is no text then it is not locked HasLock = Len( doc.LockHolders(0) ) > 0 End Function '/** ' * Determines if the document is locked but the current user. ' */ Private Function CurrentUserHasLock( doc As NotesDocument ) As Boolean CurrentUserHasLock = HasLock ( doc ) If CurrentUserHasLock Then If doc.LockHolders(0) = Doc.ParentDatabase.Parent.UserName Then CurrentUserHasLock = True Else 'Not the current user. CurrentUserHasLock = False End If End If End Function '/** ' * Determines if the document is locked but the current user. ' * This is where all the logic is ' */ Public Function IsDocumentLockedByCurrentUser (doc As NotesDocument ) As Boolean IsDocumentLockedByCurrentUser = LockingEnabled() If IsDocumentLockedByCurrentUser Then IsDocumentLockedByCurrentUser = CurrentUserHasLock( doc ) Else 'Locking not enabled IsDocumentLockedByCurrentUser = False End If End Function '/** ' * Determines if the UI document is locked but the current user. ' */ Public Function IsUIDocumentLockedByCurrentUser (uiDoc As NotesUIDocument ) As Boolean 'Check to make sure that there is a document, If there is none it is a new document IsUIDocumentLockedByCurrentUser = ( uiDoc.Document Is Nothing ) If IsUIDocumentLockedByCurrentUser Then IsUIDocumentLockedByCurrentUser= IsDocumentLockedByCurrentUser( uiDoc.Document ) End If End Function '/** ' * Determines if the the document is locked ' * This is where all the logic is ' */ Function IsDocumentLocked (doc As NotesDocument ) As Boolean isDocumentLocked = LockingEnabled() If isDocumentLocked Then isDocumentLocked = HasLock( doc ) Else 'Locking is not enabled isDocumentLocked = False End If End Function '/** ' * Determines if the UI document is locked ' */ Function IsUIDocumentLocked (uiDoc As NotesUIDocument ) As Boolean 'Check to make sure that there is a document, If there is none it is a new document If ( uiDoc.Document Is Nothing ) Then IsUIDocumentLocked = False Else IsUIDocumentLocked= IsDocumentLocked( uiDoc.Document ) End If End Function '/** ' * Locks the UI document ' */ Public Function LockUIDocument (uiDoc As NotesUIDocument ) As Boolean 'Check to make sure that there is a document, If there is none it is a new document LockUIDocument = ( uiDoc.Document Is Nothing ) If LockUIDocument Then LockUIDocument = LockDocument( uiDoc.Document ) End If End Function '/** ' * Locks the document ' */ Public Function LockDocument (doc As NotesDocument ) As Boolean LockDocument = LockingEnabled() If LockDocument Then 'Check to make locking is enabled. If Not AlreadyLocked Then 'Make sure that the document is not allready locked. If HasLock( doc ) Then If CurrentUserHasLock( doc ) Then 'Since the current user has the document locked then we don't need to. AlreadyLocked = True LockDocument = True Else 'Document is locked by someone else. Let's display the name Msgbox DOCUMENT_LOCKED + doc.LockHolders(0) , MB_ICONSTOP, DOCUMENT_LOCKED_TITLE LockDocument = False End If Else 'Make sure that there is a document to lock, New documents can not be locked. If Not Doc.IsNewNote Then Call doc.Lock End If AlreadyLocked = True LockDocument = True End If End If End If End Function '/** ' * Unlocks the UI document ' */ Public Function UnLockUIDocument (uiDoc As NotesUIDocument ) As Boolean UnLockUIDocument = ( uiDoc.Document Is Nothing ) If UnLockUIDocument Then UnLockUIDocument = UnLockDocument( uiDoc.Document ) End If End Function '/** ' * Unlocks the document ' */ Public Function UnLockDocument (doc As NotesDocument ) As Boolean UnLockDocument = False If Not doc Is Nothing Then If Not Doc.IsNewNote Then If CurrentUserHasLock( doc ) Then 'Only unlock if the current user has it locked. doc.UnLock UnLockDocument = True End If End If End If End Function End Class