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

"""\n
  Check installed business templates one \n
  by one. If one of the business templates does not \n
  use the latest revision, then we need to install.\n
"""\n
# Initialize variables\n
portal = context.getPortalObject()\n
portal_templates = portal.portal_templates\n
signature = portal.ERP5Site_getUpgraderSignature()\n
message_list = []\n
\n
previous_bt5_id = None\n
bt5_counter = 0\n
available_bt5_list = portal_templates.getRepositoryBusinessTemplateList(newest_only=True)\n
available_bt5_id_list = [x.title for x in available_bt5_list]\n
required_bt5_id_list = signature.get(\'required_bt5_id_list\', [])\n
\n
upgradable_bt5_id_list = signature.get(\'upgradable_bt5_id_list\', [])\n
if len(upgradable_bt5_id_list) > 0:\n
  raise NotImplementedError\n
\n
reinstallable_bt5_id_list = signature.get(\'reinstallable_bt5_id_list\', signature.get(\'reinstalable_bt5_id_list\', []))\n
if len(reinstallable_bt5_id_list) > 0:\n
  raise NotImplementedError\n
\n
before_triggered_bt5_id_dict = signature.get(\'before_triggered_bt5_id_dict\', {})\n
after_triggered_bt5_id_dict = signature.get(\'after_triggered_bt5_id_dict\', {})\n
update_catalog_bt5_id_list = signature.get(\'update_catalog_bt5_id_list\', [])\n
if len(update_catalog_bt5_id_list) > 0:\n
  raise NotImplementedError\n
\n
missing_id_list = [q for q in required_bt5_id_list if q not in available_bt5_id_list]\n
if len(missing_id_list) > 0:\n
  script.log(\'Missing Business Templates: %s\' % (\', \'.join(missing_id_list),))\n
  return message_list\n
\n
bt5_id_list = required_bt5_id_list\n
\n
bt5_list = []\n
for bt5_id in bt5_id_list:\n
  available_bt5 = [q for q in available_bt5_list if q.title == bt5_id][0]\n
  bt5_list.append(portal_templates.decodeRepositoryBusinessTemplateUid(available_bt5.uid))\n
\n
installed_bt5_title_list = [o.getTitle() for o in portal_templates.getInstalledBusinessTemplateList()]\n
\n
bt5_counter = portal_templates.countFolder()[0][0]\n
\n
def installBT5(bt5_url, bt5_title, previous_bt5, bt5_counter, force=False):\n
  bt5_id = "%s_%s" % (bt5_counter,  bt5_title)\n
  kw = dict(activity="SQLQueue", tag=bt5_id)\n
  if previous_bt5 is not None:\n
    kw[\'after_tag\'] = previous_bt5\n
    # We must make sure all documents from previous installations \n
    # are already indexed (specially categories).\n
    kw[\'after_method_id\'] = "immediateReindexObject"\n
  update_catalog = bt5_title in update_catalog_bt5_id_list\n
  before_triggered_bt5_id_list = before_triggered_bt5_id_dict.get(bt5_title, ())\n
  after_triggered_bt5_id_list = after_triggered_bt5_id_dict.get(bt5_title, ())\n
  keep_original_list = signature.get(\'keep_original_dict\', {}).get(bt5_title, ())\n
  if upgrade:\n
    portal_templates.activate(**kw).updateBusinessTemplateFromUrl(\n
                    bt5_url, # id=bt5_id,  \n
                    keep_original_list=keep_original_list,\n
                    before_triggered_bt5_id_list=before_triggered_bt5_id_list,\n
                    after_triggered_bt5_id_list=after_triggered_bt5_id_list,\n
                    update_catalog=update_catalog,\n
                    reinstall=force)\n
  previous_bt5_id = bt5_id\n
  bt5_counter += 1\n
  message_list.append("%s will be installed"  % bt5_url)\n
  return bt5_id\n
\n
previous_bt5 = None\n
for repository, bt5_id in bt5_list:\n
  new_bt = [x for x in available_bt5_list \\\n
            if portal_templates.decodeRepositoryBusinessTemplateUid(x.uid) == (repository, bt5_id)][0]\n
  bt5_url = \'/\'.join((repository, bt5_id))\n
  bt5_title = new_bt.title\n
  if bt5_title in reinstallable_bt5_id_list:\n
    bt5_id = installBT5(bt5_url, bt5_title, previous_bt5, bt5_counter, force=True)\n
    previous_bt5 = bt5_id\n
  else:\n
    installed_bt = portal_templates.getInstalledBusinessTemplate(bt5_title, strict=True)\n
    if installed_bt is not None:\n
      if int(installed_bt.getRevision() or 0) >= int(new_bt.revision or 0):\n
        continue\n
    if bt5_title in required_bt5_id_list:\n
      bt5_counter += 1\n
      bt5_id = installBT5(bt5_url, bt5_title, previous_bt5, bt5_counter)\n
      previous_bt5 = bt5_id\n
    elif bt5_title in installed_bt5_title_list: # update_bt5_id_list\n
      bt5_counter += 1\n
      bt5_id = installBT5(bt5_url, bt5_title, previous_bt5, bt5_counter)\n
      previous_bt5 = bt5_id\n
\n
return message_list\n


]]></string> </value>
        </item>
        <item>
            <key> <string>_params</string> </key>
            <value> <string>upgrade=0</string> </value>
        </item>
        <item>
            <key> <string>id</string> </key>
            <value> <string>ERP5Site_upgradeBusinessTemplateList</string> </value>
        </item>
      </dictionary>
    </pickle>
  </record>
</ZopeData>
