SynchronizationTool.py 16.7 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 32 33 34 35 36 37 38 39 40 41
## 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.
#
##############################################################################

"""\
ERP portal_synchronizations tool.
"""

from OFS.SimpleItem import SimpleItem
from OFS.Folder import Folder
from Products.CMFCore.utils import UniqueObject
from Globals import InitializeClass, DTMLFile, PersistentMapping, Persistent
from AccessControl import ClassSecurityInfo, getSecurityManager
from Products.CMFCore import CMFCorePermissions
from Products.ERP5SyncML import _dtmldir
from Publication import Publication,Subscriber
from Subscription import Subscription,Signature
from xml.dom.ext.reader.Sax2 import FromXmlStream, FromXml
from XMLSyncUtils import *
Sebastien Robin's avatar
Sebastien Robin committed
42
from Products.ERP5Type import Permissions
Jean-Paul Smets's avatar
Jean-Paul Smets committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
from PublicationSynchronization import PublicationSynchronization
from SubscriptionSynchronization import SubscriptionSynchronization
#import sys
#import StringIO
import string
from zLOG import *

from Conduit.ERP5Conduit import ERP5Conduit

class SynchronizationError( Exception ):
  pass

class SynchronizationTool( UniqueObject, SimpleItem,
                           SubscriptionSynchronization, PublicationSynchronization ):
  """
    This tool implements the synchronization algorithm
  """


  id       = 'portal_synchronizations'
  meta_type    = 'ERP5 Synchronizations'

  security = ClassSecurityInfo()

  #
  #  Default values.
  #
  list_publications = PersistentMapping()
  list_subscriptions = PersistentMapping()

  # Do we want to use emails ?
  #email = None
  email = 1
  same_export = 1

  def __init__( self ):
    self.list_publications = PersistentMapping()
    self.list_subscriptions = PersistentMapping()

  #
  #  ZMI methods
  #
  manage_options = ( ( { 'label'   : 'Overview'
             , 'action'   : 'manage_overview'
             }
            , { 'label'   : 'Publications'
             , 'action'   : 'managePublications'
             }
            , { 'label'   : 'Subscriptions'
             , 'action'   : 'manageSubscriptions'
             }
            , { 'label'   : 'Conflicts'
             , 'action'   : 'manageConflicts'
             }
            )
           + SimpleItem.manage_options
           )

  security.declareProtected( CMFCorePermissions.ManagePortal
               , 'manage_overview' )
  manage_overview = DTMLFile( 'dtml/explainSynchronizationTool', globals() )

  security.declareProtected( CMFCorePermissions.ManagePortal
               , 'managePublications' )
  managePublications = DTMLFile( 'dtml/managePublications', globals() )

  security.declareProtected( CMFCorePermissions.ManagePortal
               , 'addPublicationsForm' )
  addPublicationsForm = DTMLFile( 'dtml/addPublications', globals() )

  security.declareProtected( CMFCorePermissions.ManagePortal
               , 'manageSubsciptions' )
  manageSubscriptions = DTMLFile( 'dtml/manageSubscriptions', globals() )

  security.declareProtected( CMFCorePermissions.ManagePortal
               , 'manageConflicts' )
  manageConflicts = DTMLFile( 'dtml/manageConflicts', globals() )

  security.declareProtected( CMFCorePermissions.ManagePortal
               , 'addSubscriptionsForm' )
  addSubscriptionsForm = DTMLFile( 'dtml/addSubscriptions', globals() )

  security.declareProtected( CMFCorePermissions.ManagePortal
               , 'editProperties' )
  def editProperties( self
           , publisher=None
           , REQUEST=None
           ):
    """
      Form handler for "tool-wide" properties (including list of
      metadata elements).
    """
    if publisher is not None:
      self.publisher = publisher

    if REQUEST is not None:
      REQUEST[ 'RESPONSE' ].redirect( self.absolute_url()
                    + '/propertiesForm'
                    + '?manage_tabs_message=Tool+updated.'
                    )

Sebastien Robin's avatar
Sebastien Robin committed
144
  security.declareProtected(Permissions.ModifyPortalContent, 'addPublications')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
145 146 147 148 149 150 151 152 153 154 155 156 157
  def addPublications(self, id, publication_url, destination_path,
            query, xml_mapping, RESPONSE=None):
    """
      create a new publication
    """
    pub = Publication(id, publication_url, destination_path,
                      query, xml_mapping)
    if len(self.list_publications) == 0:
      self.list_publications = PersistentMapping()
    self.list_publications[id] = pub
    if RESPONSE is not None:
      RESPONSE.redirect('managePublications')

Sebastien Robin's avatar
Sebastien Robin committed
158
  security.declareProtected(Permissions.ModifyPortalContent, 'addSubscriptions')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
159 160 161
  def addSubscriptions(self, id, publication_url, subscription_url,
                       destination_path, query, xml_mapping, RESPONSE=None):
    """
Sebastien Robin's avatar
Sebastien Robin committed
162
      XXX should be renamed as addSubscription
Jean-Paul Smets's avatar
Jean-Paul Smets committed
163 164 165 166 167 168 169 170 171 172
      create a new subscription
    """
    sub = Subscription(id, publication_url, subscription_url,
                       destination_path, query, xml_mapping)
    if len(self.list_subscriptions) == 0:
      self.list_subscriptions = PersistentMapping()
    self.list_subscriptions[id] = sub
    if RESPONSE is not None:
      RESPONSE.redirect('manageSubscriptions')

Sebastien Robin's avatar
Sebastien Robin committed
173
  security.declareProtected(Permissions.ModifyPortalContent, 'editPublications')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
174 175 176 177 178 179 180 181 182 183 184
  def editPublications(self, id, publication_url, destination_path,
                       query, xml_mapping, RESPONSE=None):
    """
      modify a publication
    """
    pub = Publication(id, publication_url, destination_path,
                      query, xml_mapping)
    self.list_publications[id] = pub
    if RESPONSE is not None:
      RESPONSE.redirect('managePublications')

Sebastien Robin's avatar
Sebastien Robin committed
185
  security.declareProtected(Permissions.ModifyPortalContent, 'editSubscriptions')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
186 187 188 189 190 191 192 193 194 195 196
  def editSubscriptions(self, id, publication_url, subscription_url,
             destination_path, query, xml_mapping, RESPONSE=None):
    """
      modify a subscription
    """
    sub = Subscription(id, publication_url, subscription_url,
                       destination_path, query, xml_mapping)
    self.list_subscriptions[id] = sub
    if RESPONSE is not None:
      RESPONSE.redirect('manageSubscriptions')

Sebastien Robin's avatar
Sebastien Robin committed
197
  security.declareProtected(Permissions.ModifyPortalContent, 'deletePublications')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
198 199 200 201 202 203 204 205
  def deletePublications(self, id, RESPONSE=None):
    """
      delete a publication
    """
    del self.list_publications[id]
    if RESPONSE is not None:
      RESPONSE.redirect('managePublications')

Sebastien Robin's avatar
Sebastien Robin committed
206
  security.declareProtected(Permissions.ModifyPortalContent, 'deleteSubscriptions')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
207 208 209 210 211 212 213 214
  def deleteSubscriptions(self, id, RESPONSE=None):
    """
      delete a subscription
    """
    del self.list_subscriptions[id]
    if RESPONSE is not None:
      RESPONSE.redirect('manageSubscriptions')

Sebastien Robin's avatar
Sebastien Robin committed
215
  security.declareProtected(Permissions.ModifyPortalContent, 'ResetPublications')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
216 217 218 219 220 221 222 223
  def ResetPublications(self, id, RESPONSE=None):
    """
      reset a publication
    """
    self.list_publications[id].resetAllSubscribers()
    if RESPONSE is not None:
      RESPONSE.redirect('managePublications')

Sebastien Robin's avatar
Sebastien Robin committed
224
  security.declareProtected(Permissions.ModifyPortalContent, 'ResetSubscriptions')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
225 226 227
  def ResetSubscriptions(self, id, RESPONSE=None):
    """
      reset a subscription
Sebastien Robin's avatar
Sebastien Robin committed
228
      XXX R -> r
Jean-Paul Smets's avatar
Jean-Paul Smets committed
229 230 231 232 233 234
    """
    self.list_subscriptions[id].resetAllSignatures()
    self.list_subscriptions[id].resetAnchors()
    if RESPONSE is not None:
      RESPONSE.redirect('manageSubscriptions')

Sebastien Robin's avatar
Sebastien Robin committed
235
  security.declareProtected(Permissions.AccessContentsInformation,'getPublicationList')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
236 237 238 239 240 241 242 243 244 245 246 247
  def getPublicationList(self):
    """
      Return a list of publications
    """
    return_list = []
    if type(self.list_publications) is type([]): # For compatibility with old
                                                 # SynchronizationTool, XXX To be removed
      self.list_publications = PersistentMapping()
    for key in self.list_publications.keys():
      return_list += [self.list_publications[key]]
    return return_list

Sebastien Robin's avatar
Sebastien Robin committed
248
  security.declareProtected(Permissions.AccessContentsInformation,'getSubscriptionList')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
249 250 251 252 253 254 255 256 257 258 259 260
  def getSubscriptionList(self):
    """
      Return a list of publications
    """
    return_list = []
    if type(self.list_subscriptions) is type([]): # For compatibility with old
                                                 # SynchronizationTool, XXX To be removed
      self.list_subscriptions = PersistentMapping()
    for key in self.list_subscriptions.keys():
      return_list += [self.list_subscriptions[key]]
    return return_list

Sebastien Robin's avatar
Sebastien Robin committed
261
  security.declareProtected(Permissions.AccessContentsInformation,'getDomainList')
262 263 264
  def getDomainList(self):
    """
      Returns the list of subscriptions and publications
Sebastien Robin's avatar
Sebastien Robin committed
265 266 267
      getSynchronizationList ? (mon choix)
      getSubscriptionOrPublicationList ?

268 269 270
    """
    return self.getSubscriptionList() + self.getPublicationList()

Sebastien Robin's avatar
Sebastien Robin committed
271
  security.declareProtected(Permissions.AccessContentsInformation,'getConflictList')
272
  def getConflictList(self, path=None):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
273 274 275 276 277 278 279 280
    """
    Retrieve the list of all conflicts
    Here the list is as follow :
    [conflict_1,conflict2,...] where conflict_1 is like:
    ['publication',publication_id,object.getPath(),keyword,local_value,remote_value]
    """
    conflict_list = []
    for publication in self.getPublicationList():
Sebastien Robin's avatar
Sebastien Robin committed
281 282 283 284 285 286 287
      for subscriber in publication.getSubscriberList():
        sub_conflict_list = subscriber.getConflictList()
        for conflict in sub_conflict_list:
          #conflict.setDomain('Publication')
          conflict.setDomain(subscriber)
          #conflict.setDomainId(subscriber.getId())
          conflict_list += [conflict.__of__(self)]
Jean-Paul Smets's avatar
Jean-Paul Smets committed
288 289 290
    for subscription in self.getSubscriptionList():
      sub_conflict_list = subscription.getConflictList()
      for conflict in sub_conflict_list:
291 292
        #conflict.setDomain('Subscription')
        conflict.setDomain(subscription)
Sebastien Robin's avatar
Sebastien Robin committed
293 294
        #conflict.setDomainId(subscription.getId())
        conflict_list += [conflict.__of__(self)]
295 296 297 298
    if path is not None: # Retrieve only conflicts for a given path
      new_list = []
      for conflict in conflict_list:
        if conflict.getObjectPath() == path:
Sebastien Robin's avatar
Sebastien Robin committed
299
          new_list += [conflict.__of__(self)]
300
      return new_list
Jean-Paul Smets's avatar
Jean-Paul Smets committed
301 302
    return conflict_list

Sebastien Robin's avatar
Sebastien Robin committed
303
  security.declareProtected(Permissions.AccessContentsInformation,'getSynchronizationState')
304
  def getSynchronizationState(self, path):
305
    """
306
    context : the context on which we are looking for state
307

308 309 310
    This functions have to retrieve the synchronization state,
    it will first look in the conflict list, if nothing is found,
    then we have to check on a publication/subscription.
311

312
    This method returns a mapping between subscription and states
Sebastien Robin's avatar
Sebastien Robin committed
313 314 315 316 317

    JPS suggestion:
      path -> object, document, context, etc.
      type -> '/titi/toto' or ('','titi', 'toto') or <Base instance 1562567>
      object = self.resolveContext(context) (method to add)
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
    """
    conflict_list = self.getConflictList()
    state_list= []
    LOG('getSynchronizationState',0,'path: %s' % str(path))
    for conflict in conflict_list:
      if conflict.getObjectPath() == path:
        LOG('getSynchronizationState',0,'found a conflict: %s' % str(conflict))
        state_list += [[conflict.getDomain(),self.CONFLICT]]
    for domain in self.getDomainList():
      destination = domain.getDestinationPath()
      LOG('getSynchronizationState',0,'destination: %s' % str(destination))
      j_path = '/'.join(path)
      LOG('getSynchronizationState',0,'j_path: %s' % str(j_path))
      if j_path.find(destination)==0:
        o_id = j_path[len(destination)+1:].split('/')[0]
        LOG('getSynchronizationState',0,'o_id: %s' % o_id)
        subscriber_list = []
        if domain.domain_type==self.PUB:
          subscriber_list = domain.getSubscriberList()
        else:
          subscriber_list = [domain]
        for subscriber in subscriber_list:
          signature = subscriber.getSignature(o_id)
          if signature is not None:
            state = signature.getStatus()
            found = None
            # Make sure there is not already a conflict giving the state
            for state_item in state_list:
              if state_item[0]==subscriber:
                found = 1
            if found is None:
              state_list += [[subscriber,state]]
    return state_list
351

Sebastien Robin's avatar
Sebastien Robin committed
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
  security.declareProtected(Permissions.ModifyPortalContent, 'applyLocalValue')
  def applyLocalValue(self, conflict):
    """
      after a conflict resolution, we have decided
      to keep the local version of an object

      XXXC Local ? Remote ?
           applyPublisherValue ? (JPS 1)
           applySubscriberValue ?
           applyPublicationValue ? (JPS 2)
           applySubscriptionValue ?
           applyPublishedValue ? (JPS 3)
           applySubscribedValue ?
    """
    object = self.unrestrictedTraverse(conflict.getObjectPath())
    subscriber = conflict.getDomain()
    # get the signature:
    LOG('p_sync.setLocalObject, subscriber: ',0,subscriber)
    signature = subscriber.getSignature(object.getId()) # XXX may be change for rid
    signature.delConflict(conflict)
    if signature.getConflictList() == []:
      signature.setStatus(self.PUB_CONFLICT_MERGE)

  security.declareProtected(Permissions.ModifyPortalContent, 'applyRemoteValue')
  def applyRemoteValue(self, conflict):
    """
      after a conflict resolution, we have decided
      to keep the local version of an object
    """
    object = self.unrestrictedTraverse(conflict.getObjectPath())
    subscriber = conflict.getDomain()
    # get the signature:
    LOG('p_sync.setRemoteObject, subscriber: ',0,subscriber)
    signature = subscriber.getSignature(object.getId()) # XXX may be change for rid
    conduit = ERP5Conduit()
    for xupdate in conflict.getXupdateList():
      conduit.updateNode(xml=xupdate,object=object,force=1)
    signature.delConflict(conflict)
    if signature.getConflictList() == []:
      signature.setStatus(self.PUB_CONFLICT_MERGE)


  security.declareProtected(Permissions.ModifyPortalContent, 'manageLocalValue')
  def manageLocalValue(self, subscription_url, keyword, object_path, RESPONSE=None):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
396 397 398
    """
    Do whatever needed in order to store the local value on
    the remote server
Sebastien Robin's avatar
Sebastien Robin committed
399 400 401 402 403 404 405 406 407 408 409

    Suggestion:
      manage_applyLocalValue XXX

    Suggestion:
      add global apply (not conflict per conflict) XXX

    Suggestion (API)
      add method to view document with applied xupdate
      of a given subscriber XX (ex. viewSubscriberDocument?path=ddd&subscriber_id=dddd)
      Version=Version CPS
Jean-Paul Smets's avatar
Jean-Paul Smets committed
410 411
    """
    # Retrieve the conflict object
Sebastien Robin's avatar
Sebastien Robin committed
412 413 414 415 416 417 418 419 420 421
    LOG('manageLocalValue',0,'%s %s %s' % (str(subscription_url),
                                           str(keyword),
                                           str(object_path)))
    for conflict in self.getConflictList():
      LOG('manageLocalValue, conflict:',0,conflict)
      if conflict.getKeyword() == keyword:
        LOG('manageLocalValue',0,'found the keyword')
        if '/'.join(conflict.getObjectPath())==object_path:
          if conflict.getDomain().getSubscriptionUrl()==subscription_url:
            conflict.applyLocalValue()
Jean-Paul Smets's avatar
Jean-Paul Smets committed
422 423 424
    if RESPONSE is not None:
      RESPONSE.redirect('manageConflicts')

Sebastien Robin's avatar
Sebastien Robin committed
425 426
  security.declareProtected(Permissions.ModifyPortalContent, 'manageRemoteValue')
  def manageRemoteValue(self, subscription_url, keyword, object_path, RESPONSE=None):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
427 428 429 430
    """
    Do whatever needed in order to store the remote value locally
    and confirmed that the remote box should keep it's value
    """
Sebastien Robin's avatar
Sebastien Robin committed
431 432 433 434 435 436 437 438 439 440
    LOG('manageLocalValue',0,'%s %s %s' % (str(subscription_url),
                                           str(keyword),
                                           str(object_path)))
    for conflict in self.getConflictList():
      LOG('manageLocalValue, conflict:',0,conflict)
      if conflict.getKeyword() == keyword:
        LOG('manageLocalValue',0,'found the keyword')
        if '/'.join(conflict.getObjectPath())==object_path:
          if conflict.getDomain().getSubscriptionUrl()==subscription_url:
            conflict.applyRemoteValue()
Jean-Paul Smets's avatar
Jean-Paul Smets committed
441 442 443 444
    if RESPONSE is not None:
      RESPONSE.redirect('manageConflicts')

InitializeClass( SynchronizationTool )