DocumentReferenceConstraint.py 4.24 KB
Newer Older
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
##############################################################################
#
# Copyright (c) 2006 Nexedi SARL and Contributors. All Rights Reserved.
#          Jerome Perrin <jerome@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability 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
# garantees 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################

from Products.ERP5Type.Constraint import Constraint
30
translateString = lambda msg, **kw: msg # just to extract messages
Jean-Paul Smets's avatar
Jean-Paul Smets committed
31 32
_MARKER = []

33

Jean-Paul Smets's avatar
Jean-Paul Smets committed
34 35 36 37 38 39 40 41 42 43
class DocumentReferenceConstraint(Constraint):
  """
  This constraint checks if the document has all required coordinates
  (reference, version and language) and if there is no other document with
  the same coordinates.

  Fixing is not implemented on purpose
  (although we could, e.g. by changing version number)
  """

44 45 46 47
  _message_id_list = [ 'message_property_not_defined',
                       'message_another_document_exists',
                       'message_multiple_documents_exists' ]
  
48
  message_property_not_defined = translateString(
49
      'Property ${property_id} was not defined')
50
  message_another_document_exists = translateString(
51 52
      'Another document ${document_reference} - '
      '${document_language} - ${document_version} already exists')
53
  message_multiple_documents_exists = translateString(
54 55 56
      'Multiple (${document_count}) documents ${document_reference} - '
      '${document_language} - ${document_version} already exists')

57
  def checkConsistency(self, object, fixit=0):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
58 59 60
    """
      Implement here the consistency checker
    """
61 62
    # XXX we probably could check reference syntax here, based on regexp in
    # preferences?
Jean-Paul Smets's avatar
Jean-Paul Smets committed
63 64
    error_list = []

65 66 67 68 69
    for property_id in ('reference', 'language', 'version'):
      if object.getProperty(property_id) in (None, ''):
        error_list.append(self._generateError(object,
             self._getMessage('message_property_not_defined'),
             mapping=dict(property_id=property_id)))
Jean-Paul Smets's avatar
Jean-Paul Smets committed
70 71
    if error_list:
      return error_list
72 73 74 75 76 77 78

    # XXX isn't it better to use unrestrictedSearchResults ?
    #   potential problem is that we would get deleted documents aswell
    res = object.portal_catalog(reference=object.getReference(),
                                language=object.getLanguage(),
                                version=object.getVersion(),
                                portal_type=object.getPortalDocumentTypeList())
Jean-Paul Smets's avatar
Jean-Paul Smets committed
79 80
    res = list(res)
    if len(res) == 2: # this object and another object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
      error_list.append(self._generateError(object,
                self._getMessage('message_another_document_exists'),
                mapping=dict(document_reference=object.getReference(),
                             document_language=object.getLanguage(),
                             document_version=object.getVersion())))

    if len(res) > 2:
      # this is very serious since there are many objects with the same
      # reference
      error_list.append(self._generateError(object,
                self._getMessage('message_multiple_documents_exists'),
                mapping=dict(document_count=len(res),
                             document_reference=object.getReference(),
                             document_language=object.getLanguage(),
                             document_version=object.getVersion())))
96
    return error_list
97