## $Id: certificate.py 7665 2012-02-17 12:06:10Z henrik $
##
## 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
##
"""SIRP certificates
"""
import grok
from zope.event import notify
from zope.catalog.interfaces import ICatalog
from zope.component import getUtility
from zope.component.interfaces import IFactory, ComponentLookupError
from zope.interface import implementedBy
from waeup.sirp.university.interfaces import (
    ICertificate, ICertificateAdd, ICertificateCourse)
from waeup.sirp.university.vocabularies import course_levels

class Certificate(grok.Container):
    """A certificate.
    """
    grok.implements(ICertificate, ICertificateAdd)

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

    def __init__(self, code=u'NA', title=u'Unnamed Certificate',
                 study_mode=None, start_level=None,
                 end_level=None, application_category=None):
        super(Certificate, self).__init__()
        self.code = code
        self.title = title
        self.study_mode = study_mode
        self.start_level = start_level
        self.end_level = end_level
        self.application_category = application_category

    def longtitle(self):
        return "%s (%s)" % (self.title,self.code)

    def addCourseRef(self, course, level=100, mandatory=True):
        """Add a course referrer.
        """
        code = "%s_%s" % (course.code, level)
        self[code] = CertificateCourse(course, level, mandatory)
        self[code].__parent__ = self
        self[code].__name__ = code
        self._p_changed = True

    def delCourseRef(self, code, level=None):
        """Delete a course referrer denoted by its code.
        """
        keys = list(self.keys()) # create list copy
        for key in keys:
            if self[key].getCourseCode() != code:
                continue
            if level is not None and str(self[key].level) != str(level):
                # found a course with correct key but wrong level...
                continue
            del self[key]
            self._p_changed = True
        return

class CertificateFactory(grok.GlobalUtility):
    """A factory for certificates.
    """
    grok.implements(IFactory)
    grok.name(u'waeup.Certificate')
    title = u"Create a new certificate.",
    description = u"This factory instantiates new certificate instances."

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

    def getInterfaces(self):
        return implementedBy(Certificate)

class CertificateCourse(grok.Model):
    grok.implements(ICertificateCourse)

    def __init__(self, course=None, level=100, mandatory=True):
        self.course = course
        self.level = level
        self.mandatory = mandatory

    def getCourseCode(self):
        """Get code of a course.
        """
        return self.course.code

    def longtitle(self):
        return "%s in level %s" % (self.course.code,
                   course_levels.getTerm(self.level).title)

class CertificateCourseFactory(grok.GlobalUtility):
    """A factory for certificate courses.
    """
    grok.implements(IFactory)
    grok.name(u'waeup.CertificateCourse')
    title = u"Create a new certificate course.",
    description = u"This factory instantiates new certificate courses."

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

    def getInterfaces(self):
        return implementedBy(CertificateCourse)

@grok.subscribe(ICertificate, grok.IObjectRemovedEvent)
def handle_certificate_removed(certificate, event):
    """If a certificate is deleted, we make sure that also referrers to
    student studycourse objects are removed.
    """
    code = certificate.code

    # Find all student studycourses that refer to given certificate...
    try:
        cat = getUtility(ICatalog, name='students_catalog')
    except ComponentLookupError:
        # catalog not available. This might happen during tests.
        return

    results = cat.searchResults(certcode=(code, code))
    for student in results:
        # Remove that referrer...
        studycourse = student['studycourse']
        studycourse.certificate = None
        notify(grok.ObjectModifiedEvent(student))
        student.loggerInfo('ObjectRemovedEvent', 'removed: certificate')
    return
