##############################################################
# zNote Bibliography Management Tool v0.6
#
# copyright 2001, 2002 John Maxwell - jmax@portal.ca
#
# This product is licensed under the GNU General Public License. 
# Please see license.txt for details.
#
#  zNote 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.
# 
#  zNote 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.
#
#################################################################


import string
from OFS import Folder
from Globals import DTMLFile
from Products.ZCatalog import ZCatalog


class zNote(Folder.Folder):
    "zNote Folder Class"
    # sets up a zNote folder with tools for managing 
    # ParsedXML-based bibliographic entries

    meta_type = "zNote Folder"
    
    # couple of code modules:
    from format import *
    from formActions import newEntryMethod

    # Format modules. Add more as needed!
    from Style_APA import APA
    from Style_Chicago import Chicago

    
    manage_options=(
        (
          {'label': 'Manage', 'action': 'manage_editForm' ,},
        )+
        (Folder.ObjectManager.ObjectManager.manage_options[0],)
        )
    
    
    def __init__(self, id, title='zNotes', initialNID=0, style='APA'):
        "# initialize new instance"
        self.id = id  # NOT optional
        self.title = title
        self.NID = initialNID
        self.style = style
        self.colour1 = '#ffeebb'
        self.colour2 = '#443377'
        self.colour3 = '#eee4ee'
        self.colour4 = '#ffffee'
        #
        self.tempbuffer = []
        # Add a ZCatalog:
        ZCatalog.manage_addZCatalog(self,'ZEntry_catalog', 'zEntry ZCatalog')
        
        
    def manage_editAction(self, title, style, colour1, colour2, colour3, colour4, RESPONSE=None):
        "basic management interface"
        self.title = title
        self.style = style
        self.colour1 = colour1
        self.colour2 = colour2
        self.colour3 = colour3
        self.colour4 = colour4
        self._p_changed = 1  
        RESPONSE.redirect('./manage_editForm')
    
    
    def importEntries(self, file, RESPONSE=None):
        "form-handler to import from external XML file"
        # This takes an XML file from the regular browser-form-upload
        # makes one big ParsedXML object, then breaks this object up 
        # into individual entries.
        #
        # First, bring in the XML and store it as a temporary object
        self.manage_addProduct['ParsedXML'].manage_addParsedXML(
            id='TempImport', 
            contentType="text/xml", 
            useNamespaces=0, 
            file=file )
        # Next, pull it apart and make new entries out of the 
        # significant nodes 
        c = 0
        root = self.TempImport.documentElement
        for thisName in ['book', 'booksection', 'jarticle', 'thesis', 'esource', 'proceedings']:
            for myNode in root.getElementsByTagName(thisName):
                newId = 'z' + string.zfill(str(self.NID + 1), 5) + '.xml'
                # form the title:
                # first, the authors
                AuthsList = []
                AEs = myNode.childNodes
                for AE in AEs:
                    if AE.nodeName == 'author' or AE.nodeName == 'editor':
                        LN = AE.getElementsByTagName('lastname')[0]
                        AuthsList.append(LN.firstChild.nodeValue)
                # then date and title
                Date = myNode.getElementsByTagName('date')[0].firstChild.nodeValue
                fullTitle = myNode.getElementsByTagName('title')[0].firstChild.nodeValue
                Title = fullTitle.split(':')[0]
                Title = Title.upper()[0] + Title[1:]
                myTitle = myNode.composeTitle(AuthsList, Date, Title)
                # create the object:
                self.manage_addProduct['ParsedXML'].manage_addParsedXML(
                    id=newId, 
                    title=myTitle,
                    useNamespaces=0, 
                    contentType="text/xml",
                    file=str(myNode) )
                self.NID = self.NID + 1
                c = c + 1
        # and clean up after
        try:
            self.manage_delObjects('TempImport')
        except:
            return "couldn't delete the temp object"
        # report back
        return "Imported " + str(c) + " entries."
        
        
        
    ##### DTML Interfaces #########################################
    #
    # Basic stuff:
    index_html = DTMLFile('www/index_html', globals())
    zNoteCSS = DTMLFile('www/zNoteCSS', globals())
    topFrame = DTMLFile('www/topFrame', globals())
    tocFrame = DTMLFile('www/tocFrame', globals())
    bibToc = DTMLFile('www/bibToc', globals())
    about = DTMLFile('www/about', globals())
    todolist = DTMLFile('www/todolist', globals())
    biblio_dtd = DTMLFile('www/biblio_dtd', globals())
    search_form = DTMLFile('www/search_form', globals())
    search_results = DTMLFile('www/search_results', globals())
    getNotes = DTMLFile('www/getNotes', globals())
    exportAll = DTMLFile('www/exportAll', globals())
    #
    # New Entry Forms:
    newBookForm = DTMLFile('www/newBookForm', globals())
    newBookSectionForm = DTMLFile('www/newBookSectionForm', globals())
    newJArticleForm = DTMLFile('www/newJArticleForm', globals())
    newThesisForm = DTMLFile('www/newThesisForm', globals())
    newESourceForm = DTMLFile('www/newESourceForm', globals())
    newProceedingsForm = DTMLFile('www/newProceedingsForm', globals())
    importEntriesForm = DTMLFile('www/importEntriesForm', globals())
    #
    # ZMI:
    manage_editForm = DTMLFile('www/manage_editForm', globals())
    #
    # Formatting:
    View = DTMLFile('www/View', globals())
    viewChecked = DTMLFile('www/viewChecked', globals())


    ##### internal utility routines: #####
    
    # somebody want to suggest a more elegant way of doing this?
    def oprint(self, line):
        self.tempbuffer.append(line)
                

####### Constructor ##################################################

def manage_addzNoteAction(self, id, title, initialNID=0, style="APA", REQUEST=None): 
    "Add a new zNote Folder instance"
    if title=='': title='zNotes'
    self._setObject(id, zNote(id, title, initialNID, style))
    if REQUEST is not None:
        return self.manage_main(self, REQUEST)

manage_addzNoteForm = DTMLFile('manage_addzNoteForm', globals()) 


