## $Id: export.py 12249 2014-12-15 16:27:44Z henrik $
##
## Copyright (C) 2012 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
##
"""Exporters for documents.
"""
import grok
from zope.securitypolicy.interfaces import IPrincipalRoleMap
from waeup.ikoba.interfaces import ICSVExporter
from waeup.ikoba.interfaces import MessageFactory as _
from waeup.ikoba.utils.batching import ExporterBase
from waeup.ikoba.utils.helpers import iface_names
from waeup.ikoba.documents.interfaces import IPDFDocument, IHTMLDocument


class PDFDocumentExporter(grok.GlobalUtility, ExporterBase):
    """Exporter for documents.
    """
    grok.implements(ICSVExporter)
    grok.name('pdfdocuments')

    #: Fieldnames considered by this exporter
    fields = tuple(sorted(iface_names(IPDFDocument))) + (
        'users_with_local_roles',)

    #: The title under which this exporter will be displayed
    title = _(u'Public PDF Documents')

    def mangle_value(self, value, name, context=None):
        """Hook for mangling values in derived classes
        """
        if name == 'users_with_local_roles':
            value = []
            role_map = IPrincipalRoleMap(context)
            for local_role, user_name, setting in role_map.getPrincipalsAndRoles():
                value.append({'user_name':user_name,'local_role':local_role})
        return super(PDFDocumentExporter, self).mangle_value(
            value, name, context)

    def export(self, documents, filepath=None):
        """Export `documents`, an iterable, as CSV file.

        If `filepath` is ``None``, a raw string with CSV data is returned.
        """
        writer, outfile = self.get_csv_writer(filepath)
        for document in documents:
            self.write_item(document, writer)
        return self.close_outfile(filepath, outfile)

    def export_all(self, site, filepath=None):
        """Export documents in documentscontainer into filepath as CSV data.

        If `filepath` is ``None``, a raw string with CSV data is returned.
        """
        documents = site.get('documents', {})
        return self.export(documents.values(), filepath)


class HTMLDocumentExporter(PDFDocumentExporter):
    """Exporter for documents.
    """
    grok.name('htmldocuments')

    #: Fieldnames considered by this exporter
    fields = tuple(sorted(iface_names(IHTMLDocument))) + (
        'users_with_local_roles',)

    #: The title under which this exporter will be displayed
    title = _(u'Public HTML Documents')