Posts tagged Lotus Script

Vector class for Lotus Script

The following is a script library written in Lotus Script to allow programmers to use a vector in their Lotus Script code.

Option Declare
Option Base 0

Public Class Vector

	'Private data members
	Private vArray    As Variant
	Private vCapacity As Integer
	Private vSize     As Integer

	Public Sub New()

		'Initialize variables
		Call SetCapacity(0)
		Call SetSize(0)
		Call Reserve(10)

	End Sub

	Public Sub Delete()

		Call EraseAll()

		'Clear the array
		Set vArray = Nothing

	End Sub

	Private Sub SetCapacity(newCapacity As Integer)

		vCapacity = newCapacity

	End Sub

	Public Function Capacity() As Integer

		Capacity = vCapacity

	End Function

	Private Sub SetSize(newSize As Integer)

		vSize = newSize

	End Sub

	Public Function Size() As Integer

		Size = vSize

	End Function

	Public Function Empty() As Boolean

		'Declare variables
		Dim valid As Boolean

		'Get the size
		If (Size() = 0) Then

			valid = True

		Else

			valid = False

		End If

		'Return if the vector is empty
		Empty = valid

	End Function

	Public Sub Reserve(minCapacity As Integer)

		'Declare variables
		Dim openElements As Integer
		Dim newCapacity As Integer

		'Initialize variables
		openElements = Capacity() - Size()
		newCapacity = Capacity()

		'Check if there are any open slots remaining
		If (openElements = 0) Then

			newCapacity = (Capacity() * 1.5)

		End If

		'Make sure that the minimum capacity is stored
		If (newCapacity < minCapacity) Then

			newCapacity = minCapacity

		End If

		'Check if a change to the capacity is being made
		If (Not(Capacity() = newCapacity)) Then

			'Check the size to determine if the array's contents need to be
			'saved while redimensioning the array
			If (Size() = 0) Then

				'Nothing stored yet, perform redim
				Redim vArray(newCapacity)

			Else

				'Keep the array's contents while extending its boundaries
				Redim Preserve array(newCapacity)

			End If

			'Update the capacity
			Call SetCapacity(newCapacity)

		End If

	End Sub

	Public Sub PushBack(newItem As Variant)

		'Check if the passed item/items is actually an array or a list
		If ((Not(Isarray(newItem))) And (Not(Islist(newItem)))) Then

			'Add the single item
			Call AddItem(newItem)

		Else

			'Add multiple items
			Forall item In newItem

				Call AddItem(item)

			End Forall

		End If

	End Sub

	Private Sub AddItem(newItem As Variant)

		'Declare variables
		Dim newSize As Integer

		'Initialize variables
		newSize = Size() + 1

		'Make sure there is enough capacity in the vector
		Call Reserve(newSize)

		'Insert the item into the vector
		If (Isobject(newItem)) Then

			Set vArray(Size()) = newItem

		Else

			vArray(Size()) = newItem

		End If

		'Increment the vector size
		Call SetSize(newSize)

	End Sub

	Public Function At(index As Integer) As Variant

		'Check the vector's index boundaries
		If ((index < 0) Or (index => Size())) Then Error 2000, _
		"[Vector Class]:(Function: At): Index out of bounds."

		'Check if the item is an object
		If (Isobject(vArray(index))) Then

			Set At = vArray(index)

		Else

			At = vArray(index)

		End If

	End Function

	Public Sub Erase(index As Integer)

		'Check if the index is out of bounds
		If ((index < 0) Or (index => Size())) Then Error 2000, _
		"[Vector Class]:(Function: Erase): Index out of bounds."

		'Declare variables
		Dim newArray As Variant
		Dim x As Integer
		Dim y As Integer

		'Initialize variables
		y = 0

		'Make a new array with the same capacity
		Redim newArray(Capacity())

		'Loop through each element of the vector
		For x = 0 To Size() - 1

			'Check if the current index is the one to remove
			If ((x <> index) And (Not(y > Size()))) Then

				'Check if the item is an object
				If (Isobject(vArray(x))) Then

					Set newArray(y) = vArray(x)

				Else

					newArray(y) = vArray(x)

				End If

				'Increment the index counter
				y = y + 1

			End If

		Next x

		'Subtract one from the vector size
		Call SetSize(Size() - 1)

		'Set the new array as the vector
		vArray = newArray

	End Sub

	Public Function BeginIndex() As Integer

		BeginIndex = 0

	End Function

	Public Function EndIndex() As Integer

		EndIndex = Size() - 1

	End Function

	Public Sub EraseAll()

		'Declare variables
		Dim x As Integer

		'Loop through each element of the array
		For x = BeginIndex() To EndIndex()

			'Delete each element of the arra
			'Check if the item is an object
			If (Isobject(vArray(x))) Then

				Set vArray(x) = Nothing

			End If

		Next

		'Reset the size.
		Call SetSize(0)

	End Sub

End Class

Copy the above code and paste into a script library for the database where you would like to use a vector. Then, you can use the example below to use the vector class.

Use "libVector"

Dim vector As New Vector()
dim obj As String

obj = "hello"

'Load any object into the vector.
Call vector.PushBack(obj)
Call vector.PushBack(obj)

'Loop through the vector to retrieve objects.
For i = vector.BeginIndex() To vector.EndIndex()

  Print "Item: " & i & " " & vector.At(i)

Next

Clearing the Client Version from the Lotus Notes Directory

With the latest version of Lotus Notes (8.5.1), you can now view the version of your clients by looking in the People -> by Client Version view. One problem with this, however, is that your users will show each client they have logged in as. Over the years, you may accumulate many versions for each user.

During an upgrade, you may wish to see what users are using the previous version of client against the ones who have the new version installed. But first, we’ll need to clean up the directory so we don’t see all this old version history.

To clean up these fields, you need to write an agent that will empty them for each selected person. This will allow you to run the clean up agent on only the users you wish to run it on.

Writing the agent
Open up the pubnames.ntf template file in your Notes Designer. You’ll need to go to Code and double-click on Agents to see the current agents for the template.

We’ll create a new agent and give it a name. Below is the code for the agent. Copy and paste it in the Designer.

Option Public
Option Declare

Sub Initialize()

  'Declare.
  Dim s As New NotesSession
  Dim db  As NotesDatabase
  Dim dc As NotesDocumentCollection
  Dim doc As NotesDocument

  'Initialize.
  Set db = s.Currentdatabase
  Set dc  = db.Unprocesseddocuments
  Set doc = dc.Getfirstdocument()

  While(Not(doc Is Nothing))

    If (doc.Form(0) = "Person") Then

      Call doc.Removeitem("ClntBld")
      Call doc.Removeitem("ClntDate")
      Call doc.Removeitem("ClntDgst")
      Call doc.Removeitem("ClntMachine")
      Call doc.Removeitem("ClntPltfrm")
      Call doc.Save(False, False, False)

    End If

    Set doc = dc.Getnextdocument(doc)

  Wend

End Sub

Now, after you refresh your names.nsf file, you can go to the Action menu and find your agent. Running the agent will only process those Person documents that you have selected.

String Replace Function For Lotus Script

Here is a handy function that I sometimes use in my Lotus Script programs. 

'===============================================================================
' +----------------------------------------------------------------------------+
' | Function: strreplace
' +----------------------------------------------------------------------------+
' | Accepts: src, the value to search for to replace.
' |          dest, the value to replace with.
' |          arg, the string to search within.
' +----------------------------------------------------------------------------+
' | Description:
' | Replaces all occurances of src with dest in the string arg.
' +----------------------------------------------------------------------------+
'===============================================================================
Public Function strreplace(Byval src As String, Byval dest As String,
Byval arg As String) As String

    'Declare variables
    Dim pos As Integer

    'Initialize
    pos = Instr(arg, src)

    'Loop through the string
    While (pos > 0)
        arg = Left(arg, pos - 1) + dest + Mid(arg, pos + Len(src))
        pos = Instr(pos + Len(dest), arg, src)
    Wend

    'Return the replaced string
    strreplace = arg

End Function

Here is an example of how to use the function:

newString = strreplace("WORLD", "World", "Hello WORLD!")

The newString variable would contain “Hello World!”