'Get a list of aliases of components that shall be exported
together with the master resource
@param tablename: the tablename of the master resource
@param aliases: the list of required components
@returns: a list of component aliases'

@staticmethod
def components_to_export(tablename, aliases):

    s3db = current.s3db
    if (aliases is not None):
        names = (aliases if aliases else None)
        hooks = s3db.get_components(tablename, names=names)
    else:
        hooks = {}
    hierarchy_link = s3db.hierarchy_link(tablename)
    (filtered, unfiltered) = ({}, {})
    for (alias, hook) in hooks.items():
        key = (hook.tablename, hook.pkey, hook.lkey, hook.rkey, hook.fkey)
        if hook.filterby:
            filtered[alias] = key
        else:
            unfiltered[key] = alias
    components = []
    for (alias, key) in filtered.items():
        if (key in unfiltered):
            if (alias == hierarchy_link):
                hierarchy_link = unfiltered[key]
            continue
        if (alias != hierarchy_link):
            components.append(alias)
    for (key, alias) in unfiltered.items():
        if (alias != hierarchy_link):
            components.append(alias)
    if hierarchy_link:
        components.append(hierarchy_link)
    return components
