##############################################################################
#
# 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.Workflow import Workflow
from collections import deque

class Questionnaire(Workflow):
  """
  A Answer is contained in an Answer Set and related to a Question.
  It holds a value in one of its value fields depending on the value type.
  """
  meta_type = 'ERP5 Questionnaire'
  portal_type = 'Questionnaire'
  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.Reference,
             PropertySheet.Workflow,)

  def getPOT(self):
    """ 
    get the pot, copy from:
    "dcworkfow2dot.py":http://awkly.org/Members/sidnei/weblog_storage/blog_27014
    and Sidnei da Silva owns the copyright of the this function
    """
    out = []
    transition_dict = {}
    out.append('digraph "%s" {' % self.getTitle())
    transition_with_init_state_list = []
    for state in self.contentValues(portal_type='Question State'):
      out.append('%s [shape=box,label="%s",' \
                    'style="filled",fillcolor="#ffcc99"];' % \
                 (state.getId(), '%s) %s' %(
                   state.getIntIndex(),
                   state.getResourceTitle())))
      # XXX Use API instead of getDestinationValueList
      for available_transition in state.contentValues(portal_type='Answer Transition'):
        transition_with_init_state_list.append(available_transition.getId())
        destination_state = available_transition.getDestinationValue()
        if destination_state is None:
          # take care of 'remain in state' transitions
          destination_state = state
        # 
        key = (state.getId(), destination_state.getId())
        value = transition_dict.get(key, [])
        value.append(available_transition.getTitle())
        transition_dict[key] = value

    for k, v in transition_dict.items():
      out.append('%s -> %s [label="%s"];' % (k[0], k[1],
                                              ',\\n'.join(v)))

    out.append('}')
    return '\n'.join(out)

