ChadSmiley Blog Things about family, life, coding, and more

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
Filed under: Domino Leave a comment
Comments (6) Trackbacks (0)
  1. Nice stuff, Chad, but why “static” functions, instead of using it as a real locking wrapper class, like this:
    Dim docLock As New DocumentLock(doc)
    If docLock.isLocked() Then
    ‘ Do something… perhaps unlock it:
    Call docLock.unlock() ‘ Throws an “exception” if wrong user
    Else
    ‘ Do something else…perhaps lock it:
    Call docLock.lock() ‘ Throws an “exception” if doc locking is disabled
    End If
    The methods you have, would be rewritten as:
    Public Sub new(doc As Variant) ‘ To be able to send both NotesDocument and NotesUIDocument
    Public Function isLockingEnabled() As Boolean
    Public Function isLockedByCurrentUser () As Boolean
    Public Function lock() As Boolean
    Public Function unlock() As Boolean
    Public Function toString() As String ‘ Returns something like “Document XYZ locked by ABC”, to get simple debug logging and such.

    The reason I bug you with this, is that I have my own DocumentLock class, that contains the same methods, but with support for R5 (using specific lock documents)

  2. Johan – Thanks for your suggestions, I usually shy away from using Variants but in this case I see the advantage. I will incorporate these changes also.

  3. Chad,

    Can we implement this on web, if yes can u please help in how to use this code for the wen

  4. Chad

    Sorry for one more question

    I used the class in the webquery open like
    flag = DocLock.IsDocumentLocked (wdoc)

    it is giving “False” if the document is opened in web by user and is not locked
    I am locking it by “Call DocLock.LockDocument(wdoc)” in the same webqueryopen agent

    if is save i can unlock it like “call DocLock.UnLockDocument(wdoc) in webquerysave but how can i unlock if i just close the document without saving

    Please help

  5. How to give document locking in Lotusnotes R5

  6. There are examples out there on how to do this, try { Link } My objective is to make the R6 document locking better and easier to use.


Leave a comment


No trackbacks yet.