## $Id: document.py 12168 2014-12-08 06:17:30Z henrik $
##
## Copyright (C) 2014 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
##
"""
These are the document tickets.
"""
import grok
from time import time
from grok import index
from hurry.workflow.interfaces import IWorkflowInfo, IWorkflowState
from zope.event import notify
from zope.component import getUtility
from zope.component.interfaces import IFactory
from zope.interface import implementedBy
from zope.i18n import translate
from waeup.ikoba.interfaces import IIkobaUtils, IObjectHistory
from waeup.ikoba.interfaces import MessageFactory as _
from waeup.ikoba.utils.helpers import attrs_to_fields, get_current_principal
from waeup.ikoba.documents.interfaces import (
    IDocument, IDocument, IDocumentsUtils)
from waeup.ikoba.documents.utils import generate_document_id

class Document(grok.Container):
    """This is a document.
    """
    grok.implements(IDocument)
    grok.provides(IDocument)
    grok.baseclass()

    user_id = None

    def __init__(self):
        super(Document, self).__init__()
        # The site doesn't exist in unit tests
        try:
            self.document_id = generate_document_id()
        except AttributeError:
            self.document_id = u'd123'
        return

    @property
    def history(self):
        history = IObjectHistory(self)
        return history

    @property
    def state(self):
        state = IWorkflowState(self).getState()
        return state

    @property
    def translated_state(self):
        try:
            TRANSLATED_STATES = getUtility(
                IDocumentsUtils).TRANSLATED_DOCUMENT_STATES
            return TRANSLATED_STATES[self.state]
        except KeyError:
            return

    @property
    def class_name(self):
        return self.__class__.__name__

    @property
    def formatted_transition_date(self):
        try:
            return self.last_transition_date.strftime('%Y-%m-%d %H:%M:%S')
        except AttributeError:
            return

    @property
    def connected_files(self):
        return

    @property
    def is_verifiable(self):
        return True, None

    def setMD5(self):
        """Determine md5 checksum of all files and store checksums as
        document attributes.
        """
        return
        
Document = attrs_to_fields(Document)


class DocumentFactory(grok.GlobalUtility):
    """A factory for documents.
    """
    grok.implements(IFactory)
    grok.name(u'waeup.Document')
    title = u"Create a new document.",
    description = u"This factory instantiates new documents."

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

    def getInterfaces(self):
        return implementedBy(Document)

@grok.subscribe(IDocument, grok.IObjectAddedEvent)
def handle_document_added(document, event):
    """If a document is added the transition create is fired.
    The latter produces a logging message.
    """
    if IWorkflowState(document).getState() is None:
        IWorkflowInfo(document).fireTransition('create')
    return
