ERP5Conduit.py 37.2 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) 2002 Nexedi SARL and Contributors. All Rights Reserved.
#          Sebastien Robin <seb@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.ERP5SyncML.SyncCode import SyncCode
30
from Products.ERP5SyncML.XMLSyncUtils import XMLSyncUtilsMixin
Jean-Paul Smets's avatar
Jean-Paul Smets committed
31 32 33 34 35 36
from Products.ERP5SyncML.Subscription import Conflict
from Products.CMFCore.utils import getToolByName
from Products.ERP5SyncML.XupdateUtils import XupdateUtils
from Products.ERP5Type.Utils import convertToUpperCase
from Products.ERP5Type.Accessor.TypeDefinition import list_types
from xml.dom.ext.reader.Sax2 import FromXml
37
from DateTime.DateTime import DateTime
Jean-Paul Smets's avatar
Jean-Paul Smets committed
38 39
from email.MIMEBase import MIMEBase
from email import Encoders
Sebastien Robin's avatar
Sebastien Robin committed
40 41
from AccessControl import ClassSecurityInfo
from Products.ERP5Type import Permissions
42 43
import pickle
import string
44 45
from xml.dom.ext import PrettyPrint
from cStringIO import StringIO
46
import re, copy
Jean-Paul Smets's avatar
Jean-Paul Smets committed
47 48 49

from zLOG import LOG

50
class ERP5Conduit(XMLSyncUtilsMixin):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
  """
    A conduit is a piece of code in charge of

    - updating an object attributes from an XUpdate XML stream

    (Conduits are not in charge of creating new objects which
    are eventually missing in a synchronisation process)

    If an object has be created during a synchronisation process,
    the way to proceed consists in:

    1- creating an empty instance of the appropriate class
      in the appropriate directory

    2- updating that empty instance with the conduit

    The first implementation of ERP5 synchronisation
    will define a default location to create new objects and
    a default class. This will be defined at the level of the synchronisation
    tool

72 73 74 75 76 77 78 79 80 81 82 83 84 85
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx
    Look carefully when we are adding elements,
    for example, when we do 'insert-after', with 2 xupdate:element,
    so adding 2 differents objects, actually it adds only XXXX one XXX object
    In this case the getSubObjectDepth(), doesn't have
    too much sence
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    There is also one problem, when we synchronize a conflict, we are not waiting
    the response of the client, so that we are not sure if it take into account,
    we may have CONFLICT_NOT_SYNCHRONIZED AND CONFLICT_SYNCHRONIZED
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Jean-Paul Smets's avatar
Jean-Paul Smets committed
86

87
  """
Jean-Paul Smets's avatar
Jean-Paul Smets committed
88

Sebastien Robin's avatar
Sebastien Robin committed
89 90
  # Declarative security
  security = ClassSecurityInfo()
Jean-Paul Smets's avatar
Jean-Paul Smets committed
91

Sebastien Robin's avatar
Sebastien Robin committed
92
  security.declareProtected(Permissions.AccessContentsInformation,'getEncoding')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
93 94 95 96 97 98
  def getEncoding(self):
    """
    return the string corresponding to the local encoding
    """
    return "iso-8859-1"

Sebastien Robin's avatar
Sebastien Robin committed
99
  security.declareProtected(Permissions.ModifyPortalContent, '__init__')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
100 101 102
  def __init__(self):
    self.args = {}

Sebastien Robin's avatar
Sebastien Robin committed
103
  security.declareProtected(Permissions.ModifyPortalContent, 'addNode')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
104 105 106 107
  def addNode(self, xml=None, object=None, previous_xml=None, force=0, **kw):
    """
    A node is added

108 109 110 111 112 113 114 115
    xml : the xml wich contains what we want to add

    object : from where we want to add something

    previous_xml : the previous xml of the object, if any

    force : apply updates even if there's a conflict

Jean-Paul Smets's avatar
Jean-Paul Smets committed
116 117
    This fucntion returns conflict_list, wich is of the form,
    [conflict1,conflict2,...] where conclict1 is of the form :
118
    [object.getPath(),keyword,local_and_actual_value,subscriber_value]
Jean-Paul Smets's avatar
Jean-Paul Smets committed
119 120
    """
    conflict_list = []
121
    xml = self.convertToXml(xml)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
122 123 124 125
    LOG('addNode',0,'xml_reconstitued: %s' % str(xml))
    # In the case where this new node is a object to add
    LOG('addNode',0,'object.id: %s' % object.getId())
    LOG('addNode',0,'xml.nodeName: %s' % xml.nodeName)
126
    LOG('addNode',0,'isSubObjectAdd: %i' % self.getSubObjectDepth(xml))
Sebastien Robin's avatar
Sebastien Robin committed
127
    LOG('addNode',0,'isHistoryAdd: %i' % self.isHistoryAdd(xml))
128
    if xml.nodeName in self.XUPDATE_INSERT_OR_ADD and self.getSubObjectDepth(xml)==0:
Sebastien Robin's avatar
Sebastien Robin committed
129 130 131 132 133
      if self.isHistoryAdd(xml)!=-1: # bad hack XXX to be removed
        for element in self.getXupdateElementList(xml):
          xml = self.getElementFromXupdate(element)
          conflict_list += self.addNode(xml=xml,object=object,
                          previous_xml=previous_xml, force=force, **kw)
134 135
    elif xml.nodeName == 'object':
      object_id = self.getAttribute(xml,'id')
136
      docid = self.getObjectDocid(xml)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
137 138 139
      LOG('addNode',0,'object_id: %s' % object_id)
      if object_id is not None:
        try:
140 141 142
          subobject = object._getOb(object_id)
        except (AttributeError, KeyError):
          subobject = None
143
        if subobject is None: # If so, it doesn't exist
Jean-Paul Smets's avatar
Jean-Paul Smets committed
144 145 146
          portal_type = ''
          if xml.nodeName == 'object':
            portal_type = self.getObjectType(xml)
147 148
          elif xml.nodeName in self.XUPDATE_INSERT_OR_ADD: # Deprecated ???
            portal_type = self.getXupdateObjectType(xml) # Deprecated ???
Jean-Paul Smets's avatar
Jean-Paul Smets committed
149 150
          portal_types = getToolByName(object,'portal_types')
          LOG('ERP5Conduit.addNode',0,'portal_type: |%s|' % str(portal_type))
151 152 153 154 155 156 157 158 159 160 161
          if docid==None: # ERP5 content
            portal_types.constructContent(type_name = portal_type,
                                              container = object,
                                              id = object_id)
          else: # CPS content
            # This is specific to CPS, we will call the proxy tool
            px_tool= getToolByName(object,'portal_proxies')
            proxy_type = 'document'
            if portal_type == 'Workspace':
              proxy_type = 'folder'
            proxy = px_tool.createEmptyProxy(proxy_type,
162
                                   object,portal_type,object_id,docid=docid)
163 164 165 166 167 168
            proxy.isIndexable = 0 # So it will not be reindexed, this prevent errors
            # Calculate rpath
            utool = getToolByName(object, 'portal_url')
            rpath = utool.getRelativeUrl(object)
            px_tool._modifyProxy(proxy,rpath)

169
          subobject = object._getOb(object_id)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
170
        self.newObject(object=subobject,xml=xml)
171
    elif xml.nodeName in self.XUPDATE_INSERT_OR_ADD \
172
         and self.getSubObjectDepth(xml)>=1:
173 174 175 176
      sub_object_id = self.getSubObjectId(xml)
      LOG('addNode',0,'getSubObjectModification number: %s' % sub_object_id)
      if previous_xml is not None and sub_object_id is not None:
        LOG('addNode',0,'previous xml is not none and also sub_object_id')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
177
        # Find the previous xml corresponding to this subobject
178
        sub_previous_xml = self.getSubObjectXml(sub_object_id,previous_xml)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
179 180 181 182
        LOG('addNode',0,'isSubObjectModification sub_p_xml: %s' % str(sub_previous_xml))
        if sub_previous_xml is not None:
          sub_object = None
          try:
183 184
            sub_object = object._getOb(sub_object_id)
          except (AttributeError, KeyError):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
185 186 187 188 189 190 191 192 193 194
            pass
          if sub_object is not None:
            LOG('addNode',0,'subobject.id: %s' % sub_object.id)
            # Change the xml in order to directly apply
            # modifications to the subobject
            sub_xml = self.getSubObjectXupdate(xml)
            LOG('addNode',0,'sub_xml: %s' % str(sub_xml))
            # Then do the udpate
            conflict_list += self.addNode(xml=sub_xml,object=sub_object,
                            previous_xml=sub_previous_xml, force=force)
195 196 197
    elif xml.nodeName == self.history_tag or self.isHistoryAdd(xml)>0:
      # We want to add a workflow action
      wf_tool = getToolByName(object,'portal_workflow')
198
      wf_id = self.getAttribute(xml,'id')
199 200 201 202 203
      if wf_id is None: # History added by xupdate
        wf_id = self.getHistoryIdFromSelect(xml)
        xml = self.getElementNodeList(xml)[0]
      LOG('addNode, workflow_history id:',0,wf_id)
      LOG('addNode, workflow_history xml:',0,xml)
Sebastien Robin's avatar
Sebastien Robin committed
204 205 206 207 208 209 210 211 212
      #for action in self.getWorkflowActionFromXml(xml):
      status = self.getStatusFromXml(xml)
      LOG('addNode, status:',0,status)
      wf_conflict_list = self.isWorkflowActionAddable(object=object,
                                             status=status,wf_tool=wf_tool,
                                             xml=xml)
      LOG('addNode, workflow_history wf_conflict_list:',0,wf_conflict_list)
      if wf_conflict_list==[] or force:
        LOG('addNode, setting status:',0,'ok')
213
        wf_tool.setStatusOf(wf_id,object,status)
Sebastien Robin's avatar
Sebastien Robin committed
214 215
      else:
        conflict_list += wf_conflict_list
216 217 218
    elif xml.nodeName in self.local_role_list:
      # We want to add a local role
      roles = self.convertXmlValue(xml.childNodes[0].data,data_type='tokens')
219
      roles = list(roles) # Needed for CPS, or we have a CPS error
220 221 222
      user = roles[0]
      roles = roles[1:]
      object.manage_setLocalRoles(user,roles)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
223 224 225 226
    else:
      conflict_list += self.updateNode(xml=xml,object=object, force=force, **kw)
    return conflict_list

Sebastien Robin's avatar
Sebastien Robin committed
227
  security.declareProtected(Permissions.ModifyPortalContent, 'deleteNode')
228
  def deleteNode(self, xml=None, object=None, object_id=None, force=None, **kw):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
229 230 231 232 233
    """
    A node is deleted
    """
    # In the case where this new node is a object to delete
    LOG('ERP5Conduit',0,'deleteNode')
234 235
    LOG('ERP5Conduit',0,'deleteNode, object.id: %s' % object.getId())
    conflict_list = []
236 237
    if xml is not None:
      xml = self.convertToXml(xml)
238 239 240
    if object_id is None:
      LOG('ERP5Conduit',0,'deleteNode, SubObjectDepth: %i' % self.getSubObjectDepth(xml))
      if xml.nodeName == self.xml_object_tag:
241
        object_id = self.getAttribute(xml,'id')
242 243 244 245 246
      elif self.getSubObjectDepth(xml)==1:
        object_id = self.getSubObjectId(xml)
      elif self.getSubObjectDepth(xml)==2:
        # we have to call delete node on a subsubobject
        sub_object_id = self.getSubObjectId(xml)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
247
        try:
248 249 250 251
          sub_object = object._getOb(sub_object_id)
          sub_xml = self.getSubObjectXupdate(xml)
          conflict_list += self.deleteNode(xml=sub_xml,object=sub_object,
                                           force=force)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
252 253
        except KeyError:
          pass
254 255 256 257
    else: # We do have an object_id
      try:
        object._delObject(object_id)
      except (AttributeError, KeyError):
258
        LOG('ERP5Conduit',0,'deleteNode, Unable to delete: %s' % str(object_id))
259 260
        pass
    return conflict_list
Jean-Paul Smets's avatar
Jean-Paul Smets committed
261

Sebastien Robin's avatar
Sebastien Robin committed
262
  security.declareProtected(Permissions.ModifyPortalContent, 'updateNode')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
263 264 265 266 267 268 269 270 271
  def updateNode(self, xml=None, object=None, previous_xml=None, force=0, **kw):
    """
    A node is updated with some xupdate
      - xml : the xml corresponding to the update, it should be xupdate
      - object : the object on wich we want to apply the xupdate
      - [previous_xml] : the previous xml of the object, it is mandatory
                         when we have sub objects
    """
    conflict_list = []
272
    xml = self.convertToXml(xml)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
273
    LOG('updateNode',0,'xml.nodeName: %s' % xml.nodeName)
Sebastien Robin's avatar
Sebastien Robin committed
274
    LOG('updateNode, force: ',0,force)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
275 276
    # we have an xupdate xml
    if xml.nodeName == 'xupdate:modifications':
277 278
      #xupdate_utils = XupdateUtils()
      xupdate_utils = self
Jean-Paul Smets's avatar
Jean-Paul Smets committed
279 280 281 282 283
      conflict_list += xupdate_utils.applyXupdate(object=object,xupdate=xml,conduit=self,
                                 previous_xml=previous_xml, force=force)
    # we may have only the part of an xupdate
    else:
      args = {}
284
      LOG('isSubObjectModification',0,'result: %s' % str(self.isSubObjectModification(xml)))
Jean-Paul Smets's avatar
Jean-Paul Smets committed
285
      if self.isProperty(xml) and not(self.isSubObjectModification(xml)):
286
        keyword = None
287 288
        for subnode in self.getAttributeNodeList(xml):
          if subnode.nodeName=='select':
289 290 291
            LOG('updateNode',0,'selection: %s' % str(subnode.nodeValue))
            select_list = subnode.nodeValue.split('/') # Something like:
                                                       #('','object[1]','sid[1]')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
292 293
            new_select_list = ()
            for select_item in select_list:
294 295 296
              if select_item.find('[')>=0:
                select_item = select_item[:select_item.find('[')]
              new_select_list += (select_item,)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
297 298 299
            select_list = new_select_list # Something like : ('','object','sid')
            keyword = select_list[len(select_list)-1] # this will be 'sid'

300 301
        data_xml = xml
        data = None
302 303
        LOG('updateNode',0,'keyword: %s' % str(keyword))
        if not (xml.nodeName in self.XUPDATE_INSERT_OR_ADD):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
304 305 306 307 308
          for subnode in self.getElementNodeList(xml):
            if subnode.nodeName=='xupdate:element':
              for subnode1 in subnode.attributes:
                if subnode1.nodeName=='name':
                  keyword = subnode1.nodeValue
309 310 311 312 313 314
              data_xml = subnode
        if len(self.getElementNodeList(data_xml))==0:
          try:
            data = data_xml.childNodes[0].data
          except IndexError: # There is no data
            data = None
Jean-Paul Smets's avatar
Jean-Paul Smets committed
315 316
        if not (keyword in self.NOT_EDITABLE_PROPERTY):
          # We will look for the data to enter
317 318 319
          data_type = object.getPropertyType(keyword)
          LOG('updateNode',0,'data_type: %s' % str(data_type))
          data = self.convertXmlValue(data,data_type=data_type)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
320 321 322 323 324 325 326 327
          args[keyword] = data
          args = self.getFormatedArgs(args=args)
          # This is the place where we should look for conflicts
          # For that we need :
          #   - data : the data from the remote box
          #   - old_data : the data from this box but at the time of the last synchronization
          #   - current_data : the data actually on this box
          isConflict = 0
Sebastien Robin's avatar
Sebastien Robin committed
328
          if (previous_xml is not None) and (not force): # if no previous_xml, no conflict
329
            old_data = self.getObjectProperty(keyword,previous_xml,data_type=data_type)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
330 331 332 333 334
            current_data = object.getProperty(keyword)
            LOG('updateNode',0,'Conflict data: %s' % str(data))
            LOG('updateNode',0,'Conflict old_data: %s' % str(old_data))
            LOG('updateNode',0,'Conflict current_data: %s' % str(current_data))
            if (old_data != current_data) and (data != current_data):
335 336 337
              LOG('updateNode',0,'Conflict on : %s' % keyword)
              # Hack in order to get the synchronization working for demo
              # XXX this have to be removed after
338 339
              #if not (data_type in self.binary_type_list):
              if 1:
340 341
                # This is a conflict
                isConflict = 1
342 343
                string_io = StringIO()
                PrettyPrint(xml,stream=string_io)
Sebastien Robin's avatar
Sebastien Robin committed
344 345
                conflict = Conflict(object_path=object.getPhysicalPath(),
                                    keyword=keyword)
346
                conflict.setXupdate(string_io.getvalue())
Sebastien Robin's avatar
Sebastien Robin committed
347 348 349
                if not (data_type in self.binary_type_list):
                  conflict.setLocalValue(current_data)
                  conflict.setRemoteValue(data)
350 351 352 353
                conflict_list += [conflict]
                #conflict_list += [Conflict(object_path=object.getPhysicalPath(),
                #                           keyword=keyword,
                #                           xupdate=string_io)]
354 355
                                           #publisher_value=current_data, # not needed any more
                                           #subscriber_value=data)] # not needed any more
Jean-Paul Smets's avatar
Jean-Paul Smets committed
356 357
          # We will now apply the argument with the method edit
          if args != {} and (isConflict==0 or force):
358 359
            LOG('updateNode',0,'object._edit, args: %s' % str(args))
            object._edit(**args)
360 361 362
            # It is sometimes required to do something after an edit
            if hasattr(object,'manage_afterEdit'):
              object.manage_afterEdit()
363

Jean-Paul Smets's avatar
Jean-Paul Smets committed
364 365 366 367
        if keyword == 'object':
          # This is the case where we have to call addNode
          LOG('updateNode',0,'we will add sub-object')
          conflict_list += self.addNode(xml=subnode,object=object,force=force)
368
        elif keyword == self.history_tag:
369
          # This is the case where we have to call addNode
370
          LOG('updateNode',0,'we will add history')
371
          conflict_list += self.addNode(xml=subnode,object=object,force=force)
372 373 374 375 376 377 378
        elif keyword == self.local_role_tag:
          # This is the case where we have to call addNode
          LOG('updateNode',0,'we will add a local role')
          roles = self.convertXmlValue(data,data_type='tokens')
          user = roles[0]
          roles = roles[1:]
          object.manage_setLocalRoles(user,roles)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
379 380 381
      elif self.isSubObjectModification(xml):
        # We should find the object corresponding to
        # this update, so we have to look in the previous_xml
382 383 384 385 386
        sub_object_id = self.getSubObjectId(xml)
        LOG('updateNode',0,'getSubObjectModification number: %s' % sub_object_id)
        if previous_xml is not None and sub_object_id is not None:
          LOG('updateNode',0,'previous xml is not none and also sub_object_id')
          sub_previous_xml = self.getSubObjectXml(sub_object_id,previous_xml)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
387 388 389 390
          LOG('updateNode',0,'isSubObjectModification sub_p_xml: %s' % str(sub_previous_xml))
          if sub_previous_xml is not None:
            sub_object = None
            try:
391
              sub_object = object[sub_object_id]
Jean-Paul Smets's avatar
Jean-Paul Smets committed
392 393 394 395 396 397 398 399 400 401 402 403 404
            except KeyError:
              pass
            if sub_object is not None:
              LOG('updateNode',0,'subobject.id: %s' % sub_object.id)
              # Change the xml in order to directly apply
              # modifications to the subobject
              sub_xml = self.getSubObjectXupdate(xml)
              LOG('updateNode',0,'sub_xml: %s' % str(sub_xml))
              # Then do the udpate
              conflict_list += self.updateNode(xml=sub_xml,object=sub_object, force=force,
                              previous_xml=sub_previous_xml)
    return conflict_list

Sebastien Robin's avatar
Sebastien Robin committed
405
  security.declareProtected(Permissions.AccessContentsInformation,'getFormatedArgs')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
  def getFormatedArgs(self, args=None):
    """
    This lookd inside the args dictionnary and then
    convert any unicode string to string
    """
    LOG('ERP5Conduit.getFormatedArgs',0,'args: %s' % str(args))
    new_args = {}
    for keyword in args.keys():
      data = args[keyword]
      if type(keyword) is type(u"a"):
        keyword = keyword.encode(self.getEncoding())
      if type(data) is type([]) or type(data) is type(()):
        new_data = []
        for item in data:
          if type(item) is type(u"a"):
            item = item.encode(self.getEncoding())
422
            item = item.replace('@@@','\n')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
423 424 425 426
          new_data += [item]
        data = new_data
      if type(data) is type(u"a"):
        data = data.encode(self.getEncoding())
427
        data = data.replace('@@@','\n')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
428 429 430 431 432 433 434 435 436
      if keyword == 'binary_data':
        LOG('ERP5Conduit.getFormatedArgs',0,'binary_data keyword: %s' % str(keyword))
        msg = MIMEBase('application','octet-stream')
        Encoders.encode_base64(msg)
        msg.set_payload(data)
        data = msg.get_payload(decode=1)
      new_args[keyword] = data
    return new_args

Sebastien Robin's avatar
Sebastien Robin committed
437
  security.declareProtected(Permissions.AccessContentsInformation,'isProperty')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
438 439 440 441
  def isProperty(self, xml):
    """
    Check if it is a simple property
    """
442
    bad_list = (self.sub_object_exp,self.history_exp)
443 444
    for subnode in self.getAttributeNodeList(xml):
      if subnode.nodeName=='select':
Jean-Paul Smets's avatar
Jean-Paul Smets committed
445 446
        value = subnode.nodeValue
        for bad_string in bad_list:
447
          if re.search(bad_string,value) is not None:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
448 449 450
            return 0
    return 1

Sebastien Robin's avatar
Sebastien Robin committed
451
  security.declareProtected(Permissions.AccessContentsInformation,'getSubObjectXupdate')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
452 453 454 455 456
  def getSubObjectXupdate(self, xml):
    """
    This will change the xml in order to change the update
    from the object to the subobject
    """
457 458 459 460
    xml = copy.deepcopy(xml)
    for subnode in self.getAttributeNodeList(xml):
      if subnode.nodeName=='select':
        subnode.nodeValue = self.getSubObjectSelect(subnode.nodeValue)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
461 462
    return xml

Sebastien Robin's avatar
Sebastien Robin committed
463
  security.declareProtected(Permissions.AccessContentsInformation,'isHistoryAdd')
464 465
  def isHistoryAdd(self, xml):
    bad_list = (self.history_exp)
466 467
    for subnode in self.getAttributeNodeList(xml):
      if subnode.nodeName=='select':
468 469 470
        value = subnode.nodeValue
        for bad_string in bad_list:
          if re.search(bad_string,value) is not None:
Sebastien Robin's avatar
Sebastien Robin committed
471 472 473 474
            if re.search(self.bad_history_exp,value) is None:
              return 1
            else:
              return -1
475 476
    return 0

Sebastien Robin's avatar
Sebastien Robin committed
477
  security.declareProtected(Permissions.AccessContentsInformation,'isSubObjectModification')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
478 479 480 481
  def isSubObjectModification(self, xml):
    """
    Check if it is a modification from an subobject
    """
482
    good_list = (self.sub_object_exp,)
483 484
    for subnode in self.getAttributeNodeList(xml) :
      if subnode.nodeName=='select':
Jean-Paul Smets's avatar
Jean-Paul Smets committed
485 486 487
        value = subnode.nodeValue
        LOG('isSubObjectModification',0,'value: %s' % value)
        for good_string in good_list:
488
          if re.search(good_string,value) is not None:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
489 490 491
            return 1
    return 0

Sebastien Robin's avatar
Sebastien Robin committed
492
  security.declareProtected(Permissions.AccessContentsInformation,'getSubObjectDepth')
493 494 495 496 497 498 499 500 501
  def getSubObjectDepth(self, xml):
    """
    Give the Depth of a subobject modification
    0 means, no depth
    1 means it is a subobject
    2 means it is more depth than subobject
    """
    LOG('getSubObjectDepth',0,'xml.nodeName: %s' % xml.nodeName)
    if xml.nodeName in self.XUPDATE_TAG:
502 503 504
      i = 0
      if xml.nodeName in self.XUPDATE_INSERT:
        i = 1
505 506 507 508 509 510
      LOG('getSubObjectDepth',0,'xml2.nodeName: %s' % xml.nodeName)
      for subnode in self.getAttributeNodeList(xml):
        LOG('getSubObjectDepth',0,'subnode.nodeName: %s' % subnode.nodeName)
        if subnode.nodeName == 'select':
          value = subnode.nodeValue
          LOG('getSubObjectDepth',0,'subnode.nodeValue: %s' % subnode.nodeValue)
511 512 513 514 515 516 517 518 519 520
          if re.search(self.sub_sub_object_exp,value) is not None:
            return 2 # This is sure in all cases
          elif re.search(self.sub_object_exp,value) is not None:
            #new_select = self.getSubObjectSelect(value) # Still needed ???
            #if self.getSubObjectSelect(new_select) != new_select:
            #  return (2 - i)
            #return (1 - i)
            return (2 - i)
          elif re.search(self.object_exp,value) is not None:
            return (1 - i)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
521 522
    return 0

Sebastien Robin's avatar
Sebastien Robin committed
523
  security.declareProtected(Permissions.AccessContentsInformation,'getSubObjectSelect')
524
  def getSubObjectSelect(self, select):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
525
    """
526 527 528
    Return a string wich is the selection for the subobject
    ex: for "/object[@id='161']/object[@id='default_address']/street_address"
    if returns "/object[@id='default_address']/street_address"
Jean-Paul Smets's avatar
Jean-Paul Smets committed
529
    """
530 531 532 533 534 535
    if re.search(self.object_exp,select) is not None:
      s = '/'
      if re.search('/.*/',select) is not None: # This means we have more than just object
        new_value = select[select.find(s,select.find(s)+1):]
      else:
        new_value = '/'
536 537 538
      select = new_value
    return select

Sebastien Robin's avatar
Sebastien Robin committed
539
  security.declareProtected(Permissions.AccessContentsInformation,'getSubObjectId')
540 541 542 543 544 545 546
  def getSubObjectId(self, xml):
    """
    Return the id of the subobject in an xupdate modification
    """
    object_id = None
    for subnode in self.getAttributeNodeList(xml):
      if subnode.nodeName=='select':
Jean-Paul Smets's avatar
Jean-Paul Smets committed
547
        value = subnode.nodeValue
548 549 550 551
        if re.search(self.object_exp,value) is not None:
          s = "'"
          first = value.find(s)+1
          object_id = value[first:value.find(s,first)]
552 553 554
          return object_id
    return object_id

Sebastien Robin's avatar
Sebastien Robin committed
555
  security.declareProtected(Permissions.AccessContentsInformation,'getHistoryIdFromSelect')
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
  def getHistoryIdFromSelect(self, xml):
    """
    Return the id of the subobject in an xupdate modification
    """
    object_id = None
    for subnode in self.getAttributeNodeList(xml):
      if subnode.nodeName=='select':
        value = subnode.nodeValue
        if re.search(self.history_exp,value) is not None:
          s = self.history_tag
          object_id = value[value.find(s):]
          object_id = object_id[object_id.find("'")+1:]
          object_id = object_id[:object_id.find("'")]
          return object_id
    return object_id

Sebastien Robin's avatar
Sebastien Robin committed
572
  security.declareProtected(Permissions.AccessContentsInformation,'getSubObjectXml')
573 574 575 576 577 578 579 580
  def getSubObjectXml(self, object_id, xml):
    """
    Return the xml of the subobject which as the id object_id
    """
    xml = self.convertToXml(xml)
    for subnode in self.getElementNodeList(xml):
      if subnode.nodeName==self.xml_object_tag:
        LOG('getSub0bjectXml: object_id:',0,object_id)
581
        if object_id == self.getAttribute(subnode,'id'):
582 583
          return subnode
    return None
Jean-Paul Smets's avatar
Jean-Paul Smets committed
584

585 586 587 588 589 590 591 592 593 594 595
#   def getObjectId(self, xml):
#     """
#     Retrieve the id
#     # XXX Deprecated, we should use instead, getParameter(xml,'id') XXX
#     """
#     for attribute in self.getAttributeNodeList(xml):
#       if attribute.nodeName == 'id':
#         data = attribute.childNodes[0].data
#         return self.convertXmlValue(data)
#     return None

Sebastien Robin's avatar
Sebastien Robin committed
596
  security.declareProtected(Permissions.AccessContentsInformation,'getAttribute')
597
  def getAttribute(self, xml, param):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
598
    """
599
    Retrieve the given parameter from the xml
Jean-Paul Smets's avatar
Jean-Paul Smets committed
600
    """
601
    for attribute in self.getAttributeNodeList(xml):
602
      if attribute.nodeName == param:
603
        data = attribute.childNodes[0].data
604
        return self.convertXmlValue(data,data_type='string')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
605 606
    return None

Sebastien Robin's avatar
Sebastien Robin committed
607
  security.declareProtected(Permissions.AccessContentsInformation,'getObjectDocid')
608 609 610 611 612 613 614 615 616
  def getObjectDocid(self, xml):
    """
    Retrieve the docid
    """
    for subnode in self.getElementNodeList(xml):
      if subnode.nodeName == 'docid':
        data = subnode.childNodes[0].data
        return self.convertXmlValue(data)
    return None
Jean-Paul Smets's avatar
Jean-Paul Smets committed
617

Sebastien Robin's avatar
Sebastien Robin committed
618
  security.declareProtected(Permissions.AccessContentsInformation,'getObjectProperty')
619
  def getObjectProperty(self, property, xml, data_type=None):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
620 621 622
    """
    Retrieve the given property
    """
623
    xml = self.convertToXml(xml)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
624 625 626
    # document, with childNodes[0] a DocumentType and childNodes[1] the Element Node
    for subnode in self.getElementNodeList(xml):
      if subnode.nodeName == property:
627 628
        if data_type is None:
          data_type = self.getPropertyType(subnode)
629 630 631 632
        try:
          data = subnode.childNodes[0].data
        except IndexError: # There is no data
          data = None
633 634
        data =  self.convertXmlValue(data, data_type=data_type)
        LOG('getObjectProperty',0,'prop; %s, data_type:%s, data: %s' % (property,data_type,data))
Jean-Paul Smets's avatar
Jean-Paul Smets committed
635 636 637
        return data
    return None

Sebastien Robin's avatar
Sebastien Robin committed
638
  security.declareProtected(Permissions.AccessContentsInformation,'convertToXml')
639
  def convertToXml(self,xml):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
640 641 642 643 644 645
    """
    if xml is a string, convert it to a node
    """
    if type(xml) in (type('a'),type(u'a')):
      xml = FromXml(xml)
      xml = xml.childNodes[1] # Because we just created a new xml
646 647 648
    # If we have the xml from the node erp5, we just take the subnode
    if xml.nodeName=='erp5':
      xml = self.getElementNodeList(xml)[0]
Jean-Paul Smets's avatar
Jean-Paul Smets committed
649 650
    return xml

Sebastien Robin's avatar
Sebastien Robin committed
651
  security.declareProtected(Permissions.AccessContentsInformation,'getObjectType')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
652 653 654 655 656
  def getObjectType(self, xml):
    """
    Retrieve the portal type from an xml
    """
    portal_type = None
657 658 659 660 661
    for subnode in self.getAttributeNodeList(xml):
      if subnode.nodeName=='portal_type':
        portal_type = subnode.nodeValue
        portal_type = self.convertXmlValue(portal_type)
        return portal_type
Jean-Paul Smets's avatar
Jean-Paul Smets committed
662 663
    return portal_type

Sebastien Robin's avatar
Sebastien Robin committed
664
  security.declareProtected(Permissions.AccessContentsInformation,'getPropertyType')
665 666 667 668 669 670 671 672 673 674 675 676
  def getPropertyType(self, xml):
    """
    Retrieve the portal type from an xml
    """
    p_type = None
    for subnode in self.getAttributeNodeList(xml):
      if subnode.nodeName=='type':
        p_type = subnode.nodeValue
        p_type = self.convertXmlValue(p_type,data_type='string')
        return p_type
    return p_type

Sebastien Robin's avatar
Sebastien Robin committed
677
  security.declareProtected(Permissions.AccessContentsInformation,'getXupdateObjectType')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
678 679 680
  def getXupdateObjectType(self, xml):
    """
    Retrieve the portal type from an xupdate
681
    XXXX  This should not be used any more !!! XXXXXXXXXXX
Jean-Paul Smets's avatar
Jean-Paul Smets committed
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
    """
    if xml.nodeName.find('xupdate')>=0:
      for subnode in self.getElementNodeList(xml):
        if subnode.nodeName == 'xupdate:element':
          for subnode1 in self.getElementNodeList(subnode):
            if subnode1.nodeName == 'xupdate:attribute':
              for attribute in subnode1.attributes:
                if attribute.nodeName == 'name':
                  if attribute.nodeValue == 'portal_type':
                    data = subnode1.childNodes[0].data
                    data = self.convertXmlValue(data)
                    return data
    return None


Sebastien Robin's avatar
Sebastien Robin committed
697
  security.declareProtected(Permissions.ModifyPortalContent, 'newObject')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
698 699 700 701 702 703
  def newObject(self, object=None, xml=None):
    """
      modify the object with datas from
      the xml (action section)
    """
    args = {}
704 705 706
    # Retrieve the list of users with a role and delete default roles
    user_role_list = map(lambda x:x[0],object.get_local_roles())
    object.manage_delLocalRoles(user_role_list)
Sebastien Robin's avatar
Sebastien Robin committed
707 708
    if hasattr(object,'workflow_history'):
      object.workflow_history = {}
Jean-Paul Smets's avatar
Jean-Paul Smets committed
709 710 711 712
    if xml.nodeName.find('xupdate')>= 0:
      xml = self.getElementNodeList(xml)[0]
    for subnode in self.getElementNodeList(xml):
      if not(subnode.nodeName in self.NOT_EDITABLE_PROPERTY):
713
        keyword_type = self.getPropertyType(subnode)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
714 715 716 717 718 719 720 721
        LOG('newObject',0,str(subnode.childNodes))
        # This is the case where the property is a list
        keyword=str(subnode.nodeName)
        if len(subnode.childNodes) > 0: # We check that this tag is not empty
          data = subnode.childNodes[0].data
          args[keyword]=data
        LOG('newObject',0,'keyword: %s' % str(keyword))
        LOG('newObject',0,'keywordtype: %s' % str(keyword_type))
722 723
        #if args.has_key(keyword):
        #  LOG('newObject',0,'data: %s' % str(args[keyword]))
Jean-Paul Smets's avatar
Jean-Paul Smets committed
724
        if args.has_key(keyword):
725
          args[keyword] = self.convertXmlValue(args[keyword],keyword_type)
726
      elif subnode.nodeName in self.ADDABLE_PROPERTY:
Sebastien Robin's avatar
Sebastien Robin committed
727
        self.addNode(object=object,xml=subnode, force=1)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
728 729
    # We should first edit the object
    args = self.getFormatedArgs(args=args)
730
    LOG('newObject',0,"object.getphyspath: %s" % str(object.getPhysicalPath()))
Jean-Paul Smets's avatar
Jean-Paul Smets committed
731 732 733
    LOG('newObject',0,"args: %s" % str(args))
    # edit the object with a dictionnary of arguments,
    # like {"telephone_number":"02-5648"}
734
    object._edit(**args)
735 736 737
    if hasattr(object,'manage_afterEdit'):
      object.manage_afterEdit()

Jean-Paul Smets's avatar
Jean-Paul Smets committed
738 739

    # Then we may create subobject
740
    for subnode in self.getElementNodeList(xml):
Sebastien Robin's avatar
Sebastien Robin committed
741
      if subnode.nodeName in (self.xml_object_tag,): #,self.history_tag):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
742 743
        self.addNode(object=object,xml=subnode)

Sebastien Robin's avatar
Sebastien Robin committed
744
  security.declareProtected(Permissions.AccessContentsInformation,'getStatusFromXml')
745 746 747 748 749 750 751 752 753 754 755
  def getStatusFromXml(self, xml):
    """
    Return a worklow status from xml
    """
    status = {}
    for subnode in self.getElementNodeList(xml):
      keyword = self.convertXmlValue(subnode.nodeName)
      value = self.getObjectProperty(keyword,xml)
      status[keyword] = value
    return status

Sebastien Robin's avatar
Sebastien Robin committed
756
  security.declareProtected(Permissions.AccessContentsInformation,'getXupdateElementList')
757 758 759 760 761 762 763 764 765 766 767
  def getXupdateElementList(self, xml):
    """
    Retrieve the list of xupdate:element subnodes
    """
    e_list = []
    for subnode in self.getElementNodeList(xml):
      if subnode.nodeName in self.XUPDATE_EL:
        e_list += [subnode]
    LOG('getXupdateElementList, e_list:%',0,e_list)
    return e_list

Sebastien Robin's avatar
Sebastien Robin committed
768
  security.declareProtected(Permissions.AccessContentsInformation,'getElementFromXupdate')
769 770 771 772 773 774 775 776 777 778 779 780
  def getElementFromXupdate(self, xml):
    """
    from a xupdate:element returns the element as xml
    """
    if xml.nodeName in self.XUPDATE_EL:
      result = '<'
      result += xml.attributes[0].nodeValue
      for subnode in self.getElementNodeList(xml):  #getElementNodeList
        if subnode.nodeName == 'xupdate:attribute':
          result += ' ' + subnode.attributes[0].nodeValue + '='
          result += '"' + subnode.childNodes[0].nodeValue + '"'
      result += '>'
781 782 783 784 785 786 787
      # Then dumps the xml and remove what we does'nt want
      xml_string = StringIO()
      PrettyPrint(xml,xml_string)
      xml_string = xml_string.getvalue()
      maxi = max(xml_string.find('>')+1,\
                 xml_string.rfind('</xupdate:attribute>')+len('</xupdate:attribute>'))
      result += xml_string[maxi:xml_string.find('</xupdate:element>')]
788 789
      result += '</' + xml.attributes[0].nodeValue + '>'
      LOG('getElementFromXupdate, result:',0,result)
790
      return self.convertToXml(result)
791 792
    return xml

Sebastien Robin's avatar
Sebastien Robin committed
793
  security.declareProtected(Permissions.AccessContentsInformation,'getWorkflowActionFromXml')
794 795 796 797 798 799 800 801 802 803 804 805 806
  def getWorkflowActionFromXml(self, xml):
    """
    Return the list of workflow actions
    """
    action_list = []
    if xml.nodeName in self.XUPDATE_EL:
      action_list += [xml]
      return action_list
    for subnode in self.getElementNodeList(xml):
      if subnode.nodeName == self.action_tag:
        action_list += [subnode]
    return action_list

Sebastien Robin's avatar
Sebastien Robin committed
807
  security.declareProtected(Permissions.AccessContentsInformation,'convertXmlValue')
808
  def convertXmlValue(self, data, data_type=None):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
809 810
    """
    It is possible that the xml change the value, for example
811 812 813 814 815 816
    there is some too much '\n' and some spaces. We have to do some extra
    things so that we convert correctly the vlalue
    """
    if data is None:
      if data_type in self.list_type_list:
        data = ()
817 818
      if data_type in self.text_type_list:
        data = ''
819
      return data
Jean-Paul Smets's avatar
Jean-Paul Smets committed
820 821 822
    data = data.replace('\n','')
    if type(data) is type(u"a"):
      data = data.encode(self.getEncoding())
823 824 825 826 827 828
    # We can now convert string in tuple, dict, binary...
    if data_type in self.list_type_list:
      if type(data) is type('a'):
        data = tuple(data.split('@@@'))
    elif data_type in self.text_type_list:
      data = data.replace('@@@','\n')
829 830 831 832 833 834 835
#     elif data_type in self.binary_type_list:
#       data = data.replace('@@@','\n')
#       msg = MIMEBase('application','octet-stream')
#       Encoders.encode_base64(msg)
#       msg.set_payload(data)
#       data = msg.get_payload(decode=1)
    elif data_type in self.pickle_type_list:
836 837 838 839 840
      data = data.replace('@@@','\n')
      msg = MIMEBase('application','octet-stream')
      Encoders.encode_base64(msg)
      msg.set_payload(data)
      data = msg.get_payload(decode=1)
841
      data = pickle.loads(data)
842 843 844 845 846 847 848
    elif data_type in self.date_type_list:
      data = DateTime(data)
    elif data_type in self.dict_type_list:
      dict_list = map(lambda x:x.split(':'),data[1:-1].split(','))
      data = map(lambda (x,y):(x.replace(' ','').replace("'",''),int(y)),dict_list)
      data = dict(data)
    LOG('convertXmlValue',0,'data: %s' % str(data))
Jean-Paul Smets's avatar
Jean-Paul Smets committed
849 850
    return data

851 852
  # XXX is it the right place ? It should be in XupdateUtils, but here we
  # have some specific things to do
Sebastien Robin's avatar
Sebastien Robin committed
853
  security.declareProtected(Permissions.ModifyPortalContent, 'applyXupdate')
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
  def applyXupdate(self, object=None, xupdate=None, conduit=None, force=0, **kw):
    """
    Parse the xupdate and then it will call the conduit
    """
    conflict_list = []
    if type(xupdate) in (type('a'),type(u'a')):
      xupdate = FromXml(xupdate)

    for subnode in self.getElementNodeList(xupdate):
      sub_xupdate = self.getSubObjectXupdate(subnode)
      selection_name = ''
      if subnode.nodeName in self.XUPDATE_INSERT_OR_ADD:
        conflict_list += conduit.addNode(xml=sub_xupdate,object=object, \
                                         force=force, **kw)
      elif subnode.nodeName in self.XUPDATE_DEL:
        conflict_list += conduit.deleteNode(xml=sub_xupdate, object=object, \
                                         force=force, **kw)
      elif subnode.nodeName in self.XUPDATE_UPDATE:
        conflict_list += conduit.updateNode(xml=sub_xupdate, object=object, \
                                         force=force, **kw)
      #elif subnode.nodeName in self.XUPDATE_INSERT:
      #  conflict_list += conduit.addNode(xml=subnode, object=object, force=force, **kw)

    return conflict_list

Sebastien Robin's avatar
Sebastien Robin committed
879 880 881 882 883
  def isWorkflowActionAddable(self, object=None,status=None,wf_tool=None,xml=None):
    """
    Some checking in order to check if we should add the workfow or not
    """
    conflict_list = []
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
    if status.has_key('action'):
      action_name = status['action']
      authorized = 0
      authorized_actions = wf_tool.getActionsFor(object)
      LOG('isWorkflowActionAddable, status:',0,status)
      LOG('isWorkflowActionAddable, authorized_actions:',0,authorized_actions)
      for action in authorized_actions:
        if action['id']==action_name:
          authorized = 1
      if not authorized:
        string_io = StringIO()
        PrettyPrint(xml,stream=string_io)
        conflict = Conflict(object_path=object.getPhysicalPath(),
                            keyword=self.history_tag)
        conflict.setXupdate(string_io.getvalue())
        conflict.setRemoteValue(status)
        conflict_list += [conflict]
Sebastien Robin's avatar
Sebastien Robin committed
901
    return conflict_list