Thursday, January 27, 2011

Get Database Information for Configuration Documents

I've done a lot of work lately to make our applications more configurable. Most of it is related to having separate servers for development, QA and production, and needing to be able to change reference databases without requiring code changes at each stop in the change control process.

As part of that, I wanted to make it easier to enter the information we required for the reference databases. Sure, you could find the database and then type in the server name and path name (or replica ID), but we had so many that it became a burden to do it that way.

So I created the script below. This could be a function in a script library or could be added in the form globals.


Sub SetDatabaseInfo(actionID As String, docObj As Variant, titleField As String, serverField As String, pathField As String, repIDField As String)
%REM
This function allows the user to select a database and set up to four fields with information from the selected database:
(0) Database Title
(1) Server
(2) File path
(3) Replica ID

Parameters are:
actionID = 'Set' to set the field values, 'Clear' to clear the field values
docObj = the document to be modified. Can be a NotesDocument or NotesUIDocument
titleField = item name for the database title
serverField = item name for the database server name
pathField = item name for the database path
repIDField = item name for the database replica ID

If Cancel is chosen in the dialog, the document passed in is not changed.
The user receives an error if the file they chose cannot be opened.
%END REM


Dim ws As New NotesUIWorkspace
Dim nname As NotesName
Dim session As New NotesSession
Dim db As NotesDatabase
Dim notesDataPath As String
Dim filePath As Variant
Dim result As Variant
Const PROMPT_CHOOSEDATABASE = 13
Dim fileInfo(3) As String
Dim docObjType As String

docObjType = Typename(docObj)
If docObjType <> "NOTESDOCUMENT" And docObjType <> "NOTESUIDOCUMENT" Then Exit Sub

Select Case Ucase(actionID)
Case "SET"
notesDataPath = Ucase(session.GetEnvironmentString("Directory",True)) & "\"
result = ws.Prompt(PROMPT_CHOOSEDATABASE,"Choose Database","Choose the database for which you want information:","","")
If Isempty(result) Then
Exit Sub
End If

Set db = New NotesDatabase("","")
If Not db.Open(result(0),result(1)) Then
Messagebox "Error opening database; you may not have access to it",48,"Error"
End If
filePath = Ucase(db.FilePath)
If result(0) = "" Then
filePath = Strright(filePath,notesDataPath)
End If
fileInfo(0) = db.Title
fileInfo(1) = result(0)
fileInfo(2) = filePath
fileInfo(3) = db.ReplicaID

If fileInfo(0) <> "" Then
If docObjType = "NOTESDOCUMENT" Then
If titleField <> "" Then
If docObj.HasItem(titleField) Then Call docObj.ReplaceItemValue(titleField,fileInfo(0))
End If
If pathField <> "" Then
If docObj.HasItem(pathField) Then Call docObj.ReplaceItemValue(pathField,fileInfo(2))
End If
If repIDField <> "" Then
If docObj.HasItem(repIDField) Then Call docObj.ReplaceItemValue(repIDField,fileInfo(3))
End If
If serverField <> "" Then
Set nname = New NotesName(fileinfo(1))
If docObj.HasItem(serverField) Then Call docObj.ReplaceItemValue(serverField,nname.Abbreviated)
End If
Elseif docObjType = "NOTESUIDOCUMENT" Then
If titleField <> "" Then
If docObj.Document.HasItem(titleField) Then Call docObj.FieldSetText(titleField,fileInfo(0))
End If
If pathField <> "" Then
If docObj.Document.HasItem(pathField) Then Call docObj.FieldSetText(pathField,fileInfo(2))
End If
If repIDField <> "" Then
If docObj.Document.HasItem(repIDField) Then Call docObj.FieldSetText(repIDField,fileInfo(3))
End If
If serverField <> "" Then
Set nname = New NotesName(fileinfo(1))
If docObj.Document.HasItem(serverField) Then Call docObj.FieldSetText(serverField,nname.Abbreviated)
End If
End If
End If

Case "CLEAR"
If docObjType = "NOTESDOCUMENT" Then
If titleField <> "" Then
If docObj.HasItem(titleField) Then Call docObj.ReplaceItemValue(titleField,"")
End If
If pathField <> "" Then
If docObj.HasItem(pathField) Then Call docObj.ReplaceItemValue(pathField,"")
End If
If repIDField <> "" Then
If docObj.HasItem(repIDField) Then Call docObj.ReplaceItemValue(repIDField,"")
End If
If serverField <> "" Then
If docObj.HasItem(serverField) Then Call docObj.ReplaceItemValue(serverField,"")
End If
Elseif docObjType = "NOTESUIDOCUMENT" Then
If titleField <> "" Then
If docObj.Document.HasItem(titleField) Then Call docObj.FieldSetText(titleField,"")
End If
If pathField <> "" Then
If docObj.Document.HasItem(pathField) Then Call docObj.FieldSetText(pathField,"")
End If
If repIDField <> "" Then
If docObj.Document.HasItem(repIDField) Then Call docObj.FieldSetText(repIDField,"")
End If
If serverField <> "" Then
If docObj.Document.HasItem(serverField) Then Call docObj.FieldSetText(serverField,"")
End If
End If

End Select
End Sub


This LotusScript was converted to HTML using the ls2html routine,
provided by Julian Robichaux at nsftools.com.


The comments at the top of the code explain pretty well what is going on: pass it the action you want to take, the field names you want to update and the object containing those fields and it does the work. I honestly don't remember why I wrote it to be able to update either a NotesUIDocument or a NotesDocument, since I think I always pass it a NotesUIDocument, but it gives you the flexibility to pass either object type.

Technorati:

No comments: