'Automatic conflict resolution
@param item: the conflicting import item
@param repository: the repository the item comes from
@param resource: the resource the item shall be imported to'

@classmethod
def onconflict(cls, item, repository, resource):

    s3db = current.s3db
    _debug = current.log.debug
    tablename = resource.tablename
    resolver = s3db.get_config(tablename, 'onconflict')
    _debug(('Resolving    conflict    in    %s' % resource.tablename))
    _debug(('Repository:    %s' % repository.name))
    _debug(('Conflicting    item:    %s' % item))
    _debug(('Method:    %s' % item.method))
    if resolver:
        _debug('Applying    custom    rule')
        resolver(item, repository, resource)
        if item.conflict:
            _debug('Do    not    accept')
        else:
            _debug('Accept    per    custom    rule')
    else:
        _debug('Applying    default    rule')
        ttable = s3db.sync_task
        policies = S3ImportItem.POLICY
        query = (((ttable.repository_id == repository.id) & (ttable.resource_name == tablename)) & (ttable.deleted != True))
        task = current.db(query).select(limitby=(0, 1)).first()
        if (task and item.original):
            original = item.original
            conflict_policy = task.conflict_policy
            if (conflict_policy == policies.OTHER):
                _debug('Accept    by    default')
                item.conflict = False
            elif (conflict_policy == policies.NEWER):
                xml = current.xml
                if ((xml.MTIME in original) and (s3_utc(original[xml.MTIME]) <= item.mtime)):
                    _debug('Accept    because    newer')
                    item.conflict = False
                else:
                    _debug('Do    not    accept')
            elif (conflict_policy == policies.MASTER):
                if (((current.xml.MCI in original) and (original.mci == 0)) or (item.mci == 1)):
                    _debug('Accept    because    master')
                    item.conflict = False
                else:
                    _debug('Do    not    accept')
            else:
                _debug('Do    not    accept')
                pass
        else:
            _debug('Accept    because    no    rule    found')
            item.conflict = False
