From 6432cda0948e54c7645d36728d60b2b1a7027746 Mon Sep 17 00:00:00 2001
From: Arnaud Fontaine <arnaud.fontaine@nexedi.com>
Date: Wed, 1 Feb 2012 12:31:45 +0900
Subject: [PATCH] When loading Component, look for matching reference in
 portal_components rather than ID.

---
 product/ERP5Type/dynamic/component_class.py | 27 ++++++++++++---------
 product/ERP5Type/dynamic/dynamic_module.py  |  6 +++--
 2 files changed, 20 insertions(+), 13 deletions(-)

diff --git a/product/ERP5Type/dynamic/component_class.py b/product/ERP5Type/dynamic/component_class.py
index e61d60e082..5b2261c0c0 100644
--- a/product/ERP5Type/dynamic/component_class.py
+++ b/product/ERP5Type/dynamic/component_class.py
@@ -57,26 +57,31 @@ class ComponentModule(ModuleType):
 from types import ModuleType
 from zLOG import LOG, INFO
 
-def generateComponentClassWrapper(namespace):
+def generateComponentClassWrapper(namespace, portal_type):
   def generateComponentClass(component_name):
     site = getSite()
 
+    # XXX-arnau: erp5.component.extension.VERSION.REFERENCE perhaps but there
+    # should be a a way to specify priorities such as portal_skins maybe?
     component_id = '%s.%s' % (namespace, component_name)
     try:
-      component = getattr(site.portal_components, component_id)
-    except AttributeError:
+      # XXX-arnau: Performances?
+      component = site.portal_catalog.unrestrictedSearchResults(
+        parent_uid=site.portal_components.getUid(),
+        reference=component_name,
+        validation_state=('validated', 'modified'),
+        portal_type=portal_type)[0].getObject()
+    except IndexError:
       LOG("ERP5Type.dynamic", INFO,
           "Could not find %s, perhaps it has not been migrated yet?" % \
             component_id)
 
-      raise
+      raise AttributeError("Component %s not found or not validated" % \
+                             component_id)
     else:
-      if component.getValidationState() == 'validated':
-        new_module = ModuleType(component_id, component.getDescription())
-        component.load(new_module.__dict__, validated_only=True)
-        LOG("ERP5Type.dynamic", INFO, "Loaded successfully %s" % component_id)
-        return new_module
-      else:
-        raise AttributeError("Component %s not validated" % component_id)
+      new_module = ModuleType(component_id, component.getDescription())
+      component.load(new_module.__dict__, validated_only=True)
+      LOG("ERP5Type.dynamic", INFO, "Loaded successfully %s" % component_id)
+      return new_module
 
   return generateComponentClass
diff --git a/product/ERP5Type/dynamic/dynamic_module.py b/product/ERP5Type/dynamic/dynamic_module.py
index 9134a1666d..efd8a64263 100644
--- a/product/ERP5Type/dynamic/dynamic_module.py
+++ b/product/ERP5Type/dynamic/dynamic_module.py
@@ -129,8 +129,10 @@ def initializeDynamicModules():
 
   erp5.component.extension = registerDynamicModule(
     'erp5.component.extension',
-    generateComponentClassWrapper('erp5.component.extension'))
+    generateComponentClassWrapper('erp5.component.extension',
+                                  'Extension Component'))
 
   erp5.component.document = registerDynamicModule(
     'erp5.component.document',
-    generateComponentClassWrapper('erp5.component.document'))
+    generateComponentClassWrapper('erp5.component.document',
+                                  'Document Component'))
-- 
2.30.9