##############################################################################
#
# Copyright (c) 2008 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################from zope.app.container.contained import ObjectAddedEvent
"""

$Id$
"""
from zope import interface, component
from zope.app.component.interfaces import ISite
from zope.app.zopeappgenerations import getRootFolder
from zope.app.component.hooks import getSite, setSite
from zope.component.interfaces import ComponentLookupError

from zc.shortcut.interfaces import IShortcut

from zojax.content.shortcut.shortcut import shortCutAdded


def evolve(context):
    root = getRootFolder(context)
    old_site = getSite()
    for site in findObjectsMatching(root, ISite.providedBy):
        setSite(site)
        try:
            for shortcut in findObjectsMatching(site, IShortcut.providedBy):
                print 'updating shortcut', shortcut
                try:
                    shortCutAdded(shortcut, None)
                except ComponentLookupError:
                    pass
        finally:
            setSite(old_site)


def findObjectsMatching(root, condition):
    if condition(root):
        yield root

    if hasattr(root, 'values') and callable(root.values):
        for subobj in root.values():
            for match in findObjectsMatching(subobj, condition):
                yield match

e0dabd2ea95cd30bef84fb6c342821c03a732845
##############################################################################
#
# Copyright (c) 2008 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################from zope.app.container.contained import ObjectAddedEvent
"""

$Id$
"""
from zope import interface, component
from zope.app.component.interfaces import ISite
from zope.app.zopeappgenerations import getRootFolder
from zope.app.component.hooks import getSite, setSite
from zope.component.interfaces import ComponentLookupError

from zc.shortcut.interfaces import IShortcut

from zojax.content.shortcut.shortcut import shortCutAdded


def evolve(context):
    root = getRootFolder(context)
    old_site = getSite()
    for site in findObjectsMatching(root, ISite.providedBy):
        setSite(site)
        try:
            for shortcut in findObjectsMatching(site, IShortcut.providedBy):
                print 'updating shortcut', shortcut
                try:
                    shortCutAdded(shortcut, None)
                except ComponentLookupError:
                    pass
        finally:
            setSite(old_site)


def findObjectsMatching(root, condition):
    if condition(root):
        yield root

    if hasattr(root, 'values') and callable(root.values):
        for subobj in root.values():
            for match in findObjectsMatching(subobj, condition):
                yield match
