From 09d4de5d8e829ea7bc4c5f263412edcf1a7a494f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9rome=20Perrin?= <jerome@nexedi.com>
Date: Tue, 25 Mar 2008 18:55:14 +0000
Subject: [PATCH] Delete existing subdocuments with same reference before
 copying them. Do not use copy and paste by uids, but by path and ID instead

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@20133 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 .../PaySheetTransaction_copySubObject.xml     | 41 +++++++++++++------
 bt5/erp5_payroll/bt/revision                  |  2 +-
 2 files changed, 29 insertions(+), 14 deletions(-)

diff --git a/bt5/erp5_payroll/SkinTemplateItem/portal_skins/erp5_payroll/PaySheetTransaction_copySubObject.xml b/bt5/erp5_payroll/SkinTemplateItem/portal_skins/erp5_payroll/PaySheetTransaction_copySubObject.xml
index 57afe89466..0f1a8f5427 100644
--- a/bt5/erp5_payroll/SkinTemplateItem/portal_skins/erp5_payroll/PaySheetTransaction_copySubObject.xml
+++ b/bt5/erp5_payroll/SkinTemplateItem/portal_skins/erp5_payroll/PaySheetTransaction_copySubObject.xml
@@ -65,20 +65,32 @@
         </item>
         <item>
             <key> <string>_body</string> </key>
-            <value> <string>\'\'\'\n
-  this script copy subobject of the model (corresponding to the \n
-  portal_type_list) in the paysheet\n
+            <value> <string>\'\'\'This script copies sub objects of models (corresponding to the \n
+portal_type_list) in the paysheet.\n
 \'\'\'\n
 \n
 sub_object_list = context.getInheritedObjectValueList(portal_type_list)\n
-sub_object_uid_list = [x.getUid() for x in sub_object_list]\n
 \n
-# copy model object\n
-model = context.getSpecialiseValue()\n
-copy_data = model.manage_copyObjects(uids=sub_object_uid_list)\n
+# Erase existing sub objects with same reference\n
+delete_id_list = []\n
+for sub_object in sub_object_list:\n
+  sub_object_reference = sub_object.getProperty(\'reference\', sub_object.getId())\n
+  for existing_sub_object in context.contentValues(portal_type=portal_type_list):\n
+    if sub_object_reference == existing_sub_object.getProperty(\n
+                                    \'reference\', existing_sub_object.getId()):\n
+      delete_id_list.append(existing_sub_object.getId())\n
 \n
-# paste into context (paysheet) folder\n
-context.manage_pasteObjects(copy_data)\n
+if delete_id_list:\n
+  context.manage_delObjects(ids=delete_id_list)\n
+\n
+sub_object_by_model = dict()\n
+for sub_object in sub_object_list:\n
+  sub_object_by_model.setdefault(\n
+           sub_object.getParentValue(), []).append(sub_object.getId())\n
+\n
+for model, sub_object_id_list in sub_object_by_model.items():\n
+  copy_data = model.manage_copyObjects(sub_object_id_list)\n
+  context.manage_pasteObjects(copy_data)\n
 </string> </value>
         </item>
         <item>
@@ -125,12 +137,15 @@ context.manage_pasteObjects(copy_data)\n
                             <string>_getattr_</string>
                             <string>context</string>
                             <string>sub_object_list</string>
-                            <string>append</string>
-                            <string>$append0</string>
+                            <string>delete_id_list</string>
                             <string>_getiter_</string>
-                            <string>x</string>
-                            <string>sub_object_uid_list</string>
+                            <string>sub_object</string>
+                            <string>sub_object_reference</string>
+                            <string>existing_sub_object</string>
+                            <string>dict</string>
+                            <string>sub_object_by_model</string>
                             <string>model</string>
+                            <string>sub_object_id_list</string>
                             <string>copy_data</string>
                           </tuple>
                         </value>
diff --git a/bt5/erp5_payroll/bt/revision b/bt5/erp5_payroll/bt/revision
index d903d59723..20ad60520b 100644
--- a/bt5/erp5_payroll/bt/revision
+++ b/bt5/erp5_payroll/bt/revision
@@ -1 +1 @@
-229
\ No newline at end of file
+230
\ No newline at end of file
-- 
2.30.9