Back to index

plone3  3.1.7
workflow.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2005 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 """Workflow tool xml adapters and setup handlers.
00014 
00015 $Id: workflow.py 76996 2007-06-24 00:18:49Z hannosch $
00016 """
00017 
00018 from zope.component import adapts
00019 
00020 from Products.GenericSetup.interfaces import ISetupEnviron
00021 from Products.GenericSetup.utils import exportObjects
00022 from Products.GenericSetup.utils import importObjects
00023 from Products.GenericSetup.utils import ObjectManagerHelpers
00024 from Products.GenericSetup.utils import PropertyManagerHelpers
00025 from Products.GenericSetup.utils import XMLAdapterBase
00026 
00027 from Products.CMFCore.interfaces import IConfigurableWorkflowTool
00028 from Products.CMFCore.utils import getToolByName
00029 
00030 
00031 class WorkflowToolXMLAdapter(XMLAdapterBase, ObjectManagerHelpers,
00032                              PropertyManagerHelpers):
00033 
00034     """XML im- and exporter for WorkflowTool.
00035     """
00036 
00037     adapts(IConfigurableWorkflowTool, ISetupEnviron)
00038 
00039     _LOGGER_ID = 'workflow'
00040 
00041     name = 'workflows'
00042 
00043     def _exportNode(self):
00044         """Export the object as a DOM node.
00045         """
00046         node = self._getObjectNode('object')
00047         node.appendChild(self._extractProperties())
00048         node.appendChild(self._extractObjects())
00049         node.appendChild(self._extractChains())
00050 
00051         self._logger.info('Workflow tool exported.')
00052         return node
00053 
00054     def _importNode(self, node):
00055         """Import the object from the DOM node.
00056         """
00057         if self.environ.shouldPurge():
00058             self._purgeProperties()
00059             self._purgeObjects()
00060             self._purgeChains()
00061 
00062         self._initProperties(node)
00063         self._initObjects(node)
00064         self._initChains(node)
00065 
00066         self._logger.info('Workflow tool imported.')
00067 
00068     def _extractChains(self):
00069         fragment = self._doc.createDocumentFragment()
00070         node = self._doc.createElement('bindings')
00071         child = self._doc.createElement('default')
00072         chain = self.context.getDefaultChain()
00073         for workflow_id in chain:
00074             sub = self._doc.createElement('bound-workflow')
00075             sub.setAttribute('workflow_id', workflow_id)
00076             child.appendChild(sub)
00077         node.appendChild(child)
00078         for type_id, chain in self.context.listChainOverrides():
00079             child = self._doc.createElement('type')
00080             child.setAttribute('type_id', type_id)
00081             for workflow_id in chain:
00082                 sub = self._doc.createElement('bound-workflow')
00083                 sub.setAttribute('workflow_id', workflow_id)
00084                 child.appendChild(sub)
00085             node.appendChild(child)
00086         fragment.appendChild(node)
00087         return fragment
00088 
00089     def _purgeChains(self):
00090         self.context.setDefaultChain('')
00091         for type_id, chain in self.context.listChainOverrides():
00092             self.context.setChainForPortalTypes((type_id,), None,
00093                                                 verify=False)
00094 
00095     def _initChains(self, node):
00096         for child in node.childNodes:
00097             if child.nodeName != 'bindings':
00098                 continue
00099             for sub in child.childNodes:
00100                 if sub.nodeName == 'default':
00101                     self.context.setDefaultChain(self._getChain(sub))
00102                 if sub.nodeName == 'type':
00103                     type_id = str(sub.getAttribute('type_id'))
00104                     if sub.hasAttribute('remove'):
00105                         chain = None
00106                     else:
00107                         chain = self._getChain(sub)
00108                     self.context.setChainForPortalTypes((type_id,), chain,
00109                                                         verify=False)
00110 
00111     def _getChain(self, node):
00112         workflow_ids = []
00113         for child in node.childNodes:
00114             if child.nodeName != 'bound-workflow':
00115                 continue
00116             workflow_ids.append(str(child.getAttribute('workflow_id')))
00117         return ','.join(workflow_ids)
00118 
00119 
00120 def importWorkflowTool(context):
00121     """Import workflow tool and contained workflow definitions from XML files.
00122     """
00123     site = context.getSite()
00124     tool = getToolByName(site, 'portal_workflow')
00125 
00126     importObjects(tool, '', context)
00127 
00128 def exportWorkflowTool(context):
00129     """Export workflow tool and contained workflow definitions as XML files.
00130     """
00131     site = context.getSite()
00132     tool = getToolByName(site, 'portal_workflow', None)
00133     if tool is None:
00134         logger = context.getLogger('workflow')
00135         logger.info('Nothing to export.')
00136         return
00137 
00138     exportObjects(tool, '', context)