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

'''
$RCSfile: editor_mapping.py,v $
$Revision: 852 $
$Author: markus $
$Date: 2008-04-22 17:17:03 +0200 (Di, 22. 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 *
from biocase.wrapper.cmf_base         import CMFClass



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

# check datasource availablility
if dsaObj is None:
    execfile( os.path.abspath( os.path.join( os.path.dirname( __file__ ), 'main.cgi' ) ))
    sys.exit()

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

#
# CAN WE USE THE DB METADATA RETRIEVAL? defines the template to be used
#
dbmodObj = dsaObj.getDbmodObj()
# load template
tmpl = PageMacro('Content', PageMacro.DELMODE)
tmpl.load('Content', os.path.join(templateDir, '_mapeditor.html'))

#
# READ CMF OBJ
#
cmfObj = CMFClass()
cmFile = os.path.join( dsaObj.getDsaDir(), schema )
# read preferrably pickled tmp file
readTmpCmFile(cmfObj, cmFile, psfObj)

# output data lists for 5 fields
lit   =['','','','','']
alias =['','','','','']
attr  =['','','','','']
dtyp   =['','','','','']
#
# do we edit data? then read cmf object
mapPos = int(form.getfirst('map_pos',-1))
# get concept
xpath   = form.getfirst('concept',None)
if mapPos >= 0:
    # edit data
    # get mapping list
    mapList = cmfObj.getMappingsByConcept(xpath)
    # get right mapping
    mapObj = mapList[mapPos-1]
    i = 0
    a = mapObj.getItems()
    for item in a:
        if type(item) == InstanceType:
            alias[i] = item.table
            attr[i]  = item.attr
            dtyp[i]  = item.type
        else:
            lit[i] = item
        i += 1

#
# DISPLAY: fill template 
#

# CMF read only data
tmpl['concept'] = xpath

#
# loop through 5 fields
#
for fieldNr in [1,2,3,4,5]:
    # alias drop downs
    default = alias[fieldNr-1]
    if len(default) < 1:
        default = '--select--'
    dropDown = getDropDownOptionHtml(dict([('--select--','--select--')]+[(ta.alias,ta.alias) for ta in psfObj.getTableAliasList()]), default)
    tmpl['dba%i_alias'%fieldNr] = dropDown
    #tmpl['dba%i_alias'%fieldNr] = alias[fieldNr-1]
    # attribute
    tmpl['dba%i_attr'%fieldNr] = attr[fieldNr-1]
    # data type drop downs
    default = dtyp[fieldNr-1]
    if len(default) < 1:
        default = 'text'
    dropDown = getDropDownOptionHtml(dict([('text','text'),('int','int'),('date','date'),('float','float')]), default)
    tmpl['dba%i_type'%fieldNr] = dropDown
    #tmpl['dba%i_type'%fieldNr] = dtyp[fieldNr-1]
    # literal
    tmpl['lit%i'%fieldNr] = lit[fieldNr-1]

#
# print HTML !
#
printOverHTTP( tmpl )
