#!c:\Program Files\Python39\python.exe # -*- coding: UTF-8 -*- ''' $RCSfile: editor_mapping.py,v $ $Revision$ $Author: markus $ $Date$ The BioCASE querytool ''' import os, sys # ***** include the biocase.lib directory in the python sys path for importing ***** exec(open(os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir, os.path.pardir, 'lib', 'biocase', 'adjustpath.py'))).read()) exec(open(os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir, os.path.pardir, 'lib', 'biocase', 'appinit.py'))).read()) from biocase.configtool.general import dsaObj, form, templateDir, log, printOverHTTP, readTmpCmFile, schema, getDropDownOptionHtml from biocase.wrapper.cmf_base import CMFClass from biocase.tools.templating import PageMacro ############################################################################################################ # # MAIN # #=========================================================================================================== # check datasource availablility if dsaObj is None: exec(compile(open(os.path.abspath(os.path.join(os.path.dirname(__file__), 'main.cgi')), "rb").read(), os.path.abspath(os.path.join(os.path.dirname(__file__), 'main.cgi')), 'exec')) 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) # string for error/warning messages message = [''] 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: # Check if the mapping consists of more than five elements # If yes, break the loop and issue a warning if i == 5: message.append("The mapping for this element consists of more than five elements and can only be edited directly in the concept mapping file. If you continue with the mapping editor, the excess elements will be removed!") break if not isinstance(item, str): 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 # check if metadata is available dbMeta = "false" errTables = [] tables = psfObj.getTableAliasList() if dbmodObj is not None: if dbmodObj.hasMetaInfo(): dbMeta = "true" errTables = dbmodObj.verifyTables([t.table for t in tables]) for et in errTables: for ta in [t.alias for t in tables if t.table.lower() == et.lower()]: message.append("Table/view '" + et + "', alias '" + ta + "' does not exist in database.") tmpl["dbMeta"] = dbMeta tmpl["message"] = "
".join(message) if dbMeta == "true": columns = "" for ta in tables: if not ta.table.lower() in errTables: tmp = '' for c in dbmodObj.getColumns(ta.table): tmp += '"' + c + '":"' + c + '", ' if tmp: columns += '"' + str(ta.alias) + '": {' + tmp[:-2] + "}, " elif mapPos >= 0: # Table is in error table list (i.e. column list cannot be retrieved) and we edit data: # lets add the currently mapped column to the column list, so this mapping won't be lost if saved cols = [] mapList = cmfObj.getMappingsByConcept(xpath) mapObj = mapList[mapPos - 1] for item in [i for i in mapObj.getItems() if not isinstance(i, str)]: # Only non-literals and if it's for the current table if item.table.lower() == ta.alias.lower(): cols.append(item.attr) log.debug(item.attr) if len(cols) > 0: columns += '"' + str(ta.alias) + '": {' for c in cols: columns += '"' + c + '":"' + c + '", ' columns = columns[:-2] + "}, " columns = "{" + columns + "} " else: columns = "null" tmpl["columns"] = columns # # loop through 5 fields # selectedColumns = "{" for fieldNr in [1, 2, 3, 4, 5]: selectedColumns += '"pos' + str(fieldNr) + '":"' + attr[fieldNr - 1] + '",' # 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 tables]), default) tmpl['dba%i_alias' % fieldNr] = dropDown # 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 # literal tmpl['lit%i' % fieldNr] = lit[fieldNr - 1] selectedColumns = selectedColumns[:-1] + "}" tmpl["selectedColumns"] = selectedColumns # # print HTML ! # printOverHTTP(tmpl)