Search in sources :

Example 11 with ContactIdInfo

use of com.vodafone360.people.database.tables.ContactsTable.ContactIdInfo in project 360-Engine-for-Android by 360.

the class NowPlusContactsTableTest method testSyncSetNativeIds.

@MediumTest
public void testSyncSetNativeIds() {
    final String fnName = "testSyncSetNativeIds";
    mTestStep = 1;
    Log.i(LOG_TAG, "***** EXECUTING " + fnName + "*****");
    SQLiteDatabase writableDb = mTestDatabase.getWritableDatabase();
    SQLiteDatabase readableDb = mTestDatabase.getReadableDatabase();
    startSubTest(fnName, "Creating table");
    createTable();
    // add contacts and populate contactServerIdList
    ServiceStatus status;
    List<ContactIdInfo> contactIdList = new ArrayList<ContactIdInfo>();
    for (int i = 0; i < NUM_OF_CONTACTS; i++) {
        Contact c = mTestModule.createDummyContactData();
        c.userID = TestModule.generateRandomLong();
        status = ContactsTable.addContact(c, writableDb);
        assertEquals(ServiceStatus.SUCCESS, status);
        ContactIdInfo contactInfo = new ContactIdInfo();
        contactInfo.localId = c.localContactID;
        contactInfo.nativeId = TestModule.generateRandomInt();
        contactIdList.add(contactInfo);
    }
    status = ContactsTable.syncSetNativeIds(contactIdList, writableDb);
    assertEquals(ServiceStatus.SUCCESS, status);
    for (ContactIdInfo contactIdInfo : contactIdList) {
        Contact fetchedContact = new Contact();
        status = ContactsTable.fetchContact(contactIdInfo.localId, fetchedContact, readableDb);
        assertEquals(ServiceStatus.SUCCESS, status);
        assertEquals(contactIdInfo.nativeId, fetchedContact.nativeContactId);
    }
}
Also used : ContactIdInfo(com.vodafone360.people.database.tables.ContactsTable.ContactIdInfo) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) ServiceStatus(com.vodafone360.people.service.ServiceStatus) ArrayList(java.util.ArrayList) Contact(com.vodafone360.people.datatypes.Contact) MediumTest(android.test.suitebuilder.annotation.MediumTest)

Example 12 with ContactIdInfo

use of com.vodafone360.people.database.tables.ContactsTable.ContactIdInfo in project 360-Engine-for-Android by 360.

the class NowPlusContactsTableTest method testValidateContactId.

@MediumTest
public void testValidateContactId() {
    final String fnName = "testValidateContactId";
    mTestStep = 1;
    Log.i(LOG_TAG, "***** EXECUTING " + fnName + "*****");
    Log.i(LOG_TAG, "Validates a contact id");
    SQLiteDatabase writableDb = mTestDatabase.getWritableDatabase();
    SQLiteDatabase readableDb = mTestDatabase.getWritableDatabase();
    startSubTest(fnName, "Creating table");
    createTable();
    startSubTest(fnName, "Add Contact");
    Contact contact = mTestModule.createDummyContactData();
    Long serverId = TestModule.generateRandomLong();
    ServiceStatus status = ContactsTable.addContact(contact, readableDb);
    assertEquals(ServiceStatus.SUCCESS, status);
    assertTrue(ContactsTable.modifyContactServerId(contact.localContactID, serverId, contact.userID, writableDb));
    ContactIdInfo idInfo = ContactsTable.validateContactId(contact.localContactID, readableDb);
    assertEquals(serverId, idInfo.serverId);
    assertTrue(contact.localContactID == idInfo.localId);
    Long fetchedServerId = ContactsTable.fetchServerId(contact.localContactID, readableDb);
    assertEquals(serverId, fetchedServerId);
    SQLiteStatement statement = ContactsTable.fetchLocalFromServerIdStatement(readableDb);
    assertTrue(statement != null);
    Long localId = ContactsTable.fetchLocalFromServerId(serverId, statement);
    assertEquals(Long.valueOf(idInfo.localId), localId);
    localId = ContactsTable.fetchLocalFromServerId(serverId, null);
    assertEquals(localId, null);
    Log.i(LOG_TAG, "***********************************************");
    Log.i(LOG_TAG, fnName + " has completed successfully");
    Log.i(LOG_TAG, "***********************************************");
    Log.i(LOG_TAG, "");
}
Also used : ContactIdInfo(com.vodafone360.people.database.tables.ContactsTable.ContactIdInfo) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SQLiteStatement(android.database.sqlite.SQLiteStatement) ServiceStatus(com.vodafone360.people.service.ServiceStatus) Contact(com.vodafone360.people.datatypes.Contact) MediumTest(android.test.suitebuilder.annotation.MediumTest)

Example 13 with ContactIdInfo

use of com.vodafone360.people.database.tables.ContactsTable.ContactIdInfo in project 360-Engine-for-Android by 360.

the class PeopleContactsApi method getContact.

/**
 * Gets a contact from its native id.
 *
 * @param nativeId the native id of the contact
 * @return an array of ContactChange representing the contact, null if not found
 */
public ContactChange[] getContact(long nativeId) {
    try {
        final SQLiteDatabase readableDb = mDbh.getReadableDatabase();
        if (NativeChangeLogTable.isContactChangeInList(nativeId, ContactChangeType.DELETE_CONTACT, readableDb)) {
            // the contact exists as a deleted contact in the NativeChangeLogTable
            // return one ContactChange with the deleted contact flag
            ContactChange[] changes = new ContactChange[1];
            changes[0] = new ContactChange(ContactChange.TYPE_DELETE_CONTACT);
            return changes;
        } else {
            // get the corresponding local contact id
            final ContactIdInfo info = ContactsTable.fetchContactIdFromNative((int) nativeId, readableDb);
            if (info != null) {
                // we found the contact on CAB, let's get the details
                final ContactChange[] existingDetails = ContactDetailsTable.getContactChanges((long) info.localId, false, readableDb);
                // get also the deleted details if any
                final ContactChange[] deletedDetails = NativeChangeLogTable.getDeletedDetails((long) info.localId, readableDb);
                if (existingDetails != null && deletedDetails != null) {
                    // merge both arrays
                    ContactChange[] mergedDetails = new ContactChange[existingDetails.length + deletedDetails.length];
                    System.arraycopy(existingDetails, 0, mergedDetails, 0, existingDetails.length);
                    System.arraycopy(deletedDetails, 0, mergedDetails, existingDetails.length, deletedDetails.length);
                    return mergedDetails;
                } else if (existingDetails != null) {
                    return existingDetails;
                } else {
                    return deletedDetails;
                }
            }
        }
    } catch (Exception e) {
        LogUtils.logE("getContact(" + nativeId + "), error: " + e);
    }
    // no contact found
    return null;
}
Also used : ContactIdInfo(com.vodafone360.people.database.tables.ContactsTable.ContactIdInfo) SQLiteDatabase(android.database.sqlite.SQLiteDatabase)

Example 14 with ContactIdInfo

use of com.vodafone360.people.database.tables.ContactsTable.ContactIdInfo in project 360-Engine-for-Android by 360.

the class UploadServerContacts method processNewContactsResp.

/**
 * Called when a server response is received during a new contact sync. The
 * server ID, user ID and contact detail unique IDs are extracted from the
 * response and the NowPlus database updated. Possibly server errors are
 * also handled.
 *
 * @param resp Response from server.
 */
private void processNewContactsResp(final DecodedResponse resp) {
    ServiceStatus status = BaseEngine.getResponseStatus(BaseDataType.CONTACT_CHANGES_DATA_TYPE, resp.mDataTypes);
    if (status == ServiceStatus.SUCCESS) {
        ContactChanges contactChanges = (ContactChanges) resp.mDataTypes.get(0);
        ListIterator<Contact> itContactSrc = contactChanges.mContacts.listIterator();
        ListIterator<Contact> itContactDest = mContactChangeList.listIterator();
        List<ServerIdInfo> contactServerIdList = new ArrayList<ServerIdInfo>();
        List<ServerIdInfo> detailServerIdList = new ArrayList<ServerIdInfo>();
        while (itContactSrc.hasNext()) {
            if (!itContactDest.hasNext()) {
                /**
                 * The response should contain the same number of contacts
                 * as was supplied but must handle the error.
                 */
                status = ServiceStatus.ERROR_COMMS_BAD_RESPONSE;
                break;
            }
            Contact contactSrc = itContactSrc.next();
            Contact contactDest = itContactDest.next();
            if (Settings.ENABLED_CONTACTS_SYNC_TRACE) {
                String name = null;
                String sns = null;
                for (ContactDetail detail : contactDest.details) {
                    if (detail.key == ContactDetail.DetailKeys.VCARD_NAME) {
                        if (detail.value != null) {
                            VCardHelper.Name nameObj = detail.getName();
                            if (nameObj != null) {
                                name = nameObj.toString();
                            }
                        }
                    }
                    if (detail.key == ContactDetail.DetailKeys.VCARD_INTERNET_ADDRESS) {
                        sns = detail.alt;
                    }
                }
                LogUtils.logV("UploadServerContacts." + "processNewContactsResp() Contact uploaded: SID" + " = " + contactSrc.contactID + ", name = " + name + ", sns = " + sns + ", no of details = " + contactDest.details.size() + ", deleted=" + contactSrc.deleted);
            }
            if (contactSrc.contactID != null && contactSrc.contactID.longValue() != -1L) {
                if (contactDest.contactID == null || !contactDest.contactID.equals(contactSrc.contactID)) {
                    ServerIdInfo info = new ServerIdInfo();
                    info.localId = contactDest.localContactID;
                    info.serverId = contactSrc.contactID;
                    info.userId = contactSrc.userID;
                    contactServerIdList.add(info);
                }
            } else {
                LogUtils.logE("UploadServerContacts." + "processNewContactsResp() The server failed to " + "add the following contact: " + contactDest.localContactID + ", server ID = " + contactDest.contactID);
                mFailureList += "Failed to add contact: " + contactDest.localContactID + "\n";
                for (ContactDetail d : contactDest.details) {
                    LogUtils.logV("Failed Contact Info: " + contactDest.localContactID + ", Detail: " + d.key + ", " + d.keyType + " = " + d.value);
                }
            }
            status = handleUploadDetailChanges(contactSrc, contactDest, detailServerIdList);
        }
        if (status != ServiceStatus.SUCCESS) {
            /**
             * Something is going wrong - cancel the update *
             */
            complete(status);
            return;
        }
        long startTime = System.nanoTime();
        List<ContactIdInfo> dupList = new ArrayList<ContactIdInfo>();
        status = ContactsTable.syncSetServerIds(contactServerIdList, dupList, mDb.getWritableDatabase());
        if (status != ServiceStatus.SUCCESS) {
            complete(status);
            return;
        }
        status = ContactDetailsTable.syncSetServerIds(detailServerIdList, mDb.getWritableDatabase());
        if (status != ServiceStatus.SUCCESS) {
            complete(status);
            return;
        }
        if (dupList.size() > 0) {
            LogUtils.logV("UploadServerContacts.processNewContactsResp() Found " + dupList.size() + " duplicate contacts. Trying to remove them...");
            if (VersionUtils.is2XPlatform()) {
                // This is a very important distinction for 2.X devices!
                // the NAB IDs from the contacts we first import are stripped away
                // So we won't have the correct ID if syncMergeContactList() is executed
                // This is critical because a chain reaction will cause a Contact Delete in the end
                // Instead we can syncDeleteContactList() which should be safe on 2.X!
                status = mDb.syncDeleteContactList(dupList, false, true);
            } else {
                status = mDb.syncMergeContactList(dupList);
            }
            if (status != ServiceStatus.SUCCESS) {
                complete(status);
                return;
            }
            markDbChanged();
        }
        mDbSyncTime += (System.nanoTime() - startTime);
        while (itContactDest.hasNext()) {
            Contact contactDest = itContactDest.next();
            LogUtils.logE("UploadServerContacts.processNewContactsResp() " + "The server failed to add the following contact (not " + "included in returned list): " + contactDest.localContactID);
            mFailureList += "Failed to add contact (missing from return " + "list): " + contactDest.localContactID + "\n";
        }
        updateProgress();
        sendNextContactAdditionsPage();
        return;
    }
    complete(status);
}
Also used : ContactIdInfo(com.vodafone360.people.database.tables.ContactsTable.ContactIdInfo) ArrayList(java.util.ArrayList) ServerIdInfo(com.vodafone360.people.database.DatabaseHelper.ServerIdInfo) ContactChanges(com.vodafone360.people.datatypes.ContactChanges) Contact(com.vodafone360.people.datatypes.Contact) ContactDetail(com.vodafone360.people.datatypes.ContactDetail) ServiceStatus(com.vodafone360.people.service.ServiceStatus) VCardHelper(com.vodafone360.people.datatypes.VCardHelper)

Example 15 with ContactIdInfo

use of com.vodafone360.people.database.tables.ContactsTable.ContactIdInfo in project 360-Engine-for-Android by 360.

the class PeopleContactsApiTest method testGetNativeContactsIdsMerge.

/**
 * Tests the getNativeContactsIds() method when a merge has to be performed.
 */
public void testGetNativeContactsIdsMerge() {
    final long[] NATIVE_IDS = { 10, 85, 12, 103, 44, 38 };
    final long[] DELETED_IDS = { NATIVE_IDS[0], NATIVE_IDS[5], NATIVE_IDS[3] };
    PeopleContactsApi pca = new PeopleContactsApi(mDatabaseHelper);
    // the database is empty, it shall not return any ids
    assertNull(pca.getNativeContactsIds());
    // let's add the contacts
    for (int i = 0; i < NATIVE_IDS.length; i++) {
        ContactChange[] contact = filterContactChanges(ContactChangeHelper.randomContact(ContactChange.INVALID_ID, ContactChange.INVALID_ID, NATIVE_IDS[i]));
        assertTrue(pca.addNativeContact(contact));
    }
    // check that they exist and in the right order (ascending)
    long[] ids = pca.getNativeContactsIds();
    assertEquals(NATIVE_IDS.length, ids.length);
    assertTrue(checkExistAndAscendingOrder(NATIVE_IDS, ids));
    // delete them from CAB as it was coming from backend or user
    // this has to be done without PeopleContactsApi wrapper as it does not support
    // operations for Contacts not related to the native address book
    List<ContactIdInfo> ciiList = new ArrayList<ContactIdInfo>(DELETED_IDS.length);
    final SQLiteDatabase readableDb = mDatabaseHelper.getReadableDatabase();
    try {
        final SQLiteStatement nativeToLocalId = ContactsTable.fetchLocalFromNativeIdStatement(readableDb);
        for (int i = 0; i < DELETED_IDS.length; i++) {
            final ContactIdInfo cii = new ContactIdInfo();
            cii.localId = ContactsTable.fetchLocalFromNativeId((int) DELETED_IDS[i], nativeToLocalId);
            cii.nativeId = (int) DELETED_IDS[i];
            ciiList.add(cii);
        }
    } finally {
        readableDb.close();
    }
    mDatabaseHelper.syncDeleteContactList(ciiList, false, true);
    // check the returned list of ids which should be similar as before
    ids = pca.getNativeContactsIds();
    assertEquals(NATIVE_IDS.length, ids.length);
    assertTrue(checkExistAndAscendingOrder(NATIVE_IDS, ids));
    // check that the deleted ids have the deleted flag
    for (int i = 0; i < DELETED_IDS.length; i++) {
        final ContactChange[] cc = pca.getContact(DELETED_IDS[i]);
        assertEquals(cc[0].getType(), ContactChange.TYPE_DELETE_CONTACT);
    }
}
Also used : ContactIdInfo(com.vodafone360.people.database.tables.ContactsTable.ContactIdInfo) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SQLiteStatement(android.database.sqlite.SQLiteStatement) ArrayList(java.util.ArrayList) PeopleContactsApi(com.vodafone360.people.engine.contactsync.PeopleContactsApi) ContactChange(com.vodafone360.people.engine.contactsync.ContactChange)

Aggregations

ServiceStatus (com.vodafone360.people.service.ServiceStatus)13 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)11 ContactIdInfo (com.vodafone360.people.database.tables.ContactsTable.ContactIdInfo)10 ArrayList (java.util.ArrayList)9 ContactsTable (com.vodafone360.people.database.tables.ContactsTable)7 Contact (com.vodafone360.people.datatypes.Contact)7 ContactDetail (com.vodafone360.people.datatypes.ContactDetail)7 SQLiteStatement (android.database.sqlite.SQLiteStatement)5 MediumTest (android.test.suitebuilder.annotation.MediumTest)4 SQLException (android.database.SQLException)3 ServerIdInfo (com.vodafone360.people.database.DatabaseHelper.ServerIdInfo)3 Cursor (android.database.Cursor)2 ContentValues (android.content.ContentValues)1 SQLiteConstraintException (android.database.sqlite.SQLiteConstraintException)1 SQLiteException (android.database.sqlite.SQLiteException)1 ContactChanges (com.vodafone360.people.datatypes.ContactChanges)1 VCardHelper (com.vodafone360.people.datatypes.VCardHelper)1 ContactChange (com.vodafone360.people.engine.contactsync.ContactChange)1 PeopleContactsApi (com.vodafone360.people.engine.contactsync.PeopleContactsApi)1 NativeContactDetails (com.vodafone360.people.tests.TestModule.NativeContactDetails)1