Search in sources :

Example 6 with ContactIdInfo

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

the class DatabaseHelper method syncDeleteContactDetailList.

/**
 * Function used by the contact sync engine to delete a list of contact
 * details from the database.
 *
 * @param contactDetailList The list of details which has been deleted on
 *            the server
 * @param serverIdList A list of server IDs if known, or null
 * @param syncToServer true if the details need to be sent to the server
 * @param syncToNative true if the contacts need to be added to the native
 *            phonebook
 * @param meProfile - TRUE if the added contact is Me profile.
 * @return SUCCESS or a suitable error code
 * @see #deleteContactDetail(long)
 */
public ServiceStatus syncDeleteContactDetailList(List<ContactDetail> contactDetailList, boolean syncToServer, boolean syncToNative) {
    if (Settings.ENABLED_DATABASE_TRACE)
        trace(false, "DatabaseHelper.syncDeleteContactDetailList() syncToServer[" + syncToServer + "] syncToNative[" + syncToNative + "]");
    if (!Settings.ENABLE_SERVER_CONTACT_SYNC) {
        syncToServer = false;
    }
    if (!Settings.ENABLE_UPDATE_NATIVE_CONTACTS) {
        syncToNative = false;
    }
    SQLiteDatabase db = getWritableDatabase();
    boolean needFireDbUpdate = false;
    for (ContactDetail contactDetail : contactDetailList) {
        if ((contactDetail.serverContactId == null) || (contactDetail.serverContactId == -1)) {
            ContactsTable.ContactIdInfo contactIdInfo = ContactsTable.validateContactId(contactDetail.localContactID, db);
            if (contactIdInfo == null) {
                return ServiceStatus.ERROR_NOT_FOUND;
            }
            contactDetail.nativeContactId = contactIdInfo.nativeId;
            contactDetail.serverContactId = contactIdInfo.serverId;
        }
        try {
            db.beginTransaction();
            if (syncToNative) {
                if (!NativeChangeLogTable.addDeletedContactDetailChange(contactDetail, db)) {
                    return ServiceStatus.ERROR_DATABASE_CORRUPT;
                }
            }
            if (syncToServer) {
                if (!ContactChangeLogTable.addDeletedContactDetailChange(contactDetail, syncToServer, db)) {
                    return ServiceStatus.ERROR_DATABASE_CORRUPT;
                }
            }
            if (!ContactDetailsTable.deleteDetailByDetailId(contactDetail.localDetailID, db)) {
                return ServiceStatus.ERROR_DATABASE_CORRUPT;
            }
            // in order not to override the new picture before it is uploaded.
            if (contactDetail.key == ContactDetail.DetailKeys.PHOTO && TextUtils.isEmpty(contactDetail.photo_url)) {
                ContactSummaryTable.modifyPictureLoadedFlag(contactDetail.localContactID, false, db);
                deleteThumbnail(contactDetail.localContactID);
            }
            String displayName = updateContactNameInSummary(db, contactDetail.localContactID, SyncMeDbUtils.isMeProfile(this, contactDetail.localContactID));
            if (displayName == null) {
                return ServiceStatus.ERROR_DATABASE_CORRUPT;
            }
            if (updateTimelineNames(contactDetail, displayName, contactDetail.localContactID, db)) {
                needFireDbUpdate = true;
            }
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    }
    if (needFireDbUpdate) {
        fireDatabaseChangedEvent(DatabaseChangeType.ACTIVITIES, false);
    }
    return ServiceStatus.SUCCESS;
}
Also used : ContactDetail(com.vodafone360.people.datatypes.ContactDetail) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) ContactsTable(com.vodafone360.people.database.tables.ContactsTable)

Example 7 with ContactIdInfo

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

the class DatabaseHelper method syncModifyContactDetailList.

/**
 * Function used by the contact sync engine to modify a list of contact
 * details in the database.
 *
 * @param contactDetailList The list of details received from the server
 * @param serverIdList A list of server IDs if known, or null
 * @param syncToServer true if the details need to be sent to the server
 * @param syncToNative true if the contacts need to be added to the native
 *            phonebook
 * @return SUCCESS or a suitable error code
 * @see #modifyContactDetail(ContactDetail)
 */
public ServiceStatus syncModifyContactDetailList(List<ContactDetail> contactDetailList, boolean syncToServer, boolean syncToNative) {
    if (Settings.ENABLED_DATABASE_TRACE)
        trace(false, "DatabaseHelper.syncModifyContactDetailList() syncToServer[" + syncToServer + "] syncToNative[" + syncToNative + "]");
    if (!Settings.ENABLE_SERVER_CONTACT_SYNC) {
        syncToServer = false;
    }
    if (!Settings.ENABLE_UPDATE_NATIVE_CONTACTS) {
        syncToNative = false;
    }
    boolean needFireDbUpdate = false;
    SQLiteDatabase db = getWritableDatabase();
    for (ContactDetail contactDetail : contactDetailList) {
        ContactsTable.ContactIdInfo contactIdInfo = ContactsTable.validateContactId(contactDetail.localContactID, db);
        if (contactIdInfo == null) {
            return ServiceStatus.ERROR_NOT_FOUND;
        }
        contactDetail.serverContactId = contactIdInfo.serverId;
        if (contactIdInfo.syncToPhone) {
            contactDetail.syncNativeContactId = contactIdInfo.nativeId;
        } else {
            contactDetail.syncNativeContactId = -1;
        }
        try {
            db.beginTransaction();
            if (contactDetail.order != null && contactDetail.order.equals(ContactDetail.ORDER_PREFERRED)) {
                ContactDetailsTable.removePreferred(contactDetail.localContactID, contactDetail.key, db);
            }
            ServiceStatus status = ContactDetailsTable.modifyDetail(contactDetail, syncToServer, syncToNative, db);
            if (ServiceStatus.SUCCESS != status) {
                return status;
            }
            // in order not to override the new picture before it is uploaded.
            if (ContactDetail.DetailKeys.PHOTO == contactDetail.key && TextUtils.isEmpty(contactDetail.photo_url)) {
                ContactSummaryTable.modifyPictureLoadedFlag(contactDetail.localContactID, false, db);
            }
            String displayName = updateContactNameInSummary(db, contactDetail.localContactID, SyncMeDbUtils.isMeProfile(this, contactDetail.localContactID));
            if (null == displayName) {
                return ServiceStatus.ERROR_DATABASE_CORRUPT;
            }
            if (updateTimelineNames(contactDetail, displayName, null, db)) {
                needFireDbUpdate = true;
            }
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    }
    if (needFireDbUpdate) {
        fireDatabaseChangedEvent(DatabaseChangeType.ACTIVITIES, false);
    }
    return ServiceStatus.SUCCESS;
}
Also used : ContactDetail(com.vodafone360.people.datatypes.ContactDetail) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) ContactsTable(com.vodafone360.people.database.tables.ContactsTable) ServiceStatus(com.vodafone360.people.service.ServiceStatus)

Example 8 with ContactIdInfo

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

the class DatabaseHelper method syncMergeContactList.

/**
 * Function used by the contact sync engine to merge contacts which are
 * marked as duplicate by the server. This involves moving native
 * information from one contact to the other, before deleting it.
 *
 * @param contactIdList The list of contact IDs (localId, serverId and
 *            mergedLocalId should be set)
 * @return SUCCESS or a suitable error code
 */
public ServiceStatus syncMergeContactList(List<ContactsTable.ContactIdInfo> contactIdList) {
    if (Settings.ENABLED_DATABASE_TRACE)
        trace(false, "DatabaseHelper.syncMergeContactList()");
    List<ContactDetail> detailInfoList = new ArrayList<ContactDetail>();
    SQLiteDatabase writableDb = getWritableDatabase();
    SQLiteStatement contactStatement = null, contactSummaryStatement = null;
    try {
        contactStatement = ContactsTable.mergeContactStatement(writableDb);
        contactSummaryStatement = ContactSummaryTable.mergeContactStatement(writableDb);
        writableDb.beginTransaction();
        for (int i = 0; i < contactIdList.size(); i++) {
            ContactsTable.ContactIdInfo contactIdInfo = contactIdList.get(i);
            if (contactIdInfo.mergedLocalId != null) {
                contactIdInfo.nativeId = ContactsTable.fetchNativeFromLocalId(writableDb, contactIdInfo.localId);
                LogUtils.logI("DatabaseHelper.syncMergeContactList - Copying native Ids from duplicate to original contact: Dup ID " + contactIdInfo.localId + ", Org ID " + contactIdInfo.mergedLocalId + ", Nat ID " + contactIdInfo.nativeId);
                ServiceStatus status = ContactsTable.mergeContact(contactIdInfo, contactStatement);
                if (status != ServiceStatus.SUCCESS) {
                    return status;
                }
                status = ContactSummaryTable.mergeContact(contactIdInfo, contactSummaryStatement);
                if (status != ServiceStatus.SUCCESS) {
                    return status;
                }
                status = ContactDetailsTable.fetchNativeInfo(contactIdInfo.localId, detailInfoList, writableDb);
                if (status != ServiceStatus.SUCCESS) {
                    return status;
                }
                status = ContactDetailsTable.mergeContactDetails(contactIdInfo, detailInfoList, writableDb);
                if (status != ServiceStatus.SUCCESS) {
                    return status;
                }
            }
        }
        writableDb.setTransactionSuccessful();
    } finally {
        writableDb.endTransaction();
        if (contactStatement != null) {
            contactStatement.close();
            contactStatement = null;
        }
        if (contactSummaryStatement != null) {
            contactSummaryStatement.close();
            contactSummaryStatement = null;
        }
    }
    LogUtils.logI("DatabaseHelper.syncMergeContactList - Deleting duplicate contacts");
    return syncDeleteContactList(contactIdList, false, true);
}
Also used : ContactDetail(com.vodafone360.people.datatypes.ContactDetail) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SQLiteStatement(android.database.sqlite.SQLiteStatement) ContactsTable(com.vodafone360.people.database.tables.ContactsTable) ServiceStatus(com.vodafone360.people.service.ServiceStatus) ArrayList(java.util.ArrayList)

Example 9 with ContactIdInfo

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

the class ContactSummaryTable method syncSetNativeIds.

/**
 * Updates the native IDs for a list of contacts.
 *
 * @param 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.
 * @param writeableDb Writable SQLite database
 * @return SUCCESS or a suitable error code
 */
public static ServiceStatus syncSetNativeIds(List<ContactIdInfo> contactIdList, SQLiteDatabase writableDb) {
    DatabaseHelper.trace(true, "ContactSummaryTable.syncSetNativeIds()");
    if (contactIdList.size() == 0) {
        return ServiceStatus.SUCCESS;
    }
    final SQLiteStatement statement1 = writableDb.compileStatement("UPDATE " + TABLE_NAME + " SET " + Field.NATIVEID + "=? WHERE " + Field.LOCALCONTACTID + "=?");
    for (int i = 0; i < contactIdList.size(); i++) {
        final ContactIdInfo info = contactIdList.get(i);
        try {
            writableDb.beginTransaction();
            if (info.nativeId == null) {
                statement1.bindNull(1);
            } else {
                statement1.bindLong(1, info.nativeId);
            }
            statement1.bindLong(2, info.localId);
            statement1.execute();
            writableDb.setTransactionSuccessful();
        } catch (SQLException e) {
            LogUtils.logE("ContactSummaryTable.syncSetNativeIds() " + "SQLException - Unable to update contact native Ids", e);
            return ServiceStatus.ERROR_DATABASE_CORRUPT;
        } finally {
            writableDb.endTransaction();
        }
    }
    return ServiceStatus.SUCCESS;
}
Also used : ContactIdInfo(com.vodafone360.people.database.tables.ContactsTable.ContactIdInfo) SQLiteStatement(android.database.sqlite.SQLiteStatement) SQLException(android.database.SQLException)

Example 10 with ContactIdInfo

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

the class ContactDetailsTable method mergeContactDetails.

/**
 * Moves native details ownership from the duplicate contact to the original
 * contact.
 *
 * @param info the info for duplicated and original contacts
 * @param nativeInfoList the list of native details from the duplicated
 *            contact
 * @param writableDb A writable SQLite database object
 * @return SUCCESS or a suitable error code.
 */
public static ServiceStatus mergeContactDetails(ContactIdInfo info, List<ContactDetail> nativeInfoList, SQLiteDatabase writableDb) {
    DatabaseHelper.trace(true, "ContactDetailsTable.mergeContactDetails()");
    /**
     * Used to hold some contact details info.
     */
    final class DetailsInfo {

        /**
         * The detail local id.
         */
        public Long localId;

        /**
         * The detail server id.
         */
        public Long serverId;

        public DetailsInfo(Long localId, Long serverId) {
            this.localId = localId;
            this.serverId = serverId;
        }
    }
    // the list of details from the original contact
    List<DetailsInfo> detailLocalIds = new ArrayList<DetailsInfo>();
    // the result cursor from the original contact details query
    Cursor cursor = null;
    try {
        // Retrieve a list of detail local IDs from the merged contact
        // (original one)
        final String[] args = { String.valueOf(info.mergedLocalId) };
        cursor = writableDb.rawQuery(QUERY_DETAIL_LOCAL_AND_SERVER_IDS_BY_LOCAL_CONTACT_ID, args);
        while (cursor.moveToNext()) {
            if (!cursor.isNull(0) && !cursor.isNull(1)) {
                // only adding details with a detailServerId (Name and
                // Nickname don't have detailServerIds)
                detailLocalIds.add(new DetailsInfo(cursor.getLong(0), cursor.getLong(1)));
            }
        }
        DatabaseHelper.trace(true, "ContactDetailsTable.mergeContactDetails(): detailLocalIds.size()=" + detailLocalIds.size());
    } catch (Exception e) {
        LogUtils.logE("ContactDetailsTable.mergeContactDetails() Exception - " + "Unable to query merged contact details list", e);
        return ServiceStatus.ERROR_DATABASE_CORRUPT;
    } finally {
        if (cursor != null) {
            cursor.close();
            cursor = null;
        }
    }
    try {
        final ContentValues cv = new ContentValues();
        // duplicated contact to the original contact
        for (int infoListIndex = 0; infoListIndex < nativeInfoList.size(); infoListIndex++) {
            final ContactDetail detailInfo = nativeInfoList.get(infoListIndex);
            // Change the ownership
            for (int detailsIndex = 0; detailsIndex < detailLocalIds.size(); detailsIndex++) {
                final DetailsInfo currentDetails = detailLocalIds.get(detailsIndex);
                if (currentDetails.serverId.equals(detailInfo.unique_id)) {
                    cv.put(Field.LOCALCONTACTID.toString(), info.mergedLocalId);
                    cv.put(Field.NATIVECONTACTID.toString(), detailInfo.nativeContactId);
                    cv.put(Field.NATIVEDETAILID.toString(), detailInfo.nativeDetailId);
                    cv.put(Field.NATIVEDETAILVAL1.toString(), detailInfo.nativeVal1);
                    cv.put(Field.NATIVEDETAILVAL2.toString(), detailInfo.nativeVal2);
                    cv.put(Field.NATIVEDETAILVAL3.toString(), detailInfo.nativeVal3);
                    cv.put(Field.NATIVESYNCCONTACTID.toString(), detailInfo.syncNativeContactId);
                    DatabaseHelper.trace(true, "ContactDetailsTable.mergeContactDetails():" + " changing ownership for duplicated detail: " + detailInfo);
                    writableDb.update(TABLE_NAME, cv, Field.DETAILLOCALID + "=" + currentDetails.localId, null);
                    cv.clear();
                    detailLocalIds.remove(detailsIndex);
                    break;
                }
            }
        }
        return ServiceStatus.SUCCESS;
    } catch (SQLException e) {
        LogUtils.logE("ContactDetailsTable.mergeContactDetails() SQLException - " + "Unable to merge contact detail native info", e);
        return ServiceStatus.ERROR_DATABASE_CORRUPT;
    }
}
Also used : ContentValues(android.content.ContentValues) ContactDetail(com.vodafone360.people.datatypes.ContactDetail) SQLException(android.database.SQLException) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) SQLiteException(android.database.sqlite.SQLiteException) SQLException(android.database.SQLException)

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