## $Id: container.py 7847 2012-03-12 15:31:57Z 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,
    IApplicantsContainerProvider, IApplicant
    )
from waeup.kofa.utils.helpers import attrs_to_fields

def generate_applicant_id(container=None):
    if container is not None:
        aid = u"%s_%d" % (container.code, r().randint(99999,1000000))
        while aid in container.keys():
            aid = u"%s_%d" % (container.code, r().randint(99999,1000000))
        return aid
    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)

    #: These are 'class-attributes'. Do not fiddle around with it in
    #: instances of this class.
    #:
    #: The title of this container as displayed in add-forms. It should
    #: give some idea about what kind of container this is (one or two words)
    container_title = _(u'Basic')
    #: The `container_description` gives a short explanation about for what
    #: purposes this container type serves (a few words only).
    container_description = _(u'handles basic applicants')
    #: `iface` is the interface of the child objects.
    iface = IApplicant
    #: The name of the child object factory. This name is used in views
    #: to create proper applicant objects.
    factory_name = 'waeup.Applicant'

    @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 self.iface.providedBy(applicant):
            raise TypeError(
                '%s containers contain only %s instances' %(
                self.container_title, self.iface))
        applicant_id = generate_applicant_id(container=self)
        applicant.applicant_id = applicant_id
        self[applicant.application_number] = applicant
        return

ApplicantsContainer = attrs_to_fields(ApplicantsContainer)

class ApplicantsContainerProvider(grok.GlobalUtility):
    """A utility that provides basic applicants containers.
    """
    grok.implements(IApplicantsContainerProvider)
    grok.name('waeup.kofa.applicants.ApplicantsContainer')

    #: The applicants container type this provider provides:
    #: :class:`ApplicantsContainer`.
    factory = ApplicantsContainer

factory = Factory(lambda : ApplicantsContainer, 'ApplicantsContainer')
grok.global_utility(factory, IFactory, name='waeup.ApplicantsContainer')
