I finally got fed up writing ad-hoc export functions for applications so I have written a generic export library for Lotus Notes. The library is written in LotusScript and can be applied to any database.
Below I’ll discuss the concepts I have defined and show how the code is used.
The export library consists of four core concepts that translate into four core (abstract) classes:
|
Below I’ll explain each concept in a little more detail.
OutputStream: In the most simple form this is a thin wrapper around the built in NotesStream class. The class has been wrapped since it allows me to subclass OutputStream while keeping the classes that use OutputStream unaware of the differences. I have defined three sub-classes so far:
ExportSource:
ExportStrategy:
ExportFormatter:
|
You combine the above concepts to export data. If you need to export the selected documents to a CSV-file using the format of the current view you would combine:
- LineFileOutputStream pointing to a file on disk
- SelectedExportSource to export the documents the use as selected
- CsvExportStrategy to write the exported data as CSV
- ViewExportFormatter to format the data as the current view
Example 1:
Export selected documents based on a view to a CSV-file on disk.
Option Public Option Explicit Use "CLASS: ExportSource" Use "CLASS: OutputStream" Use "CLASS: ExportFormatter" Use "CLASS: ExportStrategy" Sub Initialize 'declarations Dim session As New NotesSession Dim db As NotesDatabase Dim view As NotesView Set db = session.GetDatabase("", "export_db.nsf") Set view = db.GetView("ExportDocuments") 'we want to export selected documents only Dim es As New SelectedExportSource() 'we want to format the documents as the current view (notesTestDocuments) Dim formatter As New ViewExportFormatter(Nothing, "notesTestDocuments", Nothing) 'the documents should be written to a file replacing any existing file Dim out As New LineFileOutputStream("d:\export.csv", True) Call out.Open() 'export the data as CSV to the created OutputStream and the ExportSource Dim strategy As New CsvExportStrategy(out, es) Call strategy.Export(formatter, True) 'close the OutputStream Call out.Close() End Sub
Example 2:
Export all documents in a view to a richtext item on a document as XML.
Option Public Option Explicit Use "CLASS: ExportSource" Use "CLASS: OutputStream" Use "CLASS: ExportFormatter" Use "CLASS: ExportStrategy" Sub Initialize 'declarations Dim ws As New NotesUIWorkspace Dim session As New NotesSession Dim db As NotesDatabase Dim view As NotesView Dim doc As NotesDocument Dim rt As NotesRichTextItem 'get current database and view Set db = session.CurrentDatabase Set view = ws.CurrentView.View 'we want to export all documents in the current view Dim es As New ViewExportSource(view) 'we want to format the documents as the current view (notesTestDocuments) Dim formatter As New ViewExportFormatter(view, "", Nothing) 'the documents should be written to a rich text item Set doc = db.CreateDocument Call doc.ReplaceItemValue("Form", "TestDocument") Call doc.ReplaceItemValue("Title", "XML Export " + Now) Set rt = New NotesRichTextItem(doc, "Body") Dim out As New RichTextOutputStream(rt) Call out.Open() 'export the data as XML to the created OutputStream and the ExportSource Dim strategy As New XmlExportStrategy(out, es) Call strategy.Export(formatter, True) 'close the OutputStream Call out.Close() 'save document Call doc.Save(True, False) End Sub
Since some exports (i.e. selected documents as CSV to a file) are done quite frequently I have defined some helper classes (really based on the Facade pattern). These helper classes are called Exporters and wrap the above example 1 on one line of code:
Option Public Option Explicit Use "CLASS: Exporter" Sub Initialize Dim exporter As New CsvExporter("d:\export.csv", "notesTestDocuments", True) End Sub