#
# -*- coding: utf-8 -*-

''' TBD '''


############################################################################################################
#
#   IMPORTS
#
#===========================================================================================================
from biocase import __version__
from datetime   import datetime
import logging
# debugging to file
log = logging.getLogger("lib.querygenerator")




############################################################################################################
#
#   CLASSES
#
#===========================================================================================================
class QueryGenerator:
    '''A class to generate biocase protocol xml strings.'''
    
    NS_BioCASe = "http://www.biocase.org/schemas/protocol/1.3"
    XSD_BioCASe = "http://www.bgbm.org/biodivinf/Schema/protocol_1_3.xsd"
    
    # ------------------------------------------------------------------------------------
    def getCapabilitiesProtocol(self):
        return u'''<?xml version="1.0" encoding="UTF-8"?>
    <!-- XML generated by biocase.querytool.querygenerator -->
    <request 
        xmlns="%s" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="%s %s">
        <header>
            <version software="querytool">%s</version>
            <sendTime>%s</sendTime>
            <source/>
            <destination/>
            <type>capabilities</type>
        </header>
    </request>''' %(QueryGenerator.NS_BioCASe, QueryGenerator.NS_BioCASe, QueryGenerator.XSD_BioCASe, __version__, str(datetime.now() ))
    
    # ------------------------------------------------------------------------------------
    def getScanProtocol(self, concept, NS="http://www.tdwg.org/schemas/abcd/1.2", filterObj=None):
        if filterObj is not None:
            filter = "\n<filter>%s\n</filter>\n"%filterObj.getProtocolXML()
        else:
            filter = ''
        return u'''<?xml version="1.0" encoding="UTF-8"?>
    <!-- XML generated by biocase.querytool.querygenerator -->
    <request 
        xmlns="%s" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="%s %s">
        <header>
            <version software="querytool">%s</version>
        <sendTime>%s</sendTime>
            <type>scan</type>
        </header>
        <scan>
            <requestFormat>%s</requestFormat>
            <concept>%s</concept>
            %s
        </scan>
    </request>''' %(QueryGenerator.NS_BioCASe, QueryGenerator.NS_BioCASe, QueryGenerator.XSD_BioCASe, __version__, str(datetime.now()), NS, concept, filter)
    

    # ------------------------------------------------------------------------------------
    def getSearchProtocol(self, NS="http://www.tdwg.org/schemas/abcd/1.2", respNS="http://www.tdwg.org/schemas/abcd/1.2", start=0, limit=10, count=False, filterObj=None, destination=''):
        '''Parameter destination should be the access point for the service.'''
        # prepare filter string
        if filterObj is not None:
            filter = filterObj.getProtocolXML()
        else:
            filter = ''
        return u'''<?xml version="1.0" encoding="UTF-8"?>
        <!-- XML generated by biocase.querytool.querygenerator -->
        <request 
            xmlns="%s" 
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
            xsi:schemaLocation="%s %s">
            <header>
                <version software="querytool">%s</version>
                <sendTime>%s</sendTime>
                <source/>
                <destination>%s</destination>
                <type>search</type>
            </header>
            <search>
                <requestFormat>%s</requestFormat>
                <responseFormat start="%i" limit="%i">%s</responseFormat>
                <filter>
                  %s
                </filter>
                <count>%s</count>
            </search>
        </request>
        ''' %(QueryGenerator.NS_BioCASe, QueryGenerator.NS_BioCASe, QueryGenerator.XSD_BioCASe, __version__, str(datetime.now()), destination, NS, start, limit, respNS, filter, str(count))



if __name__ ==  "__main__":
	qg = QueryGenerator()
	print qg.getCapabilitiesProtocol()
	print "-"*40
	print qg.getScanProtocol(concept="/DataSets/DataSet/Units/Unit/UnitID")
	print "-"*40
	print qg.getSearchProtocol()
