Back to index

plone3  3.1.7
UniqueIdAnnotationTool.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2004 Zope Corporation and Contributors. All Rights Reserved.
00004 #
00005 # This software is subject to the provisions of the Zope Public License,
00006 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
00007 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00008 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00009 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00010 # FOR A PARTICULAR PURPOSE.
00011 #
00012 ##############################################################################
00013 """Unique Id Annotation Tool
00014 
00015 Provides support for managing unique id annotations.
00016 
00017 $Id: UniqueIdAnnotationTool.py 77004 2007-06-24 08:57:54Z yuppie $
00018 """
00019 
00020 from AccessControl import ClassSecurityInfo
00021 from Acquisition import aq_base, aq_inner, aq_parent
00022 from Acquisition import Implicit
00023 from Globals import InitializeClass
00024 from Globals import Persistent
00025 from OFS.interfaces import IObjectClonedEvent
00026 from OFS.PropertyManager import PropertyManager
00027 from OFS.SimpleItem import SimpleItem
00028 from zope.app.container.interfaces import IObjectAddedEvent
00029 from zope.component import queryUtility
00030 from zope.interface import implements
00031 
00032 from Products.CMFCore.utils import getToolByName
00033 from Products.CMFCore.utils import registerToolInterface
00034 from Products.CMFCore.utils import UniqueObject
00035 
00036 from Products.CMFUid.interfaces import IUniqueIdAnnotation
00037 from Products.CMFUid.interfaces import IUniqueIdAnnotationManagement
00038 from Products.CMFUid.interfaces import UniqueIdError
00039 
00040 
00041 class UniqueIdAnnotation(Persistent, Implicit):
00042 
00043     """Unique id object used as annotation on (content) objects.
00044     """
00045 
00046     implements(IUniqueIdAnnotation)
00047 
00048     def __init__(self, obj, id):
00049         """See IUniqueIdAnnotation.
00050         """
00051         self._uid = None
00052         self.id = id
00053         setattr(obj, id, self)
00054 
00055     def __call__(self):
00056         """See IUniqueIdAnnotation.
00057         """
00058         return self._uid
00059 
00060     def getId(self):
00061         """See IUniqueIdAnnotation.
00062         """
00063         return self.id
00064 
00065     def setUid(self, uid):
00066         """See IUniqueIdAnnotation.
00067         """
00068         self._uid = uid
00069 
00070 InitializeClass(UniqueIdAnnotation)
00071 
00072 def handleUidAnnotationEvent(ob, event):
00073     """ Event subscriber for (IUniqueIdAnnotation, IObjectEvent) events
00074     """
00075 
00076     if IObjectAddedEvent.providedBy(event):
00077         if event.newParent is not None:
00078             anno_tool = queryUtility(IUniqueIdAnnotationManagement)
00079             uid_handler = getToolByName(ob, 'portal_uidhandler', None)
00080             if anno_tool is not None:
00081                 remove_on_add = anno_tool.getProperty('remove_on_add',False)
00082                 remove_on_clone = anno_tool.getProperty('remove_on_clone',False)
00083                 assign_on_add = anno_tool.getProperty('assign_on_add',False)
00084 
00085                 if (remove_on_add and remove_on_clone) or assign_on_add:
00086                     try:
00087                         uid_handler.unregister(ob)
00088                     except UniqueIdError:
00089                         # did not have one
00090                         pass
00091                 if assign_on_add:
00092                     # assign new uid
00093                     uid_handler.register(ob)
00094                  
00095     elif IObjectClonedEvent.providedBy(event):
00096         anno_tool = queryUtility(IUniqueIdAnnotationManagement)
00097         uid_handler = getToolByName(ob, 'portal_uidhandler', None)
00098 
00099         if anno_tool is not None:
00100             remove_on_clone = anno_tool.getProperty('remove_on_clone', False)
00101             assign_on_clone = anno_tool.getProperty('assign_on_clone', False)
00102             if remove_on_clone or assign_on_clone:
00103                 try:
00104                     uid_handler.unregister(ob)
00105                 except UniqueIdError:
00106                     # did not have one
00107                     pass
00108             if assign_on_clone:
00109                 # assign new uid
00110                 uid_handler.register(ob)
00111         
00112 class UniqueIdAnnotationTool(UniqueObject, SimpleItem, PropertyManager):
00113 
00114     __doc__ = __doc__ # copy from module
00115 
00116     implements(IUniqueIdAnnotationManagement)
00117     __implements__ = (
00118         SimpleItem.__implements__,
00119     )
00120 
00121     manage_options = (
00122         PropertyManager.manage_options +
00123         SimpleItem.manage_options
00124     )
00125 
00126     id = 'portal_uidannotation'
00127     alternative_id = "portal_standard_uidannotation"
00128     meta_type = 'Unique Id Annotation Tool'
00129 
00130     security = ClassSecurityInfo()
00131 
00132     remove_on_add = True
00133     remove_on_clone = True
00134     assign_on_add = False
00135     assign_on_clone = False
00136     _properties = (
00137     {'id': 'remove_on_add', 'type': 'boolean', 'mode': 'w',
00138      'label': "Remove the objects unique id on add (and import)"},
00139     {'id': 'remove_on_clone', 'type': 'boolean', 'mode': 'w',
00140      'label': 'Remove the objects unique id on clone (CAUTION !!!)'},
00141     {'id': 'assign_on_add', 'type': 'boolean', 'mode': 'w',
00142      'label': "Assign a unique ID when an object is added"},
00143     {'id': 'assign_on_clone', 'type': 'boolean', 'mode': 'w',
00144      'label': "Assign a unique ID when an object is cloned"},
00145     )
00146 
00147     security.declarePrivate('__call__')
00148     def __call__(self, obj, id):
00149         """See IUniqueIdAnnotationManagement.
00150         """
00151         return UniqueIdAnnotation(obj, id)
00152 
00153 InitializeClass(UniqueIdAnnotationTool)
00154 registerToolInterface('portal_uidannotation', IUniqueIdAnnotationManagement)