Subscription.py 25.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
35 36 37 38
from Products.ERP5Type.Document.Folder import Folder
from Products.ERP5Type.Base import Base
from Products.ERP5Type import Permissions
from Products.ERP5Type import PropertySheet
Sebastien Robin's avatar
Sebastien Robin committed
39
from DateTime import DateTime
Jean-Paul Smets's avatar
Jean-Paul Smets committed
40 41 42 43
from zLOG import LOG

import md5

44 45
#class Conflict(SyncCode, Implicit):
class Conflict(SyncCode, Base):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
46 47 48
  """
    object_path : the path of the obect
    keyword : an identifier of the conflict
49 50
    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
51 52

  """
53 54
  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
55 56
    self.object_path=object_path
    self.keyword = keyword
57 58 59
    self.setLocalValue(publisher_value)
    self.setRemoteValue(subscriber_value)
    self.subscriber = subscriber
60
    self.resetXupdate()
Jean-Paul Smets's avatar
Jean-Paul Smets committed
61 62 63

  def getObjectPath(self):
    """
64
    get the object path
Jean-Paul Smets's avatar
Jean-Paul Smets committed
65 66 67
    """
    return self.object_path

68
  def getPublisherValue(self):
69 70 71
    """
    get the domain
    """
72
    return self.publisher_value
73

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
  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

105 106 107 108 109
  def setLocalValue(self, value):
    """
    get the domain
    """
    try:
110
      self.publisher_value = value
111
    except TypeError: # It happens when we try to store StringIO
112
      self.publisher_value = None
113

114
  def getSubscriberValue(self):
115 116 117
    """
    get the domain
    """
118
    return self.subscriber_value
119 120 121 122 123 124

  def setRemoteValue(self, value):
    """
    get the domain
    """
    try:
125
      self.subscriber_value = value
126
    except TypeError: # It happens when we try to store StringIO
127
      self.subscriber_value = None
128

129
  def applyPublisherValue(self):
Sebastien Robin's avatar
Sebastien Robin committed
130 131 132 133 134
    """
      after a conflict resolution, we have decided
      to keep the local version of this object
    """
    p_sync = getToolByName(self,'portal_synchronizations')
135
    p_sync.applyPublisherValue(self)
Sebastien Robin's avatar
Sebastien Robin committed
136

137 138 139 140 141 142 143 144
  def applyPublisherDocument(self):
    """
      after a conflict resolution, we have decided
      to keep the local version of this object
    """
    p_sync = getToolByName(self,'portal_synchronizations')
    p_sync.applyPublisherDocument(self)

145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
  def getPublisherDocument(self):
    """
      after a conflict resolution, we have decided
      to keep the local version of this object
    """
    p_sync = getToolByName(self,'portal_synchronizations')
    return p_sync.getPublisherDocument(self)

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

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

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

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

185
  def applySubscriberValue(self,object=None):
Sebastien Robin's avatar
Sebastien Robin committed
186 187 188 189
    """
    get the domain
    """
    p_sync = getToolByName(self,'portal_synchronizations')
190
    p_sync.applySubscriberValue(self,object=object)
Sebastien Robin's avatar
Sebastien Robin committed
191

192
  def setSubscriber(self, subscriber):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
193 194 195
    """
    set the domain
    """
196
    self.subscriber = subscriber
Jean-Paul Smets's avatar
Jean-Paul Smets committed
197

198
  def getSubscriber(self):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
199 200 201
    """
    get the domain
    """
202
    return self.subscriber
Jean-Paul Smets's avatar
Jean-Paul Smets committed
203

204 205 206 207 208 209
  def getKeyword(self):
    """
    get the domain
    """
    return self.keyword

210
  def getPropertyId(self):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
211
    """
212
    get the property id
Jean-Paul Smets's avatar
Jean-Paul Smets committed
213
    """
214
    return self.keyword
Jean-Paul Smets's avatar
Jean-Paul Smets committed
215

216
class Signature(SyncCode,Folder):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
217 218 219 220 221
  """
    status -- SENT, CONFLICT...
    md5_object -- An MD5 value of a given document
    #uid -- The UID of the document
    id -- the ID of the document
222
    gid -- the global id of the document
Jean-Paul Smets's avatar
Jean-Paul Smets committed
223 224 225 226 227 228
    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
  """

  # Constructor
229
  def __init__(self,gid=None, id=None, status=None, xml_string=None):
230
    self.setGid(gid)
231
    self.setId(id)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
232 233 234 235 236 237
    self.status = status
    self.setXML(xml_string)
    self.partial_xml = None
    self.action = None
    self.setTempXML(None)
    self.resetConflictList()
238
    self.md5_string = None
Jean-Paul Smets's avatar
Jean-Paul Smets committed
239
    self.force = 0
240 241
    self.setSubscriberXupdate(None)
    self.setPublisherXupdate(None)
242
    Folder.__init__(self,id)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
243 244 245 246 247 248 249 250 251 252 253 254 255 256

  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
        self.setXML(self.getTempXML())
      self.setTempXML(None)
257
      self.setPartialXML(None)
258
      self.setSubscriberXupdate(None)
Sebastien Robin's avatar
Sebastien Robin committed
259
      self.setPublisherXupdate(None)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
260 261
      if len(self.getConflictList())>0:
        self.resetConflictList()
Sebastien Robin's avatar
Sebastien Robin committed
262 263 264
      # XXX This may be a problem, if the document is changed
      # during a synchronization
      self.setLastSynchronizationDate(DateTime())
265 266 267
    if status == self.NOT_SYNCHRONIZED:
      self.setTempXML(None)
      self.setPartialXML(None)
268 269 270
    elif status in (self.PUB_CONFLICT_MERGE,self.SENT):
      # 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
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289

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

  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
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
  def getLastModificationDate(self):
    """
      get the last modfication date, so that we don't always
      check the xml
    """
    return getattr(self,'modification_date',None)

  def setLastModificationDate(self,value):
    """
      set the last modfication date, so that we don't always
      check the xml
    """
    setattr(self,'modification_date',value)

  def getLastSynchronizationDate(self):
    """
      get the last modfication date, so that we don't always
      check the xml
    """
    return getattr(self,'synchronization_date',None)

  def setLastSynchronizationDate(self,value):
    """
      set the last modfication date, so that we don't always
      check the xml
    """
    setattr(self,'synchronization_date',value)

Jean-Paul Smets's avatar
Jean-Paul Smets committed
318 319 320 321 322 323 324
  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
325
      self.setMD5(xml)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346

  def getXML(self):
    """
      set the XML corresponding to the object
    """
    return self.xml

  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

347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
  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
371 372 373 374 375 376 377 378 379 380
  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
    """
381
    return self.md5_string
Jean-Paul Smets's avatar
Jean-Paul Smets committed
382 383 384 385 386 387 388 389

  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
    """
390
    return ((md5.new(xml_string).digest()) == self.getMD5())
Jean-Paul Smets's avatar
Jean-Paul Smets committed
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415

  def setRid(self, rid):
    """
      set the rid
    """
    self.rid = rid

  def getRid(self):
    """
      get the rid
    """
    return self.rid

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

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

416 417 418 419 420 421 422 423 424 425 426 427
  def setGid(self, gid):
    """
      set the id
    """
    self.gid = gid

  def getGid(self):
    """
      get the id
    """
    return self.gid

Jean-Paul Smets's avatar
Jean-Paul Smets committed
428 429 430 431 432
  def setPartialXML(self, xml):
    """
    Set the partial string we will have to
    deliver in the future
    """
433 434
    if type(xml) is type(u'a'):
      xml = xml.encode('utf-8')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
435 436 437 438 439 440 441
    self.partial_xml = xml

  def getPartialXML(self):
    """
    Set the partial string we will have to
    deliver in the future
    """
442
    #LOG('Subscriber.getPartialXML',0,'partial_xml: %s' % str(self.partial_xml))
443 444
    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
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
    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
    """
479
    LOG('setConflictList, list',0,conflict_list)
Sebastien Robin's avatar
Sebastien Robin committed
480
    if conflict_list is None or conflict_list==[]:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
481 482
      self.resetConflictList()
    else:
Sebastien Robin's avatar
Sebastien Robin committed
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
      self.conflict_list = conflict_list

  def delConflict(self, conflict):
    """
    Return the actual action for a partial synchronization
    """
    LOG('delConflict, conflict',0,conflict)
    conflict_list = []
    for c in self.getConflictList():
      LOG('delConflict, c==conflict',0,c==aq_base(conflict))
      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
499

500 501 502 503 504 505 506 507 508 509 510 511 512
def addSubscription( self, id, title='', REQUEST=None ):
    """
        Add a new Category and generate UID by calling the
        ZSQLCatalog
    """
    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):
class Subscription(SyncCode, Implicit, Folder):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
  """
    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

530 531
    gpg_key -- the name of a gpg key to use

Jean-Paul Smets's avatar
Jean-Paul Smets committed
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
    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

  """

548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
  meta_type='ERP5 Subscription'
  portal_type='Subscription' # may be useful in the future...
  isPortalContent = 1
  isRADContent = 1
  icon = None


  # 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
571 572

  # Constructor
573
  def __init__(self, id, title, publication_url, subscription_url, destination_path, query, xml_mapping, gpg_key):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
574 575 576 577 578 579 580 581 582
    """
      We need to create a dictionnary of
      signatures of documents which belong to the synchronisation
      process
    """
    self.id = id
    self.publication_url = (publication_url)
    self.subscription_url = str(subscription_url)
    self.destination_path = str(destination_path)
583
    self.setQuery(query)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
584 585 586
    self.xml_mapping = xml_mapping
    self.anchor = None
    self.session_id = 0
587
    #self.signatures = PersistentMapping()
Jean-Paul Smets's avatar
Jean-Paul Smets committed
588 589 590
    self.last_anchor = '00000000T000000Z'
    self.next_anchor = '00000000T000000Z'
    self.domain_type = self.SUB
591
    self.gpg_key = gpg_key
592 593
    self.setGidGenerator(None)
    self.setIdGenerator(None)
594 595
    Folder.__init__(self, id)
    self.title = title
596

Jean-Paul Smets's avatar
Jean-Paul Smets committed
597 598
    #self.signatures = PersitentMapping()

599 600 601 602 603 604 605 606 607 608 609 610
  def getTitle(self):
    """
    getter for title
    """
    return getattr(self,'title',None)

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

Jean-Paul Smets's avatar
Jean-Paul Smets committed
611 612 613 614 615 616 617 618 619 620 621 622
  # Accessors
  def getRemoteId(self, id, path=None):
    """
      Returns the remote id from a know local id
      Returns None if...
      path allows to implement recursive sync
    """
    pass

  def getSynchronizationType(self, default=None):
    """
    """
623 624 625
    # XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    # XXX for debugging only, to be removed
    dict_sign = {}
626 627
    for o in self.objectValues():
      dict_sign[o.getId()] = o.getStatus()
628 629
    LOG('getSignature',0,'signatures_status: %s' % str(dict_sign))
    # XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Jean-Paul Smets's avatar
Jean-Paul Smets committed
630
    code = self.SLOW_SYNC
631
    if len(self.objectValues()) > 0:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
632 633 634 635 636 637
      code = self.TWO_WAY
    if default is not None:
      code = default
    LOG('Subscription',0,'getSynchronizationType: %s' % code)
    return code

638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
  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

    return 1 if the session id was not seen, 0 if already seen
    """
    last_session_id = getattr(self,'last_session_id',None)
    if last_session_id == session_id:
      return 0
    self.last_session_id = session_id
    return 1


  def getLastSentMessage(self):
    """
    This is the getter for the last message we have sent
    """
    return getattr(self,'last_sent_message','')

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

Jean-Paul Smets's avatar
Jean-Paul Smets committed
664 665 666 667 668 669 670 671 672 673 674 675 676
  def getLocalId(self, rid, path=None):
    """
      Returns the local id from a know remote id
      Returns None if...
    """
    pass

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

677 678 679 680 681 682
  def getDomainType(self):
    """
      return the ID
    """
    return self.domain_type

Jean-Paul Smets's avatar
Jean-Paul Smets committed
683 684 685 686 687 688 689 690 691 692 693 694
  def setId(self, id):
    """
      set the ID
    """
    self.id = id

  def getQuery(self):
    """
      return the query
    """
    return self.query

695 696 697 698 699 700
  def getGPGKey(self):
    """
      return the gnupg key name
    """
    return getattr(self,'gpg_key','')

701 702 703 704 705 706
  def setGPGKey(self, value):
    """
      setter for the gnupg key name
    """
    self.gpg_key = value

Jean-Paul Smets's avatar
Jean-Paul Smets committed
707 708 709 710
  def setQuery(self, query):
    """
      set the query
    """
711 712
    if query in (None,''):
      query = 'objectValues'
Jean-Paul Smets's avatar
Jean-Paul Smets committed
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
    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):
    """
      return the publication url
    """
    self.publication_url = publication_url

733
  def getXMLMapping(self):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
734 735 736
    """
      return the xml mapping
    """
737 738
    xml_mapping = getattr(self,'xml_mapping','asXML')
    return xml_mapping
Jean-Paul Smets's avatar
Jean-Paul Smets committed
739

740
  def setXMLMapping(self, xml_mapping):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
741 742 743 744 745
    """
      return the xml mapping
    """
    self.xml_mapping = xml_mapping

746
  def setGidGenerator(self, method):
747 748 749 750
    """
    This set the method name wich allows to find a gid
    from any object
    """
751 752 753
    if method in (None,''):
      method = 'getId'
    self.gid_generator = method
754 755 756 757 758 759 760 761

  def getGidGenerator(self):
    """
    This get the method name wich allows to find a gid
    from any object
    """
    return self.gid_generator

762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
  def getGidFromObject(self, object):
    """
    """
    o_base = aq_base(object)
    o_gid = None
    LOG('getGidFromObject',0,'gidgenerator : %s' % repr(self.getGidGenerator()))
    gid_gen = self.getGidGenerator()
    if callable(gid_gen):
      o_gid=gid_gen(object)
    elif hasattr(o_base, gid_gen):
      LOG('getGidFromObject',0,'there is the gid generator')
      generator = getattr(object, self.getGidGenerator())
      o_gid = generator()
      LOG('getGidFromObject',0,'o_gid: %s' % repr(o_gid))
    return o_gid

778 779 780 781 782 783 784 785 786 787
  def getObjectFromGid(self, gid):
    """
    This tries to get the object with the given gid
    This uses the query if it exist
    """
    signature = self.getSignature(gid)
    # First look if we do already have the mapping between
    # the id and the gid
    object_list = self.getObjectList()
    destination = self.getDestination()
788
    LOG('getObjectFromGid',0,'gid: %s' % repr(gid))
789 790 791 792 793
    if signature is not None:
      o_id = signature.getId()
      o = None
      try:
        o = destination._getOb(o_id)
794
      except (AttributeError, KeyError, TypeError):
795 796 797 798 799
        pass
      if o is not None and o in object_list:
        return o
    for o in object_list:
      LOG('getObjectFromGid',0,'working on : %s' % repr(o))
800 801 802
      o_gid = self.getGidFromObject(o)
      if o_gid == gid:
        return o
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
    LOG('getObjectFromGid',0,'returning None')
    return None

  def getObjectList(self):
    """
    This returns the list of sub-object corresponding
    to the query
    """
    destination = self.getDestination()
    query = self.getQuery()
    query_list = []
    if type(query) is type('a'):
      query_method = getattr(destination,query,None)
      if query_method is not None:
        query_list = query_method()
    if callable(query):
      query_list = query(destination)
Sebastien Robin's avatar
Sebastien Robin committed
820 821
    return filter(lambda x: x.id.find('conflict_copy')<0,query_list)

822 823
#     if query is not None:
#       query_list = query()
Sebastien Robin's avatar
Sebastien Robin committed
824
#    return query_list
825

826
  def generateNewIdWithGenerator(self, object=None,gid=None):
827 828 829
    """
    This tries to generate a new Id
    """
830 831 832 833
    LOG('generateNewId, object: ',0,object.getPhysicalPath())
    id_generator = self.getIdGenerator()
    LOG('generateNewId, id_generator: ',0,id_generator)
    if id_generator is not None:
834
      o_base = aq_base(object)
835 836 837 838 839
      new_id = None
      if callable(id_generator):
        new_id = id_generator(object)
      elif hasattr(o_base, id_generator):
        generator = getattr(object, id_generator)
840
        new_id = generator()
841 842
      LOG('generateNewId, new_id: ',0,new_id)
      return new_id
843 844
    return None

845
  def setIdGenerator(self, method):
846 847 848 849
    """
    This set the method name wich allows to generate
    a new id
    """
850
    self.id_generator = method
851 852 853 854 855 856 857

  def getIdGenerator(self):
    """
    This get the method name wich allows to generate a new id
    """
    return self.id_generator

Jean-Paul Smets's avatar
Jean-Paul Smets committed
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
  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

876 877 878 879 880 881
  def getDestination(self):
    """
      return the destination object itself
    """
    return self.unrestrictedTraverse(self.getDestinationPath())

Jean-Paul Smets's avatar
Jean-Paul Smets committed
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
  def setDestinationPath(self, destination_path):
    """
      set the destination path
    """
    self.destination_path = destination_path

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

  def getSessionId(self):
    """
      return the session id
    """
    self.session_id += 1
    return self.session_id

  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
    """
945
    self._setObject( signature.getGid(), signature )
Jean-Paul Smets's avatar
Jean-Paul Smets committed
946

947
  def delSignature(self, gid):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
948 949 950
    """
      add a Signature to the subscription
    """
951 952
    #del self.signatures[gid]
    self._delObject(gid)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
953

954
  def getSignature(self, gid):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
955 956 957
    """
      add a Signature to the subscription
    """
958 959 960 961
    o = None
    if gid in self.objectIds():
      o = self._getOb(gid)
    return o
Jean-Paul Smets's avatar
Jean-Paul Smets committed
962 963 964 965 966

  def getSignatureList(self):
    """
      add a Signature to the subscription
    """
967
    return self.objectValues()
Jean-Paul Smets's avatar
Jean-Paul Smets committed
968

969
  def hasSignature(self, gid):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
970 971 972
    """
      Check if there's a signature with this uid
    """
973 974
    #return self.signatures.has_key(gid)
    return gid in self.objectIds()
Jean-Paul Smets's avatar
Jean-Paul Smets committed
975 976 977 978 979

  def resetAllSignatures(self):
    """
      Reset all signatures
    """
980 981 982
    #self.signatures = PersistentMapping()
    for o in self.objectValues():
      self._delObject(o.id)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
983

984
  def getGidList(self):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
985 986 987
    """
    Returns the list of ids from signature
    """
988
    return self.objectIds()
Jean-Paul Smets's avatar
Jean-Paul Smets committed
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002

  def getConflictList(self):
    """
    Return the list of all conflicts from all signatures
    """
    conflict_list = []
    for signature in self.getSignatureList():
      conflict_list += signature.getConflictList()
    return conflict_list

  def startSynchronization(self):
    """
    Set the status of every object as NOT_SYNCHRONIZED
    """
1003
    for o in self.objectValues():
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1004
      # Change the status only if we are not in a conflict mode
1005
      if not(o.getStatus() in (self.CONFLICT,self.PUB_CONFLICT_MERGE,
1006
                                                        self.PUB_CONFLICT_CLIENT_WIN)):
1007 1008 1009
        o.setStatus(self.NOT_SYNCHRONIZED)
        o.setPartialXML(None)
        o.setTempXML(None)