Date tip published: | 09/27/2000 |
Description: | Learn to create paths to related databases without hard coding the path! |
To learn more about Notes application development and LotusScript use the following links:
R5 Application Development CLP Package
R5 LotusScript Package
Many Notes and Domino applications are developed as a set of several Notes databases. Developers sometimes have little control over the directory path where there application is installed and/or their application will run on many different servers, so they need to code their applications to be directory path and server independent. In plain English, this means don't hard code in that server name or directory path if you want your application to always work regardless of its location! Another issue is different server platforms. Unix will use forward slashes and Windows machines will use a backslash for directory paths. If your application is going to run on both Unix and Windows platforms, then hard-coding the path will cause problems. There are several techniques to use to maintain this server and directory independence. This tip will cover LotusScript and @function methods. The basic steps for both script and @functions are:
- Access the current database.
- Isolate the location of the server on which the current database resides.
- Isolate the directory path to the current database.
- Use the server and path information obtained in Steps 2 and 3 to access documents, views, and even other databases in the same directory
LotusScript technique
LotusScript provides several methods that you can use to access a database. All of these methods require you to specify in the first two arguments the server on which to find the database and the file path to where the database resides on that server.
For example, the New method of the NotesDatabase class has this syntax:
Set notesDatabase = New NotesDatabase( server$, dbfile$ )
The server$ argument is a String value that identifies the server on which the desired database exists.
The dbfile$ argument is also a String. This parameter specifies the location of the file on the server. This argument includes the file name and the path from the Notes data directory. If the file does not exist in a directory under the Notes data directory, you must specify the full directory path including the drive specification.
You can neutralize the server$ and dbfile$ arguments that you pass to these and other database access methods by following the techniques described in this tip. This assumes your set of databases in your application are all installed in the same directory, but your application will not care what the directory is since you will determine this directory in your code.
Another assumption is that you need to maintain the same database file names. You could also use a profile document to store the file name of each database in your application and allow an administrator to edit the profile document and enter the file names of the databases used. For now we will assume your database file names will always remain the same.
Step 1 - Access the current database
Use the CurrentDatabase property of the NotesSession class to set an object reference variable for the current database:
Dim session As New NotesSession
Dim dbCur As NotesDatabase
Set dbCur = session.CurrentDatabase
Step 2 - Isolate the server
Use the Server property of the NotesDatabase class to return the server on which the database resides. If the database resides on your local system, this property returns an empty string.
Step 3 - Isolate the directory path to the current database
Use the FileName and FilePath properties of NotesDatabase to return the name and location of the database. One thing to be aware of is that FilePath returns the full path plus the file name, not just the file path by itself. While this isn't exactly what we want, we can combine these two pieces of information with some other LotusScript functions to extract the directory path of the current database. For example, the LotusScript Left function returns a given number of characters from the beginning of the string. We can use the Len function to determine how many characters are in the file path and file name strings and then the string length difference can be used return the number of characters in the directory path. The following code fragment shows how to access the directory path to the current database using the FilePath and FileName properties of the NotesDatabase class.
Dim sDirectoryPath As String
sDirectoryPath = Left( dbCur.FilePath, Len(dbCur.FilePath) - Len(dbCur.FileName) )
Step 4 - Access another database
Access the other databases using the server, file name and directory path you determined in steps 1 through 3 above.
Dim dbOther As New NotesDatabase( sServer, sDirectoryPath & "otherdatabase.nsf" )
Formula Language Technique
To compute the server name and path using the formula language follow these steps:
Step 1 - Access the current database
In Notes formula language you can use the @DbName function to return both the server location and the name and file path of the current database. @DbName returns a list. The first element of the list is the server name and the second element is the path and file name. @DbName is used in step 2 to access the server for current database.
Step 2 - Isolate the server
To neutralize the server parameter so it returns the name of the server that the database is located on, use the following statement. The @DbName function returns the current database's information as a two element list. The server name is the first element. The second element consists of the database path and filename. To retrieve the current server name, simply pick off the first element of the list using the @Subset function.
Server := @Subset( @DbName; 1 ) ;
Step 3 - Isolate the directory path to the current database
To isolate the current database's directory path, access the file path information using the @DbName function. In the formula statement below, @Subset is used to return the last element of the @DbName two-element list, the database's complete file path. String manipulation is then used to return just the directory path from the complete file path. @LowerCase is used to neutralize the case of the file name and @LeftBack returns the left portion of the file path up to "currentdatabase.nsf", which is the file name of the current database.
CurrentFilePath := @Subset( @DbName;-1) ;
DirectoryPath := @LeftBack( @lowercase(CurrentFilePath) ; "currentdatabase.nsf" )
Unlike the LotusScript technique above, when you use this formula language technique you must know the name of the current database. There is no @formula to determine just the name of the database like in LotusScript. You can use @leftback to search for the directory separator, but be aware that this is different depending on the operating system used. Windows machines use a backslash (\), Unix machines use a forward slash (/), and MacIntosh machines use a colon (:).
Step 4 - Access the other database
Access other databases using the server, file name, and directory path you determined in steps 1 through 3 above. The following code listing shows all four steps:
Server := @Subset( @DbName; 1 ) ;
CurrentFilePath := @Subset( @DbName; -1) ;
DirectoryPath := @Left( @lowercase(CurrentFilePath) ; "currentdatabase.nsf" )
Database := DirectoryPath + "otherdatabase.nsf"
@Command([FileOpenDatabase]; Server : Database )
|