From 32cd12a82c483dc739752ec443283d7adb4f7035 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Aur=C3=A9lien=20Calonne?= <aurel@nexedi.com>
Date: Wed, 27 Aug 2008 14:47:40 +0000
Subject: [PATCH] new method that returns the last length generated id by id
 tool

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@23202 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5/Tool/IdTool.py | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/product/ERP5/Tool/IdTool.py b/product/ERP5/Tool/IdTool.py
index 5aae8fa544..e77527c8fd 100644
--- a/product/ERP5/Tool/IdTool.py
+++ b/product/ERP5/Tool/IdTool.py
@@ -148,6 +148,29 @@ class IdTool(BaseTool):
       self.dict_length_ids = PersistentMapping()
     return self.dict_length_ids.items()
 
+  security.declareProtected(Permissions.AccessContentsInformation,
+                            'getLastGeneratedId')
+  def getLastLengthGeneratedId(self,id_group=None,default=None):
+    """
+    Get the last length id generated
+    """
+    # check in persistent mapping if exists
+    if getattr(self, 'dict_length_ids', None) is not None:
+      last_id = self.dict_length_ids.get(id_group)
+      if last_id is not None:
+        return last_id.value - 1
+    # otherwise check in mysql
+    portal_catalog = getToolByName(self, 'portal_catalog').getSQLCatalog()
+    query = getattr(portal_catalog, 'z_portal_ids_get_last_id', None)
+    if query is None:
+      raise AttributeError, 'Error while getting last Id: ' \
+            'z_portal_ids_get_last_id could not ' \
+            'be found.'
+    result = query(id_group=id_group)
+    if len(result):
+      return result[0]['last_id'] - 1
+    return default
+
   security.declareProtected(Permissions.AccessContentsInformation,
                             'generateNewLengthIdList')
   def generateNewLengthIdList(self, id_group=None, id_count=1, default=None,
@@ -162,7 +185,7 @@ class IdTool(BaseTool):
       "Length" is because the id is stored in a python object inspired by
       BTrees.Length. It doesn't have to be a length.
 
-      store : if we want do store the new id into the zodb, we want it
+      store : if we want to store the new id into the zodb, we want it
               by default
     """
     new_id = None
-- 
2.30.9