'Find the original record for a possible duplicate:
- if the record contains a UUID, then only that UUID is used
to match the record with an existing DB record
- otherwise, if the record contains some values for unique
fields, all of them must match the same existing DB record
@param table: the table
@param record: the record as dict or S3XML Element'

@classmethod
def original(cls, table, record, mandatory=None):

    db = current.db
    xml = current.xml
    xml_decode = xml.xml_decode
    VALUE = xml.ATTRIBUTE['value']
    UID = xml.UID
    ATTRIBUTES_TO_FIELDS = xml.ATTRIBUTES_TO_FIELDS
    pkeys = [f for f in table.fields if table[f].unique]
    pvalues = Storage()
    get = record.get
    if (type(record) is etree._Element):
        xpath = record.xpath
        xexpr = ("%s[@%s='%%s']" % (xml.TAG['data'], xml.ATTRIBUTE['field']))
        for f in pkeys:
            v = None
            if ((f == UID) or (f in ATTRIBUTES_TO_FIELDS)):
                v = get(f, None)
            else:
                child = xpath((xexpr % f))
                if child:
                    child = child[0]
                    v = child.get(VALUE, xml_decode(child.text))
            if v:
                pvalues[f] = v
    elif isinstance(record, dict):
        for f in pkeys:
            v = get(f, None)
            if v:
                pvalues[f] = v
    else:
        raise TypeError
    query = None
    for f in pvalues:
        if (f == UID):
            continue
        _query = (table[f] == pvalues[f])
        if (query is not None):
            query = (query | _query)
        else:
            query = _query
    fields = cls.import_fields(table, pvalues, mandatory=mandatory)
    if (query is not None):
        original = db(query).select(limitby=(0, 2), *fields)
        if (len(original) == 1):
            return original.first()
    if (UID in pvalues):
        uid = xml.import_uid(pvalues[UID])
        query = (table[UID] == uid)
        original = db(query).select(limitby=(0, 1), *fields).first()
        if original:
            return original
    return None
