## $Id: catalog.py 7819 2012-03-08 22:28:46Z henrik $
##
## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##
"""Components to help cataloging and searching objects.
"""
import grok
from grok import index
from hurry.query import Eq
from hurry.query.interfaces import IQuery
from hurry.query.query import Query
from zope.catalog.catalog import ResultSet
from zope.component import getUtility
from zope.intid.interfaces import IIntIds

from waeup.kofa.app import University
from waeup.kofa.interfaces import IQueryResultItem

# not yet used
class KofaQuery(Query):
    """A hurry.query-like query that supports also ``apply``.
    """
    grok.implements(IQuery)

    def apply(self, query):
        """Get a catalog's BTree set of intids conforming to a query.
        """
        return query.apply()

    def searchResults(self, query):
        """Get a set of ZODB objects conforming to a query.
        """
        results = self.apply(query)
        if results is not None:
            uidutil = getUtility(IIntIds)
            results = ResultSet(results, uidutil)
        return results

grok.global_utility(KofaQuery)

# not yet used
class QueryResultItem(object):
    grok.implements(IQueryResultItem)
    url = None
    title = None
    description = None

    def __init__(self, context, view):
        self.context = context
        self.url = view.url(context)
        self.title = context.title
        self.description = ''
