Search in sources :

Example 41 with ContactChange

use of com.vodafone360.people.engine.contactsync.ContactChange in project 360-Engine-for-Android by 360.

the class ContactDetailsTable method addNativeContactDetails.

/**
     * Adds the provided contact details to the ContactDetail table.
     * 
     * Note: the provided ContactChange are modified with the corresponding internalDetailId from the
     *       database insertion.
     * 
     * @see ContactChange
     * 
     * @param contactChange the contact details
     * @param writeableDb the db where to write
     * @return true if successful, false otherwise
     */
public static boolean addNativeContactDetails(ContactChange[] contactChange, SQLiteDatabase writeableDb) {
    try {
        final ContentValues values = new ContentValues();
        ContactChange currentChange;
        // go through the ContactChange and add their details to the ContactDetails table
        for (int i = 0; i < contactChange.length; i++) {
            currentChange = contactChange[i];
            values.clear();
            prepareNativeContactDetailInsert(currentChange, values);
            currentChange.setInternalDetailId(writeableDb.insertOrThrow(TABLE_NAME, null, values));
        }
    } catch (Exception e) {
        return false;
    }
    return true;
}
Also used : ContentValues(android.content.ContentValues) ContactChange(com.vodafone360.people.engine.contactsync.ContactChange) SQLiteException(android.database.sqlite.SQLiteException) SQLException(android.database.SQLException)

Example 42 with ContactChange

use of com.vodafone360.people.engine.contactsync.ContactChange in project 360-Engine-for-Android by 360.

the class ContactDetailsTable method getContactChanges.

/**
     * Gets an array of ContactChange from the contact's local id.
     * 
     * @see ContactChange
     * 
     * @param localId the local id of the contact to get
     * @return an array of ContactChange
     */
public static ContactChange[] getContactChanges(long localId, boolean nativeSyncableOnly, SQLiteDatabase readableDb) {
    final String[] SELECTION = { String.valueOf(localId) };
    final String QUERY_STRING = nativeSyncableOnly ? QUERY_NATIVE_SYNCABLE_CONTACT_DETAILS_BY_LOCAL_ID : QUERY_CONTACT_DETAILS_BY_LOCAL_ID;
    Cursor cursor = null;
    try {
        cursor = readableDb.rawQuery(QUERY_STRING, SELECTION);
        if (cursor.getCount() > 0) {
            final ContactChange[] changes = new ContactChange[cursor.getCount()];
            int index = 0;
            while (cursor.moveToNext()) {
                // fill the ContactChange class with contact detail data if not empty
                // StringVal=7
                String value = cursor.getString(7);
                // prevent null pointer (however should the detail be even stored in People DB if null?)
                if (value == null)
                    value = "";
                // Key=6
                final int key = cursor.isNull(6) ? ContactChange.KEY_UNKNOWN : mapInternalKeyToContactChangeKey(cursor.getInt(6));
                // Type=4, OrderNo=5
                final int flags = mapInternalTypeAndOrderToContactChangeFlag(cursor.isNull(4) ? 0 : cursor.getInt(4), cursor.getInt(5));
                // create the change
                final ContactChange change = new ContactChange(key, value, flags);
                changes[index++] = change;
                // LocalContactId=0
                change.setInternalContactId(cursor.isNull(0) ? ContactChange.INVALID_ID : cursor.getLong(0));
                // DetailLocalId=1
                change.setInternalDetailId(cursor.isNull(1) ? ContactChange.INVALID_ID : cursor.getInt(1));
                // NativeDetailId=2
                change.setNabDetailId(cursor.isNull(2) ? ContactChange.INVALID_ID : cursor.getLong(2));
                // NativeContactIdDup=3
                change.setNabContactId(cursor.isNull(3) ? ContactChange.INVALID_ID : cursor.getLong(3));
                // DetailServerId=8
                change.setBackendDetailId(cursor.isNull(8) ? ContactChange.INVALID_ID : cursor.getLong(8));
                if (nativeSyncableOnly) {
                    // in this mode, we have to tell if the detail is new or updated
                    if (change.getNabDetailId() == ContactChange.INVALID_ID) {
                        change.setType(ContactChange.TYPE_ADD_DETAIL);
                    } else {
                        change.setType(ContactChange.TYPE_UPDATE_DETAIL);
                    }
                }
            }
            if (index == changes.length) {
                return changes;
            } else if (index > 0) {
                // there were some empty details, need to trim the array
                final ContactChange[] trimmed = new ContactChange[index];
                System.arraycopy(changes, 0, trimmed, 0, index);
                return trimmed;
            }
        }
    } catch (Exception e) {
        // what else can we do?
        LogUtils.logE("ContactDetailsTable.getContactChanges(): " + e);
    } finally {
        CursorUtils.closeCursor(cursor);
    }
    return null;
}
Also used : Cursor(android.database.Cursor) ContactChange(com.vodafone360.people.engine.contactsync.ContactChange) SQLiteException(android.database.sqlite.SQLiteException) SQLException(android.database.SQLException)

Example 43 with ContactChange

use of com.vodafone360.people.engine.contactsync.ContactChange in project 360-Engine-for-Android by 360.

the class DatabaseHelper method convertContactChange.

/**
     * Converts a ContactChange object into an equivalent ContactDetail object.
     * 
     * @see ContactChange
     * @see ContactDetail
     * @param change the ContactChange to convert
     * @return the equivalent ContactDetail
     */
public ContactDetail convertContactChange(ContactChange change) {
    final ContactDetail detail = new ContactDetail();
    final int flag = change.getFlags();
    // conversion is not straightforward, needs a little tweak
    final int key = ContactDetailsTable.mapContactChangeKeyToInternalKey(change.getKey());
    detail.localContactID = change.getInternalContactId() != ContactChange.INVALID_ID ? change.getInternalContactId() : null;
    detail.localDetailID = change.getInternalDetailId() != ContactChange.INVALID_ID ? change.getInternalDetailId() : null;
    detail.nativeContactId = change.getNabContactId() != ContactChange.INVALID_ID ? new Integer((int) change.getNabContactId()) : null;
    detail.nativeDetailId = change.getNabDetailId() != ContactChange.INVALID_ID ? new Integer((int) change.getNabDetailId()) : null;
    detail.unique_id = change.getBackendDetailId() != ContactChange.INVALID_ID ? new Long(change.getBackendDetailId()) : null;
    detail.key = DetailKeys.values()[key];
    detail.keyType = DetailKeyTypes.values()[ContactDetailsTable.mapContactChangeFlagToInternalType(flag)];
    detail.value = change.getValue();
    detail.order = ContactDetailsTable.mapContactChangeFlagToInternalOrder(flag);
    return detail;
}
Also used : ContactDetail(com.vodafone360.people.datatypes.ContactDetail)

Example 44 with ContactChange

use of com.vodafone360.people.engine.contactsync.ContactChange in project 360-Engine-for-Android by 360.

the class DatabaseHelper method addNativeContact.

/**
     * Adds a native contact to the people database and makes sure that the
     * related tables are updated (Contact, ContactDetail, ContactSummary and
     * Activities).
     * 
     * @param contact the contact to add
     * @return true if successful, false otherwise
     */
public boolean addNativeContact(ContactChange[] contact) {
    if (contact == null || contact.length <= 0)
        return false;
    final SQLiteDatabase wdb = getWritableDatabase();
    try {
        wdb.beginTransaction();
        // add the contact in the Contacts table
        final ContentValues values = ContactsTable.getNativeContentValues(contact[0]);
        final long internalContactId = ContactsTable.addContact(values, wdb);
        if (internalContactId != -1) {
            // sets the newly created internal contact id to all the
            // ContactChange
            setInternalContactId(contact, internalContactId);
            // details
            if (!ContactDetailsTable.addNativeContactDetails(contact, wdb)) {
                return false;
            }
            // from this point, legacy code will be called...
            final Contact legacyContact = convertNativeContactChanges(contact);
            // ...update timeline and contact summary with legacy code...
            if (!updateTimelineAndContactSummaryWithLegacyCode(legacyContact, wdb)) {
                return false;
            }
        } else {
            return false;
        }
        wdb.setTransactionSuccessful();
        return true;
    } catch (Exception e) {
        LogUtils.logE("addNativeContact() - Error:" + e);
    } finally {
        if (wdb != null) {
            wdb.endTransaction();
        }
    }
    return false;
}
Also used : ContentValues(android.content.ContentValues) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SQLiteException(android.database.sqlite.SQLiteException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) SQLException(android.database.SQLException) Contact(com.vodafone360.people.datatypes.Contact)

Example 45 with ContactChange

use of com.vodafone360.people.engine.contactsync.ContactChange in project 360-Engine-for-Android by 360.

the class ContactChangeHelper method randomContactUpdate.

public static ContactChange[] randomContactUpdate(ContactChange[] contact) {
    final int originalDetailsNum = contact.length;
    final long internalCntId = contact[0].getInternalContactId();
    final long backendCntId = contact[0].getBackendContactId();
    final long nabCntId = contact[0].getNabContactId();
    int numDeletedDetails = 0, curNumDeletedDetails = 0, numAddedDetails = 0, curNumAddedDetails = 0, numChangedDetails = 0, curNumChangedDetails = 0;
    // At best we can only delete all but one detail
    numDeletedDetails = randomPositiveInt(originalDetailsNum - 1);
    // At best we can only change all details minus the ones we will delete
    numChangedDetails = randomPositiveInt(originalDetailsNum - numDeletedDetails);
    // Reasonable maximum of details we can add
    numAddedDetails = randomPositiveInt(MAX_NUM_DETAILS_ADD);
    final int updateCcListSize = numAddedDetails + numChangedDetails + numDeletedDetails;
    ContactChange[] updateCcList = new ContactChange[updateCcListSize];
    for (int i = 0; i < updateCcListSize; i++) {
        if (curNumChangedDetails < numChangedDetails) {
            updateCcList[i] = generateUpdateDetailChange(contact[i]);
            curNumChangedDetails++;
            continue;
        }
        if (curNumDeletedDetails < numDeletedDetails) {
            updateCcList[i] = generateDeleteDetailChange(contact[i]);
            curNumDeletedDetails++;
            continue;
        }
        if (curNumAddedDetails < numAddedDetails) {
            int key;
            do {
                // get a random key and don't let it be a unique key to simplify things
                key = randomKey();
                if (isKeyPlatformSupported(key)) {
                    if (!isUniqueKeyForContact(key) || (isUniqueKeyForContact(key) && !isKeyPresent(key, updateCcList) && !isKeyPresent(key, contact))) {
                        break;
                    }
                }
            } while (true);
            // The new detail, preferred flag not allowed to simplify things a little
            final ContactChange addDetailCc = randomContactChange(key, internalCntId, backendCntId, nabCntId, false);
            //addDetailCc.setInternalDetailId(randomPositiveLong());
            addDetailCc.setType(ContactChange.TYPE_ADD_DETAIL);
            updateCcList[i] = addDetailCc;
            curNumAddedDetails++;
        }
    }
    return updateCcList;
}
Also used : ContactChange(com.vodafone360.people.engine.contactsync.ContactChange)

Aggregations

ContactChange (com.vodafone360.people.engine.contactsync.ContactChange)33 Name (com.vodafone360.people.datatypes.VCardHelper.Name)6 Organisation (com.vodafone360.people.datatypes.VCardHelper.Organisation)6 PeopleContactsApi (com.vodafone360.people.engine.contactsync.PeopleContactsApi)6 Cursor (android.database.Cursor)5 Suppress (android.test.suitebuilder.annotation.Suppress)5 ContactDetail (com.vodafone360.people.datatypes.ContactDetail)5 ArrayList (java.util.ArrayList)5 SQLException (android.database.SQLException)4 SQLiteException (android.database.sqlite.SQLiteException)4 Uri (android.net.Uri)4 PostalAddress (com.vodafone360.people.datatypes.VCardHelper.PostalAddress)4 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)3 Contact (com.vodafone360.people.datatypes.Contact)3 ContentValues (android.content.ContentValues)2 StructuredName (android.provider.ContactsContract.CommonDataKinds.StructuredName)2 MediumTest (android.test.suitebuilder.annotation.MediumTest)2 SmallTest (android.test.suitebuilder.annotation.SmallTest)2 ContactIdInfo (com.vodafone360.people.database.tables.ContactsTable.ContactIdInfo)2 NativeContactsApi (com.vodafone360.people.engine.contactsync.NativeContactsApi)2