#!/System/Library/Frameworks/Python.framework/Versions/2.5/Resources/Python.app/Contents/MacOS/Python
# -*- coding: UTF-8 -*-

'''
$RCSfile: dbconnection.py,v $
$Revision: 897 $
$Author: markus $
$Date: 2008-04-29 18:15:52 +0200 (Di, 29. Apr 2008) $
The BioCASE querytool
'''

import os, sys
# ***** include the biocase.lib directory in the python sys path for importing *****
execfile( os.path.abspath( os.path.join( os.path.dirname( __file__ ), os.pardir, os.pardir, 'lib', 'biocase', 'adjustpath.py' ) ))

from biocase.configtool.general import *


############################################################################################################
#
#   MAIN
#
#===========================================================================================================

# check authentication!
if dsa is None:
    execfile( os.path.abspath( os.path.join( os.path.dirname( __file__ ), 'main.cgi' ) ))
    sys.exit()

# check authentication!
authorize(form, dsa=dsa)

# load html template
tmpl = PageMacro('Content', PageMacro.DELMODE)
tmpl.load('Content', os.path.join(templateDir, '_dbconnection.html'))


#
# get psfObj
#
psfObj = dsaObj.getPSFObj(tmp=False)

#
# look for new form values
#
if form.has_key('action'):
    psfObj.dbIP       = form.getfirst('host', None)
    psfObj.database = form.getfirst('database', None)
    psfObj.user       = form.getfirst('user', None)
    psfObj.password   = form.getfirst('password', None)
    psfObj.dbms       = form.getfirst('dbms', None)
    psfObj.encoding   = form.getfirst('encoding', None)
    # write new PSFile
    psfObj.writePSFile()
    log.debug( "PSFile written")
#
# fill template 
#
# general stuff
tmpl['dsa'] = dsa
tmpl['wrapper_url'] = dsaObj.getBioCASeAccessPoint()

# dbconnection status
log.debug("Get DBConnectionStatus")
tmpl['status'] = dsaObj.getDBConnectionStatus()

# list of available dbms modules
dbmsList = getDBMSList()
if psfObj.dbms is not None:
    # dbms already selected
    dropDown = getDropDownOptionHtml(dbmsList, psfObj.dbms)
else:
    dropDown = getDropDownOptionHtml(['--select--']+dbmsList, '--select--')
tmpl['dbms_optionlist'] = dropDown

# list of available encodings
encList = getEncodingList()
if psfObj.encoding is not None:
    # encoding already selected
    dropDown = getDropDownOptionHtml(encList, psfObj.encoding)
else:
    dropDown = getDropDownOptionHtml(['--select--']+encList, '--select--')
tmpl['encoding_optionlist'] = dropDown

# connection parameters
tmpl['host']     = psfObj.dbIP
tmpl['database'] = psfObj.database
tmpl['user']     = psfObj.user
tmpl['password'] = psfObj.password


#
# print HTML !
#
printOverHTTP( tmpl )
