"""
Service that will find the most commonly used locations in events (ideally these would be the most popular) that are
defined by foursquare data and return them back.
"""

import logging
import json

from rhobot.components.storage.enums import CypherFlags

from sleekxmpp.plugins.base import base_plugin
from rhobot.namespace import WGS_84, SCHEMA, EVENT, LOCATION
from rdflib.namespace import RDFS

logger = logging.getLogger(__name__)


class SearchHandler(base_plugin):
    """
    Search the database for the content
    """
    name = 'search_handler'
    description = 'Knowledge Provider'
    dependencies = {'rho_bot_storage_client', 'rho_bot_rdf_publish', }

    type_requirements = {str(WGS_84.SpatialThing), }

    def plugin_init(self):
        pass

    def post_init(self):
        base_plugin.post_init(self)
        self.xmpp['rho_bot_rdf_publish'].add_search_handler(self._rdf_request_message)

    def _rdf_request_message(self, rdf_payload):
        """
        Find node to do work over.
        :return:
        """
        query = """MATCH (n:`%s`:`%s`)<-[r:`%s`]-(m)
                   RETURN n AS node, count(r) AS rels, n.`%s` AS name
                   ORDER BY rels DESC LIMIT 10""" % (str(WGS_84.SpatialThing),
                                                     str(LOCATION.Address),
                                                     str(EVENT.place),
                                                     str(SCHEMA.name))

        translation_key = dict()
        translation_key.update(json.loads(CypherFlags.TRANSLATION_KEY.value['default']))
        translation_key[str(SCHEMA.name)] = 'name'
        translation_key['http://degree'] = 'rels'

        params = {CypherFlags.TRANSLATION_KEY.value['var']: json.dumps(translation_key)}

        logger.debug('Executing query: %s' % query)

        result = self.xmpp['rho_bot_storage_client'].execute_cypher(query, **params)

        print 'Found: %s results' % len(result.results)

        for res in result.results:
            print '  %s (%s)' % (res.flags[str(SCHEMA.name)], res.flags['http://degree'])

        return result


search_handler = SearchHandler

@@ -2111,16 +2111,22 @@
 n result
+, None
 %0A%0A%0Asearc

Update to match search return api change.