From 5475fea13160547eed94853dd6899ad904681bd4 Mon Sep 17 00:00:00 2001
From: Yoshinori Okuji <yo@nexedi.com>
Date: Tue, 12 Apr 2005 15:05:49 +0000
Subject: [PATCH] Support wildcards in Paths.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@2866 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5/Document/BusinessTemplate.py | 48 ++++++++++++++++++++++-
 1 file changed, 47 insertions(+), 1 deletion(-)

diff --git a/product/ERP5/Document/BusinessTemplate.py b/product/ERP5/Document/BusinessTemplate.py
index 0cb2ca0018..a65a5d645b 100755
--- a/product/ERP5/Document/BusinessTemplate.py
+++ b/product/ERP5/Document/BusinessTemplate.py
@@ -39,6 +39,8 @@ from Products.ERP5Type.Utils import readLocalTest, writeLocalTest, removeLocalTe
 from Products.ERP5Type.Utils import readLocalDocument, writeLocalDocument, importLocalDocument, removeLocalDocument
 from Products.ERP5Type.XMLObject import XMLObject
 import cStringIO
+import fnmatch
+import re
 from Products.ERP5Type.Cache import clearCache
 
 from zLOG import LOG
@@ -147,7 +149,51 @@ class ObjectTemplateItem(BaseTemplateItem):
     BaseTemplateItem.uninstall(self, context, **kw)
 
 
-class PathTemplateItem(ObjectTemplateItem): pass
+class PathTemplateItem(ObjectTemplateItem):
+  """
+    This class is used to store objects with wildcards supported.
+  """
+  def __init__(self, id_list, tool_id=None, **kw):
+    BaseTemplateItem.__init__(self, id_list, tool_id=tool_id, **kw)
+    id_list = self._archive.keys()
+    self._archive.clear()
+    self._path_archive = PersistentMapping()
+    for id in id_list:
+      self._path_archive[id] = None
+
+  def _resolvePath(self, folder, relative_url_list, id_list):
+    """
+      This method calls itself recursively.
+      
+      The folder is the current object which contains sub-objects.
+      The list of ids are path components. If the list is empty,
+      the current folder is valid.
+    """
+    if len(id_list) == 0:
+      return ['/'.join(relative_url_list)]
+      
+    id = id_list[0]
+    if re.search('[\*\?\[\]]', id) is None:
+      # If the id has no meta character, do not have to check all objects.
+      object = folder._getOb(id)
+      return self._resolvePath(object, relative_url_list + [id], id_list[1:])
+      
+    path_list = []
+    for object_id in fnmatch.filter(folder.objectIds(), id):
+      path_list.extend(self._resolvePath(folder._getOb(object_id), relative_url_list + [object_id], id_list[1:]))
+    return path_list
+      
+  def build(self, context, **kw):
+    BaseTemplateItem.build(self, context, **kw)
+    p = context.getPortalObject()
+    for path in self._path_archive.keys():
+      for relative_url in self._resolvePath(p, [], path.split('/')):
+        object = p.unrestrictedTraverse(relative_url)
+        #if not object.cb_isCopyable():
+        #  raise CopyError, eNotSupported % escape(relative_url)
+        object = object._getCopy(context)
+        self._archive[relative_url] = object
+        object.wl_clearLocks()
 
 
 class CategoryTemplateItem(ObjectTemplateItem):
-- 
2.30.9