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

'''
$RCSfile: main.py,v $
$Revision: 911 $
$Author: markus $
$Date: 2008-04-30 23:52:39 +0200 (Mi, 30. 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' ) ))


try:
    from biocase.querytool.general import *
    # only to test that 4suite is installed
    from biocase.tools.xslt import transformXML
except ImportError:
    # redirect to lib test page
    print "Location: ../utilities/testlibs.cgi"
    print



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


# display global DSA select box?
if dsa is None:
    # use the datasource.html template to select a datasource first
    tmpl = PageMacro('Content', PageMacro.DELMODE)
    tmpl.load('Content', os.path.join(skinDir, 'datasource.html'))
    # get list of available dsa's
    ds_list = [dsa.name for dsa in getDsaList()]
    log.info("DSA list from datasource directory: %s" %str(ds_list))
    tmpl['datasource_options'] = getDropDownOptionHtml(['--select--']+ds_list, '--select--')
    if wrapper_url is not None:
        tmpl['wrapper_url'] = wrapper_url
else:
    # generate the search form for this dsa. use the form.html template
    tmpl = PageMacro('Content', PageMacro.DELMODE)
    tmpl.load('Content', os.path.join(skinDir, 'form.html'))
    tmpl['wrapper_url'] = wrapper_url
    tmpl['dsa'] = dsa
    # get the relevant form fields from preferences
    concepts = []
    if schemaObj is not None:
        log.info("Use schema %s to build the form."%schema)
        schema = schemaObj.NS
        for conObj in schemaObj.concepts:
            conDict = {'label_display':conObj.label.replace('_',' '), 'label':conObj.label}
            if not conObj.cops == u'~':
                optionHtml = getDropDownHtml('cop%s'%conObj.label, [c for c in conObj.cops] )
            else:
                optionHtml = ''
            conDict['copOptions'] = optionHtml
            concepts.append(conDict)
        # create grouping drop down
        valDict = {}
        for c in schemaObj.concepts:
            valDict[c.label.replace('_',' ')] = c.label
        tmpl['groupby_options'] = getDropDownOptionHtml(vals=valDict, default=schemaObj.grouping.label)
    else:
        log.info("Cant find preferences for the selected schema %s."%schema)
    tmpl.expand('Content', 'conceptlist', concepts)
    # create alternative schema selection
    tmpl['schema'] = schema
    tmpl['schema_options'] = getDropDownOptionHtml(vals=dict([('','')] + [(s.label,s.NS) for s in prefs.schemas if s.NS != schema and s.hasConcepts()]))

#
# print HTML !
#
printOverHTTP( tmpl )
        

