from z3c.sqlalchemy.mapper import MappedClassBase

from tdh.metadata import config, utils
from tdh.metadata.sources.base import BaseQuerySource

TABLE_DB_CONNECTION = config.DB_CONNECTIONS['research-services']
TABLE_NAME = 'LU_KEYWORDS'
TABLE_NAME_ABSOLUTE = '%s.%s' % (TABLE_DB_CONNECTION['db-schema'], TABLE_NAME)

class ResearchKeyword(MappedClassBase):
    pass

utils.createAndRegisterSAMapper(
    db_connector=TABLE_DB_CONNECTION['db-connector-id'],
    table_name=TABLE_NAME,
    db_schema=TABLE_DB_CONNECTION['db-schema'],
    table_name_absolute=TABLE_NAME_ABSOLUTE,
    mapper_class=ResearchKeyword,
    primary_keys=['keycode'],
)


def ResearchKeywordQuerySourceFactory():
    return BaseQuerySource(
        db_connector=TABLE_DB_CONNECTION['db-connector-id'],
        table_name_absolute=TABLE_NAME_ABSOLUTE,
        value_field='keyword',
        token_field='keyword',
        title_field='keyword',
        query_fields=['keyword'],
        query_limit=5,
        restricted_to_source=False,
from tdh.metadata import config, utils
from tdh.metadata.sources.base import BaseQuerySource

TABLE_DB_CONNECTION = config.DB_CONNECTIONS['research-services']
TABLE_NAME = 'RESEARCH_CODE_VALUES'
TABLE_NAME_ABSOLUTE = '%s.%s' % (TABLE_DB_CONNECTION['db-schema'], TABLE_NAME)

# Create mapper to the research_code_values table
class AnzrcsCode(MappedClassBase):
    pass

utils.createAndRegisterSAMapper(
    db_connector=TABLE_DB_CONNECTION['db-connector-id'],
    table_name=TABLE_NAME,
    db_schema=TABLE_DB_CONNECTION['db-schema'],
    table_name_absolute=TABLE_NAME_ABSOLUTE,
    mapper_class=AnzrcsCode,
    primary_keys=['research_code', 'research_code_type'],
)

# Creating the SEO codes query source
# - need to restrict the Anzrcs codes to those where research_code_type = "SEO"
# - override the queryCriteria method to achieve this
class SEOCodesQuerySource(BaseQuerySource):

    """Source for querying ANZRCS Social-Economic Objective (SEO) codes.

    This class uses the ANZRCS mapper with a restriction to ensure only
    SEO codes are searched for matches. It also customises the display of
    term titles for research codes."""