This example simulates a smart search business object which locates a customer that may be contained in one of many different databases. What actually happens is that code in the BeforeOpenTables event of the data environment creates an object based on the gopher class and prompts a user for the database to use. Code in the gopher class opens the appropriate database. This strategy allows us to use Visual FoxPro in a 3-tiered model where user services are not tightly bound to data services as is the case in many of today's client-server environments.
While this example only provides a choice between the Visual FoxPro TESTDATA and Tastrade sample databases, you can use this basic approach to provide a smart search business object which knows how and where to look across a corporate network for necessary databases.
To open the project for the gopher sample
MODIFY PROJECT (HOME(2) + 'servers\gopher\foxsrch')
To run the gopher sample
#DEFINE FOXHOME HOME(2)
The following code is included in the BeforeOpenTables event of the Data Environment in Wing1.scx:
LOCAL oGopher,lUseRemote
THIS.AddObject('cursor1','cursor')
THIS.cursor1.Alias = 'employee'
lUseRemote=(MESSAGEBOX('Do you want to use Remote Data?',36) = 6)
oGopher=CreateObject('FoxSearch.Gopher')
oGopher.UpdateDE(THIS,m.lUseRemote)
RELEASE oGopher
The following code defines the Gopher class. The UpdateDE method takes an object reference to a data environment and a logical value as parameters. Based on the value of the logical value, the UpdateDE method sets different CursorSource values for a cursor in the data environment.
#DEFINE FOXHOME HOME(2)
DEFINE CLASS Gopher AS Custom OLEPUBLIC
oDERef = ''
PROCEDURE UpdateDE
PARAMETER oNewDE,lRemote
IF TYPE('oNewDE')#'O' OR ISNULL(m.oNewDE)
RETURN .F.
ENDIF
THIS.oDERef = m.oNewDE
IF !m.lRemote
* Use local data
THIS.oDERef.cursor1.database = FOXHOME + 'DATA\TESTDATA.DBC'
THIS.oDERef.cursor1.cursorsource = 'Employee'
ELSE
* Use remote data (simiulated)
THIS.oDERef.cursor1.database = FOXHOME + ;
'Tastrade\DATA\Tastrade.DBC'
THIS.oDERef.cursor1.cursorsource = 'Employee'
ENDIF
THIS.oDERef = ''
ENDPROC
ENDDEFINE