さて今回も DocumentDiffer クラス (2) に引き続き、コードをご紹介します。
今回は、実際に差分を作成する process関数 です。 process関数の引数は、更新後のNotes文書 (NotesDocument) です。
実際に処理をおこなっているのは、Forall の部分です。 New で必要なフィールドのみ oldDoc にコピーしているので、今度はそれを1つずつ、更新後の文書の同名フィールドと比べているわけです。
' ---- Public methods ----
Function process(doc As NotesDocument) As NotesDocument
Dim session As New NotesSession
Dim tmpItem As NotesItem
' 差分文書が残っている場合は再作成
If Not(Me.diffDoc Is Nothing) Then
Delete Me.diffDoc
End If
Set Me.diffDoc = session.CurrentDatabase.CreateDocument
' アイテムごとに更新を確認
Forall item In Me.oldDoc.Items
If doc.HasItem(item.Name) Then
Set tmpItem = doc.GetFirstItem(item.Name)
If tmpItem.Text <> item.Text Then
Call item.CopyItemToDocument(Me.diffDoc, item.Name)
End If
End If
End Forall
Set process = Me.diffDoc
End Function
ちなみにアイテムの比較は単に .Text を使用していますが、これは少し手抜きです。 例えば複数値の格納された .Text の結果が、単数の結果と一致することがあるかもしれません。
process関数は作成した差分文書を戻り値として返します。 もし差分をNotes文書で残す必要がある場合、この文書に適切な Form (入力フォームそのままでもいいかも) や、作成者フィールド他を設定して保存すればOKです。
さて次回は、toString 関数などの便利系関数のコードを見てみましょう。