##############################################################################
#
# Copyright (c) 2006 Nexedi SARL and Contributors. All Rights Reserved.
#                    Romain Courteaud <romain@nexedi.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.
#
##############################################################################

from AccessControl import ClassSecurityInfo

from Products.ERP5Type import Permissions, PropertySheet
from Products.ERP5Type.XMLObject import XMLObject
from Products.ERP5Workflow.Document.Transition import Transition
from zLOG import LOG, ERROR, DEBUG, WARNING

class AnswerTransition(Transition):
  """
  Other than a normal Transition an Answer Transition lives
  inside a Question State. That make it necessary to overload some methods.
  """
  meta_type = 'ERP5 Answer Transition'
  portal_type = 'Answer 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 getWorkflow(self):
    return self.getParentValue().getParentValue()

  def execute(self, document, form_kw=None):
    """
    Execute transition.
    """
    # Call the before script
    #self._executeBeforeScript(document)

    # Modify the state
    self._changeState(document)

    # Get variable values
    status_dict = self.getWorkflow().getCurrentStatusDict(document)
    status_dict['undo'] = 0

    # Modify workflow history
    state_bc_id = self.getWorkflow().getStateBaseCategory()
    status_dict[state_bc_id] = document.getCategoryMembershipList(state_bc_id)[0]

    state_object = document.unrestrictedTraverse(status_dict[state_bc_id])
    object = self.getWorkflow().getStateChangeInformation(document, state_object, transition=self)

    # Update all variables
    variable_list = self.getWorkflow().contentValues(portal_type='Variable')
    for variable in variable_list:
      if variable.getAutomaticUpdate():
        # if we have it in form get it from there 
        # otherwise use default
        variable_title = variable.getTitle()
        if form_kw.has_key(variable_title):
           status_dict[variable_title] = form_kw[variable_title] 
        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)
    variable_list = self.contentValues(portal_type='Transition Variable')
    for variable in variable_list:
      status_dict[variable.getCausalityTitle()] = variable.getInitialValue(object=object)
        
    self.getWorkflow()._updateWorkflowHistory(document, status_dict)

    # 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.getWorkflow().getStateBaseCategory()
      document.setCategoryMembership(state_bc_id, state)
