public static ContentValues buildContentValues(CapsuleOwnership capsule) { ContentValues values = new ContentValues(); values.put(CapsuleContract.Ownerships.ACCOUNT_NAME, capsule.getAccountName()); values.put(CapsuleContract.Ownerships.CAPSULE_ID, capsule.getId()); values.put(CapsuleContract.Ownerships.ETAG, capsule.getEtag()); return values; }
public static long determineBestId( ContentResolver resolver, CapsuleOperations operations, CapsuleOwnership capsule) { // Build the query URI Uri uri = CapsuleContract.Ownerships.CONTENT_URI .buildUpon() .appendQueryParameter( CapsuleContract.Query.Parameters.INNER_JOIN, CapsuleContract.Capsules.TABLE_NAME) .build(); // Query Cursor c = resolver.query( uri, CapsuleContract.Ownerships.CAPSULE_JOIN_PROJECTION, CapsuleContract.Capsules.TABLE_NAME + "." + CapsuleContract.Capsules.SYNC_ID + " = ?" + " AND " + CapsuleContract.Capsules.TABLE_NAME + "." + CapsuleContract.Capsules.SYNC_ID + " != ?", new String[] {String.valueOf(capsule.getSyncId()), "0"}, null); // Check if there is a row long id; if (c.getCount() < 1) { // There is no existing row id = 0; } else if (c.getCount() > 1) { // There is more than one row, so get the first matching one c.moveToFirst(); id = c.getLong(c.getColumnIndex(CapsuleContract.Ownerships.OWNERSHIP_ID_ALIAS)); // Add any extra IDs to a collection List<Long> duplicateIds = new ArrayList<Long>(); while (c.moveToNext()) { duplicateIds.add( c.getLong(c.getColumnIndex(CapsuleContract.Ownerships.OWNERSHIP_ID_ALIAS))); } // Build operations to remove the extra IDs operations.buildOwnershipCleanup(duplicateIds); } else { // There is only one row, so get the ID c.moveToFirst(); id = c.getLong(c.getColumnIndex(CapsuleContract.Ownerships.OWNERSHIP_ID_ALIAS)); } // Close the cursor c.close(); // If a better ID was found (non-zero), re-assign it if (id > 0) { capsule.setOwnershipId(id); } return id; }
public void buildOwnershipUpdate( CapsuleOwnership ownership, boolean withYield, CapsuleContract.SyncStateAction syncAction) { // Build the URI Uri uri = CapsuleOperations.appendDirtyQueryParam( syncAction, ContentUris.withAppendedId( CapsuleContract.Ownerships.CONTENT_URI, ownership.getOwnershipId())); // Build the UPDATE operation ContentProviderOperation.Builder builder = ContentProviderOperation.newUpdate(uri); builder.withValues(Ownerships.buildContentValues(ownership)); builder.withYieldAllowed(withYield); // Add it to the collection this.mOperations.add(builder.build()); }
public void buildOwnershipUpdate( CapsuleOwnership ownership, boolean withYield, int capsuleIdBackRefIndex, CapsuleContract.SyncStateAction syncAction) { // Build the URI Uri uri = CapsuleOperations.appendDirtyQueryParam( syncAction, ContentUris.withAppendedId( CapsuleContract.Ownerships.CONTENT_URI, ownership.getOwnershipId())); // Build the UPDATE operation ContentProviderOperation.Builder builder = ContentProviderOperation.newUpdate(uri); builder.withValues(Ownerships.buildContentValues(ownership)); builder.withYieldAllowed(withYield); // Add the back value reference index for the Capsule ID if (capsuleIdBackRefIndex >= 0) { builder.withValueBackReference(CapsuleContract.Ownerships.CAPSULE_ID, capsuleIdBackRefIndex); } // Add it to the collection this.mOperations.add(builder.build()); }
public static boolean save( ContentResolver resolver, Capsule capsule, CapsuleContract.SyncStateAction syncAction) { // Build the ContentProviderOperations for the save CapsuleOperations operations = new CapsuleOperations(resolver); operations.buildOwnershipSave(capsule, syncAction); // Apply the batch operation ContentProviderResult[] results = operations.applyBatch(); // Make sure the rows were properly updated boolean success = true; // Check the first result (Capsule operation) if (results[0].uri != null) { // The Capsule operation was an INSERT, so parse the ID from the URI capsule.setId(ContentUris.parseId(results[0].uri)); } else { // The Capsule operation was an UPDATE, so make sure a row was updated if (results[0].count < 1) { success = false; } } // Check the second result (Ownership operation) if (results[1].uri != null) { // The Ownership operation was an INSERT, so parse the ID from the URI ((CapsuleOwnership) capsule).setOwnershipId(ContentUris.parseId(results[1].uri)); } else { // The Ownership operation was an UPDATE, so make sure a row was updated if (results[1].count < 1) { success = false; } } return success; }