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

'''
$RCSfile: details.py,v $
$Revision: 834 $
$Author: markus $
$Date: 2008-04-17 19:59:49 +0200 (Do, 17. 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.querytool.general import *
from biocase.querytool.querydispatcher import QueryDispatcher
from biocase.querytool.querygenerator import QueryGenerator



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


log.info("ORIGINAL FILTER STRING: %s"%unicode(form['filter'].value, errors='replace'))
filterObj = createFilterObjFromString(form['filter'].value, schemaObj)
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()
recStatus = QD.sendQuery(wrapper_url, protocolXML, security_role=security_role)

content = QD.getContent(pretty=True)
logDiagnostics(QD.getDiagnostics())

log.debug("WRAPPER CONTENT FOUND::::::::::::::::::")
log.debug(content)


if content is None:
    # no wrapper results found
    log.info("ERROR: NO PROTOCOL CONTENT FOUND")
    print 'Content-Type: text/plain; charset=utf-8\n\n'
    print '-----------------------------'
    print 'There was no wrapper result !'
    print 'Please check debug log.'
    print '-----------------------------'
else:
    print 'Content-Type: text/xml; charset=utf-8\n'
    print content


