Tuesday, September 16, 2008

Finding and flagging "orphan" documents

A recent project that I was on had an issue with a user who happened to copy documents and paste them back into the database and then delete the original --- as quoted, the user said "the database was slow, so I was trying to shake things up."

Well, little did this user know that these were documents that had children/response documents associated with them. Now that the parent/child relationship was broken, the users were not too happy about this.

First, I had to find all the orphan docs before I associated them. So I wrote this little function which worked out pretty well:

Function IsOrphan(docResponse As NotesDocument) As Integer

Dim s As New NotesSession
Dim db As NotesDatabase
Dim docParent As NotesDocument
Dim docUNID As String
Dim parentUNID As String
Dim parentUNIDLength As Integer

Set db = s.CurrentDatabase
IsOrphan = False

'Check to see if this doc is really a response...
If docResponse.IsResponse Then
Else
Exit Function
End If

parentUNID = docResponse.ParentDocumentUNID
parentUNIDLength = Len(parentUNID)

Select Case parentUNIDLength
Case 32
Set docParent = db.GetDocumentByUNID(parentUNID)
If docParent Is Nothing Then
IsOrphan = True
Else
'Is the doc valid?
If docParent.IsValid = True Then
'See if this doc is a deletion stub...
If docParent.IsDeleted = True Then
IsOrphan = True
Else
docUNID = docParent.UniversalID
'Check to see if they are the same... [idiot check]...
If docUNID = parentUNID Then
Else
IsOrphan = True
End If
End If
Else
'Not valid...
IsOrphan = True
End If
End If

Case 1 To 31
'Parent UNID is not a UNID...
IsOrphan = True

Case 0
'Response, but no Parent UNID...
IsOrphan = True

End Select

End Function

Thought I would share this...