com.vodafone360.people.database.tables
Class ContactsTable

java.lang.Object
  extended by com.vodafone360.people.database.tables.ContactsTable

public abstract class ContactsTable
extends java.lang.Object

Contains all the functionality related to the Contacts database table. This table only contains high level information for a contact. Each contact contains many contact details (see ContactDetailsTable), these details include name, phone number, address, etc. This class is never instantiated hence all methods must be static.

Version:
%I%, %G%

Nested Class Summary
static class ContactsTable.ContactIdInfo
          Contains ID information used to identify a contact.
private static class ContactsTable.Field
          An enumeration of all the field names in the database.
 
Field Summary
private static int ABOUTME
           
private static int CONTACTID
           
private static int FRIEND
           
private static int GENDER
           
private static int LOCALID
          Column indices which match the query string returned by getQueryStringSql(String).
private static int NATIVE_CONTACTID
           
private static java.lang.String QUERY_CONTACT_NATIVE_ID_FROM_LOCAL_ID
          SQL query string to get the contact native id from its local id.
private static java.lang.String QUERY_NATIVE_CONTACTS_IDS
          SELECT NativeContactId FROM ContactsTable WHERE NativeContactId IS NOT NULL ORDER BY NativeContactId
private static java.lang.String SQL_STRING_LOCAL_ID_EQUAL_QUESTION_MARK
          String equal to "LocalId = ?"
private static int SYNCTOPHONE
           
static java.lang.String TABLE_NAME
          The name of the table as it appears in the database.
private static int UPDATED
           
private static int USERID
           
 
Constructor Summary
ContactsTable()
           
 
Method Summary
static ServiceStatus addContact(Contact contact, SQLiteDatabase writeableDb)
          Adds a contact to the table.
static long addContact(ContentValues contact, SQLiteDatabase writeableDb)
          Adds the provided contact.
static void create(SQLiteDatabase writeableDb)
          Create Contacts Table.
static ServiceStatus deleteContact(long localContactID, SQLiteDatabase writeableDb)
          Deletes a contact from the Contacts table.
static ServiceStatus fetchContact(long localContactId, Contact contact, SQLiteDatabase readableDb)
          Fetches a contact from the database.
static ContactsTable.ContactIdInfo fetchContactIdFromNative(int nativeContactId, SQLiteDatabase readableDb)
          Searches for a contact based on native ID and if found, returns the ID information
static java.util.List<Contact> fetchContactList(SQLiteDatabase readableDB)
          Fetches a List with Contacts from Database Uses the persistence helper to build the objects The Contactdetails are not read, only the contacts
static ServiceStatus fetchContactServerIdList(java.util.ArrayList<java.lang.Long> orderedServerIdList, SQLiteDatabase readableDb)
          Returns a complete list of server IDs from the Contacts table in ascending order.
static java.lang.Long fetchLocalFromNativeId(java.lang.Integer nativeContactId, SQLiteStatement statement)
          Finds a native ID in the table
static SQLiteStatement fetchLocalFromNativeIdStatement(SQLiteDatabase readableDb)
          Provides a statement that can be used to find a contact native ID in the table
static java.lang.Long fetchLocalFromServerId(java.lang.Long contactServerId, SQLiteStatement statement)
          Searches the table for a contact server ID and if found, returns the local ID
static SQLiteStatement fetchLocalFromServerIdStatement(SQLiteDatabase readableDb)
          Provides a statement that can be used to find a contact server ID in the table
static long fetchLocalIdFromUserId(java.lang.Long userId, SQLiteDatabase readableDb)
           
private static java.lang.Integer fetchNativeFromLocalId(java.lang.Long localContactId, SQLiteStatement statement)
          Returns the native ID associated with a contact
private static SQLiteStatement fetchNativeFromLocalIdStatement(SQLiteDatabase readableDb)
          Provides a statement that can be used to find a contact native ID in the table
static java.lang.Long fetchServerId(java.lang.Long localContactId, SQLiteDatabase readableDb)
          Finds the server ID associated with a contact
protected static boolean fetchSyncToPhone(long localContactId, SQLiteDatabase readableDb)
          Fetches the syncToPhone flag for a particular contact.
static long fetchUserIdFromLocalContactId(java.lang.Long localContactId, SQLiteDatabase readableDb)
           
private static ContentValues fillUpdateData(Contact contact)
          Returns a ContentValues object that can be used to insert or modify a contact in the table.
private static java.lang.String getFullQueryList()
          Fetches the list of table fields that can be injected into an SQL query statement.
static long getNativeContactId(long localContactId, SQLiteDatabase readableDb)
          Gets the native contact id.
static long[] getNativeContactsIds(SQLiteDatabase readableDb)
          Gets a list of native ids for the contacts.
static ContentValues getNativeContentValues(ContactChange contactChange)
          Gets the native related ContentValues for the provided ContactChange.
private static void getQueryData(Cursor c, Contact contact)
          Reads a cursor at its current position to populate a Contact object
private static java.lang.String getQueryStringSql(java.lang.String whereClause)
          Returns a full SQL query statement to fetch the contact information.
static ServiceStatus modifyContact(Contact contact, SQLiteDatabase writableDb)
          Updates contact information.
static boolean modifyContactServerId(long localContactId, java.lang.Long serverId, java.lang.Long userId, SQLiteDatabase writeableDb)
          Modifies the contact server ID and user ID stored in the contacts table.
static Cursor openContactsCursor(SQLiteDatabase readableDb)
          Selects all contacts from the contacts table
static boolean setNativeContactId(long localContactId, long nativeContactId, SQLiteDatabase writableDb)
          Sets the native contact id for a contact.
static ServiceStatus syncSetNativeIds(java.util.List<ContactsTable.ContactIdInfo> contactIdList, SQLiteDatabase writableDb)
          Updates the native IDs for a list of contacts.
static ServiceStatus syncSetServerIds(java.util.List<DatabaseHelper.ServerIdInfo> serverIdList, java.util.List<ContactsTable.ContactIdInfo> dupList, SQLiteDatabase writableDb)
          Updates the server and user IDs for a list of contacts.
static ContactsTable.ContactIdInfo validateContactId(long localContactId, SQLiteDatabase readableDb)
          Validates a contact exists and returns the relevant contact id information.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TABLE_NAME

public static final java.lang.String TABLE_NAME
The name of the table as it appears in the database.

See Also:
Constant Field Values

LOCALID

private static final int LOCALID
Column indices which match the query string returned by getQueryStringSql(String).

See Also:
Constant Field Values

CONTACTID

private static final int CONTACTID
See Also:
Constant Field Values

USERID

private static final int USERID
See Also:
Constant Field Values

ABOUTME

private static final int ABOUTME
See Also:
Constant Field Values

FRIEND

private static final int FRIEND
See Also:
Constant Field Values

GENDER

private static final int GENDER
See Also:
Constant Field Values

UPDATED

private static final int UPDATED
See Also:
Constant Field Values

NATIVE_CONTACTID

private static final int NATIVE_CONTACTID
See Also:
Constant Field Values

SYNCTOPHONE

private static final int SYNCTOPHONE
See Also:
Constant Field Values

QUERY_NATIVE_CONTACTS_IDS

private static final java.lang.String QUERY_NATIVE_CONTACTS_IDS
SELECT NativeContactId FROM ContactsTable WHERE NativeContactId IS NOT NULL ORDER BY NativeContactId


QUERY_CONTACT_NATIVE_ID_FROM_LOCAL_ID

private static final java.lang.String QUERY_CONTACT_NATIVE_ID_FROM_LOCAL_ID
SQL query string to get the contact native id from its local id.


SQL_STRING_LOCAL_ID_EQUAL_QUESTION_MARK

private static final java.lang.String SQL_STRING_LOCAL_ID_EQUAL_QUESTION_MARK
String equal to "LocalId = ?" for SQL queries.

Constructor Detail

ContactsTable

public ContactsTable()
Method Detail

create

public static void create(SQLiteDatabase writeableDb)
                   throws SQLException
Create Contacts Table.

Parameters:
writeableDb - A writable SQLite database
Throws:
SQLException - If an SQL compilation error occurs

getFullQueryList

private static java.lang.String getFullQueryList()
Fetches the list of table fields that can be injected into an SQL query statement. The getQueryData(Cursor, Contact) method can be used to obtain the data from the query.

Returns:
The query string

getQueryStringSql

private static java.lang.String getQueryStringSql(java.lang.String whereClause)
Returns a full SQL query statement to fetch the contact information. The getQueryData(Cursor, Contact) method can be used to obtain the data from the query.

Parameters:
whereClause - An SQL where clause (without the "WHERE"). Cannot be null.
Returns:
The query string

getQueryData

private static void getQueryData(Cursor c,
                                 Contact contact)
Reads a cursor at its current position to populate a Contact object

Parameters:
c - A cursor returned from a SELECT query on the projection returned by getFullQueryList().
contact - An newly constructed Contact object that will be filled in.

fillUpdateData

private static ContentValues fillUpdateData(Contact contact)
Returns a ContentValues object that can be used to insert or modify a contact in the table.

Parameters:
contact - The source contact object
Returns:
The ContentValues object containing the data.

validateContactId

public static ContactsTable.ContactIdInfo validateContactId(long localContactId,
                                                            SQLiteDatabase readableDb)
Validates a contact exists and returns the relevant contact id information.

Parameters:
localContactId - The local Id of the contact to find
readableDb - Readable database object
Returns:
A ContactIdInfo object if successful, or NULL if the contact does not exist

deleteContact

public static ServiceStatus deleteContact(long localContactID,
                                          SQLiteDatabase writeableDb)
Deletes a contact from the Contacts table.

Parameters:
localContactID - The primary key ID of the contact
writeableDb - Writeable SQLite database
Returns:
SUCCESS or a suitable error code

modifyContactServerId

public static boolean modifyContactServerId(long localContactId,
                                            java.lang.Long serverId,
                                            java.lang.Long userId,
                                            SQLiteDatabase writeableDb)
Modifies the contact server ID and user ID stored in the contacts table.

Parameters:
localContactId - The primary key ID of the contact
serverId - The new server ID
userId - The nwe user ID
writeableDb - Writeable SQLite database
Returns:
SUCCESS or a suitable error code

fetchContactIdFromNative

public static ContactsTable.ContactIdInfo fetchContactIdFromNative(int nativeContactId,
                                                                   SQLiteDatabase readableDb)
Searches for a contact based on native ID and if found, returns the ID information

Parameters:
nativeContactId - The native contact ID to search for
readableDb - Readable SQLite database
Returns:
A ContactIdInfo object if found, otherwise null.

addContact

public static ServiceStatus addContact(Contact contact,
                                       SQLiteDatabase writeableDb)
Adds a contact to the table. See fillUpdateData(Contact) for which fields from the Contact object are read.

Parameters:
contact - The source Contact object
writeableDb - Writeable SQLite database
Returns:
SUCCESS or a suitable error code

fetchServerId

public static java.lang.Long fetchServerId(java.lang.Long localContactId,
                                           SQLiteDatabase readableDb)
Finds the server ID associated with a contact

Parameters:
localContactId - The primary key ID of the contact to find
readableDb - Readable SQLite database
Returns:
The server ID if found, otherwise null.

modifyContact

public static ServiceStatus modifyContact(Contact contact,
                                          SQLiteDatabase writableDb)
Updates contact information. See fillUpdateData(Contact) for which fields from the Contact object are read.

Parameters:
contact - The source contact object
writeableDb - Writeable SQLite database
Returns:
SUCCESS or a suitable error code

fetchContact

public static ServiceStatus fetchContact(long localContactId,
                                         Contact contact,
                                         SQLiteDatabase readableDb)
Fetches a contact from the database.

Parameters:
localContactId - The primary key ID of the contact to find
contact - A newly created contact object that will be populated by this function.
readableDb - Readable SQLite database
Returns:
SUCCESS, ERROR_NOT_FOUND or another suitable error code

fetchContactServerIdList

public static ServiceStatus fetchContactServerIdList(java.util.ArrayList<java.lang.Long> orderedServerIdList,
                                                     SQLiteDatabase readableDb)
Returns a complete list of server IDs from the Contacts table in ascending order.

Parameters:
orderedServerIdList - A list that will be populated with the ordered server IDs
readableDb - Readable SQLite database
Returns:
SUCCESS or a suitable error code

fetchLocalFromServerIdStatement

public static SQLiteStatement fetchLocalFromServerIdStatement(SQLiteDatabase readableDb)
Provides a statement that can be used to find a contact server ID in the table

Parameters:
readableDb - Readable SQLite database
Returns:
The SQLite statement
See Also:
fetchLocalFromServerId(Long, SQLiteStatement)

fetchLocalFromServerId

public static java.lang.Long fetchLocalFromServerId(java.lang.Long contactServerId,
                                                    SQLiteStatement statement)
Searches the table for a contact server ID and if found, returns the local ID

Parameters:
contactServerId - The server ID
statement - The statement returned by fetchLocalFromServerIdStatement(SQLiteDatabase)
Returns:
The local contact ID or NULL if it server ID was not found.
See Also:
fetchLocalFromServerIdStatement(SQLiteDatabase)

syncSetServerIds

public static ServiceStatus syncSetServerIds(java.util.List<DatabaseHelper.ServerIdInfo> serverIdList,
                                             java.util.List<ContactsTable.ContactIdInfo> dupList,
                                             SQLiteDatabase writableDb)
Updates the server and user IDs for a list of contacts. Also prepares a list of duplicates which will be filled with the Ids for contacts already present in the table (i.e. server ID has already been used). In the case that a duplicate is found, the ContactIdInfo object will also include the local ID of the original contact (see ContactIdInfo#mergedLocalId ).

Parameters:
serverIdList - A list of ServerIdInfo objects. For each object, the local ID must match a local contact ID in the table. The Server ID and User ID will be used for the update.
dupList - On return this will be populated with a list of contacts which have server IDs already present in the table.
writeableDb - Writeable SQLite database
Returns:
SUCCESS or a suitable error code

syncSetNativeIds

public static ServiceStatus syncSetNativeIds(java.util.List<ContactsTable.ContactIdInfo> contactIdList,
                                             SQLiteDatabase writableDb)
Updates the native IDs for a list of contacts.

Parameters:
contactIdList - A list of ContactIdInfo objects. For each object, the local ID must match a local contact ID in the table. The Native ID will be used for the update. Other fields are unused.
writeableDb - Writeable SQLite database
Returns:
SUCCESS or a suitable error code

fetchLocalFromNativeIdStatement

public static SQLiteStatement fetchLocalFromNativeIdStatement(SQLiteDatabase readableDb)
Provides a statement that can be used to find a contact native ID in the table

Parameters:
readableDb - Readable SQLite database
Returns:
The SQLite statement
See Also:
fetchLocalFromNativeId(Integer, SQLiteStatement)

fetchLocalFromNativeId

public static java.lang.Long fetchLocalFromNativeId(java.lang.Integer nativeContactId,
                                                    SQLiteStatement statement)
Finds a native ID in the table

Parameters:
nativeContactId - The native ID to find
statement - The statement returned by fetchLocalFromNativeIdStatement(SQLiteDatabase).
Returns:
The local contact ID if the contact is found, or NULL.
See Also:
fetchLocalFromNativeIdStatement(SQLiteDatabase)

fetchSyncToPhone

protected static boolean fetchSyncToPhone(long localContactId,
                                          SQLiteDatabase readableDb)
Fetches the syncToPhone flag for a particular contact.

Parameters:
localContactId - The primary key ID of the contact to find
readableDb - Readable SQLite database
Returns:
The state of the syncToPhone flag (true or false). Defaults to false if the contact is not found or a database error occurs.

fetchNativeFromLocalIdStatement

private static SQLiteStatement fetchNativeFromLocalIdStatement(SQLiteDatabase readableDb)
Provides a statement that can be used to find a contact native ID in the table

Parameters:
readableDb - Readable SQLite database
Returns:
The SQLite statement
See Also:
fetchNativeFromLocalId(Long, SQLiteStatement)

fetchNativeFromLocalId

private static java.lang.Integer fetchNativeFromLocalId(java.lang.Long localContactId,
                                                        SQLiteStatement statement)
Returns the native ID associated with a contact

Parameters:
localContactId - The primary key ID of the contact to find
statement - The statement provided by fetchNativeFromLocalIdStatement(SQLiteDatabase).
Returns:
Native Contact ID or NULL if the contact was not found.
See Also:
fetchNativeFromLocalIdStatement(SQLiteDatabase)

fetchLocalIdFromUserId

public static long fetchLocalIdFromUserId(java.lang.Long userId,
                                          SQLiteDatabase readableDb)

fetchUserIdFromLocalContactId

public static long fetchUserIdFromLocalContactId(java.lang.Long localContactId,
                                                 SQLiteDatabase readableDb)

openContactsCursor

public static Cursor openContactsCursor(SQLiteDatabase readableDb)
Selects all contacts from the contacts table

Parameters:
readableDb - A readable Database
Returns:
Cursor for the select

fetchContactList

public static java.util.List<Contact> fetchContactList(SQLiteDatabase readableDB)
Fetches a List with Contacts from Database Uses the persistence helper to build the objects The Contactdetails are not read, only the contacts

Parameters:
readableDB - A readable Database
Returns:
java.util.list with contacts

getNativeContactsIds

public static long[] getNativeContactsIds(SQLiteDatabase readableDb)
Gets a list of native ids for the contacts.

Parameters:
readableDb - the people database to query from
Returns:
an ordered array containing all the found native ids, null if none

getNativeContentValues

public static ContentValues getNativeContentValues(ContactChange contactChange)
Gets the native related ContentValues for the provided ContactChange.

Parameters:
contactChange - the ContactChange from which to extract the native values
Returns:
the native ContentValues

addContact

public static long addContact(ContentValues contact,
                              SQLiteDatabase writeableDb)
Adds the provided contact.

Parameters:
contact - the ContentValues representing the contact to add
Returns:
the contact id if successful, -1 if the insertion failed

getNativeContactId

public static long getNativeContactId(long localContactId,
                                      SQLiteDatabase readableDb)
Gets the native contact id.

Parameters:
localContactId - the local id of the contact
readDatabase - the database to read from
Returns:
the native contact id, -1 otherwise

setNativeContactId

public static boolean setNativeContactId(long localContactId,
                                         long nativeContactId,
                                         SQLiteDatabase writableDb)
Sets the native contact id for a contact.

Parameters:
localContactId - the local contact id
nativeContactId - the native contact id to set
writableDb - the database where to write
Returns:
true if successful, false otherwise


JavaDoc