<?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[

from DateTime import DateTime\n
person = state_change[\'object\']\n
portal = person.getPortalObject()\n
# Get required arguments\n
kwargs = state_change.kwargs\n
software_release_url_string = state_change.kwargs[\'software_release\']\n
software_title = kwargs["software_title"]\n
software_type = kwargs.get("software_type") or "RootSoftwareInstance"\n
instance_xml = kwargs["instance_xml"]\n
sla_xml = kwargs.get("sla_xml") or ""\n
is_slave = kwargs.get("shared", False)\n
state = kwargs.get(\'state\') or \'started\'\n
\n
sale_order_portal_type = "Sale Order"\n
sale_order_line_portal_type = "Sale Order Line"\n
hosting_subscription_portal_type = "Hosting Subscription"\n
\n
if is_slave == True:\n
  software_instance_portal_type = "Slave Instance"\n
else:\n
  software_instance_portal_type = "Software Instance"\n
\n
tag = "%s_%s_inProgress" % (person.getUid(), \n
                               software_title)\n
\n
# Check if it already exists\n
cleanup_resource = portal.portal_preferences.getPreferredInstanceCleanupResource()\n
request_software_instance = None\n
for si in portal.portal_catalog(\n
  portal_type=software_instance_portal_type,\n
  title=software_title,\n
  ):\n
  try:\n
    cleanup_delivery_line = si.Item_getInstancePackingListLine(cleanup_resource)\n
  except ValueError:\n
    request_software_instance = si\n
    break\n
  else:\n
    if cleanup_delivery_line.getSimulationState() != \'delivered\':\n
      request_software_instance = si\n
      break\n
\n
if (request_software_instance is None):\n
  hosting_subscription_reference = "HOSTSUBS-%s" % context.getPortalObject().portal_ids\\\n
      .generateNewId(id_group=\'slap_hosting_subscription_reference\', id_generator=\'uid\')\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
  else:\n
\n
    setup_service_relative_url = portal.portal_preferences.getPreferredInstanceSetupResource()\n
    \n
    software_instance = portal.getDefaultModule(portal_type=software_instance_portal_type).newContent(\n
            portal_type=software_instance_portal_type,\n
            source_reference=software_type,\n
            title=software_title,\n
            text_content=instance_xml,\n
            sla_xml=sla_xml,\n
            activate_kw={\'tag\': tag},\n
            **portal.Base_getNewSoftwareInstanceCoordinate()\n
    )\n
    software_instance.portal_workflow.doActionFor(software_instance, \'validate_action\')\n
    subscription = portal.getDefaultModule(portal_type=hosting_subscription_portal_type).newContent(\n
      portal_type=hosting_subscription_portal_type,\n
      reference=hosting_subscription_reference,\n
      title=software_title,\n
      activate_kw={\'tag\': tag},\n
    )\n
    if state == \'started\':\n
      software_instance.startRequested()\n
    elif state == \'stopped\':\n
      software_instance.stopRequested()    \n
\n
    software_release_document = context.portal_catalog.getResultValue(\n
      portal_type=\'Software Release\',\n
      url_string=software_release_url_string)\n
\n
    sale_order = portal.getDefaultModule(portal_type=sale_order_portal_type).newContent(\n
      portal_type=sale_order_portal_type,\n
      destination_value=person,\n
      destination_section_value=person,\n
      destination_decision_value=person,\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
      price_currency="currency_module/EUR",\n
      activate_kw={\'tag\': tag},\n
    )\n
    sale_order_line = sale_order.newContent(\n
      portal_type=sale_order_line_portal_type,\n
      resource=setup_service_relative_url,\n
      quantity=1,\n
      aggregate_value_list=[software_instance, subscription, software_release_document],\n
      activate_kw={\'tag\': tag},\n
    )\n
    portal.portal_workflow.doActionFor(sale_order, "order_action")\n
\n
    # Update Open Order\n
    # does this order have a client-specific Open Order\n
    open_order = portal.portal_catalog.getResultValue(\n
      default_destination_section_uid=person.getUid(),\n
      portal_type="Open Sale Order",\t \t \n
      validation_state="validated")\n
    if open_order is None:\n
      person_slap_interface_state = person.getSlapState()\n
      if person_slap_interface_state == \'open_order_created\':\n
        open_order_url = context.portal_workflow.getInfoFor(\n
          ob=person, name=\'open_order_url\', \n
          wf_id=\'person_slap_interface_workflow\')\n
        open_order = portal.restrictedTraverse(open_order_url)\n
      else:\n
        raise ValueError(\'Person has no open order created\')\n
  \n
    open_order.newContent(\n
      portal_type="Open Sale Order Line",\n
      title=software_title,\n
      aggregate_value=subscription,\n
      activate_kw={\'tag\': tag},\n
    )\n
\n
    # Find Computer partition\n
    # XXX Link subscript to open order\n
else:\n
  # Update existing software instance\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
  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


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