# -*- coding: utf8 -*-
'''
R E Q U E S T - C L A S S
**********************************************
*       R E Q U E S T - C L A S S
*       base class for request parsers
**********************************************
$RCSfile: sp2k_response.py,v $
$Revision: 1231 $
$Author: j.holetschek $
$Date: 2012-11-21 15:12:34 +0100 (Mi, 21. Nov 2012) $

Base class to generate a request object with the help of
a protocol specific request parser.
'''

import types
from xml.sax                            import parse, parseString
from biocase.wrapper.protocol.base_request     import RequestBaseClass
from biocase.wrapper.protocol.base_response     import ResponseBaseClass
from cgi                                 import FieldStorage
from os.path                             import isfile
import xml.etree.ElementTree as ET
from biocase.tools.xmlutils              import element, subelement, addOptionalAttributes
from biocase.wrapper.errorclasses         import *
from biocase.wrapper.protocol.base_request     import RequestBaseClass
from biocase.wrapper.sql.operators        import compressOperator
import biocase.wrapper
from biocase.wrapper.operations       import getOperationsObjectForDBMod
from biocase.wrapper.typeconverter    import DBtransformerClass
from biocase.wrapper.operations   import recordStatus
from datetime        import datetime

import biocase.configuration
cfg = biocase.configuration.Cfg()

import logging
log = logging.getLogger("pywrapper.protocol.sp2k")

# ------------------------------------------------------------------------------------
class ResponseClass(ResponseBaseClass):
    '''Response class implementing the species2000 protocol for responses.'''
    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    def __init__(self, dsaObj, diagnosticsHandler):
        ResponseBaseClass.__init__(self, dsaObj, diagnosticsHandler)
        # status about paging, record count etc.
        # self.recordStatus = recordStatus()
        
    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    def init(self):
        '''No doc to init.'''
        pass

    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    def setSearchContent(self, etree, recStatus=None):
        self.type = "search"
        if etree is not None:
            self.content = etree
        else:
            self.content = element(None, 'XMLRESPONSE')
            if self.requestObj.rtype == '1':
                #<TYPE1RESULT NUMBER="0" />
                subelement(self.content, None, 'TYPE1RESULT', NUMBER=0)
            else:
                log.info("No content provided by search.")
        self.recordStatus = recStatus

    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    def getProtocolDocument(self, dbmod=None):
        # add metadata comments to output
        # add DBmod used:
        if dbmod is not None:            
            self.doc.append(ET.Comment("Using pywrapper database module %s."%dbmod))
        # add timestamp (2001-09-11T09:30:47-05:00)
        self.doc.append(ET.Comment("Send time: %s."% str(datetime.now())))
        # add content
        if ET.iselement(self.content):
            self.doc.append(self.content)
        else:
            log.info("No content provided by search.")
        # add errors as HTML comments
        for err in PyWrapperError.lastErrors:
            self.doc.append(ET.Comment("ERROR CODE=%s: %s"%(err.code, err.msg)))
        #
        # return protocol document as ET
        return ResponseBaseClass.getProtocolDocument(self)
        
        

