composition.py 9.14 KB
Newer Older
1 2 3 4
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2010 Nexedi SA and Contributors. All Rights Reserved.
5
#                    Julien Muchembled <jm@nexedi.com>
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# guarantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
##############################################################################

from AccessControl import ClassSecurityInfo
from Acquisition import aq_base
from Products.ERP5Type import Permissions
33
from Products.ERP5Type.Cache import transactional_cached
34
from Products.ERP5Type.Utils import sortValueList
35
from Products.ERP5.Document.Predicate import Predicate
36
from Products.ERP5.Document.BusinessProcess import BusinessProcess
37 38
from Products.ZSQLCatalog.SQLCatalog import Query, ComplexQuery

39 40

@transactional_cached()
41 42 43 44 45 46 47 48 49 50
def _getEffectiveModel(self, start_date=None, stop_date=None):
  """Return the most appropriate model using effective_date, expiration_date
  and version number.
  An effective model is a model which start and stop_date are equal (or
  excluded) to the range of the given start and stop_date and with the
  higher version number (if there is more than one)

  XXX Should we moved this function to a class ? Which one ?
      What about reusing IVersionable ?
  """
51
  reference = self.getProperty('reference')
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
  if not reference:
    return self

  query_list = [Query(reference=reference),
                Query(portal_type=self.getPortalType()),
                Query(validation_state=('deleted', 'invalidated'),
                      operator='NOT')]
  if start_date is not None:
    query_list.append(ComplexQuery(Query(effective_date=None),
                                   Query(effective_date=start_date,
                                         range='ngt'),
                                   logical_operator='OR'))
  if stop_date is not None:
    query_list.append(ComplexQuery(Query(expiration_date=None),
                                   Query(expiration_date=stop_date,
                                         range='min'),
                                   logical_operator='OR'))

  # XXX What to do the catalog returns nothing (either because 'self' was just
  #     created and not yet indexed, or because it was invalidated) ?
  #     For the moment, we raise.
  model_list = self.getPortalObject().portal_catalog.unrestrictedSearchResults(
      query=ComplexQuery(logical_operator='AND', *query_list),
      sort_on=(('version', 'descending'),))
76 77 78
  if not model_list:
    raise KeyError('No %s found with the reference %s between %s and %s' % \
            (self.getPortalType(), reference, start_date, stop_date))
79 80
  return model_list[0].getObject()

81

82 83 84 85 86 87 88 89 90 91 92
# We do have clever caching here, since container_list does not contain objects
# with no subobject. Example:
#  If a SO (-> TC1, TC2) has 1 SOL (without specialise) and 1 TML,
#  "SOL.asComposedDocument()" gives:
#  1. _effective_model_list equals to
#     [SOL] + [SO, TC1, TC2] = [SOL, SO, TC1, TC2]
#  2. first call to objectValues passes container_list = [SO, TC1, TC2]
#     to  _findPredicateList (SOL being filtered out)
#  After evaluation of "SOL.asComposedDocument()" and "SO.asComposedDocument()",
#  _findPredicateList has only 1 entry in its cache.
@transactional_cached()
93
def _findPredicateList(container_list, portal_type=None):
94 95 96
  predicate_list = []
  reference_dict = {}
  for container in container_list:
97
    for ob in container.contentValues(portal_type=portal_type):
98 99 100 101 102 103 104 105
      if isinstance(ob, Predicate):
        # reference is used to hide lines on farther containers
        reference = ob.getProperty('reference')
        if reference:
          reference_set = reference_dict.setdefault(ob.getPortalType(), set())
          if reference in reference_set:
            continue
          reference_set.add(reference)
106
        predicate_list.append(ob)
107 108 109
  return predicate_list


110
class asComposedDocument(object):
111 112 113 114 115
  """Return a temporary object which is the composition of all effective models

  The returned value is a temporary copy of the given object. The list of all
  effective models (specialise values) is stored in a private attribute.
  Collecting predicates (from effective models) is done lazily. Predicates can
116
  be accessed through contentValues/objectValues.
117 118 119

  This class should be seen as a function, and it is named accordingly.
  It is out of CompositionMixin class to avoid excessive indentation.
120 121
  """

122
  def __new__(cls, orig_self, portal_type_list=None):
123
    self = orig_self.asContext(_portal_type_list=portal_type_list) # XXX-JPS orig_self -> original_self - please follow conventions
124
    base_class = self.__class__
125
    self.__class__ = type(base_class.__name__, (cls, base_class, BusinessProcess), {})
126 127
              # here we could inherit many "useful" classes dynamically - héhé
              # that would be a "real" abstract composition system
128
    self._effective_model_list = \
Aurel's avatar
Aurel committed
129
      orig_self._findEffectiveSpecialiseValueList(specialise_type_list=portal_type_list)
130 131
    return self

132
  def __init__(self, orig_self, portal_type_list=None):
133 134 135 136
    # __new__ does not call __init__ because returned object
    # is wrapped in an acquisition context.
    assert False

137 138 139 140
  def asComposedDocument(self, portal_type_list=None):
    assert False, "not useful yet"
    # If required, this must be implemented by calling 'asComposedDocument' on
    # the original object (because the parameters may differ).
141 142 143

  @property
  def _folder_handler(self):
144 145 146 147 148 149 150 151 152 153 154
    assert False

  def __getattr__(self, name):
    raise AttributeError(name)

  def objectValues(self, spec=None, meta_type=None, portal_type=None,
                   sort_on=None, sort_order=None, checked_permission=None,
                   **kw):
    assert spec is meta_type is checked_permission is None, "not useful yet"
    object_list = getattr(aq_base(self), '_predicate_list', None)
    if object_list is None:
155 156
      container_list = tuple(filter(len, self._effective_model_list))
      object_list = _findPredicateList(container_list, self._portal_type_list)
157 158 159 160 161 162 163
      self._predicate_list = object_list
    if portal_type is not None:
      if isinstance(portal_type, str):
        portal_type = (portal_type,)
      object_list = filter(lambda x: x.getPortalType() in portal_type,
                           object_list)
    return sortValueList(object_list, sort_on, sort_order, **kw)
164 165


166 167 168 169 170 171 172 173 174 175
class CompositionMixin:
  """
  """

  # Declarative security
  security = ClassSecurityInfo()
  security.declareObjectProtected(Permissions.AccessContentsInformation)

  security.declareProtected(Permissions.AccessContentsInformation,
                            'asComposedDocument')
Julien Muchembled's avatar
Julien Muchembled committed
176
  asComposedDocument = transactional_cached()(asComposedDocument)
177 178 179

  # XXX add accessors to get properties from '_effective_model_list' ?
  #     (cf PaySheetModel)
180

181
  def _findEffectiveSpecialiseValueList(self, specialise_type_list=None):
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
    """Return a list of effective specialised objects that is the
    inheritance tree.
    An effective object is an object which have start_date and stop_date
    included to the range of the given parameters start_date and stop_date.

    This algorithm uses Breadth First Search.
    """
    start_date = self.getStartDate()
    stop_date = self.getStopDate()
    def getEffectiveModel(model):
      return _getEffectiveModel(model, start_date, stop_date)
    model_list = [self]
    model_set = set(model_list)
    model_index = 0
    while model_index < len(model_list):
      model = model_list[model_index]
      model_index += 1
199
      # we don't use getSpecialiseValueList to avoid acquisition on the parent
200 201
      for model in map(getEffectiveModel, model.getValueList('specialise',
                                      portal_type=specialise_type_list or ())):
202 203
        if model not in model_set:
          model_set.add(model)
204
          if 1: #model.test(self): # XXX
205
            model_list.append(model)
206 207 208 209 210
    try:
      parent_asComposedDocument = self.getParentValue().asComposedDocument
    except AttributeError:
      pass
    else:
211 212 213
      model_list += [model for model in parent_asComposedDocument(
                             specialise_type_list)._effective_model_list
                           if model not in model_set]
214
    return model_list
Julien Muchembled's avatar
Julien Muchembled committed
215

216
del asComposedDocument # to be unhidden (and renamed ?) if needed