from . import querying
from logging import getLogger


logger = getLogger("sir")


def index_entity(solr_connection, query, search_entity):
    """
    Indexes a single entity type.

    :param solr.Solr solr_connection:
    :param sqlalchemy.orm.query.Query query:
    :param sir.schema.searchentities.SearchEntity search_entity:
    """
    for row in query:
        data = query_result_to_dict(search_entity, row)
        logger.info("Sending a document to solr: %s", data)
        solr_connection.add(data)


def query_result_to_dict(entity, obj):
    """
    Converts the result of single ``query`` result into a dictionary via the
    field specification of ``entity``.

    :param sir.schema.searchentities.SearchEntity entity:
    :param obj: A :ref:`declarative <sqla:declarative_toplevel>` object.
    """
    data = {}
    for field in entity.fields:
        fieldname = field.name
        tempvals = set()
        for path in field.paths:
            for val in querying._iterate_path_values(path, obj):
                tempvals.add(val)
        if len(tempvals) == 1:
            tempvals = tempvals.pop()
        if field.transformfunc is not None:
            tempvals = field.transformfunc(tempvals)
        data[fieldname] = tempvals
    return data

@@ -855,16 +855,62 @@
 fields:%0A
+        logger.debug(%22Field: %25s%22, field.name)%0A
         
@@ -932,16 +932,16 @@
 ld.name%0A
-
         
@@ -990,16 +990,59 @@
 .paths:%0A
+            logger.debug(%22Path: %25s%22, path)%0A
         
@@ -1132,16 +1132,61 @@
 dd(val)%0A
+        logger.debug(%22Values: %25s%22, tempvals)%0A
         
@@ -1290,16 +1290,16 @@
 t None:%0A
-
         
@@ -1335,32 +1335,110 @@
 mfunc(tempvals)%0A
+            logger.debug(%22Values after applying transformfunc: %25s%22, tempvals)%0A
         data%5Bfie

add some debugging statements to query_result_to_dict