<?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>instance = state_change[\'object\']\n
hosting_subscription = instance.getSpecialiseValue(portal_type="Hosting Subscription")\n
person_relative_url = hosting_subscription.getDestinationSection()\n
portal = instance.getPortalObject()\n
isTransitionPossible = portal.portal_workflow.isTransitionPossible\n
\n
# Check if instance is allocated\n
partition = instance.getAggregate(portal_type="Computer Partition")\n
# Get current delivery\n
delivery = instance.getCausalityValue(portal_type=["Sale Order", "Sale Packing List"])\n
# Get latest workflow transition\n
state = instance.getSlapState()\n
started = "start_requested"\n
stopped = "stop_requested"\n
destroyed = "destroy_requested"\n
assert state in [started, stopped, destroyed]\n
# Get all needed services\n
setup_service_relative_url = portal.portal_preferences.getPreferredInstanceSetupResource()\n
setup_service = portal.restrictedTraverse(setup_service_relative_url)\n
hosting_service_relative_url = portal.portal_preferences.getPreferredInstanceHostingResource()\n
hosting_service = portal.restrictedTraverse(hosting_service_relative_url)\n
cleanup_service_relative_url = portal.portal_preferences.getPreferredInstanceCleanupResource()\n
cleanup_service = portal.restrictedTraverse(cleanup_service_relative_url)\n
# Get software release\n
software_release_document = portal.portal_catalog.getResultValue(\n
  portal_type=\'Software Release\',\n
  url_string=instance.getRootSoftwareReleaseUrl())\n
assert software_release_document is not None\n
\n
sale_order_portal_type = "Sale Order"\n
sale_order_line_portal_type = "Sale Order Line"\n
\n
if (partition is None):\n
  # If partition is not allocated yet, only a sale order can be instanciated\n
  if (delivery is None):\n
    assert state != destroyed\n
\n
    sale_order = portal.getDefaultModule(portal_type=sale_order_portal_type).newContent(\n
      portal_type=sale_order_portal_type,\n
      destination=person_relative_url,\n
      destination_section=person_relative_url,\n
      destination_decision=person_relative_url,\n
      start_date=DateTime(),\n
      received_date=DateTime(),\n
      # XXX Hardcoded values\n
      specialise=\'sale_trade_condition_module/vifib_trade_condition\',\n
      source="organisation_module/vifib_internet",\n
      source_section="organisation_module/vifib_internet",\n
      price_currency="currency_module/EUR",\n
    )\n
    sale_order_line = sale_order.newContent(\n
      portal_type=sale_order_line_portal_type,\n
      resource=setup_service_relative_url,\n
      quantity_unit="unit/piece",\n
      quantity=1,\n
      price=setup_service.getSaleSupplyLineBasePrice(),\n
      aggregate_value_list=[instance,\n
                            hosting_subscription,\n
                            software_release_document\n
      ],\n
    )\n
    sale_order.order()\n
    instance.edit(causality_value=sale_order)\n
\n
  else:\n
    assert delivery.getPortalType() == sale_order_portal_type\n
    if state == destroyed:\n
      # Can destroy the instance before allocation\n
      delivery.cancel()\n
    else:\n
      # Update the software release if it has been changed before allocation\n
      sale_order_line = delivery.contentValues(portal_type=sale_order_line_portal_type)[0]\n
      if sale_order_line.getAggregateValue(portal_type=\'Software Release\').getUrlString() != instance.getRootSoftwareReleaseUrl():\n
        software_release = portal.portal_catalog.getResultValue(portal_type=\'Software Release\',\n
          url_string=instance.getRootSoftwareReleaseUrl())\n
        aggregate_list = [software_release.getRelativeUrl(), \n
                          instance.getRelativeUrl(),\n
                          hosting_subscription.getRelativeUrl()]\n
        sale_order_line.edit(aggregate_list=aggregate_list)\n
else:\n
  assert delivery is not None\n
\n
  if (delivery.getPortalType() == "Sale Order" and delivery.getSimulationState() == "confirmed"):\n
    if (state == stopped):\n
      # Try to find the setup packing list created from the open order to\n
      # associate it instead of the sale order\n
      packing_list = delivery.getCausalityRelatedValue(portal_type="Sale Packing List")\n
      if (packing_list  is not None):\n
        instance.edit(causality_value=packing_list)\n
    else:\n
      # Create a new packing list from scratch\n
      if (state == started):\n
        service_relative_url = hosting_service_relative_url\n
        service = hosting_service\n
      elif (state == destroyed):\n
        service_relative_url = cleanup_service_relative_url\n
        service = cleanup_service\n
\n
      packing_list = context.Instance_createSalePackingList(state_change, service)\n
      instance.edit(causality_value=packing_list)\n
\n
      if (state == started):\n
        packing_list.confirm()\n
      elif (state == destroyed):\n
        packing_list.confirm()\n
      packing_list.startBuilding()\n
\n
  elif delivery.getPortalType() == "Sale Packing List":\n
    line_list = delivery.contentValues(portal_type="Sale Packing List Line")\n
    assert len(line_list) == 1\n
    line = line_list[0]\n
    service_relative_url = line.getResource()\n
\n
    def letsclone(service_relative_url):\n
      new_delivery = delivery.Base_createCloneDocument(batch_mode=1)\n
      new_delivery.edit(start_date=DateTime(), stop_date=DateTime(), causality_value=None)\n
      new_delivery.contentValues(portal_type="Sale Packing List Line")[0].edit(resource=service_relative_url)\n
      return new_delivery\n
\n
    new_delivery = None\n
    if (service_relative_url == setup_service_relative_url):\n
      if (state == started):\n
        new_delivery = letsclone(hosting_service_relative_url)\n
        new_delivery.confirm()\n
        instance.edit(causality_value=new_delivery)\n
      elif (state == stopped):\n
        pass\n
      elif (state == destroyed):\n
        new_delivery = letsclone(cleanup_service_relative_url)\n
        new_delivery.confirm()\n
        instance.edit(causality_value=new_delivery)\n
      else:\n
        raise NotImplementedError\n
\n
    elif (service_relative_url == hosting_service_relative_url):\n
      if (state == started):\n
        if (delivery.getSimulationState() == \'stopped\'):\n
          delivery.deliver()\n
          new_delivery = letsclone(hosting_service_relative_url)\n
          new_delivery.confirm()\n
          instance.edit(causality_value=new_delivery)\n
      elif (state == stopped):\n
        if isTransitionPossible(delivery, "stop"):\n
          delivery.stop()\n
      elif (state == destroyed):\n
        if isTransitionPossible(delivery, "stop"):\n
          delivery.stop()\n
        if isTransitionPossible(delivery, "deliver"):\n
          delivery.deliver()\n
        new_delivery = letsclone(cleanup_service_relative_url)\n
        new_delivery.confirm()\n
        instance.edit(causality_value=new_delivery)\n
      else:\n
        raise NotImplementedError\n
    elif (service_relative_url == cleanup_service_relative_url):\n
      raise NotImplementedError, "Not update should be allowed"\n
    else:\n
      raise NotImplementedError\n
\n
    if new_delivery is not None:\n
      new_delivery.startBuilding()\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>Instance_changePromiseState</string> </value>
        </item>
      </dictionary>
    </pickle>
  </record>
</ZopeData>
