'Import data from an S3XML element tree.
@param id: record ID or list of record IDs to update
@param tree: the element tree
@param ignore_errors: continue at errors (=skip invalid elements)
@param job_id: restore a job from the job table (ID or UID)
@param delete_job: delete the import job from the job table
@param commit_job: commit the job (default)
@todo: update for link table support'

def import_tree(self, id, tree, job_id=None, ignore_errors=False, delete_job=False, commit_job=True, strategy=None, update_policy=None, conflict_policy=None, last_sync=None, onconflict=None):

    from s3import import S3ImportJob
    db = current.db
    xml = current.xml
    auth = current.auth
    tablename = self.tablename
    table = self.table
    if (job_id is not None):
        self.error = None
        self.error_tree = None
        try:
            import_job = S3ImportJob(table, job_id=job_id, strategy=strategy, update_policy=update_policy, conflict_policy=conflict_policy, last_sync=last_sync, onconflict=onconflict)
        except:
            self.error = current.ERROR.BAD_SOURCE
            return False
        if delete_job:
            import_job.delete()
            return True
        job_id = import_job.job_id
        item_table = import_job.item_table
        items = db((item_table.job_id == job_id)).select()
        load_item = import_job.load_item
        for item in items:
            success = load_item(item)
            if (not success):
                self.error = import_job.error
                self.error_tree = import_job.error_tree
        import_job.restore_references()
        if commit_job:
            if (self.error and (not ignore_errors)):
                return False
        else:
            return import_job
        import_prep = current.response.s3.import_prep
        if import_prep:
            tree = import_job.get_tree()
            callback(import_prep, (self, tree), tablename=tablename)
            if self.skip_import:
                current.log.debug(('Skipping    import    to    %s' % self.tablename))
                self.skip_import = False
                return True
    else:
        if ('id' not in table.fields):
            self.error = current.ERROR.BAD_RESOURCE
            return False
        self.error = None
        self.error_tree = None
        import_prep = current.response.s3.import_prep
        if import_prep:
            if (not isinstance(tree, etree._ElementTree)):
                tree = etree.ElementTree(tree)
            callback(import_prep, (self, tree), tablename=tablename)
            if self.skip_import:
                current.log.debug(('Skipping    import    to    %s' % self.tablename))
                self.skip_import = False
                return True
        elements = xml.select_resources(tree, tablename)
        if (not elements):
            return True
        UID = xml.UID
        if (id and (UID in table)):
            if (not isinstance(id, (tuple, list))):
                query = (table._id == id)
            else:
                query = table._id.belongs(id)
            originals = db(query).select(table[UID])
            uids = [row[UID] for row in originals]
            matches = []
            import_uid = xml.import_uid
            append = matches.append
            for element in elements:
                element_uid = import_uid(element.get(UID, None))
                if (not element_uid):
                    continue
                if (element_uid in uids):
                    append(element)
            if (not matches):
                first = elements[0]
                if (len(elements) and (not first.get(UID, None))):
                    first.set(UID, uids[0])
                    matches = [first]
            if (not matches):
                self.error = current.ERROR.NO_MATCH
                return False
            else:
                elements = matches
        import_job = S3ImportJob(table, tree=tree, files=self.files, strategy=strategy, update_policy=update_policy, conflict_policy=conflict_policy, last_sync=last_sync, onconflict=onconflict)
        add_item = import_job.add_item
        for element in elements:
            success = add_item(element=element, components=self.components)
            if (not success):
                self.error = import_job.error
                self.error_tree = import_job.error_tree
        if (self.error and (not ignore_errors)):
            return False
    auth.rollback = (not commit_job)
    success = import_job.commit(ignore_errors=ignore_errors, log_items=self.get_config('oncommit_import_item'))
    auth.rollback = False
    self.error = import_job.error
    self.import_count += import_job.count
    self.import_created += import_job.created
    self.import_updated += import_job.updated
    self.import_deleted += import_job.deleted
    job_mtime = import_job.mtime
    if ((self.mtime is None) or (job_mtime and (job_mtime > self.mtime))):
        self.mtime = job_mtime
    if self.error:
        if ignore_errors:
            self.error = ('%s    -    invalid    items    ignored' % self.error)
        self.error_tree = import_job.error_tree
    elif (not success):
        raise RuntimeError('Import    failed    without    error    message')
    if ((not success) or (not commit_job)):
        db.rollback()
    if (not commit_job):
        import_job.store()
        return import_job
    elif (job_id is not None):
        import_job.delete()
    return ((self.error is None) or ignore_errors)
