ERP5WorkflowTool.py 50.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2015 Nexedi SARL and Contributors. All Rights Reserved.
#                    Wenjie Zheng <wenjie.zheng@tiolive.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################
wenjie.zheng's avatar
wenjie.zheng committed
29

30 31 32 33 34 35 36 37 38 39
import os
import shutil
import sys
import subprocess
import time
import struct
import cPickle
import urllib2
import re

wenjie.zheng's avatar
wenjie.zheng committed
40
from AccessControl import ClassSecurityInfo, Unauthorized
41
from AccessControl.SecurityInfo import ModuleSecurityInfo
wenjie.zheng's avatar
wenjie.zheng committed
42 43 44 45 46 47 48 49 50
from Acquisition import aq_base, Implicit, Explicit
from App.config import getConfiguration
from base64 import b64encode, b64decode, decodestring
from cStringIO import StringIO
from DateTime import DateTime
from itertools import izip
from MethodObject import Method
from MySQLdb import ProgrammingError, OperationalError
from Persistence import Persistent
51
from Products.CMFActivity.ActiveResult import ActiveResult
wenjie.zheng's avatar
wenjie.zheng committed
52 53 54 55 56 57 58 59
from Products.CMFCore.utils import Message as _
from Products.CMFCore.utils import getToolByName, _getAuthenticatedUser
from Products.CMFCore.WorkflowTool import WorkflowTool as OriginalWorkflowTool
from Products.CMFCore.WorkflowCore import ObjectMoved, ObjectDeleted,\
                                          WorkflowException
from Products.DCWorkflow.DCWorkflow import DCWorkflowDefinition
from Products.DCWorkflow.Transitions import TRIGGER_WORKFLOW_METHOD
from Products.ERP5 import _dtmldir
60 61
from Products.ERP5.Document.BusinessTemplate import BusinessTemplateMissingDependency
from Products.ERP5.genbt5list import generateInformation
wenjie.zheng's avatar
wenjie.zheng committed
62 63 64 65 66 67 68 69 70 71
from Products.ERP5Type import Permissions, PropertySheet
from Products.ERP5Type.Base import Base
from Products.ERP5Type.Cache import transactional_cached, CachingMethod
from Products.ERP5Type.Core.Folder import Folder
from Products.ERP5Type.DiffUtils import DiffFile
from Products.ERP5Type.Globals import InitializeClass, DTMLFile, PersistentMapping
from Products.ERP5Type.Message import translateString
from Products.ERP5Type.Tool.BaseTool import BaseTool
from Products.ZSQLCatalog.SQLCatalog import SimpleQuery, AutoQuery, ComplexQuery, NegatedQuery
from sets import ImmutableSet
72
from tempfile import mkstemp, mkdtemp
wenjie.zheng's avatar
wenjie.zheng committed
73
from types import StringTypes
74 75 76
from urllib import pathname2url, urlopen, splittype, urlretrieve
from xml.dom.minidom import parse
from xml.parsers.expat import ExpatError
wenjie.zheng's avatar
wenjie.zheng committed
77
from webdav.client import Resource
78
from zLOG import LOG, INFO, WARNING
wenjie.zheng's avatar
wenjie.zheng committed
79

80 81 82 83 84 85
"""
Most of the codes in this file are copy-pasted from patches/WorkflowTool.py.
"""

_marker = []  # Create a new marker object.

86
class ERP5WorkflowTool(BaseTool, OriginalWorkflowTool):
87 88
  """
  A new container for DC workflow and workflow;
89
  inherits methods from original WorkflowTool.py;
90 91 92 93 94 95 96 97
  contains patches from ERP5Type/pateches/WorkflowTool.py.
  """

  id            = 'portal_workflow'
  title         = 'ERP5 Workflow Tool'
  meta_type     = 'ERP5 Workflow Tool'
  portal_type   = 'ERP5 Workflow Tool'
  allowed_types = ('Workflow', 'Interaction Workflow', )
98
  all_meta_types = OriginalWorkflowTool.all_meta_types
99 100 101 102 103 104 105

  # This stores information on repositories.
  repository_dict = {}

  # Declarative Security
  security = ClassSecurityInfo()

106 107 108
  manage_options = OriginalWorkflowTool.manage_options
  manage_overview = OriginalWorkflowTool.manage_overview
  _manage_selectWorkflows = OriginalWorkflowTool._manage_selectWorkflows
109 110
  manage_selectWorkflows = OriginalWorkflowTool.manage_selectWorkflows
  manage_changeWorkflows = OriginalWorkflowTool.manage_changeWorkflows
111
  # Declarative properties
112 113 114 115 116 117 118
  property_sheets = (
    PropertySheet.Base,
    PropertySheet.XMLObject,
    PropertySheet.CategoryCore,
    PropertySheet.DublinCore,
  )

119 120 121 122 123 124
  def _jumpToStateFor(self, ob, state_id, wf_id=None, *args, **kw):
    """Inspired from doActionFor.
    This is public method to allow passing meta transition (Jump form
    any state to another in same workflow)
    """
    from Products.ERP5.InteractionWorkflow import InteractionWorkflowDefinition
125 126
    from Products.ERP5Workflow.Document.InteractionWorkflow import InteractionWorkflow
    workflow_list = self.getWorkflowValueListFor(ob.getPortalType())
127 128 129 130 131
    if wf_id is None:
      if not workflow_list:
        raise WorkflowException('No workflows found.')
      found = False
      for workflow in workflow_list:
132
        if not isinstance(workflow, InteractionWorkflowDefinition) and \
133
            not isinstance(workflow, InteractionWorkflow):
134
          if state_id in workflow.getStateIdList():
135 136
            found = True
            break
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
      if not found:
        raise WorkflowException('No workflow provides the destination state %r'\
                                                                      % state_id)
    else:
      workflow = self.getWorkflowById(wf_id)
      if workflow is None:
        raise WorkflowException('Requested workflow definition not found.')

    workflow._executeMetaTransition(ob, state_id)

  def _isJumpToStatePossibleFor(self, ob, state_id, wf_id=None):
    """Test if given state_id is available for ob
    in at least one associated workflow
    """
    from Products.ERP5.InteractionWorkflow import InteractionWorkflowDefinition
152 153
    from Products.ERP5Workflow.Document.InteractionWorkflow import InteractionWorkflow
    for workflow in (wf_id and (self[wf_id],) or self.getWorkflowValueListFor(ob.getPortalType())):
154
      if not isinstance(workflow, InteractionWorkflowDefinition) and \
155
          not isinstance(workflow, InteractionWorkflow):
156
        if state_id in workflow.getStateIdList():
157
          return True
158 159
    return False

160
  def doActionFor(self, ob, action, wf_id=None, *args, **kw):
161
    workflow_list = self.getWorkflowValueListFor(ob.getPortalType())
162
    if wf_id is None:
163
      if workflow_list == []:
164 165
        raise WorkflowException(_(u'No workflows found.'))
      found = 0
166
      for wf in workflow_list:
167 168 169 170 171 172 173
        if wf.isActionSupported(ob, action, **kw):
          found = 1
          break
      if not found:
        msg = _(u"No workflow provides the '${action_id}' action.",mapping={'action_id': action})
        raise WorkflowException(msg)
    else:
174
      wf = self.getWorkflowById(wf_id)
175 176
      if wf is None:
        raise WorkflowException(_(u'Requested workflow definition not found.'))
177 178
    return self._invokeWithNotification(
      workflow_list, ob, action, wf.doActionFor, (ob, action) + args, kw)
179 180

  def _getInfoFor(self, ob, name, default=_marker, wf_id=None, *args, **kw):
181
      workflow_list = self.getWorkflowValueListFor(ob.getPortalType())
182

183
      if wf_id is None:
184
          if workflow_list == []:
185 186 187 188 189
              if default is _marker:
                  raise WorkflowException(_(u'No workflows found.'))
              else:
                  return default
          found = 0
190
          for workflow in workflow_list:
191 192 193 194 195 196 197 198 199 200 201
              if workflow.isInfoSuported(ob, name):
                found = 1
                break
          if not found:
              if default is _marker:
                  msg = _(u"No workflow provides '${name}' information.",
                          mapping={'name': name})
                  raise WorkflowException(msg)
              else:
                  return default
      else:
202
          wf = self.getWorkflowById(wf_id)
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
          if wf is None:
              if default is _marker:
                  raise WorkflowException(
                      _(u'Requested workflow definition not found.'))
              else:
                  return default

      res = wf.getInfoFor(ob, name, default, *args, **kw)
      if res is _marker:
          msg = _(u'Could not get info: ${name}', mapping={'name': name})
          raise WorkflowException(msg)
      return res

  def getWorkflowTempObjectList(self):
    """ Return a list of converted temporary workflows.
    """
    temp_workflow_list = []
    temp_workflow_id_list = []
221 222
    for dc_workflow_id in self.getPortalObject().portal_workflow:
      dc_workflow = self.getPortalObject().portal_workflow.get(dc_workflow_id)
223 224 225 226 227 228 229 230 231
      workflow_type = dc_workflow.__class__.__name__
      if workflow_type == 'Workflow' or workflow_type == 'Interaction Workflow':
        continue
      temp_obj = 1
      temp_workflow = self.dc_workflow_asERP5Object(self, dc_workflow, temp_obj)
      temp_workflow_list.append(temp_workflow)
      temp_workflow_id_list.append(temp_workflow.getTitle())
    return temp_workflow_list

232
  def getWorkflowValueListFor(self, portal_type_id):
233 234
    """ Return a list of workflows bound to selected portal_type.
    """
235
    portal_type = self.getPortalObject().getDefaultModule(portal_type="portal_types")._getOb(portal_type_id, None)
236
    workflow_list = []
237 238 239
    if portal_type is not None:
      for workflow_id in portal_type.getTypeERP5WorkflowList():
        workflow_list.append(self._getOb(workflow_id))
240

241 242 243
    for wf in self.getWorkflowsFor(portal_type_id):
      if wf is not None:
        workflow_list.append(wf)
244 245
    return workflow_list

246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
  security.declarePrivate('getHistoryOf')
  def getHistoryOf(self, wf_id, ob):
      """ Get the history of an object for a given workflow.
      """
      if hasattr(aq_base(ob), 'workflow_history'):
          wfh = ob.workflow_history
          return wfh.get(wf_id, None)
      return ()

  security.declarePrivate('getStatusOf')
  def getStatusOf(self, wf_id, ob):
      """ Get the last element of a workflow history for a given workflow.
      """
      wfh = self.getHistoryOf(wf_id, ob)
      if wfh:
          return wfh[-1]
      return None

264
  def dc_workflow_asERP5Object(self, container, dc_workflow, temp):
wenjie.zheng's avatar
wenjie.zheng committed
265
    # create a temporary ERP5 Workflow
266 267
    workflow_type_id = dc_workflow.__class__.__name__
    if workflow_type_id == 'DCWorkflowDefinition':
268
      LOG("2.a Workflow '%s' is a DCWorkflow'"%dc_workflow.id,WARNING,' in ERP5WorkflowTool.py')
269 270 271 272 273 274 275 276 277
      workflow = container.newContent(portal_type='Workflow', temp_object=temp)
      workflow.setStateVariable(dc_workflow.state_var)
      workflow.setWorkflowManagedPermission(dc_workflow.permissions)
      workflow.setSource(dc_workflow.initial_state)
    else:
      LOG("2.b Workflow '%s' is a DC Interaction Workflow"%dc_workflow.getTitle(),WARNING,' in ERP5WorkflowTool.py')
      workflow = container.newContent(portal_type='Interaction Workflow', temp_object=temp)
      workflow.setManagerBypass(dc_workflow.manager_bypass)

278
    workflow.setReference(dc_workflow.id)
279 280 281
    workflow.edit(title=dc_workflow.title)
    workflow.edit(description=dc_workflow.description)

wenjie.zheng's avatar
wenjie.zheng committed
282
    # create transitions
283 284 285 286 287 288
    if workflow_type_id == 'DCWorkflowDefinition':
      for tid in dc_workflow.transitions:
        tdef = dc_workflow.transitions.get(tid)
        LOG("2.1 Convert transition '%s' of workflow '%s'"%(tdef.id,workflow.getTitle()),WARNING,' in ERP5WorkflowTool.py')
        transition = workflow.newContent(portal_type='Transition', temp_object=temp)
        transition.edit(title=tdef.title)
289
        transition.setReference(tdef.id)
290 291 292 293 294 295 296 297 298
        transition.setTriggerType(tdef.trigger_type)
        transition.setActboxCategory(tdef.actbox_category)
        transition.setActboxIcon(tdef.actbox_icon)
        transition.setActboxName(tdef.actbox_name)
        transition.setActboxUrl(tdef.actbox_url)
        transition.setAfterScriptId(tdef.after_script_name)
        transition.setBeforeScriptId(tdef.script_name)
        transition.setDestination(tdef.new_state_id)
        transition.guard = tdef.guard
wenjie.zheng's avatar
wenjie.zheng committed
299
      # create states (portal_type = State)
300 301 302 303 304
      for sid in dc_workflow.states:
        sdef = dc_workflow.states.get(sid)
        LOG("2.2 Convert state '%s' of workflow '%s'"%(sdef.id,workflow.getTitle()),WARNING,' in ERP5WorkflowTool.py')
        state = workflow.newContent(portal_type='State', temp_object=temp)
        state.edit(title=sdef.title)
305
        state.setReference(sdef.id)
306 307
        state.setStatePermissionRoles(sdef.permission_roles)
        state.setDestinationList(sdef.transitions)
wenjie.zheng's avatar
wenjie.zheng committed
308
      # create worklists (portal_type = Worklist)
309 310 311 312 313
      for qid in dc_workflow.worklists:
        qdef = dc_workflow.worklists.get(qid)
        LOG("2.3 Convert worklist '%s' of workflow '%s'"%(qdef.id,workflow.getTitle()),WARNING,' in ERP5WorkflowTool.py')
        worklist = workflow.newContent(portal_type='Worklist', temp_object=temp)
        worklist.edit(title=qdef.title)
314
        worklist.setReference(qdef.id)
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
        for key, values in qdef.var_matches.items():
          if key == 'portal_type':
            worklist.setMatchedPortalTypeList(values)
          elif key == 'simulation_state':
            worklist.setMatchedSimulationStateList(values)
          elif key == 'validation_state':
            worklist.setMatchedValidationStateList(values)
        worklist.setActboxCategory(qdef.actbox_category)
        worklist.setActboxIcon(qdef.actbox_icon)
        worklist.setActboxName(qdef.actbox_name)
        worklist.setActboxUrl(qdef.actbox_url)
        worklist.guard = qdef.guard
    else:
      for tid in dc_workflow.interactions:
        interaction = workflow.newContent(portal_type='Interaction', temp_object=temp)
        tdef = dc_workflow.interactions.get(tid)
        LOG("2.4 Convert interaction '%s' of workflow '%s'"%(tdef.id,workflow.getTitle()),WARNING,' in ERP5WorkflowTool.py')
        interaction.edit(title=tdef.title)
333
        interaction.setReference(tdef.id)
334 335 336 337 338 339 340 341
        interaction.setActivateScriptName(tdef.activate_script_name)
        interaction.setAfterScriptName(tdef.after_script_name)
        interaction.setBeforeCommitScriptName(tdef.before_commit_script_name)
        interaction.setBeforeScriptName(tdef.script_name)
        interaction.guard = tdef.guard
        interaction.setPortalTypeFilter(tdef.portal_type_filter)
        interaction.setPortalTypeGroupFilter(tdef.portal_type_group_filter)
        interaction.setTemporaryDocumentDisallowed(tdef.temporary_document_disallowed)
wenjie.zheng's avatar
wenjie.zheng committed
342
        #interaction.setTransitionFormId() # this is not defined in DC interaction?
343 344 345 346
        interaction.setTriggerMethodId(tdef.method_id)
        interaction.setTriggerOncePerTransaction(tdef.once_per_transaction)
        interaction.setTriggerType(tdef.trigger_type)

wenjie.zheng's avatar
wenjie.zheng committed
347
    # create scripts (portal_type = Workflow Script)
348 349 350 351 352
    for script_id in dc_workflow.scripts:
      script = dc_workflow.scripts.get(script_id)
      workflow_script = workflow.newContent(portal_type='Workflow Script', temp_object=temp)
      LOG("2.5 Convert workflow script '%s' of workflow '%s'"%(workflow_script.id,workflow.getTitle()),WARNING,' in ERP5WorkflowTool.py')
      workflow_script.edit(title=script.title)
353
      workflow_script.setId(script.id)
354
      workflow_script.setParameterSignature(script._params)
wenjie.zheng's avatar
wenjie.zheng committed
355
      #workflow_script.setCallableType(script.callable_type)# not defined in DC script?
356 357
      workflow_script.setBody(script._body)
      workflow_script.setProxyRole(script._proxy_roles)
wenjie.zheng's avatar
wenjie.zheng committed
358
    # create variables (portal_type = Variable)
359 360 361 362 363
    for vid in dc_workflow.variables:
      vdef = dc_workflow.variables.get(vid)
      variable = workflow.newContent(portal_type='Variable', temp_object=temp)
      LOG("2.6 Convert variable '%s' of workflow '%s'"%(vdef.id,workflow.getTitle()),WARNING,' in ERP5WorkflowTool.py')
      variable.edit(title=vdef.title)
364
      variable.setReference(vdef.id)
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
      variable.setAutomaticUpdate(vdef.update_always)
      variable.setDefaultExpr(vdef.default_expr)
      variable.info_guard = vdef.info_guard
      variable.setForCatalog(vdef.for_catalog)
      variable.setForStatus(vdef.for_status)
      variable.setInitialValue(vdef.default_value)
    return workflow

  def getChainDict(self):
      """Test if the given transition exist from the current state.
      """
      chain_dict = {}
      for portal_type, wf_id_list in self._chains_by_type.iteritems():
          for wf_id in wf_id_list:
              chain_dict.setdefault(wf_id, []).append(portal_type)
      return chain_dict

  _reindexWorkflowVariables = lambda self, ob: \
  hasattr(aq_base(ob), 'reindexObjectSecurity') and ob.reindexObjectSecurity()

  def getWorkflowChainDict(self, sorted=True):
    """Returns workflow chain compatible with workflow_chain_dict signature"""
    chain = self._chains_by_type.copy()
    return_dict = {}
    for portal_type, workflow_id_list in chain.iteritems():
      if sorted:
        workflow_id_list = list(workflow_id_list)
        workflow_id_list.sort()
      return_dict['chain_%s' % portal_type] = ', '.join(workflow_id_list)
    return return_dict

  def isTransitionPossible(self, ob, transition_id, wf_id=None):
    """Test if the given transition exist from the current state.
    """
399
    for workflow in (wf_id and (self[wf_id],) or self.getWorkflowValueListFor(ob.getPortalType())):
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
      state = workflow._getWorkflowStateOf(ob)
      if state and transition_id in state.getDestinationIdList():
        return 1
    return 0

  getFutureStateSetFor = lambda self, wf_id, *args, **kw: \
    self[wf_id].getFutureStateSet(*args, **kw)

  def setStatusOf(self, wf_id, ob, status):
    """ Append an entry to the workflow history.
    o Invoked by workflow definitions.
    """
    wfh = None
    has_history = 0
    if getattr(aq_base(ob), 'workflow_history', None) is not None:
        history = ob.workflow_history
        if history is not None:
            has_history = 1
            wfh = history.get(wf_id, None)
            if wfh is not None and not isinstance(wfh, WorkflowHistoryList):
                wfh = WorkflowHistoryList(list(wfh))
                ob.workflow_history[wf_id] = wfh
    if wfh is None:
        wfh = WorkflowHistoryList()
        if not has_history:
            ob.workflow_history = PersistentMapping()
        ob.workflow_history[wf_id] = wfh
    wfh.append(status)

429
  getWorkflowIds = OriginalWorkflowTool.getWorkflowIds
430

431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
  def refreshWorklistCache(self):
    """
      Refresh worklist cache table.
      - delete everything from that table
        - if it fails, create the table
      - insert new lines
        - if it fails, recrete the table and retry
    """
    # Contrary to WorkflowTool_listActions, related keys are NOT supported.
    Base_zInsertIntoWorklistTable = getattr(self, 'Base_zInsertIntoWorklistTable', None)
    if Base_zInsertIntoWorklistTable is not None:
      # XXX: Code below is duplicated from WorkflowTool_listActions
      info = self._getOAI(None)
      worklist_dict = {}
      wf_ids = self.getWorkflowIds()
      for wf_id in wf_ids:
        wf = self.getWorkflowById(wf_id)
        if wf is not None:
          a = wf.getWorklistVariableMatchDict(info, check_guard=False)
          if a is not None:
            worklist_dict[wf_id] = a
      # End of duplicated code
      if len(worklist_dict):
        Base_zClearWorklistTable = getattr(self, 'Base_zClearWorklistTable', None)
        if Base_zClearWorklistTable is None:
          LOG('WorkflowTool', 100, 'Base_zClearWorklistTable cannot be found. ' \
              'Falling back to former refresh method. Please update ' \
              'erp5_worklist_sql business template.')
          self.Base_zCreateWorklistTable()
        else:
          try:
            self.Base_zClearWorklistTable()
          except ProgrammingError, error_value:
            # 1146 = table does not exist
            if error_value[0] != 1146:
              raise
            self.Base_zCreateWorklistTable()
        portal_catalog = getToolByName(self, 'portal_catalog')
        search_result = portal_catalog.unrestrictedSearchResults
        sql_catalog = portal_catalog.getSQLCatalog()
        table_column_id_set = ImmutableSet(
            [COUNT_COLUMN_TITLE] + self.Base_getWorklistTableColumnIDList())
        security_column_id_list = list(
          sql_catalog.getSQLCatalogSecurityUidGroupsColumnsDict().values()) + \
          [x[1] for x in sql_catalog.getSQLCatalogRoleKeysList()] + \
          [x[1] for x in sql_catalog.getSQLCatalogLocalRoleKeysList()]
        security_column_id_set = set(security_column_id_list)
        assert len(security_column_id_set) == len(security_column_id_list), (
          security_column_id_set, security_column_id_list)
        del security_column_id_list
        security_column_id_set.difference_update(
          self._getWorklistIgnoredSecurityColumnSet())
        for security_column_id in security_column_id_set:
          assert security_column_id in table_column_id_set
        (worklist_list_grouped_by_condition, worklist_metadata) = \
          groupWorklistListByCondition(
            worklist_dict=worklist_dict,
            sql_catalog=sql_catalog)
        assert COUNT_COLUMN_TITLE in table_column_id_set
        for grouped_worklist_dict in worklist_list_grouped_by_condition:
          # Generate the query for this worklist_list
          (total_criterion_id_list, query) = \
            getWorklistListQuery(
              getQuery=SimpleQuery,
              grouped_worklist_dict=grouped_worklist_dict,
            )
          for criterion_id in total_criterion_id_list:
            assert criterion_id in table_column_id_set
          for security_column_id in security_column_id_set:
            assert security_column_id not in total_criterion_id_list
            total_criterion_id_list.append(security_column_id)
          group_by_expression = ', '.join(total_criterion_id_list)
          assert COUNT_COLUMN_TITLE not in total_criterion_id_list
          select_expression = 'count(*) as %s, %s' % (COUNT_COLUMN_TITLE,
                                                      group_by_expression)
          search_result_kw = {'select_expression': select_expression,
                              'group_by_expression': group_by_expression,
                              'query': query,
                              'limit': None}
          #LOG('refreshWorklistCache', WARNING, 'Using query: %s' % \
          #    (search_result(src__=1, **search_result_kw), ))
          catalog_brain_result = search_result(**search_result_kw)
          value_column_dict = {x: [] for x in table_column_id_set}
          for catalog_brain_line in catalog_brain_result.dictionaries():
            for column_id, value in catalog_brain_line.iteritems():
              if column_id in value_column_dict:
                value_column_dict[column_id].append(value)
          if len(value_column_dict[COUNT_COLUMN_TITLE]):
            try:
              Base_zInsertIntoWorklistTable(**value_column_dict)
            except (ProgrammingError, OperationalError), error_value:
              # OperationalError 1054 = unknown column
              if isinstance(error_value, OperationalError) and error_value[0] != 1054:
                raise
              LOG('WorkflowTool', 100, 'Insertion in worklist cache table ' \
                  'failed. Recreating table and retrying.',
                  error=sys.exc_info())
              self.Base_zCreateWorklistTable()
              Base_zInsertIntoWorklistTable(**value_column_dict)

  def _getWorklistIgnoredSecurityColumnSet(self):
    return getattr(self,
      'Base_getWorklistIgnoredSecurityColumnSet', lambda: ())()

  def listActions(self, info=None, object=None, src__=False):
    """
      Returns a list of actions to be displayed to the user.
          o Invoked by the portal_actions tool.
          o Allows workflows to include actions to be displayed in the
            actions box.
          o Object actions are supplied by workflows that apply to the object.
          o Global actions are supplied by all workflows.
      This patch attemps to make listGlobalActions aware of worklists,
      which allows factorizing them into one single SQL query.
      Related keys are supported.
      Warning: the worklist cache does not support them.
    """
    if object is not None or info is None:
      info = self._getOAI(object)
    chain = self.getChainFor(info.object)
    did = {}
    actions = []
    worklist_dict = {}

    document = info.object

    if document is not None:
      document_pt = document.getTypeInfo()
      if document_pt is not None:
        workflow_list = document_pt.getTypeERP5WorkflowList()
        if (workflow_list is not None) and (workflow_list is not []):
          for wf_id in workflow_list:
            did[wf_id] = None
564
            wf = self.getPortalObject().portal_workflow._getOb(wf_id, None)
565 566 567
            if wf is None:
              raise NotImplementedError ("Can not find workflow: %s, please check if the workflow exists."%wf_id)
            a = wf.listObjectActions(info)
568
            if a is not None and a != []:
569 570 571 572
              actions.extend(a)
            a = wf.getWorklistVariableMatchDict(info)
            if a is not None:
              worklist_dict[wf_id] = a
573
    # DC workflow compatibility
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
    for wf_id in chain:
      did[wf_id] = None
      wf = self.getWorkflowById(wf_id)
      if wf is not None:
        a = wf.listObjectActions(info)
        if a is not None:
          actions.extend(a)
        a = wf.getWorklistVariableMatchDict(info)
        if a is not None:
          worklist_dict[wf_id] = a

    wf_ids = self.getWorkflowIds()
    for wf_id in wf_ids:
      if not did.has_key(wf_id):
        wf = self.getWorkflowById(wf_id)
        if wf is not None:
          a = wf.getWorklistVariableMatchDict(info)
          if a is not None:
            worklist_dict[wf_id] = a

    if worklist_dict:
      portal = self.getPortalObject()
      portal_url = portal.portal_url()
      def _getWorklistActionList():
        is_anonymous = portal.portal_membership.isAnonymousUser()
        portal_catalog = portal.portal_catalog
        sql_catalog = portal_catalog.getSQLCatalog()
        catalog_security_uid_groups_columns_dict = \
          sql_catalog.getSQLCatalogSecurityUidGroupsColumnsDict()
        getSecurityUidDictAndRoleColumnDict = \
          portal_catalog.getSecurityUidDictAndRoleColumnDict
        search_result = getattr(self, "Base_getCountFromWorklistTable", None)
        use_cache = search_result is not None
        if use_cache:
          ignored_security_column_id_set = self._getWorklistIgnoredSecurityColumnSet()
          ignored_security_uid_parameter_set = {x
            for x, y in catalog_security_uid_groups_columns_dict.iteritems()
            if y in ignored_security_column_id_set
          }
          _getSecurityUidDictAndRoleColumnDict = getSecurityUidDictAndRoleColumnDict
          def getSecurityUidDictAndRoleColumnDict(**kw):
            security_uid_dict, role_column_dict, local_role_column_dict = \
              _getSecurityUidDictAndRoleColumnDict(**kw)
            for ignored_security_column_id in ignored_security_column_id_set:
              role_column_dict.pop(ignored_security_column_id, None)
              local_role_column_dict.pop(ignored_security_column_id, None)
            for ignored_security_uid_parameter in \
                ignored_security_uid_parameter_set:
              security_uid_dict.pop(ignored_security_uid_parameter)
            return security_uid_dict, role_column_dict, local_role_column_dict
          select_expression_prefix = 'sum(`%s`) as %s' % (COUNT_COLUMN_TITLE, COUNT_COLUMN_TITLE)
          # Prevent catalog from trying to join
          getQuery = SimpleQuery
        else:
          search_result = portal_catalog.unrestrictedSearchResults
          select_expression_prefix = 'count(*) as %s' % (COUNT_COLUMN_TITLE, )
          # Let catalog join as needed
          getQuery = lambda comparison_operator=None, **kw: AutoQuery(
            operator=comparison_operator,
            **kw
          )
        worklist_result_dict = {}
        # Get a list of dict of WorklistVariableMatchDict grouped by compatible
        # conditions

        (worklist_list_grouped_by_condition, worklist_metadata) = \
          groupWorklistListByCondition(
            worklist_dict=worklist_dict,
            sql_catalog=sql_catalog,
            getSecurityUidDictAndRoleColumnDict=\
              getSecurityUidDictAndRoleColumnDict,
            catalog_security_uid_groups_columns_dict=\
              catalog_security_uid_groups_columns_dict,
          )
        if src__:
          action_list = []
        for grouped_worklist_dict in worklist_list_grouped_by_condition:
          # Generate the query for this worklist_list
          (total_criterion_id_list, query) = \
            getWorklistListQuery(
              getQuery=getQuery,
              grouped_worklist_dict=grouped_worklist_dict,
            )
          group_by_expression = ', '.join(total_criterion_id_list)
          assert COUNT_COLUMN_TITLE not in total_criterion_id_list
          # If required mapping method is not present on the query, assume it
          # handles column mapping properly, and build a bare select
          # expression.
          select_expression = select_expression_prefix + ', ' \
                              + group_by_expression
          catalog_brain_result = []
          try:
            catalog_brain_result = search_result(
                                        select_expression=select_expression,
                                        group_by_expression=group_by_expression,
                                        query=query,
                                        limit=None,
                                        src__=src__)
          except Unauthorized:
            if not is_anonymous:
              raise
            LOG('WorkflowTool.listActions', WARNING,
                'Exception while computing worklists: %s'
                % grouped_worklist_dict.keys(),
                error=sys.exc_info())
            continue
          except ProgrammingError, error_value:
            # 1146 = table does not exist
            if not use_cache or error_value[0] != 1146:
              raise
            try:
              self.Base_zCreateWorklistTable()
            except ProgrammingError, error_value:
              # 1050 = table exists (alarm run just a bit too late)
              if error_value[0] != 1050:
                raise
          if src__:
            action_list.append(catalog_brain_result)
          else:
            grouped_worklist_result = sumCatalogResultByWorklist(
              grouped_worklist_dict=grouped_worklist_dict,
              catalog_result=catalog_brain_result)
            for key, value in grouped_worklist_result.iteritems():
              worklist_result_dict[key] = value + worklist_result_dict.get(key, 0)
        if not src__:
          action_list = sorted(
            generateActionList(
              worklist_metadata=worklist_metadata,
              worklist_result=worklist_result_dict,
              portal_url=portal_url),
            key=lambda x: '/'.join((x['workflow_id'], x['worklist_id'])),
          )
        return action_list

      user = str(_getAuthenticatedUser(self))
      if src__:
        actions = _getWorklistActionList()
      else:
        _getWorklistActionList = CachingMethod(_getWorklistActionList,
          id=('_getWorklistActionList', user, portal_url),
          cache_factory = 'erp5_ui_short')
        actions.extend(_getWorklistActionList())
    return actions

InitializeClass(ERP5WorkflowTool)

_sql_cast_dict = {
  'i': long,
  'l': long,
  'n': float,
  'd': DateTime,
}
_sql_cast_fallback = str

class WorkflowMethod( Method ):

    """ Wrap a method to workflow-enable it.
    """
    _need__name__=1

    def __init__(self, method, id=None, reindex=1):
        self._m = method
        if id is None:
            id = method.__name__
        self._id = id
        # reindex ignored since workflows now perform the reindexing.

    def __call__(self, instance, *args, **kw):

        """ Invoke the wrapped method, and deal with the results.
        """
        wf = getToolByName(instance, 'portal_workflow', None)
        if wf is None or not hasattr(wf, 'wrapWorkflowMethod'):
            # No workflow tool found.
            try:
                res = self._m(instance, *args, **kw)
            except ObjectDeleted, ex:
                res = ex.getResult()
            else:
                if hasattr(aq_base(instance), 'reindexObject'):
                    instance.reindexObject()
        else:
            res = wf.wrapWorkflowMethod(instance, self._id, self._m,
                                        (instance,) + args, kw)

from Products.CMFCore import WorkflowCore
# BBB: WorkflowMethod has been removed from CMFCore 2
WorkflowCore.WorkflowAction = WorkflowMethod

def getValidCriterionDict(worklist_match_dict, sql_catalog, workflow_worklist_key):
  valid_criterion_dict = {}
  metadata = None
  isValidColumn = sql_catalog.isValidColumn
  for criterion_id, criterion_value in worklist_match_dict.iteritems():
    if isValidColumn(criterion_id):
      if isinstance(criterion_value, tuple):
        criterion_value = list(criterion_value)
      elif isinstance(criterion_value, (str, int, long)):
        criterion_value = [criterion_value]
      assert criterion_id not in valid_criterion_dict
      valid_criterion_dict[criterion_id] = criterion_value
    elif criterion_id == WORKLIST_METADATA_KEY:
      metadata = criterion_value
    elif criterion_id == SECURITY_PARAMETER_ID:
      pass
    else:
      LOG('WorkflowTool_listActions', WARNING, 'Worklist %r' \
          ' filters on variable %r which is not available ' \
          'in catalog. Its value will not be checked.' % \
          (workflow_worklist_key, criterion_id))
  return valid_criterion_dict, metadata

def updateWorklistSetDict(worklist_set_dict, workflow_worklist_key, valid_criterion_dict):
  worklist_set_dict_key = valid_criterion_dict.keys()
  if len(worklist_set_dict_key):
    worklist_set_dict_key.sort()
    worklist_set_dict_key = tuple(worklist_set_dict_key)
    if worklist_set_dict_key not in worklist_set_dict:
      worklist_set_dict[worklist_set_dict_key] = {}
    worklist_set_dict[worklist_set_dict_key]\
      [workflow_worklist_key] = valid_criterion_dict

def groupWorklistListByCondition(worklist_dict, sql_catalog, getSecurityUidDictAndRoleColumnDict=None, catalog_security_uid_groups_columns_dict=None,):
  """
    Get a list of dict of WorklistVariableMatchDict grouped by compatible
    conditions.
    Strip any variable which is not a catalog column.
    Returns metadata in a separate dict.

    Example:
      Input:
        worklist_dict:
        {'workflow_A': {'worklist_AA': {'foo': (1, 2), 'bar': (3, 4)},
                        'worklist_AB': {'baz': (5, )}
                       }
         'workflow_B': {'worklist_BA': {'baz': (6, )}
                       }
        }

      Allowed columns are:
      sql_catalog.isValidColumn('foo') is True
      sql_catalog.isValidColumn('baz') is True
      sql_catalog.isValidColumn('bar') is False

      Output a 2-tuple:
        (
          [{'workflow_A/worklist_AA': {'foo': (1, 2)}
           },
           {'workflow_A/worklist_AB': {'baz': (5, )},
            'workflow_B/worklist_BA': {'baz': (6, )}
           }
          ]
        ,
          {} # Contains metadata information, one entry per worklist.
        )
  """
  # One entry per worklist group, based on filter criterions.
  worklist_set_dict = {}
  metadata_dict = {}
  for workflow_id, worklist in worklist_dict.iteritems():
    for worklist_id, worklist_match_dict in worklist.iteritems():
      workflow_worklist_key = '/'.join((workflow_id, worklist_id))
      if getSecurityUidDictAndRoleColumnDict is None:
        valid_criterion_dict, metadata = getValidCriterionDict(
          worklist_match_dict=worklist_match_dict,
          sql_catalog=sql_catalog,
          workflow_worklist_key=workflow_worklist_key)
        if metadata is not None:
          metadata_dict[workflow_worklist_key] = metadata
        updateWorklistSetDict(
          worklist_set_dict=worklist_set_dict,
          workflow_worklist_key=workflow_worklist_key,
          valid_criterion_dict=valid_criterion_dict)
      else:
        security_parameter = worklist_match_dict.get(SECURITY_PARAMETER_ID, [])
        security_kw = {}
        if len(security_parameter):
          security_kw[SECURITY_PARAMETER_ID] = security_parameter
        uid_dict, role_column_dict, local_role_column_dict = \
            getSecurityUidDictAndRoleColumnDict(**security_kw)

        for key, value in local_role_column_dict.items():
          worklist_match_dict[key] = [value]

        for local_roles_group_id, uid_list in uid_dict.iteritems():
          role_column_dict[
            catalog_security_uid_groups_columns_dict[local_roles_group_id]] = uid_list

        # Make sure every item is a list - or a tuple
        for security_column_id in role_column_dict.iterkeys():
          value = role_column_dict[security_column_id]
          if not isinstance(value, (tuple, list)):
            role_column_dict[security_column_id] = [value]
        applied_security_criterion_dict = {}
        # TODO: make security criterions be examined in the same order for all
        # worklists if possible at all.
        for security_column_id, security_column_value in \
            role_column_dict.iteritems():
          valid_criterion_dict, metadata = getValidCriterionDict(
            worklist_match_dict=worklist_match_dict,
            sql_catalog=sql_catalog,
            workflow_worklist_key=workflow_worklist_key)
          if metadata is not None:
            metadata_dict[workflow_worklist_key] = metadata
          valid_criterion_dict.update(applied_security_criterion_dict)
          # Current security criterion must be applied to all further queries
          # for this worklist negated, so the a given line cannot match multiple
          # times.
          applied_security_criterion_dict[security_column_id] = \
            ExclusionList(security_column_value)
          valid_criterion_dict[security_column_id] = security_column_value
          updateWorklistSetDict(
            worklist_set_dict=worklist_set_dict,
            workflow_worklist_key=workflow_worklist_key,
            valid_criterion_dict=valid_criterion_dict)
  return worklist_set_dict.values(), metadata_dict

def generateNestedQuery(getQuery, priority_list, criterion_dict, possible_worklist_id_dict=None):
  assert possible_worklist_id_dict is None \
         or len(possible_worklist_id_dict) != 0
  my_priority_list = priority_list[:]
  my_criterion_id = my_priority_list.pop()
  query_list = []
  append = query_list.append
  my_criterion_dict = criterion_dict[my_criterion_id]
  if len(my_priority_list) > 0:
    for criterion_value, worklist_id_dict in my_criterion_dict.iteritems():
      if possible_worklist_id_dict is not None:
        criterion_worklist_id_dict = worklist_id_dict.copy()
        # Do not use iterkeys since the dictionary will be modified in the
        # loop
        for worklist_id in criterion_worklist_id_dict.keys():
          if worklist_id not in possible_worklist_id_dict:
            del criterion_worklist_id_dict[worklist_id]
      else:
        criterion_worklist_id_dict = worklist_id_dict
      if len(criterion_worklist_id_dict):
        subcriterion_query = generateNestedQuery(
          getQuery=getQuery,
          priority_list=my_priority_list,
          criterion_dict=criterion_dict,
          possible_worklist_id_dict=criterion_worklist_id_dict)
        if subcriterion_query is not None:
          query = getQuery(comparison_operator='IN',
                        **{my_criterion_id: criterion_value})
          if isinstance(criterion_value, ExclusionTuple):
            query = NegatedQuery(query)
            query = ComplexQuery(operator='OR',
                      *(query, getQuery(**{my_criterion_id: None})))
          append(ComplexQuery(query, subcriterion_query, operator='AND'))
  else:
    possible_value_list = tuple()
    impossible_value_list = tuple()
    possible = True
    for criterion_value, criterion_worklist_id_dict \
        in my_criterion_dict.iteritems():
      if possible_worklist_id_dict is not None:
        possible = False
        for worklist_id in criterion_worklist_id_dict.iterkeys():
          if worklist_id in possible_worklist_id_dict:
            possible = True
            break
      if possible:
        if isinstance(criterion_value, ExclusionTuple):
          impossible_value_list += criterion_value
        else:
          possible_value_list += criterion_value
    value_query_list = []
    if len(possible_value_list):
      query = getQuery(
        comparison_operator='IN',
        **{my_criterion_id: possible_value_list}
      )
      value_query_list.append(query)
    if len(impossible_value_list):
      query = getQuery(
        comparison_operator='IN',
        **{my_criterion_id: impossible_value_list}
      )
      query = NegatedQuery(query)
      query = ComplexQuery(operator='OR',
                *(query, getQuery(**{my_criterion_id: None})))
      value_query_list.append(query)
    append(ComplexQuery(operator='AND', *value_query_list))
  if len(query_list):
    return ComplexQuery(operator='OR', *query_list)
  return None

def getWorklistListQuery(getQuery, grouped_worklist_dict):
  """
    Return a tuple of 3 value:
    - a select_expression with a count(*) and all columns used in
      goup_by_expression
    - a group_by_expression with all columns required for provided
      grouped_worklist_dict
    - a query applying all criterions contained in provided
      grouped_worklist_dict
  """
  query_list = []
  total_criterion_id_dict = {}
  for worklist_id, worklist in grouped_worklist_dict.iteritems():
    for criterion_id, criterion_value in worklist.iteritems():
      criterion_value_to_worklist_dict_dict = \
        total_criterion_id_dict.setdefault(criterion_id, {})
      criterion_value.sort()
      if isinstance(criterion_value, ExclusionList):
        criterion_value = ExclusionTuple(criterion_value)
      else:
        criterion_value = tuple(criterion_value)
      criterion_value_to_worklist_dict = \
        criterion_value_to_worklist_dict_dict.setdefault(criterion_value, {})
      criterion_value_to_worklist_dict[worklist_id] = None
  total_criterion_id_list = sorted(total_criterion_id_dict, key=lambda y: max(
    len(x) for x in total_criterion_id_dict[y].itervalues()))
  query = generateNestedQuery(
    getQuery=getQuery,
    priority_list=total_criterion_id_list,
    criterion_dict=total_criterion_id_dict,
  )
  assert query is not None
  assert COUNT_COLUMN_TITLE not in total_criterion_id_dict
  return (total_criterion_id_list, query)

def generateActionList(worklist_metadata, worklist_result, portal_url):
  """
    For each worklist generate action_list as expected by portal_actions.
  """
  action_list = []
  append = action_list.append
  for key, metadata in worklist_metadata.iteritems():
    document_count = worklist_result.get(key, 0)
    if document_count:
      format_data = metadata['format_data']
      format_data._push({'count': document_count})
      append({'name': metadata['worklist_title'] % format_data,
              'url': '%s/%s' % (portal_url, metadata['action_box_url'] % \
                                            format_data),
              'worklist_id': metadata['worklist_id'],
              'workflow_title': metadata['workflow_title'],
              'workflow_id': metadata['workflow_id'],
              'count': format_data['count'],
              'permissions': (),  # Predetermined.
              'category': metadata['action_box_category']})
  return action_list

def sumCatalogResultByWorklist(grouped_worklist_dict, catalog_result):
  """
    Return a dict regrouping each worklist's result, extracting it from
    catalog result.
    Build a dictionnary summing up which value combination interests which
    worklist, then iterate catalog result lines and give results to
    corresponding worklists.

    It is better to avoid reading multiple times the catalog result from
    flexibility point of view: if it must ever be changed into a cursor, this
    code will keep working nicely without needing to rewind the cursor.

    This code assumes that all worklists have the same set of criterion ids,
    and that when a criterion id is associated with an ExclusionList it is
    also true for all worklists.
  """
  worklist_result_dict = {}
  if len(catalog_result) > 0:
    # Transtype all worklist definitions where needed
    criterion_id_list = []
    class_dict = {name: _sql_cast_dict.get(x['type'], _sql_cast_fallback)
      for name, x in catalog_result.data_dictionary().iteritems()}
    for criterion_dict in grouped_worklist_dict.itervalues():
      for criterion_id, criterion_value_list in criterion_dict.iteritems():
        if type(criterion_value_list) is not ExclusionList:
          criterion_id_list.append(criterion_id)
          expected_class = class_dict[criterion_id]
          if type(criterion_value_list[0]) is not expected_class:
            criterion_dict[criterion_id] = ImmutableSet([expected_class(x) for x in criterion_value_list])
          elif type(criterion_value_list) is not ImmutableSet:
            criterion_dict[criterion_id] = ImmutableSet(criterion_dict[criterion_id])
    # Read catalog result and distribute to matching worklists
    for result_line in catalog_result:
      result_count = int(result_line[COUNT_COLUMN_TITLE])
      for worklist_id, criterion_dict in grouped_worklist_dict.iteritems():
        is_candidate = True
        for criterion_id in criterion_id_list:
          criterion_value_set = criterion_dict[criterion_id]
          if result_line[criterion_id] not in criterion_value_set:
            is_candidate = False
            break
        if is_candidate:
          try:
            worklist_result_dict[worklist_id] += result_count
          except KeyError:
            worklist_result_dict[worklist_id] = result_count
  return worklist_result_dict

class WorkflowHistoryList(Persistent):
    _bucket_size = 16

    def __init__(self, iterable=None, prev=None):
        self._prev = prev
        self._slots = []
        if iterable is not None:
            for x in iterable:
                self.append(x)

    def __add__(self, iterable):
        return self.__class__(tuple(self) + tuple(iterable))

    def __contains__(self, item):
        return item in tuple(self)

    def __eq__(self, other):
        return tuple(self) == tuple(other)

    def __getitem__(self, index):
        if index == -1:
            return self._slots[-1]
        elif isinstance(index, (int, long)):
            if index < 0:
                # XXX this implementation is not so good, but rarely used.
                index += len(self)
            iterator = self.__iter__()
            for i in xrange(index):
                iterator.next()
            return iterator.next()
        elif isinstance(index, slice):
            return self.__class__((self[x] for x in
                                   xrange(*index.indices(len(self)))))
        else:
            raise TypeError, 'tuple indices must be integers'

    def __getslice__(self, start, end):
        return self.__getitem__(slice(start, end))

    def __getstate__(self):
        return (self._prev, self._slots)

    def __iter__(self):
        bucket = self
        stack = []
        while bucket is not None:
            stack.append(bucket)
            bucket = bucket._prev
        for i in reversed(stack):
            for j in i._slots:
                yield j

    def __len__(self):
        length = len(self._slots)
        bucket = self._prev
        while bucket is not None:
            length += len(bucket._slots)
            bucket = bucket._prev
        return length

    def __mul__(self, x):
        return self.__class__(tuple(self) * x)

    def __nonzero__(self):
        return len(self._slots) != 0 or self._prev is not None

    def __repr__(self):
        #return '%s' % repr(tuple(self.__iter__()))
        return '<%s object at 0x%x %r>' % (self.__class__.__name__, id(self), tuple(self))

    def __rmul__(self, x):
        return self.__class__(x * tuple(self))

    def __setstate__(self, state):
        self._prev, self._slots = state

    def append(self, value):
        if len(self._slots) < self._bucket_size:
            self._slots.append(value)
            self._p_changed = 1
        else:
            self._prev = self.__class__(self._slots, prev=self._prev)
            self._slots = [value]

def DCWorkflowDefinition_notifyWorkflowMethod(self, ob, transition_list, args=None, kw=None):
    '''
    Allows the system to request a workflow action.  This method
    must perform its own security checks.
    '''
    if type(transition_list) in StringTypes:
      method_id = transition_list
    elif len(transition_list) == 1:
      method_id = transition_list[0]
    else:
      raise ValueError('WorkflowMethod should be attached to exactly 1 transition per DCWorkflow instance.')
    sdef = self._getWorkflowStateOf(ob)
    if sdef is None:
        raise WorkflowException, 'Object is in an undefined state'
    if method_id not in sdef.transitions:
        raise Unauthorized(method_id)
    tdef = self.transitions.get(method_id, None)
    if tdef is None or tdef.trigger_type != TRIGGER_WORKFLOW_METHOD:
        raise WorkflowException, (
            'Transition %s is not triggered by a workflow method'
            % method_id)
    if not self._checkTransitionGuard(tdef, ob):
        raise Unauthorized(method_id)
    self._changeStateOf(ob, tdef, kw)
    if getattr(ob, 'reindexObject', None) is not None:
        if kw is not None:
            activate_kw = kw.get('activate_kw', {})
        else:
            activate_kw = {}
        ob.reindexObject(activate_kw=activate_kw)

def DCWorkflowDefinition_notifyBefore(self, ob, transition_list, args=None, kw=None):
    '''
    Notifies this workflow of an action before it happens,
    allowing veto by exception.  Unless an exception is thrown, either
    a notifySuccess() or notifyException() can be expected later on.
    The action usually corresponds to a method name.
    '''
    pass

def DCWorkflowDefinition_notifySuccess(self, ob, transition_list, result, args=None, kw=None):
    '''
    Notifies this workflow that an action has taken place.
    '''
    pass

DCWorkflowDefinition.notifyWorkflowMethod = DCWorkflowDefinition_notifyWorkflowMethod
DCWorkflowDefinition.notifyBefore = DCWorkflowDefinition_notifyBefore
DCWorkflowDefinition.notifySuccess = DCWorkflowDefinition_notifySuccess

WORKLIST_METADATA_KEY = 'metadata'
SECURITY_PARAMETER_ID = 'local_roles'
COUNT_COLUMN_TITLE = 'count'

class ExclusionList(list):
  """
    This is a dummy subclass of list.
    It is only used to detect wether contained values must be negated.
    It is not to be used outside of the scope of this document nor outside
    of the scope of worklist criterion handling.
  """
  pass

class ExclusionTuple(tuple):
  """
    This is a dummy subclass of tuple.
    It is only used to detect wether contained values must be negated.
    It is not to be used outside of the scope of this document nor outside
    of the scope of worklist criterion handling.
  """
  pass