'Update synchronization settings
@param resource: available resources config
@param filters: filter applied on the resource
@param selected_repository_id: repository that is under current selection
@param properties: comment field of the pr_subscription; used to
store the ids of FTP Sync'

def _update_sync(self, resource, filters, selected_repository_id, properties):

    db = current.db
    s3db = current.s3db
    auth = current.auth
    user_id = auth.user.id
    utcnow = current.request.utcnow
    if properties:
        old_repository_id = properties['repository_id']
        if (old_repository_id != selected_repository_id):
            properties['repository_id'] = selected_repository_id
    else:
        properties = {'repository_id': selected_repository_id}
        old_repository_id = selected_repository_id
    sync_task_table = s3db.sync_task
    query = (((sync_task_table.deleted != True) & (sync_task_table.owned_by_user == user_id)) & (sync_task_table.repository_id == old_repository_id))
    row = db(query).select(sync_task_table.id, sync_task_table.repository_id, limitby=(0, 1)).first()
    if row:
        old_sync_id = properties['sync_task_id']
        if (row.repository_id != selected_repository_id):
            db((sync_task_table.repository_id == old_repository_id)).update(repository_id=selected_repository_id)
        sync_task_id = properties['sync_task_id'] = row.id
    else:
        sync_task_data = {'repository_id': selected_repository_id, 'resource_name': resource, 'mode': 2, 'strategy': ['create'], 'representation': 'cap', 'multiple_file': True, 'last_push': utcnow}
        sync_task_id = sync_task_table.insert(**sync_task_data)
        auth.s3_set_record_owner(sync_task_table, sync_task_id)
        old_sync_id = properties['sync_task_id'] = sync_task_id
    query = (FS('task_id') == old_sync_id)
    s3db.resource('sync_resource_filter', filter=query).delete()
    filters = json.loads(filters)
    filters = [filter_ for filter_ in filters if (filter_[1] is not None)]
    sync_resource_filter_table = s3db.sync_resource_filter
    if (len(filters) > 0):
        for filter_ in filters:
            prefix = str(filter_[0]).strip('[]')
            values = str(filter_[1])
            if (prefix in ['event_type_id__belongs', 'priority__belongs', 'language__belongs']):
                component = 'info'
            else:
                component = 'area_location'
            filter_string = ('%s.%s=%s' % (component, prefix, values))
            resource_filter_data = {'task_id': sync_task_id, 'tablename': resource, 'filter_string': filter_string, 'modified_on': utcnow}
            resource_filter_id = sync_resource_filter_table.insert(**resource_filter_data)
            row = db((sync_resource_filter_table.id == resource_filter_id)).select(limitby=(0, 1)).first()
            auth.s3_set_record_owner(sync_resource_filter_table, resource_filter_id)
            s3db.onaccept(sync_resource_filter_table, row)
    return properties
