## $Id: container.py 8601 2012-06-02 11:25:33Z 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
##
"""
Containers for university applicants.
"""
from random import SystemRandom as r
import grok
from zope.component.factory import Factory
from zope.component.interfaces import IFactory
from waeup.kofa.interfaces import MessageFactory as _
from waeup.kofa.applicants.interfaces import (
    IApplicantsContainer, IApplicantsContainerAdd, IApplicant
    )
from waeup.kofa.utils.helpers import attrs_to_fields
from waeup.kofa.applicants.workflow import (INITIALIZED,
    STARTED, PAID, ADMITTED, NOT_ADMITTED, SUBMITTED, CREATED)

def generate_applicant_id(container=None):
    if container is not None:
        key = r().randint(99999,1000000)
        while str(key) in container.keys():
            key = r().randint(99999,1000000)
        return u"%s_%d" % (container.code, key)
    else:
        # In some tests we don't use containers
        return u"xxx_1234"

class ApplicantsContainer(grok.Container):
    """An applicants container contains university applicants.
    """
    grok.implements(IApplicantsContainer,IApplicantsContainerAdd)

    #: A dictionary to hold per language translations of description string.
    description_dict = {}

    @property
    def local_roles(self):
        return []

    def archive(self, app_ids=None):
        """Create on-dist archive of applicants stored in this term.

        If app_ids is `None`, all applicants are archived.

        If app_ids contains a single id string, only the respective
        applicants are archived.

        If app_ids contains a list of id strings all of the respective
        applicants types are saved to disk.
        """
        raise NotImplementedError()

    def clear(self, app_ids=None, archive=True):
        """Remove applicants of type given by 'id'.

        Optionally archive the applicants.

        If id is `None`, all applicants are archived.

        If id contains a single id string, only the respective
        applicants are archived.

        If id contains a list of id strings all of the respective
        applicant types are saved to disk.

        If `archive` is ``False`` none of the archive-handling is done
        and respective applicants are simply removed from the
        database.
        """
        raise NotImplementedError()

    def addApplicant(self, applicant):
        """Add an applicant.
        """
        if not IApplicant.providedBy(applicant):
            raise TypeError(
                'ApplicantsContainers contain only IApplicant instances')
        if applicant.applicant_id is None:
            applicant_id = generate_applicant_id(container=self)
            applicant.applicant_id = applicant_id
        self[applicant.application_number] = applicant
        return

    @property
    def statistics(self):
        state_stats = {INITIALIZED:0, STARTED:0, PAID:0, SUBMITTED:0,
            ADMITTED:0, NOT_ADMITTED:0, CREATED:0}
        faculty_keys = grok.getSite()['faculties'].keys()
        fac_stats = dict([(i,0) for i in faculty_keys])
        for key in self.keys():
            state = self[key].state
            state_stats[state] += 1
            cert = getattr(self[key],'course1',None)
            if  cert is not None and state == SUBMITTED:
                faculty = cert.__parent__.__parent__.__parent__
                fac_stats[faculty.__name__] += 1
        return state_stats, fac_stats

ApplicantsContainer = attrs_to_fields(ApplicantsContainer)

# ApplicantsContainers must be importable. So we need a factory.
class ApplicantsContainerFactory(grok.GlobalUtility):
    """A factory for student online payments.
    """
    grok.implements(IFactory)
    grok.name(u'waeup.ApplicantsContainer')
    title = u"Create a new container for applicants.",
    description = u"This factory instantiates new IApplicantsContainer instances."

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

    def getInterfaces(self):
        return implementedBy(ApplicantsContainer)
