<?xml version="1.0"?>
<ZopeData>
  <record id="1" aka="AAAAAAAAAAE=">
    <pickle>
      <global name="PythonScript" module="Products.PythonScripts.PythonScript"/>
    </pickle>
    <pickle>
      <dictionary>
        <item>
            <key> <string>Script_magic</string> </key>
            <value> <int>3</int> </value>
        </item>
        <item>
            <key> <string>_bind_names</string> </key>
            <value>
              <object>
                <klass>
                  <global name="NameAssignments" module="Shared.DC.Scripts.Bindings"/>
                </klass>
                <tuple/>
                <state>
                  <dictionary>
                    <item>
                        <key> <string>_asgns</string> </key>
                        <value>
                          <dictionary>
                            <item>
                                <key> <string>name_container</string> </key>
                                <value> <string>container</string> </value>
                            </item>
                            <item>
                                <key> <string>name_context</string> </key>
                                <value> <string>context</string> </value>
                            </item>
                            <item>
                                <key> <string>name_m_self</string> </key>
                                <value> <string>script</string> </value>
                            </item>
                            <item>
                                <key> <string>name_subpath</string> </key>
                                <value> <string>traverse_subpath</string> </value>
                            </item>
                          </dictionary>
                        </value>
                    </item>
                  </dictionary>
                </state>
              </object>
            </value>
        </item>
        <item>
            <key> <string>_body</string> </key>
            <value> <string encoding="cdata"><![CDATA[

software_instance = state_change[\'object\']\n
portal = software_instance.getPortalObject()\n
# Get required arguments\n
kwargs = state_change.kwargs\n
software_release_url_string = state_change.kwargs[\'software_release\']\n
requested_partition_reference = kwargs["partition_reference"]\n
software_type = kwargs["software_type"]\n
instance_xml = kwargs["instance_xml"]\n
is_slave = kwargs.get("shared", False)\n
sla_xml = kwargs["sla_xml"]\n
state = kwargs["state"]\n
\n
from DateTime import DateTime\n
\n
if is_slave == True:\n
  software_instance_portal_type = "Slave Instance"\n
else:\n
  software_instance_portal_type = "Software Instance"\n
\n
# graph allows to "simulate" tree change after requested operation\n
graph = {}\n
# Get root software instance and create initial graph\n
root_hosting_subscription = software_instance.portal_catalog.getResultValue(\n
  uid=software_instance.SoftwareInstance_getRootHostingSubscriptionUid(),\n
)\n
\n
predecessor_list = root_hosting_subscription.getPredecessorValueList()\n
graph[root_hosting_subscription.getUid()] = [predecessor.getUid() for predecessor in predecessor_list]\n
while True:\n
  try:\n
    current_software_instance = predecessor_list.pop(0)\n
  except IndexError:\n
    break\n
  current_software_instance_predecessor_list = current_software_instance.getPredecessorValueList() or []\n
  graph[current_software_instance.getUid()] = [predecessor.getUid()\n
                                               for predecessor in current_software_instance_predecessor_list]\n
  predecessor_list.extend(current_software_instance_predecessor_list)\n
\n
tag = "%s_%s_inProgress" % (root_hosting_subscription.getUid(),\n
                               requested_partition_reference)\n
\n
# Check if it already exists\n
request_software_instance = software_instance.portal_catalog.getResultValue(\n
  portal_type=software_instance_portal_type,\n
  # XXX: User based property is used in non manual operation\n
  # XXX-2: Do we really need to use root_uid?\n
  title=requested_partition_reference,\n
  root_uid=root_hosting_subscription.getUid(),\n
)\n
\n
if (portal.portal_activities.countMessageWithTag(tag) > 0):\n
  # The software instance is already under creation but can not be fetched from catalog\n
  # As it is not possible to fetch informations, it is better to raise an error\n
  raise NotImplementedError(tag)\n
\n
if root_hosting_subscription.getTitle() == requested_partition_reference:\n
  raise ValueError(\'It is disallowed to request root software instance\')\n
\n
if (request_software_instance is None):\n
  # First time that the software instance is requested\n
  # Create a new one\n
  module = software_instance.getDefaultModule(portal_type="Software Instance")\n
  request_software_instance = module.newContent(\n
    portal_type=software_instance_portal_type,\n
    title=requested_partition_reference,\n
    source_reference=software_type,\n
    text_content=instance_xml,\n
    sla_xml=sla_xml,\n
    activate_kw={\'tag\': tag},\n
    **portal.Base_getNewSoftwareInstanceCoordinate()\n
  )\n
  request_software_instance.portal_workflow.doActionFor(request_software_instance, \'validate_action\')\n
  packing_list_line = software_instance.getAggregateRelatedValue(portal_type="Sale Packing List Line")\n
  subscription = packing_list_line.getAggregateValue(portal_type="Hosting Subscription")\n
  software_release_document = context.portal_catalog.getResultValue(\n
    portal_type=\'Software Release\',\n
    url_string=software_release_url_string)\n
  base_sale_packing_list = packing_list_line.getParentValue()\n
  trade_condition = packing_list_line.getSpecialise(portal_type="Sale Trade Condition")\n
  sale_order = portal.getDefaultModule(portal_type="Sale Order").newContent(\n
    portal_type="Sale Order",\n
    destination=base_sale_packing_list.getDestination(),\n
     destination_section=base_sale_packing_list.getDestinationSection(),\n
     destination_decision=base_sale_packing_list.getDestinationDecision(),\n
    start_date=DateTime(),\n
    received_date=DateTime(),\n
    # XXX Hardcoded values\n
    source="organisation_module/vifib_internet",\n
    source_section="organisation_module/vifib_internet",\n
    quantity_unit="unit/piece",\n
    price_currency="currency_module/EUR",\n
    activate_kw={\'tag\': tag},\n
    )\n
\n
  setup_service_relative_url = portal.portal_preferences.getPreferredInstanceSetupResource()\n
  service = portal.restrictedTraverse(setup_service_relative_url)\n
  sale_order.setSpecialise(trade_condition, portal_type="Sale Trade Condition")\n
  sale_order_line = sale_order.newContent(\n
    portal_type="Sale Order Line",\n
    resource=setup_service_relative_url,\n
    quantity=1,\n
    price=service.getSaleSupplyLineBasePrice(),\n
    aggregate_value_list=[request_software_instance,subscription,software_release_document],\n
    activate_kw={\'tag\': tag},\n
  )\n
  sale_order.order()\n
else:\n
  # Update existing software instance\n
  # Sale Packing List interaction has to be requested automatically with an interaction workflow\n
  request_software_instance.edit(\n
    text_content=instance_xml,\n
    source_reference=software_type,\n
    sla_xml=sla_xml,\n
    activate_kw={\'tag\': tag},\n
  )\n
  # Update the predecessor category of the original caller\n
  predecessor_software_instance = request_software_instance.getPredecessorRelatedValue(\n
    portal_type="Software Instance")\n
\n
  predecessor = None\n
  if predecessor_software_instance is None:\n
    sale_packing_list_line = context.SoftwareInstance_getInstanceSetupPackingListLine(state_change)\n
    hosting_subscription = sale_packing_list_line.getAggregateValue(portal_type=\'Hosting Subscription\')\n
    if request_software_instance.getPredecessorRelatedValue(portal_type="Hosting Subscription") != hosting_subscription:\n
      raise ValueError(\'Requested Software Instance %s should have a predecessor from same tree\' % request_software_instance.getRelativeUrl())\n
    else:\n
        predecessor = hosting_subscription\n
  else:\n
    predecessor = predecessor_software_instance\n
\n
  predecessor_uid_list = predecessor.getPredecessorUidList()\n
  predecessor_uid_list.remove(request_software_instance.getUid())\n
  predecessor.edit(\n
    predecessor_uid_list=predecessor_uid_list,\n
    activate_kw={\'tag\': tag},)\n
  graph[predecessor.getUid()] = predecessor_uid_list\n
\n
  if state == \'started\':\n
    request_software_instance.startRequested()\n
    request_software_instance.activate(after_tag=tag).requestStartComputerPartition()\n
  elif state == \'stopped\':\n
    request_software_instance.stopRequested()\n
    request_software_instance.activate(after_tag=tag).requestStopComputerPartition()\n
  else:\n
    raise ValueError(\'State %r is not supported\' % state)\n
predecessor_list = software_instance.getPredecessorList() + [request_software_instance.getRelativeUrl()]\n
# Add requested software instance to graph if does not exists there yet\n
if not request_software_instance.getUid() in graph:\n
  graph[request_software_instance.getUid()] = []\n
\n
# update graph to reflect requested operation\n
graph[software_instance.getUid()] = software_instance.getPredecessorUidList() + [request_software_instance.getUid()]\n
\n
# check if all elements are still connected and if there is no cycle\n
software_instance.checkConnected(graph, root_hosting_subscription.getUid())\n
software_instance.checkNotCyclic(graph)\n
\n
software_instance.edit(\n
  predecessor_list=predecessor_list,\n
  activate_kw={\'tag\': tag},)\n


]]></string> </value>
        </item>
        <item>
            <key> <string>_params</string> </key>
            <value> <string>state_change</string> </value>
        </item>
        <item>
            <key> <string>id</string> </key>
            <value> <string>SoftwareInstance_requestSoftwareInstance</string> </value>
        </item>
      </dictionary>
    </pickle>
  </record>
</ZopeData>
