Subject: Parent & Response documents deletion- Singleton and Strategy Pattern
Yes, It’s a fairly tricky issue to solve.
You can do this with good OOP.
You have to create :
assuming you have two kind of document; “Question” and “Answer”.
Create a DocumentAnswer class extending Document
Create a DocumentQuestion class extending Document and using ParentStrategy
Create a DocumentFactory methode in charge of Document creation depending of NotesDocument passed in param
(
Function DocumentFactory(Source as NotesDocument) as variant
Select case Source.Form(0)
Case “Answer”
Set DocumentFactory = New DocumentAnswer(Source)
Case “Question”
Set DocumentFactory = New DocumentQuestion(Source)
Case else
Set DocumentFactory = New Document(Source)
End Select
End Function
)
In Document class add QueryDelete, QueryUnDelete and Remove methode
(
Sub QueryDelete(Continue as variant)
If not Me.ParentStrategy is nothing then
Call Me.ParentStrategy.QueryDelete(Continue)
end if
End sub
Sub QueryUnDelete(Continue as variant)
If not Me.ParentStrategy is nothing then
Call Me.ParentStrategy.QueryUnDelete(Continue)
end if
End sub
Function Remove(Force as Integer) as Integer
If not Me.ParentStrategy is nothing then
Remove = Me.ParentStrategy.Remove(Force)
end if
End Function
)
Me.ParentStrategy.QueryDelete and QueryUnDelete aim to manage child deletion
In the Database Class add Querydocumentdelete, QueryDocumentUndelete and PostDocumentDelete methodes to trap deletions stub
Add also RemoveDocuments(NotesDocumentCollection) and RemoveDocument(NotesDocument) to manage removing without deletion flag
e.g.
Sub QueryDocumentDelete(Source as NotesDocumentCollection, Continue)
Dim CurrentDoc as NotesDocument
Dim NextDoc as NotesDocument
Dim Doc as Variant
Set CurrentDoc = Source.GetFirstDocument
Do until CurrentDoc is nothing
Set NextDoc = Source.GetNextDocument(CurrentDoc)
Set Doc = DocumentFactory(Source)
Call Doc.QueryDelete(Continue)
Set CurrentDoc = NextDoc
Loop
End Sub
Add a CurrentDatabase Methode to prevent memory leak
Static Property Get CurrentDatabase (boForce as Variant) as Variant
Dim Session as New NotesSession 'This can be a global variable
Static SINGLETON as Variant
If Isempty(SINGLETON) or boForce then
Set SINGLETION = New Database(Session.CurrentDatabase)
end if
Set CurrentDatabase = SINGLETON
End Sub
Then in Database script :
Declare a UIDatabase as variant
In postopen
Set UIDatabase = CurrentUIDatabase(False)
In QueryDocumentDelete
If UIDatabase Is Nothing Then
Set UIDatabase = CurrentDatabase(False)
End If
UIDatabase.QueryDocumentDelete Source.Documents, Continue
As in QueryDocumentUndelete, PostDelete (to adapte)
Any where in your code use CurrentDatabase.RemoveDocument or .RemoveDocuments
If You only Work with Document use DocumentFactory to create Document object then use Remove methode.