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

'''
$RCSfile: index.cgi,v $
$Revision: 905 $
$Author: j.torre $
$Date: 2008-04-30 11:57:10 +0200 (Mi, 30. Apr 2008) $
The BioCASE TestingTool
'''

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

#Import some stuff-----------------------------------------
import cgitb; cgitb.enable()
import string
import os, sys
import inspect as               _i
import os as                    _os
import os.path as               _p
import cgi
#-------------------------------------------------------------
import biocase.configuration
import logging, biocase.initlogs
biocase.initlogs.initWebappLogging()
import biocase.tools.templating
from biocase.tools.htmltools import getDropDownOptionHtml
import biocase.datasources

# get config data
cfg=biocase.configuration.Cfg()
#Get the dsa parameter
form = cgi.FieldStorage()

#------------------------------------------------------------------------------------

#Prepare the template
pm = biocase.tools.templating.PageMacro('Content', biocase.tools.templating.PageMacro.KEEPMODE)
pm.load('Content', os.path.join(cfg.wwwLocator,'testingtool', '_index.html'))


#Check that all necesary libraries are installed:
try:
  import xml.etree.ElementTree
  import biocase
  import biocase.wrapper
  pm['library_status'] = '<span class="goodstatus">OK</span>'
except:
  pm['library_status'] = '<span class="badstatus">Wrong, </span><a href="../utilities/testlibs.cgi">check libraries</a>'

#Print the headers	
print 'Content-Type: text/html; charset=UTF-8'
print # Blank line marking end of HTTP headers

dsaList = biocase.datasources.getDsaList()
if len(dsaList)>0:
    if form.has_key('dsa'):
        dsa = form['dsa'].value
        dsaObj = biocase.datasources.Datasource(dsa)
    else:
        # take first one from list
        dsaObj = dsaList[0]
        dsa = dsaObj.name
    # dsa specific
    pm['dsa'] = dsa
    pm['DataSourcesOptionList'] = getDropDownOptionHtml([d.name for d in dsaList], default=dsa)    
    #Check that the database connection is fine
    pm['db_con'] = dsaObj.getDBConnectionStatus()
else:
    pm['warning_message']='There is no datasource configured.<br/>Please <a href="../configtool/system.cgi">create a new datasource</a> first!'

print pm
