'Find all name matches for the given import item
@param item: the import item
@return: a dict {id: name, parent: parent_id} of records which
match the import item by name, or alternatively by
local name if enabled and no direct name match'

@classmethod
def name_match(cls, item):

    matches = {}
    name = item.data.get('name')
    if (not name):
        return matches
    db = current.db
    s3db = current.s3db
    table = item.table
    lower_name = s3_unicode(name).lower()
    query = ((table.name.lower() == lower_name) & (table.deleted == False))
    rows = db(query).select(table.id, table.name)
    if ((not rows) and current.deployment_settings.get_L10n_translate_org_organisation()):
        ltable = s3db.org_organisation_name
        query = (((ltable.name_l10n.lower() == lower_name) & (ltable.organisation_id == table.id)) & (ltable.deleted != True))
        rows = db(query).select(table.id, table.name)
    if rows:
        matches = dict(((row.id, {'name': row.name}) for row in rows))
        btable = s3db.org_organisation_branch
        query = (btable.branch_id.belongs(matches.keys()) & (btable.deleted != True))
        links = db(query).select(btable.organisation_id, btable.branch_id)
        for link in links:
            matches[link.branch_id]['parent'] = link.organisation_id
    return matches
