From d7cdaeda10e238a5d8be346faf9e715c3475240f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C5=81ukasz=20Nowak?= <luke@nexedi.com>
Date: Wed, 7 Mar 2012 10:51:24 +0100
Subject: [PATCH] Update Open Order simulation in alarm.

Also minimise activity hurricane by calling expand directly.

Open Orders are searched using indexation timestamp, which allows to see them
in "windows", and does not repeat expand if not needed.

Generate activities and allow to pass the tag. Use search and activate
everywhere which will allow to walk through objects without killing the cluster
even in case of really big documents.

Avoid calling isDivergent which can take few minutes to finish.

causality_state comes from well designed causality workflow, which informs
enough about delivery state. Fetching causality_state property is extremely
fast.
---
 .../transitions/deliver_action.xml            |  2 +-
 .../update_open_order_simulation.xml          | 97 +++++++++++++++++++
 .../Alarm_updateOpenOrderSimulation.xml       | 96 ++++++++++++++++++
 .../OpenOrderLine_updateSimulation.xml        | 77 +++++++++++++++
 ...eOrderModule_viewOpenPurchaseOrderList.xml | 21 ++++
 bt5/erp5_open_trade/bt/template_path_list     |  1 +
 6 files changed, 293 insertions(+), 1 deletion(-)
 create mode 100644 bt5/erp5_open_trade/PathTemplateItem/portal_alarms/update_open_order_simulation.xml
 create mode 100644 bt5/erp5_open_trade/SkinTemplateItem/portal_skins/erp5_open_trade/Alarm_updateOpenOrderSimulation.xml
 create mode 100644 bt5/erp5_open_trade/SkinTemplateItem/portal_skins/erp5_open_trade/OpenOrderLine_updateSimulation.xml
 create mode 100644 bt5/erp5_open_trade/bt/template_path_list

diff --git a/bt5/erp5_accounting/WorkflowTemplateItem/portal_workflow/accounting_workflow/transitions/deliver_action.xml b/bt5/erp5_accounting/WorkflowTemplateItem/portal_workflow/accounting_workflow/transitions/deliver_action.xml
index 8313db6a64..1a3fa7513b 100644
--- a/bt5/erp5_accounting/WorkflowTemplateItem/portal_workflow/accounting_workflow/transitions/deliver_action.xml
+++ b/bt5/erp5_accounting/WorkflowTemplateItem/portal_workflow/accounting_workflow/transitions/deliver_action.xml
@@ -86,7 +86,7 @@
       <dictionary>
         <item>
             <key> <string>text</string> </key>
-            <value> <string>python: here.getProperty(\'causality_state\', \'default\') != \'building\' and not here.isDivergent()</string> </value>
+            <value> <string>python: here.getProperty(\'causality_state\', \'default\') == \'solved\'</string> </value>
         </item>
       </dictionary>
     </pickle>
diff --git a/bt5/erp5_open_trade/PathTemplateItem/portal_alarms/update_open_order_simulation.xml b/bt5/erp5_open_trade/PathTemplateItem/portal_alarms/update_open_order_simulation.xml
new file mode 100644
index 0000000000..5175dcce29
--- /dev/null
+++ b/bt5/erp5_open_trade/PathTemplateItem/portal_alarms/update_open_order_simulation.xml
@@ -0,0 +1,97 @@
+<?xml version="1.0"?>
+<ZopeData>
+  <record id="1" aka="AAAAAAAAAAE=">
+    <pickle>
+      <global name="Alarm" module="erp5.portal_type"/>
+    </pickle>
+    <pickle>
+      <dictionary>
+        <item>
+            <key> <string>active_sense_method_id</string> </key>
+            <value> <string>Alarm_updateOpenOrderSimulation</string> </value>
+        </item>
+        <item>
+            <key> <string>description</string> </key>
+            <value>
+              <none/>
+            </value>
+        </item>
+        <item>
+            <key> <string>enabled</string> </key>
+            <value> <int>1</int> </value>
+        </item>
+        <item>
+            <key> <string>id</string> </key>
+            <value> <string>update_open_order_simulation</string> </value>
+        </item>
+        <item>
+            <key> <string>periodicity_day_frequency</string> </key>
+            <value>
+              <none/>
+            </value>
+        </item>
+        <item>
+            <key> <string>periodicity_hour</string> </key>
+            <value>
+              <tuple/>
+            </value>
+        </item>
+        <item>
+            <key> <string>periodicity_minute</string> </key>
+            <value>
+              <tuple/>
+            </value>
+        </item>
+        <item>
+            <key> <string>periodicity_minute_frequency</string> </key>
+            <value> <int>1</int> </value>
+        </item>
+        <item>
+            <key> <string>periodicity_month</string> </key>
+            <value>
+              <tuple/>
+            </value>
+        </item>
+        <item>
+            <key> <string>periodicity_month_day</string> </key>
+            <value>
+              <tuple/>
+            </value>
+        </item>
+        <item>
+            <key> <string>periodicity_start_date</string> </key>
+            <value>
+              <object>
+                <klass>
+                  <global name="DateTime" module="DateTime.DateTime"/>
+                </klass>
+                <tuple>
+                  <none/>
+                </tuple>
+                <state>
+                  <tuple>
+                    <float>0.0</float>
+                    <string>GMT</string>
+                  </tuple>
+                </state>
+              </object>
+            </value>
+        </item>
+        <item>
+            <key> <string>periodicity_week</string> </key>
+            <value>
+              <tuple/>
+            </value>
+        </item>
+        <item>
+            <key> <string>portal_type</string> </key>
+            <value> <string>Alarm</string> </value>
+        </item>
+        <item>
+            <key> <string>title</string> </key>
+            <value> <string>Updates Open Orders Simulation</string> </value>
+        </item>
+      </dictionary>
+    </pickle>
+  </record>
+</ZopeData>
diff --git a/bt5/erp5_open_trade/SkinTemplateItem/portal_skins/erp5_open_trade/Alarm_updateOpenOrderSimulation.xml b/bt5/erp5_open_trade/SkinTemplateItem/portal_skins/erp5_open_trade/Alarm_updateOpenOrderSimulation.xml
new file mode 100644
index 0000000000..b783b10b32
--- /dev/null
+++ b/bt5/erp5_open_trade/SkinTemplateItem/portal_skins/erp5_open_trade/Alarm_updateOpenOrderSimulation.xml
@@ -0,0 +1,96 @@
+<?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[
+
+kw = {}\n
+if params is None:\n
+  params = {}\n
+\n
+last_active_process = context.getLastActiveProcess()\n
+if not params.get(\'full\', False) and last_active_process is not None:\n
+  # fetch only objects modified since last alarm run\n
+  kw[\'indexation_timestamp\'] = \'>= %s\' % last_active_process.getStartDate().ISO()\n
+\n
+# register active process in order to have "windows" of last indexed objects\n
+context.newActiveProcess().getRelativeUrl()\n
+\n
+portal = context.getPortalObject()\n
+\n
+kw[\'portal_type\'] = portal.getPortalOpenOrderTypeList()\n
+\n
+portal.portal_catalog.searchAndActivate(\n
+  method_id=\'OpenOrder_updateSimulation\',\n
+#  method_kw={\'tag\': tag}, # XXX: Post merge compatibility, maybe OpenOrder_updateSimulation shall be reconfigured to being able to expand without activity (immediate expand)\n
+  packet_size=1, # As OpenOrder_updateSimulation can generate big transaction separate the calls\n
+  activate_kw={\'tag\':tag},\n
+  **kw # XXX: In one query put parents of last modified lines\n
+  )\n
+\n
+# make alarm run once at time\n
+context.activate(after_tag=tag).getId()\n
+
+
+]]></string> </value>
+        </item>
+        <item>
+            <key> <string>_params</string> </key>
+            <value> <string>tag, fixit, params</string> </value>
+        </item>
+        <item>
+            <key> <string>id</string> </key>
+            <value> <string>Alarm_updateOpenOrderSimulation</string> </value>
+        </item>
+      </dictionary>
+    </pickle>
+  </record>
+</ZopeData>
diff --git a/bt5/erp5_open_trade/SkinTemplateItem/portal_skins/erp5_open_trade/OpenOrderLine_updateSimulation.xml b/bt5/erp5_open_trade/SkinTemplateItem/portal_skins/erp5_open_trade/OpenOrderLine_updateSimulation.xml
new file mode 100644
index 0000000000..6ab191cd15
--- /dev/null
+++ b/bt5/erp5_open_trade/SkinTemplateItem/portal_skins/erp5_open_trade/OpenOrderLine_updateSimulation.xml
@@ -0,0 +1,77 @@
+<?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>activate_kw={}\n
+if tag is not None:\n
+  activate_kw={\'tag\': tag}\n
+for path in [context] + context.getCellValueList():\n
+  for item in path.getAggregateValueList():\n
+    if item.providesIExpandableItem():\n
+      applied_rule = item.getCausalityRelatedValue(portal_type=\'Applied Rule\')\n
+      if applied_rule is not None:\n
+        applied_rule.expand(activate_kw=activate_kw)\n
+      else:\n
+        item.Delivery_updateAppliedRule(activate_kw=activate_kw)\n
+</string> </value>
+        </item>
+        <item>
+            <key> <string>_params</string> </key>
+            <value> <string>tag=None</string> </value>
+        </item>
+        <item>
+            <key> <string>id</string> </key>
+            <value> <string>OpenOrderLine_updateSimulation</string> </value>
+        </item>
+      </dictionary>
+    </pickle>
+  </record>
+</ZopeData>
diff --git a/bt5/erp5_open_trade/SkinTemplateItem/portal_skins/erp5_open_trade/OpenPurchaseOrderModule_viewOpenPurchaseOrderList.xml b/bt5/erp5_open_trade/SkinTemplateItem/portal_skins/erp5_open_trade/OpenPurchaseOrderModule_viewOpenPurchaseOrderList.xml
index 5292d7ff1d..1b4a97da1a 100644
--- a/bt5/erp5_open_trade/SkinTemplateItem/portal_skins/erp5_open_trade/OpenPurchaseOrderModule_viewOpenPurchaseOrderList.xml
+++ b/bt5/erp5_open_trade/SkinTemplateItem/portal_skins/erp5_open_trade/OpenPurchaseOrderModule_viewOpenPurchaseOrderList.xml
@@ -6,6 +6,27 @@
     </pickle>
     <pickle>
       <dictionary>
+        <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/>
+                        </value>
+                    </item>
+                  </dictionary>
+                </state>
+              </object>
+            </value>
+        </item>
         <item>
             <key> <string>_objects</string> </key>
             <value>
diff --git a/bt5/erp5_open_trade/bt/template_path_list b/bt5/erp5_open_trade/bt/template_path_list
new file mode 100644
index 0000000000..4badec03c1
--- /dev/null
+++ b/bt5/erp5_open_trade/bt/template_path_list
@@ -0,0 +1 @@
+portal_alarms/update_open_order_simulation
\ No newline at end of file
-- 
2.30.9