Search in sources :

Example 6 with ServerIdInfo

use of com.vodafone360.people.database.DatabaseHelper.ServerIdInfo in project 360-Engine-for-Android by 360.

the class UploadServerContacts method handleUploadDetailChanges.

/**
     * Called when handling the server response from a new contact or modify
     * contact sync. Updates the unique IDs for all the details if necessary.
     * 
     * @param contactSrc Contact received from server.
     * @param contactDest Contact from database.
     * @param detailServerIdList List of contact details with updated unique id.
     * @return ServiceStatus object.
     */
private ServiceStatus handleUploadDetailChanges(final Contact contactSrc, final Contact contactDest, final List<ServerIdInfo> detailServerIdList) {
    if (contactSrc.contactID == null || contactSrc.contactID.longValue() == -1L) {
        LogUtils.logE("UploadServerContacts.handleUploadDetailChanges() " + "The server failed to modify the following contact: " + contactDest.localContactID);
        mFailureList += "Failed to add contact: " + contactDest.localContactID + "\n";
        return ServiceStatus.SUCCESS;
    }
    ListIterator<ContactDetail> itContactDetailSrc = contactSrc.details.listIterator();
    ListIterator<ContactDetail> itContactDetailDest = contactDest.details.listIterator();
    while (itContactDetailSrc.hasNext()) {
        if (!itContactDetailDest.hasNext()) {
            /*
                 * The response should contain the same number of details as was
                 * supplied but must handle the error.
                 */
            return ServiceStatus.ERROR_COMMS_BAD_RESPONSE;
        }
        ContactDetail contactDetailSrc = itContactDetailSrc.next();
        ContactDetail contactDetailDest = itContactDetailDest.next();
        ServerIdInfo info = new ServerIdInfo();
        info.localId = contactDetailDest.localDetailID;
        if (contactDetailSrc.unique_id != null && contactDetailSrc.unique_id.longValue() == -1L) {
            LogUtils.logE("UploadServerContacts." + "handleUploadDetailChanges() The server failed to " + "modify the following contact detail: LocalDetailId " + "= " + contactDetailDest.localDetailID + ", Key = " + contactDetailDest.key + ", value = " + contactDetailDest.value);
            mFailureList += "Failed to modify contact detail: " + contactDetailDest.localDetailID + ", for contact " + contactDetailDest.localContactID + "\n";
            info.serverId = null;
        } else {
            info.serverId = contactDetailSrc.unique_id;
        }
        detailServerIdList.add(info);
    }
    while (itContactDetailDest.hasNext()) {
        ContactDetail contactDetailDest = itContactDetailDest.next();
        mFailureList += "Failed to modify contact detail (not in return " + "list):" + contactDetailDest.localDetailID + ", for contact " + contactDetailDest.localContactID + "\n";
        LogUtils.logE("UploadServerContacts.handleUploadDetailChanges() " + "The server failed to modify the following contact detail " + "(not found in returned list): LocalDetailId = " + contactDetailDest.localDetailID + ", Key = " + contactDetailDest.key + ", value = " + contactDetailDest.value);
    }
    return ServiceStatus.SUCCESS;
}
Also used : ContactDetail(com.vodafone360.people.datatypes.ContactDetail) ServerIdInfo(com.vodafone360.people.database.DatabaseHelper.ServerIdInfo)

Example 7 with ServerIdInfo

use of com.vodafone360.people.database.DatabaseHelper.ServerIdInfo 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 8 with ServerIdInfo

use of com.vodafone360.people.database.DatabaseHelper.ServerIdInfo in project 360-Engine-for-Android by 360.

the class NowPlusContactsTest method testSyncSetServerIds.

@SmallTest
public void testSyncSetServerIds() {
    assertTrue(initialise());
    mDatabaseHelper.removeUserData();
    ServiceStatus status = mTestUtility.waitForEvent(WAIT_EVENT_TIMEOUT_MS, DbTestUtility.CONTACTS_INT_EVENT_MASK);
    assertEquals(ServiceStatus.SUCCESS, status);
    Contact addedContact = mTestModule.createDummyContactData();
    status = mDatabaseHelper.addContact(addedContact);
    assertEquals(ServiceStatus.SUCCESS, status);
    long serverId = addedContact.localContactID + 1;
    long userId = addedContact.localContactID + 2;
    List<ServerIdInfo> serverIdList = new ArrayList<ServerIdInfo>();
    ServerIdInfo info = new ServerIdInfo();
    info.localId = addedContact.localContactID;
    info.serverId = serverId;
    info.userId = userId;
    serverIdList.add(info);
    status = ContactsTable.syncSetServerIds(serverIdList, null, mDatabaseHelper.getWritableDatabase());
    assertEquals(ServiceStatus.SUCCESS, status);
    addedContact.contactID = serverId;
    addedContact.userID = userId;
    Contact fetchedContact = new Contact();
    status = mDatabaseHelper.fetchContactByServerId(serverId, fetchedContact);
    assertEquals(ServiceStatus.SUCCESS, status);
    assertTrue(TestModule.doContactsMatch(addedContact, fetchedContact));
    shutdown();
}
Also used : ServiceStatus(com.vodafone360.people.service.ServiceStatus) ArrayList(java.util.ArrayList) ServerIdInfo(com.vodafone360.people.database.DatabaseHelper.ServerIdInfo) Contact(com.vodafone360.people.datatypes.Contact) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 9 with ServerIdInfo

use of com.vodafone360.people.database.DatabaseHelper.ServerIdInfo in project 360-Engine-for-Android by 360.

the class ContactsTable method syncSetServerIds.

/**
     * 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 {@link ContactIdInfo#mergedLocalId}
     * ).
     * 
     * @param 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.
     * @param dupList On return this will be populated with a list of contacts
     *            which have server IDs already present in the table.
     * @param writeableDb Writeable SQLite database
     * @return SUCCESS or a suitable error code
     */
public static ServiceStatus syncSetServerIds(List<ServerIdInfo> serverIdList, List<ContactIdInfo> dupList, SQLiteDatabase writableDb) {
    DatabaseHelper.trace(true, "ContactsTable.syncSetServerIds()");
    if (serverIdList.size() == 0) {
        return ServiceStatus.SUCCESS;
    }
    SQLiteStatement statement1 = null;
    SQLiteStatement statement2 = null;
    try {
        writableDb.beginTransaction();
        for (int i = 0; i < serverIdList.size(); i++) {
            final ServerIdInfo info = serverIdList.get(i);
            try {
                if (info.serverId != null) {
                    if (info.userId == null) {
                        if (statement2 == null) {
                            statement2 = writableDb.compileStatement("UPDATE " + TABLE_NAME + " SET " + Field.SERVERID + "=? WHERE " + Field.LOCALID + "=?");
                        }
                        statement2.bindLong(1, info.serverId);
                        statement2.bindLong(2, info.localId);
                        statement2.execute();
                    } else {
                        if (statement1 == null) {
                            statement1 = writableDb.compileStatement("UPDATE " + TABLE_NAME + " SET " + Field.SERVERID + "=?," + Field.USERID + "=? WHERE " + Field.LOCALID + "=?");
                        }
                        statement1.bindLong(1, info.serverId);
                        statement1.bindLong(2, info.userId);
                        statement1.bindLong(3, info.localId);
                        statement1.execute();
                    }
                }
            } catch (SQLiteConstraintException e) {
                // server ID is not unique
                ContactIdInfo contactInfo = new ContactIdInfo();
                contactInfo.localId = info.localId;
                contactInfo.serverId = info.serverId;
                if (!fetchLocalIDFromServerID(writableDb, contactInfo)) {
                    writableDb.endTransaction();
                    return ServiceStatus.ERROR_DATABASE_CORRUPT;
                }
                dupList.add(contactInfo);
            } catch (SQLException e) {
                LogUtils.logE("ContactsTable.syncSetServerIds() SQLException - " + "Unable to update contact server Ids", e);
                return ServiceStatus.ERROR_DATABASE_CORRUPT;
            }
        }
        writableDb.setTransactionSuccessful();
    } finally {
        writableDb.endTransaction();
        if (statement1 != null) {
            statement1.close();
            statement1 = null;
        }
        if (statement2 != null) {
            statement2.close();
            statement2 = null;
        }
    }
    return ServiceStatus.SUCCESS;
}
Also used : SQLiteStatement(android.database.sqlite.SQLiteStatement) SQLException(android.database.SQLException) ServerIdInfo(com.vodafone360.people.database.DatabaseHelper.ServerIdInfo) SQLiteConstraintException(android.database.sqlite.SQLiteConstraintException)

Aggregations

ServerIdInfo (com.vodafone360.people.database.DatabaseHelper.ServerIdInfo)9 ServiceStatus (com.vodafone360.people.service.ServiceStatus)6 ArrayList (java.util.ArrayList)6 Contact (com.vodafone360.people.datatypes.Contact)5 ContactDetail (com.vodafone360.people.datatypes.ContactDetail)4 SQLException (android.database.SQLException)2 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)2 SQLiteStatement (android.database.sqlite.SQLiteStatement)2 MediumTest (android.test.suitebuilder.annotation.MediumTest)2 SmallTest (android.test.suitebuilder.annotation.SmallTest)2 ContactIdInfo (com.vodafone360.people.database.tables.ContactsTable.ContactIdInfo)2 ContactChanges (com.vodafone360.people.datatypes.ContactChanges)2 SQLiteConstraintException (android.database.sqlite.SQLiteConstraintException)1 Suppress (android.test.suitebuilder.annotation.Suppress)1 VCardHelper (com.vodafone360.people.datatypes.VCardHelper)1 HashSet (java.util.HashSet)1