com.vodafone360.people.engine.meprofile
Class SyncMeEngine

java.lang.Object
  extended by com.vodafone360.people.engine.BaseEngine
      extended by com.vodafone360.people.engine.meprofile.SyncMeEngine

public class SyncMeEngine
extends BaseEngine

This is an implementation for an engine to synchronize Me profile data.


Nested Class Summary
private static class SyncMeEngine.State
          Defines the contact sync mode.
 
Nested classes/interfaces inherited from class com.vodafone360.people.engine.BaseEngine
BaseEngine.IEngineEventCallback
 
Field Summary
private static java.lang.String CONTACT_CHANGES
          The type of datatype we are expecting back from the server for upload/download me profile requests.
private static int MAX_QUALITY
           
private  ApplicationCache mCache
          ApplicationCache reference to update progress bar.
private  DatabaseHelper mDbHelper
          Database.
private  boolean mFirstTimeMeSyncComplete
          Indicates if the first time sync has been completed.
private  boolean mFirstTimeSyncStarted
          Indicates if the first time sync has been ever initiated.
private  long mFromRevision
          The latest revision of Me Profile.
private  SyncMeEngine.State mState
          Current engine state.
private  UiAgent mUiAgent
          UiAgent reference to update progress bar.
private  java.util.ArrayList<ContactDetail> mUploadedMeDetails
          The Me Profile, as it was uploaded.
private static int PROGRESS_50
          Percentage used to show progress when the me sync is half complete.
 
Fields inherited from class com.vodafone360.people.engine.BaseEngine
mActiveUiRequest, mCurrentTimeout, mEngineId, mEventCallback
 
Constructor Summary
SyncMeEngine(BaseEngine.IEngineEventCallback eventCallback, DatabaseHelper db)
          The constructor.
 
Method Summary
 void addGetMeProfileContactFirstTimeRequest()
          This method adds an external request to Contacts/getMyChanges() method to update the Me Profile status server, is called by the UI at the 1st sync.
private  void addGetMeProfileContactRequest()
          This method adds an external request to Contacts/getMyChanges() method to update the Me Profile status server.
 void addUpdateMeProfileContactRequest()
          This method adds an external request to Contacts/setMe() method to update the Me Profile...
 void addUpdateMyStatusRequest(java.lang.String textStatus)
          This method adds an external request to Contacts/setMe() method to update the Me Profile status...
private  boolean checkConnectivity()
          Get current connectivity state from the NetworkAgent.
protected  void completeUiRequest(ServiceStatus status)
          A helper function that can be called by the derived engine implementation to complete the current UI request.
private  void downloadMeProfileThumbnail(java.lang.String url, long localContactId)
          The call to download the thumbnail picture for the me profile.
private  void getMeProfileChanges()
          Sends a GetMyChanges request to the server, with the current version of the me profile used as a parameter.
 long getNextRunTime()
          Return the absolute time in milliseconds when the engine needs to run (based on System.currentTimeMillis).
 boolean isFirstTimeMeSyncComplete()
          This method TRUE if the Me Profile has been synced once.
private  boolean isReady()
          The condition for the sync me engine run.
private  void newState(SyncMeEngine.State newState)
          Changes the state of the engine.
 void onCreate()
          This will be called immediately after creation.
 void onDestroy()
          This will be called just before the engine is shutdown.
protected  void onRequestComplete()
          This function must be implemented in the derived engine implementation.
 void onReset()
          This method needs to be called as part of removeAllData()/changeUser() routine.
protected  void onTimeoutEvent()
          The derived engine implementation must call the processTimeout() function (normally from within the run() implementation), otherwise this will not be called.
 void processCommsResponse(ResponseQueue.Response resp)
          Called by framework when a response to a server request is received.
private  void processGetMyChangesResponse(ResponseQueue.Response resp)
          Processes the response from a GetMyChanges request.
private  void processMeProfileThumbnailResponse(ResponseQueue.Response resp)
          This method stores the thumbnail picture for the me profile
private  boolean processPushEvent(ResponseQueue.Response resp)
          This method process the "pc" push event.
private  void processSetMeResponse(ResponseQueue.Response resp)
          Processes the response from a SetMe request.
protected  void processUiRequest(ServiceUiRequest requestId, java.lang.Object data)
          The derived engine implementation must call the processUiQueue() function (normally from within the run() implementation), otherwise this will not be called.
private  void processUpdateStatusResponse(ResponseQueue.Response resp)
          This method processes the response to status update by setMe() method
 void run()
          Do some work but anything that takes longer than 1 second must be broken up.
private  ServiceStatus setFirstTimeMeSyncComplete(boolean value)
          Helper function to update the database when the state of the mFirstTimeMeSyncComplete flag changes.
private  ServiceStatus setFirstTimeSyncStarted(boolean value)
          Helper function to update the database when the state of the #mFirstTimeMeSyncStarted flag changes.
 void setSyncStatus(SyncStatus syncStatus)
          This method sets the current progress.
private static boolean updateRevisionPostUpdate(java.lang.Integer before, java.lang.Integer after, long currentFromRevision, DatabaseHelper db)
          Updates the revision of the me profile in the local state table after the SetMe has completed.
private  void uploadMeProfile()
          * Sends a SetMe request to the server in the case that the me profile has been changed locally.
private  void uploadStatusUpdate(ContactDetail statusDetail)
          Starts uploading a status update to the server and ignores all other
 
Methods inherited from class com.vodafone360.people.engine.BaseEngine
addUiRequestToQueue, clearTimeout, completeUiRequest, deactivateEngine, emptyUiRequestQueue, engineId, genericHandleResponseType, getCurrentTimeout, isCommsResponseOutstanding, isDeactivated, isUiRequestOutstanding, onCommsInMessage, processCommsInQueue, processTimeout, processUiQueue, setReqId, setTimeout
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

mState

private SyncMeEngine.State mState
Current engine state.


mDbHelper

private DatabaseHelper mDbHelper
Database.


mFromRevision

private long mFromRevision
The latest revision of Me Profile.


mUploadedMeDetails

private java.util.ArrayList<ContactDetail> mUploadedMeDetails
The Me Profile, as it was uploaded.


mFirstTimeSyncStarted

private boolean mFirstTimeSyncStarted
Indicates if the first time sync has been ever initiated.


mFirstTimeMeSyncComplete

private boolean mFirstTimeMeSyncComplete
Indicates if the first time sync has been completed.


MAX_QUALITY

private static final int MAX_QUALITY
See Also:
Constant Field Values

mUiAgent

private final UiAgent mUiAgent
UiAgent reference to update progress bar.


mCache

private final ApplicationCache mCache
ApplicationCache reference to update progress bar.


CONTACT_CHANGES

private static final java.lang.String CONTACT_CHANGES
The type of datatype we are expecting back from the server for upload/download me profile requests.

See Also:
Constant Field Values

PROGRESS_50

private static final int PROGRESS_50
Percentage used to show progress when the me sync is half complete.

See Also:
Constant Field Values
Constructor Detail

SyncMeEngine

public SyncMeEngine(BaseEngine.IEngineEventCallback eventCallback,
                    DatabaseHelper db)
The constructor.

Parameters:
eventCallback - IEngineEventCallback
db - DatabaseHelper - database.
Method Detail

getNextRunTime

public long getNextRunTime()
Description copied from class: BaseEngine
Return the absolute time in milliseconds when the engine needs to run (based on System.currentTimeMillis). (Maybe add currentTime in the future to use as the current time, to enable JUnit tests to validate timeout functionality).

Specified by:
getNextRunTime in class BaseEngine
Returns:
-1 never needs to run, 0 needs to run as soon as possible, CurrentTime + 60000 to run in 1 minute, etc.

isReady

private boolean isReady()
The condition for the sync me engine run.

Returns:
boolean - TRUE when the engine is ready to run.

run

public void run()
Description copied from class: BaseEngine
Do some work but anything that takes longer than 1 second must be broken up. (Maybe add currentTime as parameter, to enable JUnit tests to validate timeout functionality)

Specified by:
run in class BaseEngine

onCreate

public void onCreate()
Description copied from class: BaseEngine
This will be called immediately after creation.

Specified by:
onCreate in class BaseEngine

onDestroy

public void onDestroy()
Description copied from class: BaseEngine
This will be called just before the engine is shutdown.

Specified by:
onDestroy in class BaseEngine

onRequestComplete

protected void onRequestComplete()
Description copied from class: BaseEngine
This function must be implemented in the derived engine implementation. It can do any post-request complete cleanup.

Specified by:
onRequestComplete in class BaseEngine

onTimeoutEvent

protected void onTimeoutEvent()
Description copied from class: BaseEngine
The derived engine implementation must call the processTimeout() function (normally from within the run() implementation), otherwise this will not be called. This function will be called when a timeout occurs (started by setTimeout and cancelled by clearTimeout). If this function takes longer than 1 second to complete it should be broken up.

Specified by:
onTimeoutEvent in class BaseEngine

processUiRequest

protected final void processUiRequest(ServiceUiRequest requestId,
                                      java.lang.Object data)
Description copied from class: BaseEngine
The derived engine implementation must call the processUiQueue() function (normally from within the run() implementation), otherwise this will not be called. This function is called for each UI request that arrives on the queue. It should start processing the request. If this function takes longer than 1 second to complete, it should be broken up. Once a request is finished the processUiRequest function must be called.

Specified by:
processUiRequest in class BaseEngine
Parameters:
requestId - The UI request ID
data - Request data (object type is request specific)

getMeProfileChanges

private void getMeProfileChanges()
Sends a GetMyChanges request to the server, with the current version of the me profile used as a parameter.


downloadMeProfileThumbnail

private void downloadMeProfileThumbnail(java.lang.String url,
                                        long localContactId)
The call to download the thumbnail picture for the me profile.

Parameters:
url - String - picture url of Me Profile (comes with getMyChanges())
localContactId - long - local contact id of Me Profile

uploadStatusUpdate

private void uploadStatusUpdate(ContactDetail statusDetail)
Starts uploading a status update to the server and ignores all other

Parameters:
statusDetail - - status ContactDetail

uploadMeProfile

private void uploadMeProfile()
* Sends a SetMe request to the server in the case that the me profile has been changed locally. If the me profile thumbnail has always been changed it will also be uploaded to the server.


checkConnectivity

private boolean checkConnectivity()
Get current connectivity state from the NetworkAgent. If not connected completed UI request with COMMs error.

Returns:
true NetworkAgent reports we are connected, false otherwise.

newState

private void newState(SyncMeEngine.State newState)
Changes the state of the engine.

Parameters:
newState - The new state

processCommsResponse

public final void processCommsResponse(ResponseQueue.Response resp)
Called by framework when a response to a server request is received.

Specified by:
processCommsResponse in class BaseEngine
Parameters:
resp - The response received

processMeProfileThumbnailResponse

private void processMeProfileThumbnailResponse(ResponseQueue.Response resp)
This method stores the thumbnail picture for the me profile

Parameters:
resp - Response - normally contains ExternalResponseObject for the picture

processGetMyChangesResponse

private void processGetMyChangesResponse(ResponseQueue.Response resp)
Processes the response from a GetMyChanges request. The me profile data will be merged in the local database if the response is successful. Otherwise the processor will complete with a suitable error.

Parameters:
resp - Response from server.

processSetMeResponse

private void processSetMeResponse(ResponseQueue.Response resp)
Processes the response from a SetMe request. If successful, the server IDs will be stored in the local database if they have changed. Otherwise the processor will complete with a suitable error.

Parameters:
resp - Response from server.

processUpdateStatusResponse

private void processUpdateStatusResponse(ResponseQueue.Response resp)
This method processes the response to status update by setMe() method

Parameters:
resp - Response - the expected response datatype is ContactChanges

completeUiRequest

protected void completeUiRequest(ServiceStatus status)
Description copied from class: BaseEngine
A helper function that can be called by the derived engine implementation to complete the current UI request.

Overrides:
completeUiRequest in class BaseEngine
Parameters:
status - The result of the request

updateRevisionPostUpdate

private static boolean updateRevisionPostUpdate(java.lang.Integer before,
                                                java.lang.Integer after,
                                                long currentFromRevision,
                                                DatabaseHelper db)
Updates the revision of the me profile in the local state table after the SetMe has completed. This will only happen if the version of the me profile on the server before the update matches our previous version.

Parameters:
before - Version before the update
after - Version after the update
currentFromRevision - Current version from our database
db - Database helper used for storing the change
Returns:
true if the update was done, false otherwise.

addUpdateMeProfileContactRequest

public void addUpdateMeProfileContactRequest()
This method adds an external request to Contacts/setMe() method to update the Me Profile...

Parameters:
meProfile - Contact - contact to be pushed to the server

addUpdateMyStatusRequest

public void addUpdateMyStatusRequest(java.lang.String textStatus)
This method adds an external request to Contacts/setMe() method to update the Me Profile status...

Parameters:
textStatus - String - the new me profile status to be pushed to the server

addGetMeProfileContactRequest

private void addGetMeProfileContactRequest()
This method adds an external request to Contacts/getMyChanges() method to update the Me Profile status server. Is called when "pc "push message is received


addGetMeProfileContactFirstTimeRequest

public void addGetMeProfileContactFirstTimeRequest()
This method adds an external request to Contacts/getMyChanges() method to update the Me Profile status server, is called by the UI at the 1st sync.


processPushEvent

private boolean processPushEvent(ResponseQueue.Response resp)
This method process the "pc" push event.

Parameters:
resp - Response - server response normally containing a "pc" PushEvent data type
Returns:
boolean - TRUE if a push event was found in the response

setFirstTimeSyncStarted

private ServiceStatus setFirstTimeSyncStarted(boolean value)
Helper function to update the database when the state of the #mFirstTimeMeSyncStarted flag changes.

Parameters:
value - New value to the flag. True indicates that first time sync has been started. The flag is never set to false again by the engine, it will be only set to false when a remove user data is done (and the database is deleted).
Returns:
SUCCESS or a suitable error code if the database could not be updated.

setFirstTimeMeSyncComplete

private ServiceStatus setFirstTimeMeSyncComplete(boolean value)
Helper function to update the database when the state of the mFirstTimeMeSyncComplete flag changes.

Parameters:
value - New value to the flag. True indicates that first time sync has been completed. The flag is never set to false again by the engine, it will be only set to false when a remove user data is done (and the database is deleted).
Returns:
SUCCESS or a suitable error code if the database could not be updated.

onReset

public final void onReset()
This method needs to be called as part of removeAllData()/changeUser() routine.

Overrides:
onReset in class BaseEngine

isFirstTimeMeSyncComplete

public final boolean isFirstTimeMeSyncComplete()
This method TRUE if the Me Profile has been synced once.

Returns:
boolean - TRUE if the Me Profile has been synced once.

setSyncStatus

public void setSyncStatus(SyncStatus syncStatus)
                   throws java.security.InvalidParameterException
This method sets the current progress.

Parameters:
SyncStatus - Status of the processor, must not be NULL.
Throws:
java.security.InvalidParameterException - when SyncStatus is NULL.


JavaDoc