##############################################################################
# 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.
##############################################################################
"""
Customizes the Plone site so TeamSpaces is installed
and includes useful team-specific workflows and roles
"""

from Products.CMFPlone.Portal import addPolicy
from Products.CMFPlone.interfaces.CustomizationPolicy import ICustomizationPolicy
from Products.CMFPlone.CustomizationPolicy import DefaultCustomizationPolicy
from Products.CMFCore.utils import getToolByName

def register(context, app_state):
    addPolicy('TeamSpace Site', BasicTeamPolicy())

class BasicTeamPolicy(DefaultCustomizationPolicy):
    """
    Customizes the Plone site so TeamSpaces is installed
    and includes useful team-specific workflows and roles
    """
    __implements__ = ICustomizationPolicy

    availableAtConstruction = True
    
    def customize(self, portal):
        DefaultCustomizationPolicy().customize(portal)

        portal.portal_quickinstaller.installProducts(["TeamSpace"], stoponerror=True)

        # add team-specific roles
        DEFAULT_ROLES = ('TeamMember', 'TeamReviewer')
        vr = portal.validRoles()
        for role in DEFAULT_ROLES:
            if not role in vr:
                portal.manage_defined_roles(submit='Add Role',
                                            REQUEST={'role': role})


        # add new workflows
        wf_tool = getToolByName(portal, 'portal_workflow')
        t_tool = getToolByName(portal, 'portal_types')
        
        workflows = {'plone_team_workflow':
                     'plone_team_workflow (Default Team-centric Workflow [Plone])',
                     'folder_team_workflow':
                     'folder_team_workflow (Team-centric Folder Workflow [Plone])'
                     }

        for wf in workflows:
            wf_tool.manage_addWorkflow(workflows[wf], wf)

        # change workflow associations
        f_wf = 'folder_workflow'
        types = t_tool.listContentTypes()
        ftypes = [t for t in types if f_wf in
                  wf_tool.getChainForPortalType(t)]

        for type in ftypes:
            chain = list(wf_tool.getChainForPortalType(type))
            chain[chain.index(f_wf)] = 'folder_team_workflow'
            chain = tuple(chain)
            wf_tool.setChainForPortalTypes([type], chain)

        wf_tool.setDefaultChain('plone_team_workflow')

        tm_tool = getToolByName(portal, 'portal_teams')
        tm_tool.setDefaultAllowedRoles(DEFAULT_ROLES)
        tm_tool.setDefaultRoles(['TeamMember'])

        # tweak site-wide security settings
        portal.manage_permission('Add portal content',
                                 roles=['Manager', 'Owner',
                                        'TeamMember'], acquire=1)
        portal.manage_permission('Add portal folders',
                                 roles=['Manager', 'Owner',
                                        'TeamMember'], acquire=1)
        portal.manage_permission('Add portal events',
                                 roles=['Manager', 'Owner',
                                        'TeamMember'], acquire=1)
        
