#
# Example script for creating a JDBCDriver and DataSource object
#
# The file init.tcl must be loaded prior to running this script.
#
# For the JDBC Driver, substitute different values for the 
# implementation class, URL prefix, and JAR file as needed.

# create_DB2_JDBCdriver name
#
# create_DB2_JDBCdriver creates a new JDBC driver for DB2 using the 
# implementation class: COM.ibm.db2.jdbc.app.DB2Driver, with support for JTA
# disabled.  
#
# name - a string specifying the name of the JDBC driver to be created.
#


proc create_DB2_JDBCdriver { {name DB2Driver} } {
    global VERBOSE
    set jdbc "/JDBCDriver:${name}/"
    # Create a JDBCDriver object
    set cmd {JDBCDriver create $jdbc -attribute \
	    {{ImplClass COM.ibm.db2.jdbc.app.DB2Driver}{JTAEnabled false}\
	    {UrlPrefix jdbc:db2}}}
    if {$VERBOSE} {puts [subst "# $cmd"]}
    eval $cmd
}
create_DB2_JDBCdriver

# create_DataSrc dataSrcName jdbcDriverName DBname
#
# create_DataSrc creates a data source for an application. A data source
# is an abstraction for a connection to a particular database on a given
# RDBMS. Before a data source can be created a JDBC driver for the RDBMS
# must exist. 
#
# dataSrcName - a string specifying the name of the data source to
# be created. The name of the data source must be unique to the node. If
# the data souce is not unique to the node, then an exception will be raised.
# The name should just be a simple string and should not be the fully 
# qualified name that wscp would normally expect. 
#
# jdbcDriverName - the name of an existing JDBC driver that the newly created
# data source will use for its connection to the database system.
#
# DBname - the name of an existing database.
#

proc create_DataSrc { {name testDataSource} {jdbcDriver DB2Driver} \
	{DBname WAS}} {
    global VERBOSE
    set data "/DataSource:${name}/"
    set jdbc "/JDBCDriver:${jdbcDriver}/"
    # Create a DataSource object
    set attrs "{DatabaseName $DBname}{JDBCDriver $jdbc}"
    set cmd {DataSource create $data -attribute $attrs}
    if {$VERBOSE} {puts [subst "# $cmd"]}
    eval $cmd
}
create_DataSrc 

# install_JDBCdriver jdbcDriverName
#
# install_JDBCdriver installs the JDBC driver specified as the jdbcDriverName 
# argument. A JDBC driver must be installed before a datasource can make 
# connections with it. The installation process is used to tell the JDBC driver 
# where to find the java class libraries for the database system's connection 
# manager. This procedure assumes a JDBC driver for DB2 and that the java class 
# libraries will be found in:
# 
#   /java/db2java.zip
#
# jdbcDriverName - the name of the DB2 JDBC driver to be installed.
# 

proc install_JDBCdriver {{jdbcDriver DB2Driver}} {
    global VERBOSE DB2_HOME FILE_SEPARATOR NODE
    set jdbc "/JDBCDriver:${jdbcDriver}/"
    # Install the JDBCDriver object
    set cmd {JDBCDriver install $jdbc -node $NODE -jarFile \
	    ${DB2_HOME}${FILE_SEPARATOR}java${FILE_SEPARATOR}db2java.zip}
    if {$VERBOSE} {puts [subst "# $cmd"]}
    eval $cmd
}
install_JDBCdriver

# Optional commands for uninstalling the JDBCDriver object 
# and removing the DataSource object

# Uninstall the JDBCDriver object

# uninstall_JDBCDriver jdbcDriver
#
# uninstall_JDBCDriver uninstalls a JDBC driver from the system.
#
# jdbcDriver - a string specifying the name of the JDBC driver to be 
# uninstalled from the system.

proc uninstall_JDBCDriver {{jdbcDriver DB2Driver}} {
    global VERBOSE NODE
    set jdbc "/JDBCDriver:${jdbcDriver}/"
    set cmd {JDBCDriver uninstall $jdbc -node $NODE}
    if {$VERBOSE} {puts [subst "# $cmd"]}
    eval $cmd
}
uninstall_JDBCDriver


# remove_DataSrc name
#
# remove_DataSrc removes a data source object from the system. 

proc remove_DataSrc { {name testDataSource} } {
    global VERBOSE
    set data "/DataSource:${name}/"
    set cmd {DataSource remove $data}
    if {$VERBOSE} {puts [subst "# $cmd"]}
    eval $cmd
}
remove_DataSrc 

# remove_JDBCdriver DriverName
#
# remove_JDBCdriver removes the JDBCDriver object specified by DriverName
# from the system.
#
# DriverName - a string specifying the name of the JDBC driver to be removed
# from the system. 

proc remove_JDBCdriver { {DriverName DB2Driver} } {
    global VERBOSE
    set jdbc "/JDBCDriver:${DriverName}/"
    set cmd {JDBCDriver remove $jdbc}
    if {$VERBOSE} {puts [subst "# $cmd"]}
    eval $cmd
}
remove_JDBCdriver

# Sample calls. 
#
# Order does matter. The JDBC driver must be created before the datasource
# can be created. Also, the JDBC driver must be installed before any 
# beans can access the datasource that uses the JDBC driver.

#
# create_DB2_JDBCdriver "DB2Driver"
# create_DataSrc "testDataSource" "DB2Driver"
# install_JDBCdriver "DB2Driver"
#
# 
# Now, to remove the objects that you've just created...
#
# uninstall_JDBCDriver "DB2Driver" 
# remove_DataSrc "testDataSource"
# remove_JDBCdriver "DB2Driver"