##############################################################################
# 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.
##############################################################################
"""
Description:

Created: March 31, 2004
$Id: membership.py 46903 2007-08-09 05:57:19Z rafrombrc $
"""

from sets import Set

from zope.interface import implements

from Products.CMFCore.utils import getToolByName

from Products.TeamSpace.exceptions import MemberRoleNotAllowed
from Products.TeamSpace.permissions import *
from Products.TeamSpace.relations import TeamMembershipRelation
from Products.TeamSpace.zimport import *
from Products.TeamSpace.interfaces.membership import ITeamMembership

schema = BaseContent.schema.copy()
schema['title'].default_method = 'getMemberFullname'

class TeamMembership( BaseContent ):
    """
    Stores member's data related to a single team
    """
    meta_type = 'Team Membership'

    workflow_id = 'team_membership_workflow'
    portal_type = meta_type
    global_allow = 0

    schema = schema

    implements(ITeamMembership) 

    security = ClassSecurityInfo()

    def __init__(self, id):
        BaseContent.__init__(self, id)
        # annotations serves as storage for the member's team schema
        # values
        self._annotations = PersistentMapping()
        self._team_roles = Set()

    def getTeamRoles(self):
        """
        get the contextual roles given to the member by this
        membership.
        """
        return list(self._team_roles)

    def getSecurityRoles(self):
        return self.getTeamRoles()

    security.declareProtected(ManageTeam, 'editTeamRoles')
    def editTeamRoles(self, team_roles):
        """
        edit the team roles filtering roles based on team policy
        """
        team = self.getTeam()
        allowed_team_roles = team.getAllowedTeamRoles()
        for tr in team_roles:
            if not tr in allowed_team_roles:
                raise MemberRoleNotAllowed( str(tr) )

        self._team_roles = Set(team_roles)

    def getMemberFullname(self):
        """
        get the fullname value of the associated member; used as a
        default for the title field
        """
        mem = self.getMember()
        fullname = mem.getProperty("fullname")
        return fullname

    def getTeam(self):
        """
        get the team that the membership is for
        """
        return aq_parent(aq_inner(self))

    def getMember(self):
        """
        retrieve the member for the membership
        """
        mems = self.getBRefs(relationship=TeamMembershipRelation.relationship)
        if mems:
            mem = mems[0] # there should only be one...
        else:
            # fail over to just getting the member object
            # XXX: it's lame to depend on matching id
            mtool = getToolByName(self, 'portal_membership')
            mem = mtool.getMemberById(self.id)
        return mem

    def initializeArchetype(self, **kw):
        BaseContent.initializeArchetype(self, **kw)

registerType( TeamMembership )

