From 9eb9586dc1d1a4168c4e97a6b2772692c1d9e63f Mon Sep 17 00:00:00 2001
From: Kazuhiko Shiozaki <kazuhiko@nexedi.com>
Date: Thu, 11 Mar 2010 16:59:31 +0000
Subject: [PATCH] if removed workflow is still used somewhere else, make the
 default choice 'unchecked' and displays 'Removed but used'.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@33670 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5/Document/BusinessTemplate.py     | 27 +++++-
 .../BusinessTemplate_getModifiedObject.xml    |  4 +-
 .../listbox_choice.xml                        |  2 +-
 .../TemplateTool_getModifiedObjectList.xml    |  2 +-
 .../listbox_choice.xml                        |  2 +-
 product/ERP5/bootstrap/erp5_core/bt/revision  |  2 +-
 product/ERP5/tests/testBusinessTemplate.py    | 91 +++++++++++++++++++
 7 files changed, 123 insertions(+), 7 deletions(-)

diff --git a/product/ERP5/Document/BusinessTemplate.py b/product/ERP5/Document/BusinessTemplate.py
index e675a82aff..7596588dc0 100644
--- a/product/ERP5/Document/BusinessTemplate.py
+++ b/product/ERP5/Document/BusinessTemplate.py
@@ -1602,7 +1602,7 @@ class WorkflowTemplateItem(ObjectTemplateItem):
   # So we hide modified subobjects to the user and we always reinstall
   # (or remove) everything.
 
-  def preinstall(self, context, installed_item, **kw):
+  def preinstall(self, context, installed_item, installed_bt, **kw):
     modified_object_dict = ObjectTemplateItem.preinstall(self, context,
                                                          installed_item, **kw)
     modified_workflow_dict = {}
@@ -1612,6 +1612,28 @@ class WorkflowTemplateItem(ObjectTemplateItem):
         modified_workflow_dict.setdefault('/'.join(path[:2]), ('Modified', state[1]))
       else:
         modified_workflow_dict[modified_object] = state
+    removed_workflow_id_list = [x[0].split('/', 1)[1] \
+                                for x in modified_workflow_dict.iteritems() \
+                                if x[1][0] == 'Removed']
+    if len(removed_workflow_id_list) > 0:
+      installed_chain_list = [[y.strip() for y in x.split('|')] for x in \
+                                installed_bt.getTemplatePortalTypeWorkflowChainList()]
+      new_chain_list = [[y.strip() for y in x.split('|')] for x in \
+                          context.getTemplatePortalTypeWorkflowChainList()]
+      chain_dict = getChainByType(context)[1]
+      for workflow_id in removed_workflow_id_list:
+        affected_portal_type_set = set([x[0][6:] for x in \
+                                        chain_dict.iteritems() \
+                                        if workflow_id in \
+                                        [y.strip() for y in x[1].split(',')]])
+        safe_portal_type_set = set([x[0] for x in installed_chain_list \
+                                    if x[1] == workflow_id]) - \
+                               set([x[0] for x in new_chain_list \
+                                    if x[1] == workflow_id])
+        if affected_portal_type_set - safe_portal_type_set:
+          value = modified_workflow_dict['portal_workflow/%s' % workflow_id]
+          modified_workflow_dict['portal_workflow/%s' % workflow_id] = \
+              ('Removed but used', value[1])
     return modified_workflow_dict
 
   def install(self, context, trashbin, **kw):
@@ -5079,7 +5101,8 @@ Business Template is a set of definitions, such as skins, portal types and categ
         if new_item is not None:
           if installed_item is not None and hasattr(installed_item, '_objects'):
             modified_object = new_item.preinstall(context=self, 
-                                                  installed_item=installed_item)
+                                                  installed_item=installed_item,
+                                                  installed_bt=installed_bt)
             if len(modified_object) > 0:
               modified_object_list.update(modified_object)
           else:
diff --git a/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/BusinessTemplate_getModifiedObject.xml b/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/BusinessTemplate_getModifiedObject.xml
index 953c7971b8..fe70916d07 100644
--- a/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/BusinessTemplate_getModifiedObject.xml
+++ b/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/BusinessTemplate_getModifiedObject.xml
@@ -107,11 +107,13 @@ for object_id in keys:\n
       choice_item_list=[[upgrade_title, \'install\']]\n
     else:\n
       choice_item_list=[[backup_title, \'backup\']]\n
-  elif object_state == \'Removed\':\n
+  elif object_state in (\'Removed\', \'Removed but used\'):\n
     if object_class in no_backup_dict:\n
       choice_item_list=[[remove_title, \'remove\']]\n
     else:\n
       choice_item_list=[[save_and_remove_title, \'save_and_remove\']]\n
+  else:\n
+    choice_item_list = [[install_title, \'install\']]\n
 \n
   line.edit(object_id=object_id,\n
                 object_state=object_state,\n
diff --git a/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/BusinessTemplate_viewInstallationDialog/listbox_choice.xml b/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/BusinessTemplate_viewInstallationDialog/listbox_choice.xml
index 0e3783c020..b3bf1ca640 100644
--- a/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/BusinessTemplate_viewInstallationDialog/listbox_choice.xml
+++ b/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/BusinessTemplate_viewInstallationDialog/listbox_choice.xml
@@ -259,7 +259,7 @@
       <dictionary>
         <item>
             <key> <string>_text</string> </key>
-            <value> <string>python: cell.choice_item_list and cell.choice_item_list[0][1] or []</string> </value>
+            <value> <string>python:(cell.choice_item_list and cell.object_state != \'Removed but used\') and cell.choice_item_list[0][1] or []</string> </value>
         </item>
       </dictionary>
     </pickle>
diff --git a/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/TemplateTool_getModifiedObjectList.xml b/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/TemplateTool_getModifiedObjectList.xml
index 1d066e46d5..a3692b3c72 100644
--- a/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/TemplateTool_getModifiedObjectList.xml
+++ b/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/TemplateTool_getModifiedObjectList.xml
@@ -112,7 +112,7 @@ for bt in bt_id_list:\n
         choice_item_list = [[upgrade_title, \'install\']]\n
       else:\n
         choice_item_list = [[backup_title, \'backup\']]\n
-    elif object_state == \'Removed\':\n
+    elif object_state in (\'Removed\', \'Removed but used\'):\n
       if object_class in no_backup_dict:\n
         choice_item_list = [[remove_title, \'remove\']]\n
       else:\n
diff --git a/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/TemplateTool_viewMultiInstallationDialog/listbox_choice.xml b/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/TemplateTool_viewMultiInstallationDialog/listbox_choice.xml
index 0ed79a7066..b3bf1ca640 100644
--- a/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/TemplateTool_viewMultiInstallationDialog/listbox_choice.xml
+++ b/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/TemplateTool_viewMultiInstallationDialog/listbox_choice.xml
@@ -259,7 +259,7 @@
       <dictionary>
         <item>
             <key> <string>_text</string> </key>
-            <value> <string>python: cell.choice_item_list[0][1]</string> </value>
+            <value> <string>python:(cell.choice_item_list and cell.object_state != \'Removed but used\') and cell.choice_item_list[0][1] or []</string> </value>
         </item>
       </dictionary>
     </pickle>
diff --git a/product/ERP5/bootstrap/erp5_core/bt/revision b/product/ERP5/bootstrap/erp5_core/bt/revision
index 2c87a4b00c..5303ac12b9 100644
--- a/product/ERP5/bootstrap/erp5_core/bt/revision
+++ b/product/ERP5/bootstrap/erp5_core/bt/revision
@@ -1 +1 @@
-1508
\ No newline at end of file
+1509
\ No newline at end of file
diff --git a/product/ERP5/tests/testBusinessTemplate.py b/product/ERP5/tests/testBusinessTemplate.py
index cdfa123970..472dd24058 100644
--- a/product/ERP5/tests/testBusinessTemplate.py
+++ b/product/ERP5/tests/testBusinessTemplate.py
@@ -2414,6 +2414,27 @@ class TestBusinessTemplate(ERP5TypeTestCase, LogInterceptor):
     import_bt.install(force=0, object_to_update=install_object_dict,
                       update_catalog=1)
 
+  def stepInstallWithRemoveCheckedBusinessTemplate(self, sequence=None, sequence_list=None, **kw):
+    """
+    Install importzed business template
+    """
+    import_bt = sequence.get('import_bt')
+    object_list = import_bt.preinstall()
+    install_object_dict = {}
+    for obj in object_list.keys():
+      state = object_list[obj][0]
+      if state in ('Removed', 'Removed but used'):
+        install_state = 'save_and_remove'
+      elif state == 'Modified':
+        install_state = 'backup'
+      elif state == 'New':
+        install_state = 'install'
+      else:
+        install_state = ""
+      install_object_dict[obj] = install_state
+    import_bt.install(force=0, object_to_update=install_object_dict,
+                      update_catalog=1)
+
   def stepInstallDuplicatedBusinessTemplate(self, sequence=None,
                                             sequence_list=None, **kw):
     """
@@ -2995,6 +3016,76 @@ class TestBusinessTemplate(ERP5TypeTestCase, LogInterceptor):
                        CheckInstalledInstallationState \
                        CheckBuiltBuildingState \
                        CheckSkinsLayers \
+                       CheckWorkflowExists \
+                       CheckWorkflowChainRemoved \
+                       SaveWorkflowChain \
+                       '
+    sequence_list.addSequenceString(sequence_string)
+    sequence_list.play(self, quiet=quiet)
+
+  def test_042_BusinessTemplateWithWorkflowRemoved(self, quiet=quiet, run=run_all_test):
+    if not run: return
+    if not quiet:
+      message = 'Test Business Template With Remove Of Workflow'
+      ZopeTestCase._print('\n%s ' % message)
+      LOG('Testing... ', 0, message)
+    sequence_list = SequenceList()
+    sequence_string = '\
+                       CreatePortalType \
+                       CreateWorkflow \
+                       CreateNewBusinessTemplate \
+                       UseExportBusinessTemplate \
+                       AddWorkflowToBusinessTemplate \
+                       AddWorkflowChainToBusinessTemplate \
+                       CheckModifiedBuildingState \
+                       CheckNotInstalledInstallationState \
+                       BuildBusinessTemplate \
+                       CheckBuiltBuildingState \
+                       CheckNotInstalledInstallationState \
+                       CheckObjectPropertiesInBusinessTemplate \
+                       SaveBusinessTemplate \
+                       CheckBuiltBuildingState \
+                       CheckNotInstalledInstallationState \
+                       RemoveWorkflow \
+                       RemoveBusinessTemplate \
+                       RemoveAllTrashBins \
+                       ImportBusinessTemplate \
+                       UseImportBusinessTemplate \
+                       CheckBuiltBuildingState \
+                       CheckNotInstalledInstallationState \
+                       InstallBusinessTemplate \
+                       ModifyWorkflowChain \
+                       Tic \
+                       CheckInstalledInstallationState \
+                       CheckBuiltBuildingState \
+                       CheckNoTrashBin \
+                       CheckSkinsLayers \
+                       CheckWorkflowExists \
+                       CheckWorkflowChainExists \
+                       CreateSecondBusinessTemplate \
+                       UseSecondBusinessTemplate \
+                       CheckModifiedBuildingState \
+                       CheckNotInstalledInstallationState \
+                       BuildBusinessTemplate \
+                       CheckBuiltBuildingState \
+                       CheckNotInstalledInstallationState \
+                       CheckObjectPropertiesInBusinessTemplate \
+                       SaveBusinessTemplate \
+                       CheckBuiltBuildingState \
+                       CheckNotInstalledInstallationState \
+                       RemoveBusinessTemplate \
+                       RemoveAllTrashBins \
+                       ImportBusinessTemplate \
+                       UseImportBusinessTemplate \
+                       CheckBuiltBuildingState \
+                       CheckNotInstalledInstallationState \
+                       Tic \
+                       Pdb \
+                       InstallWithRemoveCheckedBusinessTemplate \
+                       Tic \
+                       CheckInstalledInstallationState \
+                       CheckBuiltBuildingState \
+                       CheckSkinsLayers \
                        CheckWorkflowRemoved \
                        CheckWorkflowChainRemoved \
                        SaveWorkflowChain \
-- 
2.30.9