Subscription.py 40.6 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 30 31
##############################################################################
#
# 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 Globals import PersistentMapping
from time import gmtime,strftime # for anchors
from SyncCode import SyncCode
32
from AccessControl import ClassSecurityInfo
Sebastien Robin's avatar
Sebastien Robin committed
33 34
from Products.CMFCore.utils import getToolByName
from Acquisition import Implicit, aq_base
Jean-Paul Smets's avatar
Jean-Paul Smets committed
35
from Products.ERP5Type.Core.Folder import Folder
36 37 38
from Products.ERP5Type.Base import Base
from Products.ERP5Type import Permissions
from Products.ERP5Type import PropertySheet
39
from XMLSyncUtils import XMLSyncUtils
Sebastien Robin's avatar
Sebastien Robin committed
40
from DateTime import DateTime
41
from zLOG import LOG, DEBUG, INFO
Jean-Paul Smets's avatar
Jean-Paul Smets committed
42 43 44

import md5

45
try:
46
    from base64 import b64encode, b64decode, b16encode, b16decode
47
except ImportError:
48 49
    from base64 import encodestring as b64encode, decodestring as b64decode, \
        encodestring as b16encode, decodestring as b16decode
50

51 52
#class Conflict(SyncCode, Implicit):
class Conflict(SyncCode, Base):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
53 54 55
  """
    object_path : the path of the obect
    keyword : an identifier of the conflict
56 57
    publisher_value : the value that we have locally
    subscriber_value : the value sent by the remote box
Jean-Paul Smets's avatar
Jean-Paul Smets committed
58 59

  """
60
  isIndexable = 0
61
  isPortalContent = 0 # Make sure RAD generated accessors at the class level
62

63 64
  def __init__(self, object_path=None, keyword=None, xupdate=None, 
      publisher_value=None, subscriber_value=None, subscriber=None):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
65 66
    self.object_path=object_path
    self.keyword = keyword
67 68 69
    self.setLocalValue(publisher_value)
    self.setRemoteValue(subscriber_value)
    self.subscriber = subscriber
70
    self.resetXupdate()
71
    self.copy_path = None
Jean-Paul Smets's avatar
Jean-Paul Smets committed
72 73 74

  def getObjectPath(self):
    """
75
    get the object path
Jean-Paul Smets's avatar
Jean-Paul Smets committed
76 77 78
    """
    return self.object_path

79
  def getPublisherValue(self):
80 81 82
    """
    get the domain
    """
83
    return self.publisher_value
84

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
  def getXupdateList(self):
    """
    get the xupdate wich gave an error
    """
    xupdate_list = []
    if len(self.xupdate)>0:
      for xupdate in self.xupdate:
        xupdate_list+= [xupdate]
    return xupdate_list

  def resetXupdate(self):
    """
    Reset the xupdate list
    """
    self.xupdate = PersistentMapping()

  def setXupdate(self, xupdate):
    """
    set the xupdate
    """
    if xupdate == None:
      self.resetXupdate()
    else:
      self.xupdate = self.getXupdateList() + [xupdate]

  def setXupdateList(self, xupdate):
    """
    set the xupdate
    """
    self.xupdate = xupdate

116 117 118 119 120
  def setLocalValue(self, value):
    """
    get the domain
    """
    try:
121
      self.publisher_value = value
122
    except TypeError: # It happens when we try to store StringIO
123
      self.publisher_value = None
124

125
  def getSubscriberValue(self):
126 127 128
    """
    get the domain
    """
129
    return self.subscriber_value
130 131 132 133 134 135

  def setRemoteValue(self, value):
    """
    get the domain
    """
    try:
136
      self.subscriber_value = value
137
    except TypeError: # It happens when we try to store StringIO
138
      self.subscriber_value = None
139

140
  def applyPublisherValue(self):
Sebastien Robin's avatar
Sebastien Robin committed
141 142 143 144
    """
      after a conflict resolution, we have decided
      to keep the local version of this object
    """
145
    p_sync = getToolByName(self, 'portal_synchronizations')
146
    p_sync.applyPublisherValue(self)
Sebastien Robin's avatar
Sebastien Robin committed
147

148 149 150 151 152
  def applyPublisherDocument(self):
    """
      after a conflict resolution, we have decided
      to keep the local version of this object
    """
153
    p_sync = getToolByName(self, 'portal_synchronizations')
154 155
    p_sync.applyPublisherDocument(self)

156 157 158 159 160
  def getPublisherDocument(self):
    """
      after a conflict resolution, we have decided
      to keep the local version of this object
    """
161
    p_sync = getToolByName(self, 'portal_synchronizations')
162 163 164 165 166 167 168
    return p_sync.getPublisherDocument(self)

  def getPublisherDocumentPath(self):
    """
      after a conflict resolution, we have decided
      to keep the local version of this object
    """
169
    p_sync = getToolByName(self, 'portal_synchronizations')
170 171 172 173 174 175 176
    return p_sync.getPublisherDocumentPath(self)

  def getSubscriberDocument(self):
    """
      after a conflict resolution, we have decided
      to keep the local version of this object
    """
177
    p_sync = getToolByName(self, 'portal_synchronizations')
178 179 180 181 182 183 184
    return p_sync.getSubscriberDocument(self)

  def getSubscriberDocumentPath(self):
    """
      after a conflict resolution, we have decided
      to keep the local version of this object
    """
185
    p_sync = getToolByName(self, 'portal_synchronizations')
186
    return p_sync.getSubscriberDocumentPath(self)
187

188
  def applySubscriberDocument(self):
189 190 191 192
    """
      after a conflict resolution, we have decided
      to keep the local version of this object
    """
193
    p_sync = getToolByName(self, 'portal_synchronizations')
194 195
    p_sync.applySubscriberDocument(self)

196
  def applySubscriberValue(self, object=None):
Sebastien Robin's avatar
Sebastien Robin committed
197 198 199
    """
    get the domain
    """
200
    p_sync = getToolByName(self, 'portal_synchronizations')
201
    p_sync.applySubscriberValue(self, object=object)
Sebastien Robin's avatar
Sebastien Robin committed
202

203
  def setSubscriber(self, subscriber):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
204 205 206
    """
    set the domain
    """
207
    self.subscriber = subscriber
Jean-Paul Smets's avatar
Jean-Paul Smets committed
208

209
  def getSubscriber(self):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
210 211 212
    """
    get the domain
    """
213
    return self.subscriber
Jean-Paul Smets's avatar
Jean-Paul Smets committed
214

215 216 217 218 219 220
  def getKeyword(self):
    """
    get the domain
    """
    return self.keyword

221
  def getPropertyId(self):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
222
    """
223
    get the property id
Jean-Paul Smets's avatar
Jean-Paul Smets committed
224
    """
225
    return self.keyword
Jean-Paul Smets's avatar
Jean-Paul Smets committed
226

227 228 229 230 231 232
  def getCopyPath(self):
    """
    Get the path of the copy, or None if none has been made
    """
    copy_path = self.copy_path
    return copy_path
233

234 235 236 237
  def setCopyPath(self, path):
    """
    """
    self.copy_path = path
238

239
class Signature(Folder, SyncCode):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
240 241 242 243 244
  """
    status -- SENT, CONFLICT...
    md5_object -- An MD5 value of a given document
    #uid -- The UID of the document
    id -- the ID of the document
245
    gid -- the global id of the document
Jean-Paul Smets's avatar
Jean-Paul Smets committed
246 247 248 249
    rid -- the uid of the document on the remote database,
        only needed on the server.
    xml -- the xml of the object at the time where it was synchronized
  """
250
  isIndexable = 0
251
  isPortalContent = 0 # Make sure RAD generated accessors at the class level
252

Jean-Paul Smets's avatar
Jean-Paul Smets committed
253
  # Constructor
254 255 256 257 258 259
  def __init__(self,
               id=None,
               rid=None,
               status=None,
               xml_string=None,
               object=None):
260 261
    if object is not None:
      self.setPath(object.getPhysicalPath())
262
      self.setObjectId(object.getId())
Nicolas Delaby's avatar
Nicolas Delaby committed
263 264
    else:
      self.setPath(None)
265
    self.setId(id)
266 267
    self.setGid(id)
    self.setRid(rid)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
268 269 270 271 272 273
    self.status = status
    self.setXML(xml_string)
    self.partial_xml = None
    self.action = None
    self.setTempXML(None)
    self.resetConflictList()
274
    self.md5_string = None
Jean-Paul Smets's avatar
Jean-Paul Smets committed
275
    self.force = 0
276 277
    self.setSubscriberXupdate(None)
    self.setPublisherXupdate(None)
278
    Folder.__init__(self,id)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
279 280 281 282 283 284 285 286 287 288 289 290

  def setStatus(self, status):
    """
      set the Status (see SyncCode for numbers)
    """
    self.status = status
    if status == self.SYNCHRONIZED:
      temp_xml = self.getTempXML()
      self.setForce(0)
      if temp_xml is not None:
        # This happens when we have sent the xml
        # and we just get the confirmation
291
        self.setXML(temp_xml)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
292
      self.setTempXML(None)
293
      self.setPartialXML(None)
294
      self.setSubscriberXupdate(None)
Sebastien Robin's avatar
Sebastien Robin committed
295
      self.setPublisherXupdate(None)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
296 297
      if len(self.getConflictList())>0:
        self.resetConflictList()
Sebastien Robin's avatar
Sebastien Robin committed
298 299 300
      # XXX This may be a problem, if the document is changed
      # during a synchronization
      self.setLastSynchronizationDate(DateTime())
301
      self.getParentValue().removeRemainingObjectPath(self.getPath())
302 303 304
    if status == self.NOT_SYNCHRONIZED:
      self.setTempXML(None)
      self.setPartialXML(None)
305
    elif status in (self.PUB_CONFLICT_MERGE, self.SENT):
306 307
      # We have a solution for the conflict, don't need to keep the list
      self.resetConflictList()
Jean-Paul Smets's avatar
Jean-Paul Smets committed
308 309 310 311 312 313 314

  def getStatus(self):
    """
      get the Status (see SyncCode for numbers)
    """
    return self.status

315 316 317 318
  def getPath(self):
    """
      get the force value (if we need to force update or not)
    """
319
    return getattr(self, 'path', None)
320 321 322 323 324 325 326

  def setPath(self, path):
    """
      set the force value (if we need to force update or not)
    """
    self.path = path

Jean-Paul Smets's avatar
Jean-Paul Smets committed
327 328 329 330 331 332 333 334 335 336 337 338
  def getForce(self):
    """
      get the force value (if we need to force update or not)
    """
    return self.force

  def setForce(self, force):
    """
      set the force value (if we need to force update or not)
    """
    self.force = force

Sebastien Robin's avatar
Sebastien Robin committed
339 340 341 342 343
  def getLastModificationDate(self):
    """
      get the last modfication date, so that we don't always
      check the xml
    """
344
    return getattr(self, 'modification_date', None)
Sebastien Robin's avatar
Sebastien Robin committed
345 346 347 348 349 350

  def setLastModificationDate(self,value):
    """
      set the last modfication date, so that we don't always
      check the xml
    """
351
    setattr(self, 'modification_date', value)
Sebastien Robin's avatar
Sebastien Robin committed
352 353 354 355 356 357

  def getLastSynchronizationDate(self):
    """
      get the last modfication date, so that we don't always
      check the xml
    """
358
    return getattr(self, 'synchronization_date', None)
Sebastien Robin's avatar
Sebastien Robin committed
359 360 361 362 363 364

  def setLastSynchronizationDate(self,value):
    """
      set the last modfication date, so that we don't always
      check the xml
    """
365
    setattr(self, 'synchronization_date', value)
Sebastien Robin's avatar
Sebastien Robin committed
366

Jean-Paul Smets's avatar
Jean-Paul Smets committed
367 368 369 370 371 372 373
  def setXML(self, xml):
    """
      set the XML corresponding to the object
    """
    self.xml = xml
    if self.xml != None:
      self.setTempXML(None) # We make sure that the xml will not be erased
374
      self.setMD5(xml)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
375 376 377

  def getXML(self):
    """
378
      get the XML corresponding to the object
Jean-Paul Smets's avatar
Jean-Paul Smets committed
379
    """
380
    xml =  getattr(self, 'xml', None)
381 382 383
    if xml == '':
      xml = None
    return xml
Jean-Paul Smets's avatar
Jean-Paul Smets committed
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398

  def setTempXML(self, xml):
    """
      This is the xml temporarily saved, it will
      be stored with setXML when we will receive
      the confirmation of synchronization
    """
    self.temp_xml = xml

  def getTempXML(self):
    """
      get the temp xml
    """
    return self.temp_xml

399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
  def setSubscriberXupdate(self, xupdate):
    """
    set the full temp xupdate
    """
    self.subscriber_xupdate = xupdate

  def getSubscriberXupdate(self):
    """
    get the full temp xupdate
    """
    return self.subscriber_xupdate

  def setPublisherXupdate(self, xupdate):
    """
    set the full temp xupdate
    """
    self.publisher_xupdate = xupdate

  def getPublisherXupdate(self):
    """
    get the full temp xupdate
    """
    return self.publisher_xupdate

Jean-Paul Smets's avatar
Jean-Paul Smets committed
423 424 425 426 427 428 429 430 431 432
  def setMD5(self, xml):
    """
      set the MD5 object of this signature
    """
    self.md5_string = md5.new(xml).digest()

  def getMD5(self):
    """
      get the MD5 object of this signature
    """
433
    return self.md5_string
Jean-Paul Smets's avatar
Jean-Paul Smets committed
434 435 436 437 438 439 440 441

  def checkMD5(self, xml_string):
    """
    check if the given md5_object returns the same things as
    the one stored in this signature, this is very usefull
    if we want to know if an objects has changed or not
    Returns 1 if MD5 are equals, else it returns 0
    """
442
    return ((md5.new(xml_string).digest()) == self.getMD5())
Jean-Paul Smets's avatar
Jean-Paul Smets committed
443 444 445 446 447

  def setRid(self, rid):
    """
      set the rid
    """
448
    if rid is type(u'a'):
449
      rid = rid.encode('utf-8')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
450 451 452 453 454 455
    self.rid = rid

  def getRid(self):
    """
      get the rid
    """
456
    return getattr(self, 'rid', None)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
457 458 459 460 461

  def setId(self, id):
    """
      set the id
    """
462
    if id is type(u'a'):
463
      id = id.encode('utf-8')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
464 465 466 467 468 469 470 471
    self.id = id

  def getId(self):
    """
      get the id
    """
    return self.id

472 473
  def setGid(self, gid):
    """
474
      set the gid
475
    """
476
    if gid is type(u'a'):
477
      gid = gid.encode('utf-8')
478 479 480 481
    self.gid = gid

  def getGid(self):
    """
482
      get the gid
483 484 485
    """
    return self.gid

486 487 488 489
  def setObjectId(self, id):
    """
      set the id of the object associated to this signature
    """
490
    if id is type(u'a'):
491 492 493 494 495 496 497 498 499
      id = id.encode('utf-8')
    self.object_id = id

  def getObjectId(self):
    """
      get the id of the object associated to this signature
    """
    return getattr(self, 'object_id', None)

Jean-Paul Smets's avatar
Jean-Paul Smets committed
500 501 502 503 504
  def setPartialXML(self, xml):
    """
    Set the partial string we will have to
    deliver in the future
    """
505 506
    if type(xml) is type(u'a'):
      xml = xml.encode('utf-8')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
507 508 509 510 511 512 513
    self.partial_xml = xml

  def getPartialXML(self):
    """
    Set the partial string we will have to
    deliver in the future
    """
514
    #LOG('Subscriber.getPartialXML', DEBUG, 'partial_xml: %s' % str(self.partial_xml))
515 516
    if self.partial_xml is not None:
      self.partial_xml = self.partial_xml.replace('@-@@-@','--') # need to put back '--'
Jean-Paul Smets's avatar
Jean-Paul Smets committed
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
    return self.partial_xml

  def getAction(self):
    """
    Return the actual action for a partial synchronization
    """
    return self.action

  def setAction(self, action):
    """
    Return the actual action for a partial synchronization
    """
    self.action = action

  def getConflictList(self):
    """
    Return the actual action for a partial synchronization
    """
    conflict_list = []
    if len(self.conflict_list)>0:
      for conflict in self.conflict_list:
        conflict_list += [conflict]
    return conflict_list

  def resetConflictList(self):
    """
    Return the actual action for a partial synchronization
    """
    self.conflict_list = PersistentMapping()

  def setConflictList(self, conflict_list):
    """
    Return the actual action for a partial synchronization
    """
Sebastien Robin's avatar
Sebastien Robin committed
551
    if conflict_list is None or conflict_list==[]:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
552 553
      self.resetConflictList()
    else:
Sebastien Robin's avatar
Sebastien Robin committed
554 555 556 557 558 559
      self.conflict_list = conflict_list

  def delConflict(self, conflict):
    """
    Return the actual action for a partial synchronization
    """
560
    LOG('delConflict, conflict', DEBUG, conflict)
Sebastien Robin's avatar
Sebastien Robin committed
561 562
    conflict_list = []
    for c in self.getConflictList():
Nicolas Delaby's avatar
Nicolas Delaby committed
563
      #LOG('delConflict, c==conflict',0,c==aq_base(conflict))
Sebastien Robin's avatar
Sebastien Robin committed
564 565 566 567 568 569
      if c != aq_base(conflict):
        conflict_list += [c]
    if conflict_list != []:
      self.setConflictList(conflict_list)
    else:
      self.resetConflictList()
Jean-Paul Smets's avatar
Jean-Paul Smets committed
570

571 572 573 574
  def getObject(self):
    """
    Returns the object corresponding to this signature
    """
Nicolas Delaby's avatar
Nicolas Delaby committed
575
    return self.getParentValue().getObjectFromGid(self.getObjectId())
576

577 578
def addSubscription( self, id, title='', REQUEST=None ):
    """
579
    Add a new Subscribption
580 581 582 583 584 585 586 587
    """
    o = Subscription( id ,'','','','','','')
    self._setObject( id, o )
    if REQUEST is not None:
        return self.manage_main(self, REQUEST, update_menu=1)
    return o

#class Subscription(SyncCode, Implicit):
588
#class Subscription(Folder, SyncCode, Implicit, Folder, Impli):
589
class Subscription(Folder, XMLSyncUtils):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
  """
    Subscription hold the definition of a master ODB
    from/to which a selection of objects will be synchronised

    Subscription defined by::

    publication_url -- a URI to a publication

    subsribtion_url -- URL of ourselves

    destination_path -- the place where objects are stored

    query   -- a query which defines a local set of documents which
           are going to be synchronised

    xml_mapping -- a PageTemplate to map documents to XML

607 608
    gpg_key -- the name of a gpg key to use

Jean-Paul Smets's avatar
Jean-Paul Smets committed
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
    Subscription also holds private data to manage
    the synchronisation. We choose to keep an MD5 value for
    all documents which belong to the synchronisation process::

    signatures -- a dictionnary which contains the signature
           of documents at the time they were synchronized

    session_id -- it defines the id of the session
         with the server.

    last_anchor - it defines the id of the last synchronisation

    next_anchor - it defines the id of the current synchronisation

  """

625
  meta_type='ERP5 Subscription'
626
  portal_type='SyncML Subscription' # may be useful in the future...
627 628 629
  isPortalContent = 1
  isRADContent = 1
  icon = None
630
  isIndexable = 0
631
  user = None
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648

  # Declarative properties
  property_sheets = ( PropertySheet.Base
                    , PropertySheet.SimpleItem )

  allowed_types = ( 'Signatures',)

  # Declarative constructors
  constructors =   (addSubscription,)

  # Declarative security
  security = ClassSecurityInfo()
  security.declareProtected(Permissions.ManagePortal,
                            'manage_editProperties',
                            'manage_changeProperties',
                            'manage_propertiesForm',
                              )
Jean-Paul Smets's avatar
Jean-Paul Smets committed
649 650

  # Constructor
651 652
  def __init__(self, id, title, publication_url, subscription_url,
      destination_path, source_uri, target_uri, query, xml_mapping,
653
      conduit, gpg_key, id_generator, media_type, login,
654
      password, activity_enabled, alert_code, synchronize_with_erp5_sites,
655
      sync_content_type):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
656 657 658 659 660 661
    """
      We need to create a dictionnary of
      signatures of documents which belong to the synchronisation
      process
    """
    self.id = id
662
    self.setAlertCode(alert_code)
663
    self.setActivityEnabled(activity_enabled)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
664 665 666
    self.publication_url = (publication_url)
    self.subscription_url = str(subscription_url)
    self.destination_path = str(destination_path)
667 668
    self.setSourceURI(source_uri)
    self.setTargetURI(target_uri)
669
    self.setQuery(query)
670
    self.setXMLMapping(xml_mapping)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
671 672
    self.anchor = None
    self.session_id = 0
673
    #self.signatures = PersistentMapping()
Jean-Paul Smets's avatar
Jean-Paul Smets committed
674 675
    self.last_anchor = '00000000T000000Z'
    self.next_anchor = '00000000T000000Z'
676
    self.setMediaType(media_type)
677
    self.login = login
678
    self.password=password
Jean-Paul Smets's avatar
Jean-Paul Smets committed
679
    self.domain_type = self.SUB
680
    self.gpg_key = gpg_key
681
    self.setSynchronizationIdGenerator(id_generator)
682
    self.setConduit(conduit)
683 684
    Folder.__init__(self, id)
    self.title = title
685 686
    self.setSyncContentType(sync_content_type)
    self.setSynchronizeWithERP5Sites(synchronize_with_erp5_sites)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
687 688
    #self.signatures = PersitentMapping()

689
  def getAlertCodeList(self):
690
    return self.CODE_LIST
691 692 693 694 695 696 697 698 699 700

  def getAlertCode(self):
    return getattr(self, 'alert_code', 200)

  def setAlertCode(self, value):
    self.alert_code = int(value)

  def isOneWayFromServer(self):
    return self.getDomainType() == self.SUB and self.getAlertCode() == self.ONE_WAY_FROM_SERVER

701 702 703 704 705 706 707 708 709 710 711 712
  def getActivityEnabled(self):
    """
    return true if we are using activity, false otherwise
    """
    return getattr(self, 'activity_enabled', None)

  def setActivityEnabled(self, activity_enabled):
    """
    set if we are using activity or not
    """
    self.activity_enabled = activity_enabled

713 714 715 716 717 718 719 720 721 722 723
  def getTitle(self):
    """
    getter for title
    """
    return getattr(self,'title',None)

  def setTitle(self, value):
    """
    setter for title
    """
    self.title = value
724

725 726 727 728 729 730 731 732
  def setSourceURI(self, value):
    """
    setter for source_uri
    """
    self.source_uri = value

  def getSourceURI(self):
    """
733
    getter for the source_uri (the local path of the subscription data base)
734 735 736 737 738 739 740 741
    """
    return getattr(self, 'source_uri', None)

  def setTargetURI(self, value):
    """
    setter for target_uri
    """
    self.target_uri = value
742

743
  def getTargetURI(self):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
744
    """
745 746
    getter for the target_uri (the distant Publication data base we want to 
    synchronize with)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
747
    """
748
    return getattr(self, 'target_uri', None)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
749

750 751 752 753
  def setSyncContentType(self, sync_content_type):
    """
    content type used by the subscriber
    """
754
    self.sync_content_type = sync_content_type
755 756 757 758 759 760 761 762 763 764
    # the varible name is sync_content_type instead of content_type because
    # content_type seems to be a function name already used


  def getSyncContentType(self):
    """
    getter of the subscriber sync_content_type
    """
    return getattr(self, 'sync_content_type', 'application/vnd.syncml+xml')

Jean-Paul Smets's avatar
Jean-Paul Smets committed
765 766 767
  def getSynchronizationType(self, default=None):
    """
    """
768 769
    # XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    # XXX for debugging only, to be removed
Nicolas Delaby's avatar
Nicolas Delaby committed
770
    #dict_sign = {}
771
    #for o in self.getSignatureList():
Nicolas Delaby's avatar
Nicolas Delaby committed
772
      #dict_sign[o.getId()] = o.getStatus()
773
    # LOG('getSignature', DEBUG, 'signatures_status: %s' % str(dict_sign))
774
    # XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Jean-Paul Smets's avatar
Jean-Paul Smets committed
775
    code = self.SLOW_SYNC
776
    if len(self.getSignatureList()) > 0:
777
      code = self.getAlertCode()
Jean-Paul Smets's avatar
Jean-Paul Smets committed
778 779
    if default is not None:
      code = default
780
    #LOG('Subscription', DEBUG, 'getSynchronizationType: %s' % code)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
781 782
    return code

783 784
  def setXMLMapping(self, value):
    """
785
    this the name of the method used in order to set the xml
786 787 788 789 790
    """
    if value == '':
      value = None
    self.xml_mapping = value

791 792 793 794 795 796 797 798 799 800 801 802 803
  def setSynchronizeWithERP5Sites(self, synchronize_with_erp5_sites):
    """
    if the synchronisation is made with another ERP5 site, 
    synchronize_with_erp5_sites is True, False in other case
    XXX in the future, the method used to sendHttpResponse will be the same
    in all cases, so this method will be useless
    """
    self.synchronize_with_erp5_sites = synchronize_with_erp5_sites

  def getSynchronizeWithERP5Sites(self):
    """
    return True if the synchronisation is between two erp5 sites
    """
804
    return getattr(self, 'synchronize_with_erp5_sites', True)
805

806 807 808 809 810
  def checkCorrectRemoteSessionId(self, session_id):
    """
    We will see if the last session id was the same
    wich means that the same message was sent again

811
    return True if the session id was not seen, False if already seen
812
    """
813
    last_session_id = getattr(self, 'last_session_id', None)
814
    if last_session_id == session_id:
815
      return False 
816
    self.last_session_id = session_id
817
    return True
818

Sebastien Robin's avatar
Sebastien Robin committed
819 820 821 822 823
  def checkCorrectRemoteMessageId(self, message_id):
    """
    We will see if the last message id was the same
    wich means that the same message was sent again

824
    return True if the message id was not seen, False if already seen
Sebastien Robin's avatar
Sebastien Robin committed
825 826
    """
    last_message_id = getattr(self,'last_message_id',None)
827 828
    LOG('checkCorrectRemoteMessageId  last_message_id = ', DEBUG, last_message_id)
    LOG('checkCorrectRemoteMessageId  message_id = ', DEBUG, message_id)
Sebastien Robin's avatar
Sebastien Robin committed
829
    if last_message_id == message_id:
830
      return False
Sebastien Robin's avatar
Sebastien Robin committed
831
    self.last_message_id = message_id
832
    return True
Sebastien Robin's avatar
Sebastien Robin committed
833

834
  def initLastMessageId(self, last_message_id=0):
Sebastien Robin's avatar
Sebastien Robin committed
835 836 837
    """
    set the last message id to 0
    """
838
    self.last_message_id = last_message_id
839 840 841 842 843

  def getLastSentMessage(self):
    """
    This is the getter for the last message we have sent
    """
844
    return getattr(self, 'last_sent_message', '')
845 846 847 848 849 850 851

  def setLastSentMessage(self,xml):
    """
    This is the setter for the last message we have sent
    """
    self.last_sent_message = xml

852
  def getDomainType(self):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
853
    """
854
      return the ID
Jean-Paul Smets's avatar
Jean-Paul Smets committed
855
    """
856
    return self.domain_type
Jean-Paul Smets's avatar
Jean-Paul Smets committed
857 858 859 860 861 862 863 864 865 866 867 868 869

  def getId(self):
    """
      return the ID
    """
    return self.id

  def setId(self, id):
    """
      set the ID
    """
    self.id = id

870 871 872 873 874 875 876 877 878 879
  def setConduit(self, value):
    """
      set the Conduit
    """
    self.conduit = value

  def getConduit(self):
    """
      get the Conduit
    """
880
    return getattr(self, 'conduit', None)
881

Jean-Paul Smets's avatar
Jean-Paul Smets committed
882 883 884 885 886 887
  def getQuery(self):
    """
      return the query
    """
    return self.query

888 889 890 891
  def getGPGKey(self):
    """
      return the gnupg key name
    """
892
    return getattr(self, 'gpg_key', '')
893

894 895 896 897 898 899
  def setGPGKey(self, value):
    """
      setter for the gnupg key name
    """
    self.gpg_key = value

Jean-Paul Smets's avatar
Jean-Paul Smets committed
900 901 902 903
  def setQuery(self, query):
    """
      set the query
    """
904 905
    if query == '':
      query = None
Jean-Paul Smets's avatar
Jean-Paul Smets committed
906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
    self.query = query

  def getPublicationUrl(self):
    """
      return the publication url
    """
    return self.publication_url

  def getLocalUrl(self):
    """
      return the publication url
    """
    return self.publication_url

  def setPublicationUrl(self, publication_url):
    """
922
      set the publication url
Jean-Paul Smets's avatar
Jean-Paul Smets committed
923 924 925
    """
    self.publication_url = publication_url

926
  def getXMLMapping(self, force=0):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
927 928 929
    """
      return the xml mapping
    """
930 931 932
    if self.isOneWayFromServer() and force == 0:
      return None
    xml_mapping = getattr(self, 'xml_mapping', None)
933
    return xml_mapping
Jean-Paul Smets's avatar
Jean-Paul Smets committed
934

935
  def getXMLFromObject(self, object, force=0):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
936 937 938
    """
      return the xml mapping
    """
939
    xml_mapping = self.getXMLMapping(force=force)
940 941
    xml = ''
    if xml_mapping is not None:
942
      func = getattr(object, xml_mapping, None)
943 944 945
      if func is not None:
        xml = func()
    return xml
Jean-Paul Smets's avatar
Jean-Paul Smets committed
946

947
  def getMediaType(self):
948
    """
949 950
    This method return the type of media used in this session,
    for example, it could be "text/vcard" or "xml/text",...
951
    """
952
    return getattr(self, 'media_type', self.MEDIA_TYPE['TEXT_XML'])
953

954
  def setMediaType(self, media_type):
955
    """
956
    set the type of media used
957
    """
958
    if media_type in (None, ''):
959 960
      media_type = self.MEDIA_TYPE['TEXT_XML']
    self.media_type = media_type
961

962 963 964 965 966 967 968 969 970 971
  def getLogin(self):
    """
    This method return the login of this subscription
    """
    return getattr(self, 'login', '')

  def setLogin(self, new_login):
    """
    set the login at new_login
    """
972
    self.login = new_login
973 974 975 976 977 978 979 980 981 982 983

  def getPassword(self):
    """
    This method return the password of this subscription
    """
    return getattr(self, 'password', '')

  def setPassword(self, new_password):
    """
    set the password at new_password
    """
984
    self.password = new_password
985

Fabien Morin's avatar
Fabien Morin committed
986 987 988 989 990 991 992 993 994 995 996 997
  def getZopeUser(self):
    """
    This method return the zope user who begin the synchronization session
    """
    return getattr(self, 'zope_user_name', None)

  def setZopeUser(self, user_name):
    """
    This method set the zope user_name
    """
    self.zope_user_name = user_name

998 999 1000 1001
  def getAuthenticationFormat(self):
    """
      return the format of authentication
    """
1002
    return getattr(self, 'authentication_format', 'b64')
1003 1004 1005 1006 1007

  def getAuthenticationType(self):
    """
      return the type of authentication
    """
1008
    return getattr(self, 'authentication_type', 'syncml:auth-basic')
1009 1010 1011 1012 1013

  def setAuthenticationFormat(self, authentication_format):
    """
      set the format of authentication
    """
1014 1015 1016 1017
    if authentication_format in (None, ''):
      self.authentication_format = 'b64'
    else:
      self.authentication_format=authentication_format
1018 1019 1020 1021 1022

  def setAuthenticationType(self, authentication_type):
    """
      set the type of authentication
    """
1023 1024 1025 1026
    if authentication_type in (None, ''):
      self.authentication_type = 'syncml:auth-basic'
    else:
      self.authentication_type = authentication_type
1027

1028 1029 1030 1031 1032
  def getGidFromObject(self, object):
    """
    """
    o_base = aq_base(object)
    o_gid = None
1033 1034 1035 1036 1037
    conduit_name = self.getConduit()
    conduit = self.getConduitByName(conduit_name)
    gid_gen = getattr(conduit, 'getGidFromObject', None)
    LOG('getGidFromObject, Conduit :', DEBUG, conduit_name)
    LOG('getGidFromObject, gid_gen:', DEBUG, gid_gen)
1038
    if callable(gid_gen):
1039
      o_gid = gid_gen(object)
1040
    else:
1041
      raise ValueError, "The conduit "+conduit_name+"seems to not have a \
1042 1043 1044 1045 1046 1047 1048 1049
          getGidFromObject method and it must"
#    elif getattr(o_base, gid_gen, None) is not None:
#      generator = getattr(object, gid_gen)
#      o_gid = generator() # XXX - used to be o_gid = generator(object=object) which is redundant
#    elif gid_gen is not None:
#      # It might be a script python
#      generator = getattr(object,gid_gen)
#      o_gid = generator() # XXX - used to be o_gid = generator(object=object) which is redundant
1050
    o_gid = b16encode(o_gid)
1051
    LOG('getGidFromObject returning', DEBUG, o_gid)
1052 1053
    return o_gid

1054 1055 1056 1057 1058
  def getObjectFromGid(self, gid):
    """
    This tries to get the object with the given gid
    This uses the query if it exist
    """
1059
    if len(gid)%2 != 0:
Fabien Morin's avatar
Fabien Morin committed
1060
    #something encode in base 16 is always a even number of number
1061 1062
    #if not, b16decode will failed
      return None
1063
    signature = self.getSignatureFromGid(gid)
1064 1065 1066
    # First look if we do already have the mapping between
    # the id and the gid
    destination = self.getDestination()
1067
    if signature is not None and signature.getPath() is not None:
1068 1069
      o = None
      try:
1070
        o = destination.getPortalObject().restrictedTraverse(signature.getPath())
1071
      except (AttributeError, KeyError, TypeError):
1072
        pass
Nicolas Delaby's avatar
Nicolas Delaby committed
1073 1074
      o_id = signature.getObjectId()
      #try with id param too, because gid is not catalogged
1075
      object_list = self.getObjectList(gid = b16decode(gid), id = o_id)
1076
      LOG('getObjectFromGid :', DEBUG, 'object_list=%s, gid=%s, o_id=%s' % (object_list, gid, o_id))
1077 1078
      if o is not None and o in object_list:
        return o
1079
    #LOG('entering in the slow loop of getObjectFromGid !!!',0,'')
1080
    object_list = self.getObjectList(gid = b16decode(gid))
1081
    LOG('getObjectFromGid :', DEBUG, 'object_list slow loop=%s, gid=%s' % (object_list, gid))
1082
    for o in object_list:
1083 1084 1085
      o_gid = self.getGidFromObject(o)
      if o_gid == gid:
        return o
1086
    LOG('getObjectFromGid', DEBUG, 'returning None')
1087 1088 1089 1090 1091 1092
    return None

  def getObjectFromId(self, id):
    """
    return the object corresponding to the id
    """
1093
    object_list = self.getObjectList(id=id)
1094
    o = None
1095 1096
    for object in object_list:
      if object.getId() == id:
1097 1098 1099
        o = object
        break
    return o
1100

1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
  def getObjectFromRid(self, rid):
    """
    return the object corresponding to the id
    """
    signature = self.getSignatureFromRid(rid)
    destination = self.getDestination()
    o = None
    if signature is not None and signature.getPath() is not None:
      try:
        o = destination.getPortalObject().restrictedTraverse(signature.getPath())
      except:
        pass
1113
    return o
1114

1115
  def getObjectList(self, **kw):
1116 1117 1118 1119 1120 1121 1122
    """
    This returns the list of sub-object corresponding
    to the query
    """
    destination = self.getDestination()
    query = self.getQuery()
    query_list = []
1123
    if query is not None and isinstance(query, str):
1124
      query_method = getattr(destination, query, None)
1125
      if query_method is not None:
1126 1127
        query_list = query_method(**kw)
    elif callable(query): # used in the test
1128
      query_list = query(destination)
1129 1130
    else:
      LOG('This Subscriber %s provide no Query with id :' % (self.getTitle()), INFO, query)
1131 1132
    return [x for x in query_list
              if not getattr(x,'_conflict_resolution',False)]
1133

1134
  def generateNewIdWithGenerator(self, object=None, gid=None):
1135 1136 1137
    """
    This tries to generate a new Id
    """
1138
    id_generator = self.getSynchronizationIdGenerator()
1139
    if id_generator is not None:
1140
      o_base = aq_base(object)
1141 1142
      new_id = None
      if callable(id_generator):
1143
        new_id = id_generator(object, gid=gid)
1144
      elif getattr(o_base, id_generator, None) is not None:
1145
        generator = getattr(object, id_generator)
1146
        new_id = generator()
1147
      else: 
Nicolas Delaby's avatar
Nicolas Delaby committed
1148
        # This is probably a python script
1149
        generator = getattr(object, id_generator)
1150 1151
        new_id = generator(object=object, gid=gid)
      LOG('generateNewId, new_id: ', DEBUG, new_id)
1152
      return new_id
1153 1154
    return None

1155
  def setSynchronizationIdGenerator(self, method):
1156 1157 1158 1159
    """
    This set the method name wich allows to generate
    a new id
    """
1160
    if method in ('', 'None'):
1161
      method = None
1162
    self.synchronization_id_generator = method
1163

1164
  def getSynchronizationIdGenerator(self):
1165 1166 1167
    """
    This get the method name wich allows to generate a new id
    """
1168
    return getattr(self, 'synchronization_id_generator', None)
1169

Jean-Paul Smets's avatar
Jean-Paul Smets committed
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
  def getSubscriptionUrl(self):
    """
      return the subscription url
    """
    return self.subscription_url

  def setSubscriptionUrl(self, subscription_url):
    """
      set the subscription url
    """
    self.subscription_url = subscription_url

  def getDestinationPath(self):
    """
      return the destination path
    """
    return self.destination_path

1188 1189 1190 1191 1192 1193
  def getDestination(self):
    """
      return the destination object itself
    """
    return self.unrestrictedTraverse(self.getDestinationPath())

Jean-Paul Smets's avatar
Jean-Paul Smets committed
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
  def setDestinationPath(self, destination_path):
    """
      set the destination path
    """
    self.destination_path = destination_path

  def getSubscription(self):
    """
      return the current subscription
    """
    return self
1205

Sebastien Robin's avatar
Sebastien Robin committed
1206 1207 1208 1209 1210
  def setSessionId(self, session_id):
    """
      set the session id
    """
    self.session_id = session_id
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1211 1212 1213 1214 1215

  def getSessionId(self):
    """
      return the session id
    """
Sebastien Robin's avatar
Sebastien Robin committed
1216 1217 1218 1219 1220 1221 1222
    #self.session_id += 1 #to be commented
    return self.session_id

  def incrementSessionId(self):
    """
      increment and return the session id
    """
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1223
    self.session_id += 1
Sebastien Robin's avatar
Sebastien Robin committed
1224
    self.resetMessageId() # for a new session, the message Id must be reset
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1225 1226
    return self.session_id

Sebastien Robin's avatar
Sebastien Robin committed
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
  def incrementMessageId(self):
    """
      return the message id
    """
    value = getattr(self, 'message_id', 0)
    self.message_id = value +1
    return self.message_id

  def getMessageId(self):
    """
      increment and return the message id
    """
    return self.message_id

  def resetMessageId(self):
    """
      set the message id to 0
    """
    self.message_id = 0
1246

1247 1248 1249 1250 1251
  def setMessageId(self, message_id):
    """
      set the message id to message_id
    """
    self.message_id = message_id
Sebastien Robin's avatar
Sebastien Robin committed
1252

Jean-Paul Smets's avatar
Jean-Paul Smets committed
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
  def getLastAnchor(self):
    """
      return the id of the last synchronisation
    """
    return self.last_anchor

  def getNextAnchor(self):
    """
      return the id of the current synchronisation
    """
    return self.next_anchor

  def setLastAnchor(self, last_anchor):
    """
      set the value last anchor
    """
    self.last_anchor = last_anchor

  def setNextAnchor(self, next_anchor):
    """
      set the value next anchor
    """
    # We store the old next anchor as the new last one
    self.last_anchor = self.next_anchor
    self.next_anchor = next_anchor

  def NewAnchor(self):
    """
      set a new anchor
    """
    self.last_anchor = self.next_anchor
    self.next_anchor = strftime("%Y%m%dT%H%M%SZ", gmtime())

  def resetAnchors(self):
    """
      reset both last and next anchors
    """
    self.last_anchor = self.NULL_ANCHOR
    self.next_anchor = self.NULL_ANCHOR

  def addSignature(self, signature):
    """
      add a Signature to the subscription
    """
1297
    if self.getSignatureFromGid(signature.getGid()) is not None:
1298 1299
      self.delSignature(signature.getGid())
    self._setObject(signature.getGid(), aq_base(signature))
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1300

1301
  def delSignature(self, gid):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1302
    """
1303
      del a Signature of the subscription
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1304
    """
1305
    self._delObject(gid)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1306

1307
  def getSignatureFromObjectId(self, id):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1308
    """
1309
    return the signature corresponding to the id
1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322
    """
    o = None
    # XXX very slow
    for signature in self.getSignatureList():
      if id == signature.getObjectId():
        o = signature
        break
    return o

  def getSignatureFromGid(self, gid):
    """
    return the signature corresponding to the gid
    """
Nicolas Delaby's avatar
Nicolas Delaby committed
1323
    return getattr(self, gid, None)
1324 1325 1326 1327

  def getSignatureFromRid(self, rid):
    """
    return the signature corresponding to the rid
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1328
    """
1329
    o = None
1330 1331 1332 1333 1334
    # XXX very slow
    for signature in self.getSignatureList():
      if rid == signature.getRid():
        o = signature
        break
1335
    return o
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1336

1337 1338 1339 1340
  def getObjectIdList(self):
    """
    Returns the list of gids from signature
    """
1341
    return [s for s in self.getSignatureList() if s.getObjectId() is not None]
1342 1343 1344 1345 1346

  def getGidList(self):
    """
    Returns the list of gids from signature
    """
1347
    return [s.getGid() for s in self.getSignatureList() if s.getGid() is not None]
1348 1349 1350 1351 1352

  def getRidList(self):
    """
    Returns the list of rids from signature
    """
1353
    return [s.getRid() for s in self.getSignatureList() if s.getRid() is not None]
1354

Jean-Paul Smets's avatar
Jean-Paul Smets committed
1355 1356
  def getSignatureList(self):
    """
1357
      Returns the list of Signatures
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1358
    """
1359
    return self.objectValues()
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1360

1361
  def hasSignature(self, gid):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1362 1363 1364
    """
      Check if there's a signature with this uid
    """
1365
    return self.getSignatureFromGid(gid) is not None
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1366 1367 1368

  def resetAllSignatures(self):
    """
1369
      Reset all signatures in activities
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1370
    """
1371 1372 1373 1374 1375
    object_id_list = [id for id in self.getObjectIds()]
    object_list_len = len(object_id_list)
    for i in xrange(0, object_list_len, 100):
        current_id_list = object_id_list[i:i+100]
        self.activate().manage_delObjects(current_id_list)
1376

Jean-Paul Smets's avatar
Jean-Paul Smets committed
1377 1378 1379 1380 1381 1382
  def getConflictList(self):
    """
    Return the list of all conflicts from all signatures
    """
    conflict_list = []
    for signature in self.getSignatureList():
1383
      conflict_list.extend(signature.getConflictList())
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1384 1385
    return conflict_list

1386
  def getRemainingObjectPathList(self):
1387 1388 1389 1390
    """
    We should now wich objects should still
    synchronize
    """
1391
    return getattr(self, 'remaining_object_path_list', None)
1392

1393
  def setRemainingObjectPathList(self, value):
1394 1395 1396 1397
    """
    We should now wich objects should still
    synchronize
    """
1398
    setattr(self, 'remaining_object_path_list', value)
1399

1400
  def removeRemainingObjectPath(self, object_path):
1401 1402 1403 1404
    """
    We should now wich objects should still
    synchronize
    """
1405
    remaining_object_list = self.getRemainingObjectPathList()
1406 1407
    if remaining_object_list is not None:
      new_list = []
Nicolas Delaby's avatar
Nicolas Delaby committed
1408 1409 1410
      new_list.extend(remaining_object_list)
      while object_path in new_list:
        new_list.remove(object_path)
1411
      self.setRemainingObjectPathList(new_list)
1412

Jean-Paul Smets's avatar
Jean-Paul Smets committed
1413 1414 1415 1416
  def startSynchronization(self):
    """
    Set the status of every object as NOT_SYNCHRONIZED
    """
1417
    for s in self.getSignatureList():
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1418
      # Change the status only if we are not in a conflict mode
1419 1420 1421 1422 1423 1424
      if s.getStatus() not in (self.CONFLICT,
                               self.PUB_CONFLICT_MERGE,
                               self.PUB_CONFLICT_CLIENT_WIN):
        s.setStatus(self.NOT_SYNCHRONIZED)
        s.setPartialXML(None)
        s.setTempXML(None)
1425
    self.setRemainingObjectPathList(None)
1426

1427 1428 1429 1430 1431 1432

  def isAuthenticated(self):
    """
    return True if the subscriber is authenticated for this session, False 
    in other case
    """
1433 1434
    return getattr(self, 'is_authenticated', None)

1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
  def setAuthenticated(self, value):
    """
      set at True or False the value of is_authenticated is the subscriber
      is authenticated for this session or not
    """
    self.is_authenticated = value

  def encode(self, format, string_to_encode):
    """
      return the string_to_encode encoded with format format
    """
    if format in ('', None):
      return string_to_encode
    if format == 'b64':
      return b64encode(string_to_encode)
    #elif format is .... put here the other formats
    else:#if there is no format corresponding with format, raise an error
1452
      LOG('encode : unknown or not implemented format : ', INFO, format)
1453
      raise ValueError, "Sorry, the server ask for the format %s but it's unknow or not implemented" % format
1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465

  def decode(self, format, string_to_decode):
    """
      return the string_to_decode decoded with format format
    """
    string_to_decode = string_to_decode.encode('utf-8')
    if format in ('', None):
      return string_to_decode
    if format == 'b64':
      return b64decode(string_to_decode)
    #elif format is .... put here the other formats
    else:#if there is no format corresponding with format, raise an error
1466
      LOG('decode : unknown or not implemented format :', INFO, format)
1467 1468 1469 1470 1471 1472 1473
      raise ValueError, "Sorry, the format %s is unknow or not implemented" % format

  def isDecodeEncodeTheSame(self, string_encoded, string_decoded, format):
    """
      return True if the string_encoded is equal to string_decoded encoded 
      in format
    """
1474
    return self.encode(format, string_decoded) == string_encoded
1475 1476 1477 1478 1479

  def setUser(self, user):
    """
      save the user logged in to log him on each transaction
    """
1480
    self.user = user
1481 1482 1483 1484 1485

  def getUser(self):
    """
      retrun the user logged in
    """
1486
    return getattr(self, 'user', None)
1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506

  def getConduitByName(self, conduit_name):
    """
    Get Conduit Object by given name.
    The Conduit can be located in Any Products according to naming Convention
    Products.<Product Name>.Conduit.<Conduit Module> ,if conduit_name equal module's name.
    By default Conduit must be defined in Products.ERP5SyncML.Conduit.<Conduit Module>
    """
    from Products.ERP5SyncML import Conduit
    if conduit_name.startswith('Products'):
      path = conduit_name
      conduit_name = conduit_name.split('.')[-1]
      conduit_module = __import__(path, globals(), locals(), [''])
      conduit = getattr(conduit_module, conduit_name)()
    else:
      conduit_module = __import__('.'.join([Conduit.__name__, conduit_name]),
                                  globals(), locals(), [''])
      conduit = getattr(conduit_module, conduit_name)()
    return conduit