# Copyright (c) 2006, 2006 Roman Joost
# See also LICENSE.txt
# $Id$
""" Blog dependend installations, which can not be imported from
    SilvaBlog, because of cross references
"""
# Zope imports
from Products.ProxyIndex.ProxyIndex import RecordStyle
from Products.SilvaMetadata.Index import createIndexes

def setup_skin(root):
    setid = 'silva-layout'
    skinid = 'SilvaBlog'

    mds = root.service_metadata
    currentskin = mds.getMetadataValue(root, setid, 'skin')
    if currentskin == 'SilvaDefault':
        binding = mds.getMetadata(root)
        binding.setValues(setid, {'skin' : skinid})

class El:
    """Helper class to initialize the catalog lexicon
    """
    def __init__(self, **kw):
        self.__dict__.update(kw)

def setup_catalog(root):
    """Sets up the ZCatalog
    """
    # See if catalog exists, if not create one
    if not hasattr(root, 'blog_catalog'):
        root.manage_addProduct['ZCatalog'].manage_addZCatalog(
            'blog_catalog', 'Blog Catalog')

    catalog = root.blog_catalog
    lexicon_id = 'blog_lexicon'

    # Add lexicon with right splitter (Silva.UnicodeSplitter.Splitter 
    # registers under "Unicode Whitespace splitter")
    # XXX ugh, hardcoded dependency on names in ZCTextIndex
    catalog.manage_addProduct['ZCTextIndex'].manage_addLexicon(
        lexicon_id, 
        elements=[El(group='Case Normalizer', name='Case Normalizer'),
                    El(group='Stop Words', name=" Don't remove stop words"),
                    El(group='Word Splitter', 
                        name="Unicode Whitespace splitter"),
                    ]
        )

    existing_indexes = catalog.indexes()
    indexes = [
        ('id', 'FieldIndex'),
        ('meta_type', 'FieldIndex'),
        ('path', 'PathIndex'),
        ('fulltext', 'ZCTextIndex'),       
        ('version_status', 'FieldIndex'),
        ('get_modification_datetime', 'DateIndex'),
        ('publication_datetime', 'DateIndex'),
        ('get_creation_datetime', 'DateIndex'),
        ]

    for field_name, field_type in indexes:
        if field_name in existing_indexes:
            continue

        # special handling for argument passing to zctextindex
        # ranking algorithm used is best for larger text body / query 
        # size ratios
        if field_type == 'ZCTextIndex':
            extra = RecordStyle(
                {'doc_attr':field_name,
                 'lexicon_id':'blog_lexicon',
                 'index_type':'Okapi BM25 Rank'}
                )
            catalog.addIndex(field_name, field_type, extra)
            continue
        
        catalog.addIndex(field_name, field_type)

    # install some additional indexes in the blog_catalog which are out
    # of the SilvaMetadata system
    sm = root.service_metadata
    silvaextra = sm.getMetadataSet('silva-extra')
    e_names = ['keywords']
    elements = []

    for en in e_names:
        elements.append(silvaextra.getElement('keywords'))
    
    createIndexes(root.service_catalog, elements)



