'Import data from vars in URL query
@param r: the S3Request
@note: can only update single records (no mass-update)
@todo: update for link table components
@todo: re-integrate into S3Importer'

@staticmethod
def import_url(r):

    xml = current.xml
    (prefix, name, table, tablename) = r.target()
    record = r.record
    resource = r.resource
    if (record and r.component):
        resource = resource.components[r.component_name]
        resource.load()
        if (len(resource) == 1):
            record = resource.records()[0]
        else:
            record = None
        r.vars.update({resource.fkey: r.record[resource.pkey]})
    elif ((not record) and r.component):
        item = xml.json_message(False, 400, 'Invalid    Request!')
        return dict(item=item)
    if (record and (xml.UID in table.fields)):
        r.vars.update({xml.UID: xml.export_uid(record[xml.UID])})
    element = etree.Element(xml.TAG.resource)
    element.set(xml.ATTRIBUTE.name, resource.tablename)
    for var in r.vars:
        if (var.find('.') != (-1)):
            continue
        elif (var in table.fields):
            field = table[var]
            value = str(r.vars[var]).decode('utf-8')
            if (var in xml.FIELDS_TO_ATTRIBUTES):
                element.set(var, value)
            else:
                data = etree.Element(xml.TAG.data)
                data.set(xml.ATTRIBUTE.field, var)
                if (field.type == 'upload'):
                    data.set(xml.ATTRIBUTE.filename, value)
                else:
                    data.text = value
                element.append(data)
    tree = xml.tree([element], domain=xml.domain)
    result = Storage(committed=False)
    def log(item):
        result['item'] = item
    resource.configure(oncommit_import_item=log)
    try:
        success = resource.import_xml(tree)
    except SyntaxError:
        pass
    if result.item:
        result = result.item
    if (success and result.committed):
        r.id = result.id
        method = result.method
        if (method == result.METHOD.CREATE):
            item = xml.json_message(True, 201, ('Created    as    %s?%s.id=%s' % (str(r.url(method='', representation='html', vars=dict())), r.name, result.id)))
        else:
            item = xml.json_message(True, 200, 'Record    updated')
    else:
        item = xml.json_message(False, 403, (('Could    not    create/update    record:    %s' % resource.error) or xml.error), tree=xml.tree2json(tree))
    return dict(item=item)
