From ea05549cbb6aaaa71742a65837efc6f12ee2fb27 Mon Sep 17 00:00:00 2001
From: Kazuhiko Shiozaki <kazuhiko@nexedi.com>
Date: Tue, 29 Jun 2010 13:10:28 +0000
Subject: [PATCH] cleanup and implement several APIs to get configuration
 values for target solvers.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@36691 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5/Document/SolverDecision.py       | 20 +----
 .../ERP5/Document/SolverTypeInformation.py    | 87 ++++++++++++-------
 2 files changed, 62 insertions(+), 45 deletions(-)

diff --git a/product/ERP5/Document/SolverDecision.py b/product/ERP5/Document/SolverDecision.py
index 74874545ad..df741d347d 100644
--- a/product/ERP5/Document/SolverDecision.py
+++ b/product/ERP5/Document/SolverDecision.py
@@ -94,23 +94,11 @@ class SolverDecision(ConfigurableMixin, XMLObject):
     configurable object
     (implementation)
     """
-    # XXX To be implemented through type based method and using read
-    # transaction cache
-    try:
-      solver_portal_type = self.getSolverValue().getId()
-    except AttributeError:
+    solver_type = self.getSolverValue()
+    if solver_type is None:
       return {}
-
-    solver = self.getParentValue().newContent(
-      portal_type=solver_portal_type,
-      temp_object=True,
-      delivery_list=self.getDeliveryList(),
-      causality_value=self)
-    method = solver._getTypeBasedMethod(
-      'getDefaultConfigurationPropertyDict',
-      fallback_script_id='Solver_getDefaultConfigurationPropertyDict')
-
-    return method(self)
+    else:
+      solver_type.getDefaultConfigurationPropertyDict()
 
   def getExplanationMessage(self, all=False):
     """
diff --git a/product/ERP5/Document/SolverTypeInformation.py b/product/ERP5/Document/SolverTypeInformation.py
index 7a100b0458..a29abb7cfc 100644
--- a/product/ERP5/Document/SolverTypeInformation.py
+++ b/product/ERP5/Document/SolverTypeInformation.py
@@ -27,7 +27,7 @@
 ##############################################################################
 
 from AccessControl import ClassSecurityInfo
-from Products.ERP5Type import Permissions, PropertySheet
+from Products.ERP5Type import Permissions, PropertySheet, interfaces
 from Products.ERP5Type.ERP5Type import ERP5TypeInformation
 from Products.ERP5.Document.Predicate import Predicate
 from Products.ERP5Type.Cache import getReadOnlyTransactionCache
@@ -151,45 +151,74 @@ class SolverTypeInformation(Predicate, ERP5TypeInformation):
     configurable -- a configurable document (Solver Decision
                     or Target Solver)
     """
-    # Implemented through type based method
-    # and using read transaction cache
-    if configurable.getPortalType() == 'Solver Decision':
-      try:
-        solver_portal_type = configurable.getSolverValue().getId()
-      except AttributeError:
-        return {}
-    else:
-      solver_portal_type = configurable.getPortalType()
+    return self._callTypeBasetMethod(
+      self, 'getDefaultConfigurationPropertyDict')
 
-    cache = getReadOnlyTransactionCache(self)
-    if cache is not None:
-      key = ('getDefaultConfigurationPropertyDict', solver_portal_type,
-             configurable.getRelativeUrl())
-      try:
-        method = cache[key]
-      except KeyError:
-        method = self._getTypeBasedMethod(
-          'getDefaultConfigurationPropertyDict',
-          fallback_script_id='Solver_getDefaultConfigurationPropertyDict')
-        cache[key] = method
-    return method(configurable)
-
-  def getDefaultConfigurationPropertyList(self, id, configurable):
+  def getDefaultConfigurationProperty(self, property, configurable):
     """
-    Returns a list of possible values for a given property
+    Returns the default value for a given property
     (public API)
 
     configurable -- a configurable document (Solver Decision
                     or Target Solver)
+
+    TODO: XXX-JPS unify with IConfigurable
     """
+    return self.getDefaultConfigurationPropertyDict().get(property, None)
 
-  def getDefaultConfigurationProperty(self, id, configurable):
+  def getDefaultConfigurationPropertyListDict(self, configurable):
     """
-    Returns the default value for a given property
-    (public API)
+    Returns a dictionary of possible values for specified
+    configurable object
+    (implementation)
 
     configurable -- a configurable document (Solver Decision
                     or Target Solver)
+    """
+    return self._callTypeBasetMethod(
+      self, 'getDefaultConfigurationPropertyListDict')
 
-    TODO: XXX-JPS unify with IConfigurable
+  def getDefaultConfigurationPropertyList(self, property, configurable):
     """
+    Returns a list of possible values for a given property
+    (public API)
+
+    configurable -- a configurable document (Solver Decision
+                    or Target Solver)
+    """
+    return self.getDefaultConfigurationPropertyListDict().get(property, [])
+
+  def _callTypeBasedMethod(self, method_id, configurable):
+    # Implemented through type based method
+    # and using read transaction cache
+    portal_type = configurable.getPortalType()
+    if portal_type == 'Solver Decision':
+      try:
+        solver_portal_type = self.getSolverValue().getId()
+        solver = None
+      except AttributeError:
+        return {}
+    elif interfaces.ISolver.providedBy(configurable):
+      solver_portal_type = portal_type
+      solver = configurable
+    else:
+      raise NotImplementedError, '%s is not supported for configurable argument' % portal_type
+
+    cache = getReadOnlyTransactionCache(self)
+    if cache is not None:
+      key = (method_id, solver_portal_type,
+             configurable.getRelativeUrl())
+      try:
+        method = cache[key]
+      except KeyError:
+        if solver is None:
+          solver = self.getParentValue().newContent(
+            portal_type=solver_portal_type,
+            temp_object=True,
+            delivery_list=configurable.getDeliveryList(),
+            causality_value=configurable)
+        method = solver._getTypeBasedMethod(
+          method_id,
+          fallback_script_id='Solver_%s' % method_id)
+        cache[key] = method
+    return method()
-- 
2.30.9