##############################################################################
# Copyright (c) 2004-2005 Black Rock City, LLC
# Copyright (c) 2004-2005 ObjectRealms, LLC
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
##############################################################################
"""
$Id: relations.py 52774 2007-10-31 14:39:55Z brcwhit $
"""

from Products.Archetypes.ReferenceEngine import Reference
from zope.interface import implements
from interfaces import ITeamMembershipRelation, ITeamSpaceTeamRelation, IMemberTeamRelation


class TeamMembershipRelation(Reference):
    implements(ITeamMembershipRelation)
    relationship = 'Team Membership Relation'

    #@@ use events
    def beforeSourceDeleteInformTarget(self):
        """
        When a member is deleted we want to delete their memberships
        as well
        """
        tObj = self.getTargetObject()
        parent = tObj.aq_inner.aq_parent
        parent._delObject(tObj.id)

    #@@ use events
    def manage_afterAdd(self, item, container):
        """
        Also construct a MemberTeamRelation
        """
        Reference.manage_afterAdd(self, item, container)
        team = self.getTargetObject().getTeam()
        mem = self.getSourceObject()
        mem.addReference(team,
                         relationship=MemberTeamRelation.relationship,
                         referenceClass=MemberTeamRelation)
    #@@ use events
    def manage_beforeDelete(self, item, container):
        """
        Also tear down the MemberTeamRelation
        """
        Reference.manage_beforeDelete(self, item, container)
        team = self.getTargetObject().getTeam()
        mem = self.getSourceObject()
        mem.deleteReference(team,
                            relationship=MemberTeamRelation.relationship)


class MemberTeamRelation(Reference):
    implements(IMemberTeamRelation)
    relationship = 'Member Team Relation'


class TeamSpaceTeamRelation(Reference):
    implements(ITeamSpaceTeamRelation)
    relationship = 'TeamSpace Team Relation'

#@@ add ifaces

class TeamRosterTeamRelation(Reference):
    """
    Relationship between a Team and a TeamRoster
    """
    relationship = 'TeamRoster Team Relation'


class TeamContentTeamRelation(Reference):
    """
    Relationship between a Team and a piece of content that is subject
    to that team's security policy
    """
    relationship = 'Team Content Team Relation'
