"""
$Id: __init__.py,v 1.2 2005/02/26 17:56:10 sidnei Exp $
"""

from Products.Archetypes.utils import wrap_method
from Products.Archetypes.ReferenceEngine import ReferenceCatalog
from Products.Fate.event import referenceRemoved, referenceAdded

PATTERN = '__Fate__%s__'
def call(self, __name__, *args, **kw):
    return getattr(self, PATTERN % __name__)(*args, **kw)


def _deleteReference(self, referenceObject):
    ret = call(self, '_deleteReference', referenceObject)
    referenceRemoved(referenceObject)
    return ret


def addReference(self, *args, **kwargs):
    ret = call(self, 'addReference', *args, **kwargs)
    # XXX Return value should be the reference. Maybe we should check
    # just to make sure.
    referenceAdded(ret)
    return ret

wrap_method(ReferenceCatalog, 'addReference',
            addReference, pattern=PATTERN)
wrap_method(ReferenceCatalog, '_deleteReference',
            _deleteReference, pattern=PATTERN)
