ewe.database
Class Synchronizer

java.lang.Object
  extended byewe.database.Synchronizer
Direct Known Subclasses:
DoubleSynchronizer, RemoteSynchronizer

public class Synchronizer
extends Object

This class provides utilities for synchronizing items in a mobile database with another. The methods in here only support synchronization with a single other database, you will have to implement more complex methods to support any other type of synchronization.


Field Summary
static int ALWAYS
           
static int CANCELLED
           
 boolean dontSynchronizeDeletedItems
          If this is true, then deleting an item on one database will not cause the item to be deleted in the other.
 boolean dontSyncJustDebug
          This goes through the motion of synchronization but does not actually make any changes to either database.
protected  int lastConflictCommand
           
static int LOCAL_TO_REMOTE
           
static int REMOTE_TO_LOCAL
           
 boolean remoteToLocalOnly
          If this is true then the local database will take entries from remote database but not vice versa.
static int RESOLVE_LATER
           
 Time syncTime
           
 
Constructor Summary
Synchronizer(Database database)
           
 
Method Summary
static void addOrReplace(DatabaseEntry entry, FoundEntries myEntriesSortedByOID)
          Given a remote entry, add or replace it in this database.
protected static int checkSort(Database database, String sortName)
           
 void close()
          This is called at the end of the syncrhonization.
 int countRemoteDeletedEntries()
           
 int countRemoteUnsynchronizedEntries()
           
 void erase(long oid, FoundEntries myEntriesSortedByOID)
          Given the OID of an entry in the remote database, erase it completely from this database.
 void eraseDeleted(long oid)
          This is called after an OID marked as deleted on this database has been erased from the remote database.
 void eraseDeletedOnRemote(int index)
           
 void eraseEntryOnRemote(long OID)
           
static int findByOID(FoundEntries entries, long oid)
          This locates the entry with the specified OID.
 FoundEntries getByOID(Handle h)
           
static FoundEntries getByOID(Handle h, Database database)
           
protected  String getDisplayString(DatabaseEntry entry)
           
static int getMatchingEntry(DatabaseEntry remoteEntry, FoundEntries myEntriesSortedByOID)
           
static long getOID(DatabaseEntry entry)
          Get the OID for an entry.
 long getRemoteDeletedEntry(int index)
           
 byte[] getRemoteUnsynchronizedEntry(int index)
           
protected  String getResolveString(DatabaseEntry remote, DatabaseEntry local)
           
 EntriesView getUnsynchronized(Handle h)
          This gets all the unsynchronized entries, but not the deleted entries.
static EntriesView getUnsynchronized(Handle h, Database database)
           
 boolean isSynchronized(DatabaseEntry entry)
          Returns the state of the synchronized flag.
static void markAsSynchronized(DatabaseEntry entry)
           
static void markAsSynchronized(EntriesView entries, int index)
          This marks the entry at the specified index as being synchronized.
 void markRemoteAsSynchronized(int index)
           
 int resolveConflict(DatabaseEntry remote, DatabaseEntry local)
          This must resolve a conflict when an item has been modified on both databases.
 void sendEntryToRemote(DatabaseEntry de)
           
 Handle synchronize()
           
 boolean synchronize(Handle progress)
           
 Handle synchronize(String title)
          Start the synchronization and display an optional progress bar.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, toString
 

Field Detail

syncTime

public Time syncTime

lastConflictCommand

protected int lastConflictCommand

REMOTE_TO_LOCAL

public static final int REMOTE_TO_LOCAL
See Also:
Constant Field Values

LOCAL_TO_REMOTE

public static final int LOCAL_TO_REMOTE
See Also:
Constant Field Values

RESOLVE_LATER

public static final int RESOLVE_LATER
See Also:
Constant Field Values

ALWAYS

public static final int ALWAYS
See Also:
Constant Field Values

CANCELLED

public static final int CANCELLED
See Also:
Constant Field Values

remoteToLocalOnly

public boolean remoteToLocalOnly
If this is true then the local database will take entries from remote database but not vice versa. It is false by default.


dontSynchronizeDeletedItems

public boolean dontSynchronizeDeletedItems
If this is true, then deleting an item on one database will not cause the item to be deleted in the other. It is false by default.


dontSyncJustDebug

public boolean dontSyncJustDebug
This goes through the motion of synchronization but does not actually make any changes to either database.

Constructor Detail

Synchronizer

public Synchronizer(Database database)
Method Detail

checkSort

protected static int checkSort(Database database,
                               String sortName)
                        throws IllegalStateException
Throws:
IllegalStateException

getUnsynchronized

public static EntriesView getUnsynchronized(Handle h,
                                            Database database)
                                     throws IOException,
                                            IllegalStateException
Throws:
IOException
IllegalStateException

getUnsynchronized

public EntriesView getUnsynchronized(Handle h)
                              throws IOException,
                                     IllegalStateException
This gets all the unsynchronized entries, but not the deleted entries. You can get deleted items from the database directly.

Throws:
IOException
IllegalStateException - If the database does not have the "_SortBySync" sort

markAsSynchronized

public static void markAsSynchronized(DatabaseEntry entry)

markAsSynchronized

public static void markAsSynchronized(EntriesView entries,
                                      int index)
                               throws IOException
This marks the entry at the specified index as being synchronized.

Throws:
IOException

getByOID

public static FoundEntries getByOID(Handle h,
                                    Database database)
                             throws IOException,
                                    IllegalStateException
Throws:
IOException
IllegalStateException

getByOID

public FoundEntries getByOID(Handle h)
                      throws IOException,
                             IllegalStateException
Throws:
IOException
IllegalStateException

findByOID

public static int findByOID(FoundEntries entries,
                            long oid)
                     throws IOException
This locates the entry with the specified OID. The FoundEntries object must have been found using the findByOID() method (i.e. using the sortByOid criteria).

Parameters:
entries - The FoundEntries of all the entries.
oid - The oid looking for.
Returns:
The index of the entry, -1 if it was not found.
Throws:
IOException

getOID

public static long getOID(DatabaseEntry entry)
                   throws IllegalStateException
Get the OID for an entry.

Parameters:
entry - The entry to look for.
Returns:
the OID for the entry.
Throws:
IllegalStateException - if the entry does not have an OID assigned.

isSynchronized

public boolean isSynchronized(DatabaseEntry entry)
                       throws IllegalStateException
Returns the state of the synchronized flag.

Parameters:
entry - The entry to check.
Returns:
true if the entry has the syncrhonized flag set, false if not.
Throws:
IllegalStateException - If synchronization data is not set in the entry.

getMatchingEntry

public static int getMatchingEntry(DatabaseEntry remoteEntry,
                                   FoundEntries myEntriesSortedByOID)
                            throws IOException,
                                   IllegalStateException
Throws:
IOException
IllegalStateException

addOrReplace

public static void addOrReplace(DatabaseEntry entry,
                                FoundEntries myEntriesSortedByOID)
                         throws IOException,
                                IllegalStateException
Given a remote entry, add or replace it in this database.

Throws:
IOException
IllegalStateException

eraseDeleted

public void eraseDeleted(long oid)
                  throws IOException
This is called after an OID marked as deleted on this database has been erased from the remote database. It erases it from this database too.

Parameters:
oid - The oid to erase.
Throws:
IOException

erase

public void erase(long oid,
                  FoundEntries myEntriesSortedByOID)
           throws IOException
Given the OID of an entry in the remote database, erase it completely from this database.

Throws:
IOException

countRemoteUnsynchronizedEntries

public int countRemoteUnsynchronizedEntries()
                                     throws IOException
Throws:
IOException

countRemoteDeletedEntries

public int countRemoteDeletedEntries()
                              throws IOException
Throws:
IOException

getRemoteDeletedEntry

public long getRemoteDeletedEntry(int index)
                           throws IOException
Throws:
IOException

eraseDeletedOnRemote

public void eraseDeletedOnRemote(int index)
                          throws IOException
Throws:
IOException

eraseEntryOnRemote

public void eraseEntryOnRemote(long OID)
                        throws IOException
Throws:
IOException

getRemoteUnsynchronizedEntry

public byte[] getRemoteUnsynchronizedEntry(int index)
                                    throws IOException
Throws:
IOException

markRemoteAsSynchronized

public void markRemoteAsSynchronized(int index)
                              throws IOException
Throws:
IOException

sendEntryToRemote

public void sendEntryToRemote(DatabaseEntry de)
                       throws IOException
Throws:
IOException

getDisplayString

protected String getDisplayString(DatabaseEntry entry)

getResolveString

protected String getResolveString(DatabaseEntry remote,
                                  DatabaseEntry local)

resolveConflict

public int resolveConflict(DatabaseEntry remote,
                           DatabaseEntry local)
This must resolve a conflict when an item has been modified on both databases.

Parameters:
remote - The remote entry.
local - The local entry.
Returns:
RESOLVE_LATER or REMOTE_TO_LOCAL or LOCAL_TO_REMOTE or CANCELLED. This can also be ORed with ALWAYS so that the value will apply to other conflicts.

close

public void close()
This is called at the end of the syncrhonization. Override if necessary.


synchronize

public Handle synchronize()

synchronize

public Handle synchronize(String title)
Start the synchronization and display an optional progress bar.

Parameters:
title - if this is non-null a progress bar display will be shown.
Returns:
The Handle that monitors the progress of the synchronization.

synchronize

public boolean synchronize(Handle progress)
                    throws IOException
Throws:
IOException