Commit 9490b306 authored by Aurel's avatar Aurel

move methods on Subscription class to remove useless placeholder

parent 259d38bb
...@@ -168,6 +168,22 @@ class SyncMLSubscription(XMLObject): ...@@ -168,6 +168,22 @@ class SyncMLSubscription(XMLObject):
**method_kw) **method_kw)
return result_count return result_count
security.declarePrivate('generateBaseResponse')
def generateBaseResponse(self, message_id=None):
"""
Return a message containing default headers
"""
if not message_id:
message_id=self.getNextMessageId(),
syncml_response = SyncMLResponse()
syncml_response.addHeader(
session_id=self.getSessionId(),
message_id=message_id,
target=self.getUrlString(),
source=self.getSubscriptionUrlString())
syncml_response.addBody()
return syncml_response
security.declarePrivate('getSearchableSourcePath') security.declarePrivate('getSearchableSourcePath')
def getSearchableSourcePath(self): def getSearchableSourcePath(self):
""" """
...@@ -183,26 +199,44 @@ class SyncMLSubscription(XMLObject): ...@@ -183,26 +199,44 @@ class SyncMLSubscription(XMLObject):
""" """
# Build Message # Build Message
syncml_response = SyncMLResponse() syncml_response = SyncMLResponse()
# XXX Make a generic method that already exists in engines syncml_response = self.generateBaseResponse(message_id)
syncml_response.addHeader(
session_id=self.getSessionId(),
message_id=message_id,
target=self.getUrlString(),
source=self.getSubscriptionUrlString())
syncml_response.addBody()
self._getSyncMLData( self._getSyncMLData(
syncml_response=syncml_response, syncml_response=syncml_response,
min_gid=min_gid, min_gid=min_gid,
max_gid=max_gid, max_gid=max_gid,
) )
# Send the message in activity to prevent recomputation of data in case of # Send the message in activity to prevent recomputation of data in case of
# transport failure # transport failure
# activate_kw["group_method_id"] = None # activate_kw["group_method_id"] = None
# activate_kw["group_method_cost"] = .05 # activate_kw["group_method_cost"] = .05
self.activate(**activate_kw).sendMessage(xml=str(syncml_response)) self.activate(**activate_kw).sendMessage(xml=str(syncml_response))
security.declarePrivate('applySyncCommand')
def applySyncCommand(self, response_message_id, activate_kw, **kw):
"""
This methods is intented to be called by asynchronous engine in activity to
apply sync commands for a subset of data
"""
# Build Message
if response_message_id:
syncml_response = self.generateBaseResponse()
syncml_response.addBody()
else:
syncml_response = None
self._applySyncCommand(syncml_response=syncml_response, **kw)
# Send the message in activity to prevent recomputing data in case of
# transport failure
if syncml_response:
syncml_logger("---- %s sending %s notifications of sync"
% (self.getTitle(),
syncml_response.sync_confirmation_counter))
self.activate(activity="SQLQueue",
# group_method_id=None,
# group_method_cost=.05,
tag=activate_kw).sendMessage(xml=str(syncml_response))
security.declarePrivate('getAndActivate') security.declarePrivate('getAndActivate')
def getAndActivate(self, callback, activate_kw, **kw): def getAndActivate(self, callback, activate_kw, **kw):
...@@ -364,14 +398,14 @@ class SyncMLSubscription(XMLObject): ...@@ -364,14 +398,14 @@ class SyncMLSubscription(XMLObject):
Browse the list of sync command received, apply them and generate answer Browse the list of sync command received, apply them and generate answer
""" """
for action in syncml_request.sync_command_list: for action in syncml_request.sync_command_list:
self.applySyncCommand( self._applySyncCommand(
action=action, action=action,
request_message_id=syncml_request.header["message_id"], request_message_id=syncml_request.header["message_id"],
syncml_response=syncml_response, syncml_response=syncml_response,
simulate=simulate) simulate=simulate)
security.declarePrivate('applySyncCommand') security.declarePrivate('applySyncCommand')
def applySyncCommand(self, action, request_message_id, syncml_response, def _applySyncCommand(self, action, request_message_id, syncml_response,
simulate=False): simulate=False):
""" """
Apply a sync command received Apply a sync command received
...@@ -602,12 +636,7 @@ class SyncMLSubscription(XMLObject): ...@@ -602,12 +636,7 @@ class SyncMLSubscription(XMLObject):
Send an empty message containing the final tag to notify the end of Send an empty message containing the final tag to notify the end of
the "sending_modification" stage of the synchronization the "sending_modification" stage of the synchronization
""" """
syncml_response = SyncMLResponse() syncml_response = self.generateBaseResponse()
syncml_response.addHeader(
session_id=self.getSessionId(),
message_id=self.getNextMessageId(),
target=self.getUrlString(),
source=self.getSubscriptionUrlString())
syncml_response.addBody() syncml_response.addBody()
syncml_response.addFinal() syncml_response.addFinal()
......
...@@ -75,7 +75,7 @@ class SyncMLAsynchronousEngine(EngineMixin): ...@@ -75,7 +75,7 @@ class SyncMLAsynchronousEngine(EngineMixin):
if subscription.getSyncmlAlertCode() in ("one_way_from_server", if subscription.getSyncmlAlertCode() in ("one_way_from_server",
"refresh_from_server_only"): "refresh_from_server_only"):
# We only get data from server # We only get data from server
syncml_response = self._generateBaseResponse(subscription) syncml_response = subscription.generateBaseResponse()
syncml_response.addFinal() syncml_response.addFinal()
else: else:
# Make sure it is launched after indexation step # Make sure it is launched after indexation step
...@@ -102,7 +102,7 @@ class SyncMLAsynchronousEngine(EngineMixin): ...@@ -102,7 +102,7 @@ class SyncMLAsynchronousEngine(EngineMixin):
% (len(syncml_request.sync_command_list))) % (len(syncml_request.sync_command_list)))
if syncml_request.isFinal: if syncml_request.isFinal:
if not syncml_response: if not syncml_response:
syncml_response = self._generateBaseResponse(subscription) syncml_response = subscription.generateBaseResponse()
# We got and process all sync command from server # We got and process all sync command from server
# notify it that all modifications were applied # notify it that all modifications were applied
syncml_response.addFinal() syncml_response.addFinal()
...@@ -231,7 +231,7 @@ class SyncMLAsynchronousEngine(EngineMixin): ...@@ -231,7 +231,7 @@ class SyncMLAsynchronousEngine(EngineMixin):
# Server has no modification to send to client, return final message # Server has no modification to send to client, return final message
syncml_logger.info("X-> Server sending final message") syncml_logger.info("X-> Server sending final message")
if not syncml_response: if not syncml_response:
syncml_response = self._generateBaseResponse(subscriber) syncml_response = subscriber.generateBaseResponse()
syncml_response.addFinal() syncml_response.addFinal()
if subscriber.getSynchronizationState() == "finished": if subscriber.getSynchronizationState() == "finished":
...@@ -280,9 +280,9 @@ class SyncMLAsynchronousEngine(EngineMixin): ...@@ -280,9 +280,9 @@ class SyncMLAsynchronousEngine(EngineMixin):
response_id_list = [None for x in response_id_list = [None for x in
xrange(len(syncml_request.sync_command_list))] xrange(len(syncml_request.sync_command_list))]
split = getSite().portal_preferences.getPreferredSyncActionPerActivityCount() split = getSite().portal_preferences.getPreferredSyncActionPerActivityCount()
if not split: if not split: # We do not use activities
if send_response: if send_response:
syncml_response = self._generateBaseResponse(subscription) syncml_response = subscription.generateBaseResponse()
else: else:
syncml_response = None syncml_response = None
subscription.applyActionList(syncml_request, syncml_response) subscription.applyActionList(syncml_request, syncml_response)
...@@ -291,10 +291,9 @@ class SyncMLAsynchronousEngine(EngineMixin): ...@@ -291,10 +291,9 @@ class SyncMLAsynchronousEngine(EngineMixin):
activity="SQLQueue", activity="SQLQueue",
priority=ACTIVITY_PRIORITY, priority=ACTIVITY_PRIORITY,
tag=subscription.getRelativeUrl()).sendMessage(xml=str(syncml_response)) tag=subscription.getRelativeUrl()).sendMessage(xml=str(syncml_response))
else: else:
# XXX For now always split by one # XXX For now always split by one
activate = subscription.getPortalObject().portal_synchronizations.activate activate = subscription.activate
activate_kw = { activate_kw = {
"activity" :"SQLQueue", "activity" :"SQLQueue",
"priority" : ACTIVITY_PRIORITY, "priority" : ACTIVITY_PRIORITY,
...@@ -305,10 +304,9 @@ class SyncMLAsynchronousEngine(EngineMixin): ...@@ -305,10 +304,9 @@ class SyncMLAsynchronousEngine(EngineMixin):
for action in syncml_request.sync_command_list: for action in syncml_request.sync_command_list:
syncml_logger.info("---> launch action in activity %s" %(action,)) syncml_logger.info("---> launch action in activity %s" %(action,))
activate(**activate_kw).applySyncCommand( activate(**activate_kw).applySyncCommand(
subscription_path=subscription.getRelativeUrl(),
response_message_id=response_id_list.pop(), response_message_id=response_id_list.pop(),
activate_kw=activate_kw, activate_kw=activate_kw,
action=action, action=action,
request_message_id=syncml_request.header["message_id"], request_message_id=syncml_request.header["message_id"],
simulate=False) simulate=False)
# XXX Response is not send here # Response is sent by the activity
...@@ -46,15 +46,6 @@ class EngineMixin(object): ...@@ -46,15 +46,6 @@ class EngineMixin(object):
security = ClassSecurityInfo() security = ClassSecurityInfo()
def _generateBaseResponse(self, subscription):
syncml_response = SyncMLResponse()
syncml_response.addHeader(
session_id=subscription.getSessionId(),
message_id=subscription.getNextMessageId(),
target=subscription.getUrlString(),
source=subscription.getSubscriptionUrlString())
syncml_response.addBody()
return syncml_response
security.declarePrivate('_readStatusList') security.declarePrivate('_readStatusList')
def _readStatusList(self, syncml_request, domain, syncml_response=None, def _readStatusList(self, syncml_request, domain, syncml_response=None,
...@@ -83,7 +74,7 @@ class EngineMixin(object): ...@@ -83,7 +74,7 @@ class EngineMixin(object):
status['authentication_type'])) status['authentication_type']))
# XXX Not working To Review ! # XXX Not working To Review !
raise NotImplementedError("Adding credentials") raise NotImplementedError("Adding credentials")
syncml_response = self._generateBaseResponse(domain) syncml_response = domain.generateBaseResponse()
syncml_response.addCredentialMessage(domain) syncml_response.addCredentialMessage(domain)
return syncml_response return syncml_response
elif status['status_code'] == \ elif status['status_code'] == \
......
...@@ -29,7 +29,6 @@ from logging import getLogger ...@@ -29,7 +29,6 @@ from logging import getLogger
from Products.ERP5SyncML.Engine.EngineMixin import EngineMixin from Products.ERP5SyncML.Engine.EngineMixin import EngineMixin
from Products.ERP5SyncML.SyncMLConstant import SynchronizationError from Products.ERP5SyncML.SyncMLConstant import SynchronizationError
from Products.ERP5.ERP5Site import getSite
syncml_logger = getLogger('ERP5SyncML') syncml_logger = getLogger('ERP5SyncML')
...@@ -55,7 +54,7 @@ class SyncMLSynchronousEngine(EngineMixin): ...@@ -55,7 +54,7 @@ class SyncMLSynchronousEngine(EngineMixin):
# Must check what server tell about database synchronization # Must check what server tell about database synchronization
# and update the mode if required # and update the mode if required
syncml_response = self._generateBaseResponse(subscription) syncml_response = subscription.generateBaseResponse()
# Read & apply status about databases & synchronizations # Read & apply status about databases & synchronizations
try: try:
...@@ -150,7 +149,7 @@ class SyncMLSynchronousEngine(EngineMixin): ...@@ -150,7 +149,7 @@ class SyncMLSynchronousEngine(EngineMixin):
# Apply command & send modifications # Apply command & send modifications
# XXX This can be called on subscription instead # XXX This can be called on subscription instead
syncml_response = self._generateBaseResponse(subscriber) syncml_response = subscriber.generateBaseResponse()
# Apply status about object send & synchronized if any # Apply status about object send & synchronized if any
self._readStatusList(syncml_request, subscriber, syncml_response, True) self._readStatusList(syncml_request, subscriber, syncml_response, True)
......
...@@ -402,42 +402,4 @@ class SynchronizationTool(BaseTool): ...@@ -402,42 +402,4 @@ class SynchronizationTool(BaseTool):
return str(syncml_response) return str(syncml_response)
# As engines are not zodb objects, the tool acts as a placeholder for methods
# that need to be called in activities
def applySyncCommand(self, subscription_path, response_message_id,
activate_kw, **kw):
"""
This methods is intented to be called by asynchronous engine in activity to
apply sync commands for a subset of data
"""
subscription = self.restrictedTraverse(subscription_path)
assert subscription is not None, "Impossible to find subscription %s" \
% (subscription_path)
# Build Message
if response_message_id:
syncml_response = SyncMLResponse()
syncml_response.addHeader(
session_id=subscription.getSessionId(),
message_id=response_message_id,
target=subscription.getUrlString(),
source=subscription.getSubscriptionUrlString())
syncml_response.addBody()
else:
syncml_response = None
subscription.applySyncCommand(syncml_response=syncml_response, **kw)
# Send the message in activity to prevent recomputing data in case of
# transport failure
if syncml_response:
syncml_logger("---- %s sending %s notifications of sync"
% (subscription.getTitle(),
syncml_response.sync_confirmation_counter))
subscription.activate(activity="SQLQueue",
# group_method_id=None,
# group_method_cost=.05,
tag=activate_kw).sendMessage(xml=str(syncml_response))
InitializeClass(SynchronizationTool) InitializeClass(SynchronizationTool)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment