LotusScriptでNotesDocumentCollectionの全文書に同じ処理を実行する

NotesDocumentCollectionの全文書に対して処理を実行する場合、
whileを使って以下のようにコーディングする。

Dim dc as NotesDocumentCollection 
Dim doc as NotesDocument 

set doc = dc.getFirstDocument() 

while not doc is nothing 
  -- 処理 -- 
  set doc = dc.getNextDocument(doc) 
wend 

たまに最後の"set doc = dc.getNextDocument(doc)"を忘れて
無限ループになってしまうことがあるので、 全文書に対して
同じ処理をするようなWrapperクラスを作成してみる。  


%REM
    Class NDCWrapper
    Description: Wrapperクラス
%END REM
Class NDCWrapper
  Private zzpm_this As NotesDocumentCollection
 
    'Constructor
    Sub New(dc As NotesDocumentCollection)
      Set zzpm_this = dc
    End Sub
   
    'Destructor
    Sub Delete
    End Sub
   
    %REM
        Function forEach
        Description: 全文書に対して処理を実行するメソッド
    %END REM
    Function forEach(processor As DocumentProcessor) As Long
      Dim doc As NotesDocument
      Dim cnt As Long
     
      Set doc = zzpm_this.getFirstDocument()
      cnt = 0
      While Not doc Is Nothing
        If processor.procDoc(doc) Then cnt = cnt + 1
        Set doc = zzpm_this.getNextDocument(doc)
      Wend
     
      forEach = cnt
    End Function
   
    %REM
        Function getDocumentCollection
        Description: getter メソッド
    %END REM
    Function getDocumentCollection() As NotesDocumentCollection
      Set getDocumentCollection = zzpm_this
    End Function
End Class


%REM
    Class DocumentProcessor
    Description: WrapperのforEachで処理を行うクラス

%END REM
Class DocumentProcessor
    'Constructor
    Sub New
    End Sub
   
    %REM
        Function procDoc
        Description: forEachから渡されたNotesDocumentを処理するメソッド
    %END REM
    Function procDoc(doc As NotesDocument) As Boolean
    End Function
End Class


DocumentProcessorを継承のうえ、procDocをオーバーライドしたクラスをforEachに渡すと
コレクション内の全文書に対して処理を実行することができる。



◆使い方例

Class TestProcessor as DocumentProcessor
  Function procDoc(doc As NotesDocument) as Boolean
    Print doc.UniversalID
  End Function
End Class

Dim ws As New NotesUIWorkSpace
Dim dcWrapper As New NDCWrapper(ws.currentDatabase.UnprocessedDocuments)
Dim processor As New TestProcessor

Call dcWrapper.forEach(processor)  'UnprocessedDocumentsのUNIDをPrintで出力