vendor/CMF/1.6.3/CMFCore

view exportimport/workflow.py @ 0:587011552858

import CMF 1.6.3
author bdelbosc
date Mon, 23 Apr 2007 13:58:01 +0000
parents
children
line source
1 ##############################################################################
2 #
3 # Copyright (c) 2005 Zope Corporation and Contributors. All Rights Reserved.
4 #
5 # This software is subject to the provisions of the Zope Public License,
6 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
10 # FOR A PARTICULAR PURPOSE.
11 #
12 ##############################################################################
13 """Workflow tool xml adapters and setup handlers.
15 $Id$
16 """
18 import Products
20 from Products.GenericSetup.utils import exportObjects
21 from Products.GenericSetup.utils import importObjects
22 from Products.GenericSetup.utils import ObjectManagerHelpers
23 from Products.GenericSetup.utils import PropertyManagerHelpers
24 from Products.GenericSetup.utils import XMLAdapterBase
26 from Products.CMFCore.interfaces import IWorkflowTool
27 from Products.CMFCore.utils import getToolByName
30 class WorkflowToolXMLAdapter(XMLAdapterBase, ObjectManagerHelpers,
31 PropertyManagerHelpers):
33 """XML im- and exporter for WorkflowTool.
34 """
36 __used_for__ = IWorkflowTool
38 _LOGGER_ID = 'workflow'
40 name = 'workflows'
42 def _exportNode(self):
43 """Export the object as a DOM node.
44 """
45 node = self._getObjectNode('object')
46 node.appendChild(self._extractProperties())
47 node.appendChild(self._extractObjects())
48 node.appendChild(self._extractChains())
50 self._logger.info('Workflow tool exported.')
51 return node
53 def _importNode(self, node):
54 """Import the object from the DOM node.
55 """
56 if self.environ.shouldPurge():
57 self._purgeProperties()
58 self._purgeObjects()
59 self._purgeChains()
61 self._initProperties(node)
62 self._initObjects(node)
63 self._initBBBObjects(node)
64 self._initChains(node)
66 self._logger.info('Workflow tool imported.')
68 def _initBBBObjects(self, node):
69 for child in node.childNodes:
70 if child.nodeName != 'workflow':
71 continue
72 parent = self.context
74 obj_id = str(child.getAttribute('workflow_id'))
75 if obj_id not in parent.objectIds():
76 meta_type = str(child.getAttribute('meta_type'))
77 for mt_info in Products.meta_types:
78 if mt_info['name'] == meta_type:
79 parent._setObject(obj_id, mt_info['instance'](obj_id))
80 break
82 def _extractChains(self):
83 fragment = self._doc.createDocumentFragment()
84 node = self._doc.createElement('bindings')
85 child = self._doc.createElement('default')
86 chain = self.context._default_chain
87 for workflow_id in chain:
88 sub = self._doc.createElement('bound-workflow')
89 sub.setAttribute('workflow_id', workflow_id)
90 child.appendChild(sub)
91 node.appendChild(child)
92 cbt = self.context._chains_by_type
93 if cbt:
94 overrides = cbt.items()
95 overrides.sort()
96 for type_id, chain in overrides:
97 child = self._doc.createElement('type')
98 child.setAttribute('type_id', type_id)
99 for workflow_id in chain:
100 sub = self._doc.createElement('bound-workflow')
101 sub.setAttribute('workflow_id', workflow_id)
102 child.appendChild(sub)
103 node.appendChild(child)
104 fragment.appendChild(node)
105 return fragment
107 def _purgeChains(self):
108 self.context.setDefaultChain('')
109 if self.context._chains_by_type is not None:
110 self.context._chains_by_type.clear()
112 def _initChains(self, node):
113 for child in node.childNodes:
114 if child.nodeName != 'bindings':
115 continue
116 for sub in child.childNodes:
117 if sub.nodeName == 'default':
118 self.context.setDefaultChain(self._getChain(sub))
119 if sub.nodeName == 'type':
120 type_id = str(sub.getAttribute('type_id'))
121 self.context.setChainForPortalTypes((type_id,),
122 self._getChain(sub), verify=False)
124 def _getChain(self, node):
125 workflow_ids = []
126 for child in node.childNodes:
127 if child.nodeName != 'bound-workflow':
128 continue
129 workflow_ids.append(str(child.getAttribute('workflow_id')))
130 return ','.join(workflow_ids)
133 def importWorkflowTool(context):
134 """Import workflow tool and contained workflow definitions from XML files.
135 """
136 site = context.getSite()
137 tool = getToolByName(site, 'portal_workflow')
139 importObjects(tool, '', context)
141 def exportWorkflowTool(context):
142 """Export workflow tool and contained workflow definitions as XML files.
143 """
144 site = context.getSite()
145 tool = getToolByName(site, 'portal_workflow', None)
146 if tool is None:
147 logger = context.getLogger('workflow')
148 logger.info('Nothing to export.')
149 return
151 exportObjects(tool, '', context)