#!C:\Programme2\Python25\python.exe
# -*- coding: UTF-8 -*-

'''
$RCSfile: details.py,v $
$Revision: 958 $
$Author: markus $
$Date: 2010-05-10 16:06:35 +0200 (Mo, 10. Mai 2010) $
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.querytool.general import *
from biocase.tools.xslt import transformXML



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

tmpl = PageMacro('Content', PageMacro.DELMODE)
tmpl.load('Content', os.path.join(skinDir, 'details.html'))

# which detail do we need to create?
detail = form['detail'].value
detailObj = schemaObj.details[detail]

log.info("PREFS DETAILS OBJ: %s"%unicode(str(detailObj), errors='replace'))

# build a new filter object from form values of the record identifiers.
# First create list of needed COP objects
filterObj = AndClass()
for para, recID in detailObj.recID.items():
    # create an equals cop for every recID
    if form.getfirst(para, None) is not None:
        conObj = Concept(NS=schema, path=recID.path, label=para)
        cop = EqualsClass(conObj, form.getfirst(para, None))
        filterObj.addOperand(cop)
log.info("FILTER OBJ: %s"%unicode(str(filterObj), errors='replace'))

# generate the protocol
QG = QueryGenerator()
    
protocolXML = QG.getSearchProtocol(NS=schemaObj.NS, respNS=schemaObj.NS, count=False, filterObj=filterObj, destination=wrapper_url)
log.info("QUERY PROTOCOL CREATED:\n%s"%(protocolXML))

# query the wrapper
QD = QueryDispatcher()
try:
    recStatus = QD.sendQuery(wrapper_url, protocolXML, security_role=security_role)
except WrapperError, e:
    # wrapper error occured !!!
    tmpl = PageMacro('Content', PageMacro.DELMODE)
    tmpl.load('Content', '_error.html')
    tmpl['dsa']=dsa
    tmpl['ErrorMessage']=e.msg
    printOverHTTP(tmpl)
    sys.exit()

content = QD.getContent()
logDiagnostics(QD.getDiagnostics())
        
if content is None:
    # no wrapper results found
    log.info("NO CONTENT ROOT FOUND.")
    stylesheetResult = "<strong>%s</strong>" % detailObj.notAvailableMessage
else:
    if log.isEnabledFor(logging.DEBUG):
        log.debug("XML CONTENT TO BE TRANSFORMED::::::::::::::::::")
        log.debug(content)
    # apply stylesheet
    stylesheetResult = transformXML(xmlString=content, xslLoc='file://' + os.path.join(skinDir, detailObj.stylesheet).replace('\\', '/'))
    if log.isEnabledFor(logging.DEBUG):
        log.debug("XSLT TRANSFORMATION RESULT:::::::::::::::::")
        log.debug(stylesheetResult)

# update template
tmpl['dsa'] = dsa
tmpl['schema'] = schema
tmpl['filter'] = str(filterObj)
tmpl['filter_display'] = str(filterObj).replace('_', ' ')
tmpl['XSL'] = stylesheetResult
if wrapper_url is not None:
    tmpl['wrapper_url'] = wrapper_url

#
# print HTML !
#
printOverHTTP( tmpl )
