Lotus Domino script snippet: Better NotesDocumentCollection loops
This code is an example of a LotusScript design pattern.
It uses a reusable function to loop over the documents in a NotesDocumentCollection.
This simple example just copies all the documents to the current database. It shows how you can use write code once and reuse many times for different purposes, while just writing the code that matters for each document in the subclass.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
Option Declare Public Class DocumentAction Private issnCurrent As NotesSession Private idbCurrent As NotesDatabase Sub New Set issnCurrent = New NotesSession Set idbCurrent = issnCurrent.CurrentDatabase End Sub Public Function doAction(pdocProcess As NotesDocument) As Boolean Error 9999, "Not implemented" End Function End Class Public Class CopyDocumentToCurrentDb As DocumentAction Public Function doAction(pdocProcess As NotesDocument) As Boolean pdocProcess.copyToDatabase idbCurrent End Function End Class Public Sub doOnDocumentCollection(pdcCollection As NotesDocumentCollection, pobjAction As DocumentAction) Dim ldocProcess As NotesDocument, ldocNext As NotesDocument Set ldocProcess = pdcCollection.getFirstDocument() Do Until ldocProcess Is Nothing Set ldocNext = pdcCollection.getNextDocument(ldocProcess) If pobjAction.doAction(ldocProcess) Then ldocProcess.save True, True End If Set ldocProcess = ldocNext Loop End Sub Public Sub startAgent Dim lssnCurrent As New NotesSession Dim ldbSource As NotesDatabase Dim lobjAction As DocumentAction Set lobjAction = New CopyDocumentToCurrentDb Set ldbSource = lssnCurrent.getDatabase("CN=Server/O=Company", "Applications\source.nsf", False) doOnDocumentCollection ldbSource.Alldocuments, lobjAction End Sub |