Transition.py 5.75 KB
Newer Older
1 2 3 4
##############################################################################
#
# Copyright (c) 2006 Nexedi SARL and Contributors. All Rights Reserved.
#                    Romain Courteaud <romain@nexedi.com>
5
#               2015 Wenjie Zheng <wenjie.zheng@tiolive.com>
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
# 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.
#
##############################################################################

from AccessControl import ClassSecurityInfo

from Products.ERP5Type import Permissions, PropertySheet
from Products.ERP5Type.XMLObject import XMLObject
from Products.ERP5Type.Accessor.Base import _evaluateTales
34
from zLOG import LOG, ERROR, DEBUG, WARNING
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

class Transition(XMLObject):
  """
  A ERP5 Transition.
  """

  meta_type = 'ERP5 Transition'
  portal_type = 'Transition'
  add_permission = Permissions.AddPortalContent
  isPortalContent = 1
  isRADContent = 1

  # Declarative security
  security = ClassSecurityInfo()
  security.declareObjectProtected(Permissions.AccessContentsInformation)

  # Declarative properties
  property_sheets = (
             PropertySheet.Base,
             PropertySheet.XMLObject,
             PropertySheet.CategoryCore,
             PropertySheet.DublinCore,
             PropertySheet.Transition,
  )

  def execute(self, document, form_kw=None):
    """
    Execute transition.
    """
64 65
    if form_kw is None:
      form_kw = {}
Rafael Monnerat's avatar
Rafael Monnerat committed
66
    workflow = self.getParentValue()
67
    # Call the before script
68
    self._executeBeforeScript(document, form_kw=form_kw)
69 70 71

    # Modify the state
    self._changeState(document)
72 73
    ### zwj: update Role mapping, also in Workflow, initialiseDocument()
    self.getParent().updateRoleMappingsFor(document)
74
    # Get variable values
Rafael Monnerat's avatar
Rafael Monnerat committed
75
    status_dict = workflow.getCurrentStatusDict(document)
76 77 78
    status_dict['undo'] = 0

    # Modify workflow history
Rafael Monnerat's avatar
Rafael Monnerat committed
79
    state_bc_id = workflow.getStateBaseCategory()
80 81 82
    status_dict[state_bc_id] = document.getCategoryMembershipList(state_bc_id)[0]

    state_object = document.unrestrictedTraverse(status_dict[state_bc_id])
Rafael Monnerat's avatar
Rafael Monnerat committed
83
    object = workflow.getStateChangeInformation(document, state_object, transition=self)
84 85

    # Update all variables
Rafael Monnerat's avatar
Rafael Monnerat committed
86
    for variable in workflow.contentValues(portal_type='Variable'):
87
      if variable.getAutomaticUpdate():
Rafael Monnerat's avatar
Rafael Monnerat committed
88
        # if we have it in form get it from there
89 90
        # otherwise use default
        variable_title = variable.getTitle()
Rafael Monnerat's avatar
Rafael Monnerat committed
91 92
        if variable_title in form_kw:
           status_dict[variable_title] = form_kw[variable_title]
93 94 95 96 97 98
        else:
          status_dict[variable_title] = variable.getInitialValue(object=object)

    # Update all transition variables
    if form_kw is not None:
      object.REQUEST.other.update(form_kw)
Rafael Monnerat's avatar
Rafael Monnerat committed
99
    for variable in self.contentValues(portal_type='Transition Variable'):
100
      status_dict[variable.getCausalityTitle()] = variable.getInitialValue(object=object)
Rafael Monnerat's avatar
Rafael Monnerat committed
101 102

    workflow._updateWorkflowHistory(document, status_dict)
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

    # Call the after script
    self._executeAfterScript(document, form_kw=form_kw)

  def _changeState(self, document):
    """
    Change the state of the object.
    """
    state = self.getDestination()
    if state is not None:
      # Some transitions don't update the state
      state_bc_id = self.getParentValue().getStateBaseCategory()
      document.setCategoryMembership(state_bc_id, state)

  def _executeAfterScript(self, document, form_kw=None):
    """
    Execute post transition script.
    """
    if form_kw is None:
      form_kw = {}
    script_id = self.getAfterScriptId()
    if script_id is not None:
125 126 127 128 129 130

      script = self.getParent()._getOb(script_id)
      if script is not None:
        LOG("zwj: Executing after script %s for %s"%(script_id,self.getId()),WARNING,"in Transition.py.")
        #script(**form_kw) ### zwj: call the name of script to execute itself
        script.execute()
131

132 133 134 135 136 137 138 139
  def _executeBeforeScript(self, document, form_kw=None):
    """
    Execute pre transition script.
    """
    if form_kw is None:
      form_kw = {}
    script_id = self.getBeforeScriptId()
    if script_id is not None:
140 141 142 143 144 145 146
      script = self.getParent()._getOb(script_id)
      #script = getattr(document, script_id)
      #script(**form_kw)
      if script is not None:
        LOG("zwj: Executing before script %s for %s"%(script_id,self.getId()),WARNING,"in Transition.py.")
        #script(**form_kw) ### zwj: call the name of script to execute itself
        script.execute()
147

148 149 150 151 152 153 154 155 156 157 158
  def _checkPermission(self, document):
    """
    Check if transition is allowed.
    """
    expr_value = self.getGuardExpression(evaluate=0)
    if expr_value is not None:
      # do not use 'getGuardExpression' to calculate tales because
      # it caches value which is bad. Instead do it manually
      value = _evaluateTales(document, expr_value)
    else:
      value = True
Rafael Monnerat's avatar
Rafael Monnerat committed
159
    #print "CALC", expr_value, '-->', value
160
    return value