'Searches the tool database and returns data for any tool
whose text matches the query.
Data are returned in JSON format.'

@web.json
def tool_search(self, trans, **kwd):

    query = kwd.get('query', '')
    tags = listify(kwd.get('tags[]', []))
    trans.log_action(trans.get_user(), 'tool_search.search', '', {'query': query, 'tags': tags})
    results = []
    if tags:
        tags = trans.sa_session.query(trans.app.model.Tag).filter(trans.app.model.Tag.name.in_(tags)).all()
        for tagged_tool_il in [tag.tagged_tools for tag in tags]:
            for tagged_tool in tagged_tool_il:
                if (tagged_tool.tool_id not in results):
                    results.append(tagged_tool.tool_id)
        if trans.user:
            trans.user.preferences['selected_tool_tags'] = ','.join([tag.name for tag in tags])
            trans.sa_session.flush()
    elif trans.user:
        trans.user.preferences['selected_tool_tags'] = ''
        trans.sa_session.flush()
    if (len(query) > 2):
        search_results = trans.app.toolbox_search.search(query)
        if ('tags[]' in kwd):
            results = filter((lambda x: (x in results)), search_results)
        else:
            results = search_results
    return results
