'Set the training_event realm entity to the root Org of the Site'

@staticmethod
def hrm_training_event_realm_entity(table, record):

    db = current.db
    stable = db.org_site
    query = (stable.site_id == record.site_id)
    if current.deployment_settings.get_org_branches():
        site = db(query).select(stable.organisation_id, limitby=(0, 1)).first()
        if site:
            org_id = site.organisation_id
            root_org = current.cache.ram(('root_org_%s' % org_id), (lambda : current.s3db.org_root_organisation(org_id)), time_expire=120)
            otable = db.org_organisation
            org = db((otable.id == root_org)).select(otable.realm_entity, limitby=(0, 1)).first()
            if org:
                return org.realm_entity
    else:
        otable = db.org_organisation
        query &= (stable.organisation_id == otable.id)
        org = db(query).select(otable.realm_entity, limitby=(0, 1)).first()
        if org:
            return org.realm_entity
