## $Id: department.py 7733 2012-02-29 19:10:48Z uli $
##
## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##
"""University departments.
"""
import grok
from zope.component.interfaces import IFactory
from zope.interface import implementedBy
from zope.component import getUtility
from waeup.sirp.university.coursescontainer import CoursesContainer
from waeup.sirp.university.certificatescontainer import CertificatesContainer
from waeup.sirp.interfaces import ISIRPUtils
from waeup.sirp.university.interfaces import IDepartment, IDepartmentAdd

class Department(grok.Container):
    """A university department.
    """
    grok.implements(IDepartment, IDepartmentAdd)

    @property       # Make this method read_only and looking like an attr.
    def local_roles(self):
        return ['waeup.local.DepartmentManager',
                'waeup.local.ClearanceOfficer',
                'waeup.local.CourseAdviser100',
                'waeup.local.CourseAdviser200',
                'waeup.local.CourseAdviser300',
                'waeup.local.CourseAdviser400',
                'waeup.local.CourseAdviser500',
                'waeup.local.CourseAdviser600',
                ]

    # A simple counter for ids.
    next_id = 0L

    def __init__(self,
                 title=u'Unnamed Department',
                 title_prefix=u'department',
                 code=u"NA", **kw):
        super(Department, self).__init__(**kw)
        self.title = title
        self.title_prefix = title_prefix
        self.code = code
        self.courses = CoursesContainer()
        self.courses.__parent__ = self
        self.courses.__name__ = 'courses'
        self.certificates = CertificatesContainer()
        self.certificates.__parent__ = self
        self.certificates.__name__ = 'certificates'

    def traverse(self, name):
        """Deliver appropriate containers, if someone wants to go to courses
        or departments.
        """
        if name == 'courses':
            return self.courses
        elif name == 'certificates':
            return self.certificates
        return None

    def longtitle(self):
        insttypes_dict = getUtility(ISIRPUtils).getInstTypeDict()
        return "%s %s (%s)" % (
            insttypes_dict[self.title_prefix],
            self.title, self.code)


class DepartmentFactory(grok.GlobalUtility):
    """A factory for department containers.
    """
    grok.implements(IFactory)
    grok.name(u'waeup.Department')
    title = u"Create a new department.",
    description = u"This factory instantiates new department instances."

    def __call__(self, *args, **kw):
        return Department(*args, **kw)

    def getInterfaces(self):
        """Get interfaces of objects provided by this factory.
        """
        return implementedBy(Department)
