From e6232b04c4498867241e3d498bf6e1a67cf34852 Mon Sep 17 00:00:00 2001
From: Arnaud Fontaine <arnaud.fontaine@nexedi.com>
Date: Tue, 6 Mar 2012 19:53:10 +0900
Subject: [PATCH] When migrating Components from filesystem to ZODB, allow to
 specify version per Component.

---
 product/ERP5/Document/BusinessTemplate.py     | 70 ++++++++-----------
 ...mplate_migrateSourceCodeFromFilesystem.xml | 16 ++++-
 ...wMigrateSourceCodeFromFilesystemDialog.xml |  5 +-
 .../listbox.xml                               | 13 +++-
 ...rity.xml => listbox_version_item_list.xml} | 16 ++---
 .../ERP5/bootstrap/erp5_core/bt/change_log    |  3 +
 product/ERP5/bootstrap/erp5_core/bt/revision  |  2 +-
 7 files changed, 66 insertions(+), 59 deletions(-)
 rename product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/BusinessTemplate_viewMigrateSourceCodeFromFilesystemDialog/{my_version_priority.xml => listbox_version_item_list.xml} (92%)

diff --git a/product/ERP5/Document/BusinessTemplate.py b/product/ERP5/Document/BusinessTemplate.py
index 3b7479b69f..d89ab582aa 100644
--- a/product/ERP5/Document/BusinessTemplate.py
+++ b/product/ERP5/Document/BusinessTemplate.py
@@ -5904,58 +5904,44 @@ Business Template is a set of definitions, such as skins, portal types and categ
     security.declareProtected(Permissions.ManagePortal,
                               'migrateSourceCodeFromFilesystem')
     def migrateSourceCodeFromFilesystem(self,
-                                        version_priority,
+                                        component_portal_type_dict,
                                         erase_existing=False,
                                         **kw):
+      if not component_portal_type_dict:
+        return {}
+
       component_tool = self.getPortalObject().portal_components
       failed_import_dict = {}
-
-      # XXX-arnau: dirty because of copy/paste but will be refactor
-      # later with Products anyway...
-      from Products.ERP5Type.Core.DocumentComponent import DocumentComponent
-      document_id_list = self.getTemplateDocumentIdList()
-      for i, reference in enumerate(document_id_list):
-        try:
-          obj = DocumentComponent.importFromFilesystem(component_tool,
+      def migrate(component_dict, component_class, template_id_list_method):
+        migrated_id_list = []
+        for reference, version in component_dict.iteritems():
+          try:
+            obj = component_class.importFromFilesystem(component_tool,
                                                        reference,
-                                                       version_priority,
+                                                       version,
                                                        erase_existing)
-        except Exception, e:
-          failed_import_dict[reference] = str(e)
-        else:
-          document_id_list[i] = obj.getId()
-
-      self.setTemplateDocumentIdList(document_id_list)
+          except Exception, e:
+            failed_import_dict[reference] = str(e)
+          else:
+            migrated_id_list.append(obj.getId())
 
-      from Products.ERP5Type.Core.ExtensionComponent import ExtensionComponent
-      extension_id_list = self.getTemplateExtensionIdList()
-      for i, reference in enumerate(extension_id_list):
-        try:
-          obj = ExtensionComponent.importFromFilesystem(component_tool,
-                                                        reference,
-                                                        version_priority,
-                                                        erase_existing)
-        except Exception, e:
-          failed_import_dict[reference] = str(e)
-        else:
-          extension_id_list[i] = obj.getId()
+        if migrated_id_list:
+          template_id_list_method(migrated_id_list)
 
-      self.setTemplateExtensionIdList(extension_id_list)
+      component_dict = component_portal_type_dict.get('Document Component')
+      if component_dict:
+        from Products.ERP5Type.Core.DocumentComponent import DocumentComponent
+        migrate(component_dict, DocumentComponent, self.setTemplateDocumentIdList)
 
-      from Products.ERP5Type.Core.TestComponent import TestComponent
-      test_id_list = self.getTemplateTestIdList()
-      for i, reference in enumerate(test_id_list):
-        try:
-          obj = TestComponent.importFromFilesystem(component_tool,
-                                                   reference,
-                                                   version_priority,
-                                                   erase_existing)
-        except Exception, e:
-          failed_import_dict[reference] = str(e)
-        else:
-          test_id_list[i] = obj.getId()
+      component_dict = component_portal_type_dict.get('Extension Component')
+      if component_dict:
+        from Products.ERP5Type.Core.ExtensionComponent import ExtensionComponent
+        migrate(component_dict, ExtensionComponent, self.setTemplateExtensionIdList)
 
-      self.setTemplateTestIdList(test_id_list)
+      component_dict = component_portal_type_dict.get('Test Component')
+      if component_dict:
+        from Products.ERP5Type.Core.TestComponent import TestComponent
+        migrate(component_dict, TestComponent, self.setTemplateTestIdList)
 
       return failed_import_dict
 
diff --git a/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/BusinessTemplate_migrateSourceCodeFromFilesystem.xml b/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/BusinessTemplate_migrateSourceCodeFromFilesystem.xml
index a74cef3d1b..384081248e 100644
--- a/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/BusinessTemplate_migrateSourceCodeFromFilesystem.xml
+++ b/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/BusinessTemplate_migrateSourceCodeFromFilesystem.xml
@@ -50,7 +50,19 @@
         </item>
         <item>
             <key> <string>_body</string> </key>
-            <value> <string>failed_import_dict = context.migrateSourceCodeFromFilesystem(version_priority, erase_existing, **kw)\n
+            <value> <string>request = context.REQUEST\n
+object_list = context.portal_selections.getSelectionValueList(selection_name=request[\'listbox_list_selection_name\'],\n
+                                                              context=context,\n
+                                                              REQUEST=request)\n
+\n
+listbox_dict = request[\'listbox\']\n
+\n
+component_dict = {}\n
+for object in object_list:\n
+  component_dict.setdefault(object.destination_portal_type,\n
+                            {})[object.getUid()] = listbox_dict[object.getUrl()][\'version_item_list\']\n
+\n
+failed_import_dict = context.migrateSourceCodeFromFilesystem(component_dict, erase_existing, **kw)\n
 \n
 if failed_import_dict:\n
  failed_import_formatted_list = []\n
@@ -67,7 +79,7 @@ return context.Base_redirect(\'view\',\n
         </item>
         <item>
             <key> <string>_params</string> </key>
-            <value> <string>version_priority, erase_existing=False, **kw</string> </value>
+            <value> <string>erase_existing=False, **kw</string> </value>
         </item>
         <item>
             <key> <string>id</string> </key>
diff --git a/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/BusinessTemplate_viewMigrateSourceCodeFromFilesystemDialog.xml b/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/BusinessTemplate_viewMigrateSourceCodeFromFilesystemDialog.xml
index c090a6bcef..a31d5444e4 100644
--- a/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/BusinessTemplate_viewMigrateSourceCodeFromFilesystemDialog.xml
+++ b/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/BusinessTemplate_viewMigrateSourceCodeFromFilesystemDialog.xml
@@ -88,14 +88,15 @@
                 <item>
                     <key> <string>hidden</string> </key>
                     <value>
-                      <list/>
+                      <list>
+                        <string>listbox_version_item_list</string>
+                      </list>
                     </value>
                 </item>
                 <item>
                     <key> <string>left</string> </key>
                     <value>
                       <list>
-                        <string>my_version_priority</string>
                         <string>my_erase_existing</string>
                       </list>
                     </value>
diff --git a/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/BusinessTemplate_viewMigrateSourceCodeFromFilesystemDialog/listbox.xml b/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/BusinessTemplate_viewMigrateSourceCodeFromFilesystemDialog/listbox.xml
index b7f2ca7719..3a1eef4976 100644
--- a/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/BusinessTemplate_viewMigrateSourceCodeFromFilesystemDialog/listbox.xml
+++ b/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/BusinessTemplate_viewMigrateSourceCodeFromFilesystemDialog/listbox.xml
@@ -409,6 +409,10 @@
                           <string>destination_portal_type</string>
                           <string>Destination Portal Type</string>
                         </tuple>
+                        <tuple>
+                          <string>version_item_list</string>
+                          <string>Version</string>
+                        </tuple>
                       </list>
                     </value>
                 </item>
@@ -457,7 +461,12 @@
                 <item>
                     <key> <string>editable_columns</string> </key>
                     <value>
-                      <list/>
+                      <list>
+                        <tuple>
+                          <string>version_item_list</string>
+                          <string>Version</string>
+                        </tuple>
+                      </list>
                     </value>
                 </item>
                 <item>
@@ -550,7 +559,7 @@
                 </item>
                 <item>
                     <key> <string>selection_name</string> </key>
-                    <value> <string>business_template_migrate_all_component_from_filesystem_view_component_list_selection</string> </value>
+                    <value> <string>business_template_migrate_source_code_from_filesystem_view_component_list_selection</string> </value>
                 </item>
                 <item>
                     <key> <string>sort</string> </key>
diff --git a/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/BusinessTemplate_viewMigrateSourceCodeFromFilesystemDialog/my_version_priority.xml b/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/BusinessTemplate_viewMigrateSourceCodeFromFilesystemDialog/listbox_version_item_list.xml
similarity index 92%
rename from product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/BusinessTemplate_viewMigrateSourceCodeFromFilesystemDialog/my_version_priority.xml
rename to product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/BusinessTemplate_viewMigrateSourceCodeFromFilesystemDialog/listbox_version_item_list.xml
index 2bbb5c46b6..1d7bfc734d 100644
--- a/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/BusinessTemplate_viewMigrateSourceCodeFromFilesystemDialog/my_version_priority.xml
+++ b/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/BusinessTemplate_viewMigrateSourceCodeFromFilesystemDialog/listbox_version_item_list.xml
@@ -10,15 +10,15 @@
             <key> <string>delegated_list</string> </key>
             <value>
               <list>
-                <string>first_item</string>
                 <string>items</string>
+                <string>size</string>
                 <string>title</string>
               </list>
             </value>
         </item>
         <item>
             <key> <string>id</string> </key>
-            <value> <string>my_version_priority</string> </value>
+            <value> <string>listbox_version_item_list</string> </value>
         </item>
         <item>
             <key> <string>message_values</string> </key>
@@ -72,10 +72,6 @@
                     <key> <string>target</string> </key>
                     <value> <string></string> </value>
                 </item>
-                <item>
-                    <key> <string>title</string> </key>
-                    <value> <string></string> </value>
-                </item>
               </dictionary>
             </value>
         </item>
@@ -87,10 +83,6 @@
                     <key> <string>field_id</string> </key>
                     <value> <string>my_list_field</string> </value>
                 </item>
-                <item>
-                    <key> <string>first_item</string> </key>
-                    <value> <int>1</int> </value>
-                </item>
                 <item>
                     <key> <string>form_id</string> </key>
                     <value> <string>Base_viewFieldLibrary</string> </value>
@@ -101,6 +93,10 @@
                       <list/>
                     </value>
                 </item>
+                <item>
+                    <key> <string>size</string> </key>
+                    <value> <int>1</int> </value>
+                </item>
                 <item>
                     <key> <string>target</string> </key>
                     <value> <string>Click to edit the target</string> </value>
diff --git a/product/ERP5/bootstrap/erp5_core/bt/change_log b/product/ERP5/bootstrap/erp5_core/bt/change_log
index b080e911e7..28c8893fd5 100644
--- a/product/ERP5/bootstrap/erp5_core/bt/change_log
+++ b/product/ERP5/bootstrap/erp5_core/bt/change_log
@@ -1,3 +1,6 @@
+2012-03-06 arnaud.fontaine
+* Allow to specify version per Components in migration view.
+
 2012-03-06 arnaud.fontaine
 * Show Component versions defined on the BusinessTemplate in ZODB Migration view.
 
diff --git a/product/ERP5/bootstrap/erp5_core/bt/revision b/product/ERP5/bootstrap/erp5_core/bt/revision
index b58aa69e06..f8c416ccb8 100644
--- a/product/ERP5/bootstrap/erp5_core/bt/revision
+++ b/product/ERP5/bootstrap/erp5_core/bt/revision
@@ -1 +1 @@
-41028
\ No newline at end of file
+41029
\ No newline at end of file
-- 
2.30.9