From 578174d33a7f1229ccfb11254634c2d4096f6517 Mon Sep 17 00:00:00 2001
From: Yusei Tahara <yusei@nexedi.com>
Date: Mon, 14 Jan 2008 17:52:41 +0000
Subject: [PATCH] Added scriptable key field in business template.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@18701 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5/Document/BusinessTemplate.py     | 101 ++++++++++++++++++
 .../ERP5/PropertySheet/BusinessTemplate.py    |   5 +
 product/ERP5/tests/testBusinessTemplate.py    |  30 +++++-
 3 files changed, 135 insertions(+), 1 deletion(-)

diff --git a/product/ERP5/Document/BusinessTemplate.py b/product/ERP5/Document/BusinessTemplate.py
index 866c4be3d2..113a91df5b 100644
--- a/product/ERP5/Document/BusinessTemplate.py
+++ b/product/ERP5/Document/BusinessTemplate.py
@@ -3752,6 +3752,98 @@ class CatalogTopicKeyTemplateItem(BaseTemplateItem):
       xml_data = self.generateXml(path=path)
       bta.addObject(obj=xml_data, name=path, path=None)
 
+class CatalogScriptableKeyTemplateItem(BaseTemplateItem):
+
+  def build(self, context, **kw):
+    catalog = _getCatalogValue(self)
+    if catalog is None:
+      LOG('BusinessTemplate', 0, 'no SQL catalog was available')
+      return
+    sql_catalog_scriptable_keys = list(catalog.sql_catalog_scriptable_keys)
+    key_list = []
+    for key in self._archive.keys():
+      if key in sql_catalog_scriptable_keys:
+        key_list.append(key)
+      else:
+        raise NotFound, 'Scriptable key "%r" not found in catalog' %(key,)
+    if len(key_list) > 0:
+      self._objects[self.__class__.__name__+'/'+'scriptable_key_list'] = key_list
+
+  def _importFile(self, file_name, file):
+    list = []
+    xml = parse(file)
+    key_list = xml.getElementsByTagName('key')
+    for key in key_list:
+      node = key.childNodes[0]
+      value = node.data
+      list.append(str(value))
+    self._objects[file_name[:-4]] = list
+
+  def install(self, context, trashbin, **kw):
+    catalog = _getCatalogValue(self)
+    if catalog is None:
+      LOG('BusinessTemplate', 0, 'no SQL catalog was available')
+      return
+
+    sql_catalog_scriptable_keys = list(catalog.sql_catalog_scriptable_keys)
+    if context.getTemplateFormatVersion() == 1:
+      if len(self._objects.keys()) == 0: # needed because of pop()
+        return
+      keys = []
+      for k in self._objects.values().pop(): # because of list of list
+        keys.append(k)
+    else:
+      keys = self._archive.keys()
+    update_dict = kw.get('object_to_update')
+    force = kw.get('force')
+    # XXX must a find a better way to manage scriptable key
+    if update_dict.has_key('scriptable_key_list') or force:
+      if not force:
+        if update_dict.has_key('scriptable_key_list'):
+          action = update_dict['scriptable_key_list']
+        if action == 'nothing':
+          return
+      for key in keys:
+        if key not in sql_catalog_scriptable_keys:
+          sql_catalog_scriptable_keys.append(key)
+      catalog.sql_catalog_scriptable_keys = tuple(sql_catalog_scriptable_keys)
+
+  def uninstall(self, context, **kw):
+    catalog = _getCatalogValue(self)
+    if catalog is None:
+      LOG('BusinessTemplate', 0, 'no SQL catalog was available')
+      return
+    sql_catalog_scriptable_keys = list(catalog.sql_catalog_scriptable_keys)
+    object_path = kw.get('object_path', None)
+    if object_path is not None:
+      object_keys = [object_path]
+    else:
+      object_keys = self._archive.keys()
+    for key in object_keys:
+      if key in sql_catalog_scriptable_keys:
+        sql_catalog_scriptable_keys.remove(key)
+    catalog.sql_catalog_scriptable_keys = tuple(sql_catalog_scriptable_keys)
+    BaseTemplateItem.uninstall(self, context, **kw)
+
+  # Function to generate XML Code Manually
+  def generateXml(self, path=None):
+    obj = self._objects[path]
+    xml_data = '<key_list>'
+    obj.sort()
+    for key in obj:
+      xml_data += '\n <key>%s</key>' %(key)
+    xml_data += '\n</key_list>'
+    return xml_data
+
+  def export(self, context, bta, **kw):
+    if len(self._objects.keys()) == 0:
+      return
+    path = os.path.join(bta.path, self.__class__.__name__)
+    bta.addFolder(name=path)
+    for path in self._objects.keys():
+      xml_data = self.generateXml(path=path)
+      bta.addObject(obj=xml_data, name=path, path=None)
+
 class MessageTranslationTemplateItem(BaseTemplateItem):
 
   def build(self, context, **kw):
@@ -4090,6 +4182,7 @@ Business Template is a set of definitions, such as skins, portal types and categ
       '_catalog_request_key_item',
       '_catalog_multivalue_key_item',
       '_catalog_topic_key_item',
+      '_catalog_scriptable_key_item',
     ]
 
     def __init__(self, *args, **kw):
@@ -4242,6 +4335,9 @@ Business Template is a set of definitions, such as skins, portal types and categ
       self._tool_item = \
           ToolTemplateItem(
                self.getTemplateToolIdList())
+      self._catalog_scriptable_key_item = \
+          CatalogScriptableKeyTemplateItem(
+               self.getTemplateCatalogScriptableKeyList())
 
       # Build each part
       for item_name in self._item_name_list:
@@ -4866,6 +4962,9 @@ Business Template is a set of definitions, such as skins, portal types and categ
       self._tool_item = \
           ToolTemplateItem(
                self.getTemplateToolIdList())
+      self._catalog_scriptable_key_item = \
+          CatalogScriptableKeyTemplateItem(
+               self.getTemplateCatalogScriptableKeyList())
 
       for item_name in self._item_name_list:
         getattr(self, item_name).importFile(bta)
@@ -4951,6 +5050,7 @@ Business Template is a set of definitions, such as skins, portal types and categ
         'CatalogMultivalueKey' : '_catalog_multivalue_key_item',
         'CatalogTopicKey' : '_catalog_topic_key_item',
         'Tool': '_tool_item',
+        'CatalogScriptableKey' : '_catalog_scriptable_key_item',
         }
 
       object_id = REQUEST.object_id
@@ -5006,6 +5106,7 @@ Business Template is a set of definitions, such as skins, portal types and categ
                      '_catalog_request_key_item',
                      '_catalog_multivalue_key_item',
                      '_catalog_topic_key_item',
+                     '_catalog_scriptable_key_item',
                      '_portal_type_allowed_content_type_item',
                      '_portal_type_hidden_content_type_item',
                      '_portal_type_property_sheet_item',
diff --git a/product/ERP5/PropertySheet/BusinessTemplate.py b/product/ERP5/PropertySheet/BusinessTemplate.py
index fb69f1f733..452a859651 100644
--- a/product/ERP5/PropertySheet/BusinessTemplate.py
+++ b/product/ERP5/PropertySheet/BusinessTemplate.py
@@ -155,6 +155,11 @@ class BusinessTemplate:
       'type'        : 'lines',
       'mode'        : 'w',
       'default'     : () },
+    { 'id'          : 'template_catalog_scriptable_key',
+      'description' : 'A list of ids of catalog scriptable keys used by this template',
+      'type'        : 'lines',
+      'mode'        : 'w',
+      'default'     : () },
     { 'id'          : 'template_site_property_id',
       'description' : 'A list of ids of site properties used by this template',
       'type'        : 'lines',
diff --git a/product/ERP5/tests/testBusinessTemplate.py b/product/ERP5/tests/testBusinessTemplate.py
index cd23daf051..92d0b00124 100644
--- a/product/ERP5/tests/testBusinessTemplate.py
+++ b/product/ERP5/tests/testBusinessTemplate.py
@@ -1342,6 +1342,7 @@ class TestBusinessTemplate(ERP5TypeTestCase, LogInterceptor):
     request_key = 'fake_request'
     multivalue_key = 'fake_multivalue'
     topic_key = 'fake_topic'
+    scriptable_key = 'fake_search_text | fake_script_query'
     catalog = self.getCatalogTool().getSQLCatalog()
     self.failUnless(catalog is not None)
     # result table
@@ -1400,10 +1401,18 @@ class TestBusinessTemplate(ERP5TypeTestCase, LogInterceptor):
       sql_catalog_topic_keys.sort()
       catalog.sql_catalog_topic_search_keys = tuple(sql_catalog_topic_keys)
     self.failUnless(topic_key in catalog.sql_catalog_topic_search_keys)
+    # scriptable keys
+    if scriptable_key not in catalog.sql_catalog_scriptable_keys:
+      sql_catalog_scriptable_keys = list(catalog.sql_catalog_scriptable_keys)
+      sql_catalog_scriptable_keys.append(scriptable_key)
+      sql_catalog_scriptable_keys.sort()
+      catalog.sql_catalog_scriptable_keys = tuple(sql_catalog_scriptable_keys)
+    self.failUnless(scriptable_key in catalog.sql_catalog_scriptable_keys)
 
     sequence.edit(related_key=related_key, result_key=result_key, result_table=result_table, \
                   keyword_key=keyword_key, full_text_key=full_text_key, request_key=request_key, \
-                  multivalue_key=multivalue_key, topic_key=topic_key)
+                  multivalue_key=multivalue_key, topic_key=topic_key, \
+                  scriptable_key=scriptable_key)
 
   def stepModifyCatalogConfiguration(self, sequence, **kw):
     """Modify the current configuration of the catalog.
@@ -1484,6 +1493,8 @@ class TestBusinessTemplate(ERP5TypeTestCase, LogInterceptor):
     self.failUnless(multivalue_key is not None)
     topic_key = sequence.get('topic_key', None)
     self.failUnless(topic_key is not None)
+    scriptable_key = sequence.get('scriptable_key', None)
+    self.failUnless(scriptable_key is not None)
 
     bt.edit(template_catalog_related_key_list=[related_key],
             template_catalog_result_key_list=[result_key],
@@ -1493,6 +1504,7 @@ class TestBusinessTemplate(ERP5TypeTestCase, LogInterceptor):
             template_catalog_request_key_list=[request_key],
             template_catalog_multivalue_key_list=[multivalue_key],
             template_catalog_topic_key_list=[topic_key],
+            template_catalog_scriptable_key_list=[scriptable_key],
             )
 
   def stepRemoveKeysAndTable(self, sequence=list, sequence_list=None, **kw):
@@ -1515,6 +1527,8 @@ class TestBusinessTemplate(ERP5TypeTestCase, LogInterceptor):
     self.failUnless(multivalue_key is not None)
     topic_key = sequence.get('topic_key', None)
     self.failUnless(topic_key is not None)
+    scriptable_key = sequence.get('scriptable_key', None)
+    self.failUnless(scriptable_key is not None)
 
     catalog = self.getCatalogTool().getSQLCatalog()
     self.failUnless(catalog is not None)
@@ -1566,6 +1580,12 @@ class TestBusinessTemplate(ERP5TypeTestCase, LogInterceptor):
     sql_catalog_topic_keys.sort()
     catalog.sql_catalog_topic_search_keys = tuple(sql_catalog_topic_keys)
     self.failUnless(topic_key not in catalog.sql_catalog_topic_search_keys)
+    # scriptable keys
+    sql_catalog_scriptable_keys = list(catalog.sql_catalog_scriptable_keys)
+    sql_catalog_scriptable_keys.remove(scriptable_key)
+    sql_catalog_scriptable_keys.sort()
+    catalog.sql_catalog_scriptable_keys = tuple(sql_catalog_scriptable_keys)
+    self.failUnless(scriptable_key not in catalog.sql_catalog_scriptable_keys)
 
 
   def stepCheckKeysAndTableExists(self, sequence=list, sequence_list=None, **kw):
@@ -1588,6 +1608,8 @@ class TestBusinessTemplate(ERP5TypeTestCase, LogInterceptor):
     self.failUnless(multivalue_key is not None)
     topic_key = sequence.get('topic_key', None)
     self.failUnless(topic_key is not None)
+    scriptable_key = sequence.get('scriptable_key', None)
+    self.failUnless(scriptable_key is not None)
 
     catalog = self.getCatalogTool().getSQLCatalog()
     self.failUnless(catalog is not None)
@@ -1607,6 +1629,8 @@ class TestBusinessTemplate(ERP5TypeTestCase, LogInterceptor):
     self.failUnless(multivalue_key in catalog.sql_catalog_multivalue_keys)
     # topic key
     self.failUnless(topic_key in catalog.sql_catalog_topic_search_keys)
+    # scriptable key
+    self.failUnless(scriptable_key in catalog.sql_catalog_scriptable_keys)
 
   def stepCheckKeysAndTableRemoved(self, sequence=list, sequence_list=None, **kw):
     """
@@ -1628,6 +1652,8 @@ class TestBusinessTemplate(ERP5TypeTestCase, LogInterceptor):
     self.failUnless(multivalue_key is not None)
     topic_key = sequence.get('topic_key', None)
     self.failUnless(topic_key is not None)
+    scriptable_key = sequence.get('scriptable_key', None)
+    self.failUnless(scriptable_key is not None)
 
     catalog = self.getCatalogTool().getSQLCatalog()
     self.failUnless(catalog is not None)
@@ -1647,6 +1673,8 @@ class TestBusinessTemplate(ERP5TypeTestCase, LogInterceptor):
     self.failUnless(multivalue_key not in catalog.sql_catalog_multivalue_keys)
     # topic key
     self.failUnless(topic_key not in catalog.sql_catalog_topic_search_keys)
+    # scriptable key
+    self.failUnless(scriptable_key not in catalog.sql_catalog_scriptable_keys)
 
   # Roles
   def stepCreateRole(self, sequence=None, sequence_list=None, **kw):
-- 
2.30.9