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

person = state_change[\'object\']\n
portal = person.getPortalObject()\n
# Get required arguments\n
kwargs = state_change.kwargs\n
\n
# Required args\n
# Raise TypeError if all parameters are not provided\n
try:\n
  software_release_url_string = kwargs[\'software_release\']\n
  software_title = kwargs["software_title"]\n
  software_type = kwargs["software_type"]\n
  instance_xml = kwargs["instance_xml"]\n
  sla_xml = kwargs["sla_xml"]\n
  is_slave = kwargs["shared"]\n
  root_state = kwargs["state"]\n
except KeyError:\n
  raise TypeError, "Person_requestSoftwareInstance takes exactly 7 arguments"\n
\n
if is_slave not in [True, False]:\n
  raise ValueError, "shared should be a boolean"\n
\n
hosting_subscription_portal_type = "Hosting Subscription"\n
\n
tag = "%s_%s_inProgress" % (person.getUid(), \n
                               software_title)\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
# Check if it already exists\n
request_hosting_subscription_list = portal.portal_catalog(\n
  portal_type=hosting_subscription_portal_type,\n
  title=software_title,\n
  validation_state="validated",\n
  default_destination_section=person.getRelativeUrl(),\n
  limit=2,\n
  )\n
if len(request_hosting_subscription_list) > 1:\n
  raise NotImplementedError, "Too many hosting subscription %s found" % software_title\n
elif len(request_hosting_subscription_list) == 1:\n
  request_hosting_subscription = request_hosting_subscription_list[0].getObject()\n
  if (request_hosting_subscription.getSlapState() == "destroy_requested") or \\\n
     (request_hosting_subscription.getTitle() != software_title) or \\\n
     (request_hosting_subscription.getValidationState() != "validated") or \\\n
     (request_hosting_subscription.getDestinationSection() != person.getRelativeUrl()):\n
    raise NotImplementedError, "The system was not able to get the expected hosting subscription"\n
else:\n
  if (root_state == "destroyed"):\n
    # No need to create destroyed subscription.\n
    context.REQUEST.set(\'request_hosting_subscription\', None)\n
    return\n
  hosting_subscription_reference = "HOSTSUBS-%s" % context.getPortalObject().portal_ids\\\n
      .generateNewId(id_group=\'slap_hosting_subscription_reference\', id_generator=\'uid\')\n
  request_hosting_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
    destination_section=person.getRelativeUrl(),\n
    activate_kw={\'tag\': tag},\n
  )\n
\n
promise_kw = {\n
  \'instance_xml\': instance_xml,\n
  \'software_type\': software_type,\n
  \'sla_xml\': sla_xml,\n
  \'software_release\': software_release_url_string,\n
  \'shared\': is_slave,\n
}\n
\n
context.REQUEST.set(\'request_hosting_subscription\', request_hosting_subscription)\n
# Change desired state\n
if (root_state == "started"):\n
  request_hosting_subscription.requestStart(**promise_kw)\n
elif (root_state == "stopped"):\n
  request_hosting_subscription.requestStop(**promise_kw)\n
elif (root_state == "destroyed"):\n
  request_hosting_subscription.requestDestroy(**promise_kw)\n
  context.REQUEST.set(\'request_hosting_subscription\', None)\n
else:\n
  raise ValueError, "state should be started, stopped or destroyed"\n
\n
request_hosting_subscription.requestInstance(\n
  software_release=software_release_url_string,\n
  software_title=software_title,\n
  software_type=software_type,\n
  instance_xml=instance_xml,\n
  sla_xml=sla_xml,\n
  shared=is_slave,\n
  state=root_state,\n
)\n
\n
# Change the state at the end to allow to execute updateLocalRoles only once in the transaction\n
validation_state = request_hosting_subscription.getValidationState()\n
slap_state = request_hosting_subscription.getSlapState()\n
if validation_state == \'draft\':\n
  request_hosting_subscription.portal_workflow.doActionFor(request_hosting_subscription,\n
                                           \'validate_action\')\n
if (validation_state != \'archived\') and \\\n
   (slap_state == \'destroy_requested\'):\n
  # XXX TODO do not use validation workflow to filter destroyed subscription\n
  request_hosting_subscription.archive()\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>
