public static ContentValues buildContentValues(Capsule capsule) {
   ContentValues values = new ContentValues();
   values.put(CapsuleContract.Capsules.SYNC_ID, capsule.getSyncId());
   values.put(CapsuleContract.Capsules.NAME, capsule.getName());
   values.put(CapsuleContract.Capsules.LATITUDE, capsule.getLatitude());
   values.put(CapsuleContract.Capsules.LONGITUDE, capsule.getLongitude());
   return values;
 }
    public static long determineBestId(
        ContentResolver resolver, CapsuleOperations operations, Capsule capsule) {
      // Query
      Cursor c =
          resolver.query(
              CapsuleContract.Capsules.CONTENT_URI,
              new String[] {CapsuleContract.Capsules._ID},
              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) {
        // Get the first matching row to keep
        c.moveToFirst();
        id = c.getLong(c.getColumnIndex(CapsuleContract.Capsules._ID));
        // Add any extra IDs to a collection
        List<Long> duplicateIds = new ArrayList<Long>();
        while (c.moveToNext()) {
          duplicateIds.add(c.getLong(c.getColumnIndex(CapsuleContract.Capsules._ID)));
        }
        // Build operations to remove the extra IDs
        operations.buildCapsuleCleanup(id, duplicateIds);
      } else {
        // There is only one row, so move to it and get the ID
        c.moveToFirst();
        id = c.getLong(c.getColumnIndex(CapsuleContract.Capsules._ID));
      }
      // Close the cursor
      c.close();

      // If a better ID was found (non-zero), reassign it
      if (id > 0) {
        capsule.setId(id);
      }

      return id;
    }
 public void buildCapsuleUpdate(Capsule capsule, boolean withYield) {
   // URI for updating a Capsule
   Uri uri = ContentUris.withAppendedId(CapsuleContract.Capsules.CONTENT_URI, capsule.getId());
   // Build the UPDATE operation
   ContentProviderOperation.Builder builder = ContentProviderOperation.newUpdate(uri);
   builder.withValues(Capsules.buildContentValues(capsule));
   builder.withYieldAllowed(withYield);
   // Add it to the collection
   this.mOperations.add(builder.build());
 }
 public void buildDiscoverySave(Capsule capsule, CapsuleContract.SyncStateAction syncAction) {
   // Make sure there is a sync ID
   if (capsule.getSyncId() <= 0) {
     throw new InvalidParameterException("The Capsule does not have a sync ID");
   }
   // Determine the best IDs for the Capsule
   CapsuleOperations.Capsules.determineBestId(this.mResolver, this, capsule);
   CapsuleOperations.Discoveries.determineBestId(this.mResolver, this, (CapsuleDiscovery) capsule);
   // Build INSERT/UPDATE operations depending on if the Capsule and Discovery already exist
   if (capsule.getId() > 0) {
     // Capsule UPDATE
     this.buildCapsuleUpdate(capsule, /* withYield */ true);
     // Check if the Discovery exists
     if (((CapsuleDiscovery) capsule).getDiscoveryId() > 0) {
       // Discovery UPDATE
       this.buildDiscoveryUpdate((CapsuleDiscovery) capsule, /* withYield */ false, syncAction);
     } else {
       // Discovery INSERT
       this.buildDiscoveryInsert((CapsuleDiscovery) capsule, /* withYield */ false, syncAction);
     }
   } else {
     // Capsule INSERT
     this.buildCapsuleInsert(capsule, /* withYield */ true);
     // Check if the Discovery exists
     if (((CapsuleDiscovery) capsule).getDiscoveryId() > 0) {
       // Discovery UPDATE
       this.buildDiscoveryUpdate(
           (CapsuleDiscovery) capsule, /* withYield */
           false,
           this.getLastOperationIndex(),
           syncAction);
     } else {
       // Discovery INSERT
       this.buildDiscoveryInsert(
           (CapsuleDiscovery) capsule, /* withYield */
           false,
           this.getLastOperationIndex(),
           syncAction);
     }
   }
 }
  public void buildCapsuleDelete(Capsule capsule, boolean withYield) {
    // Build the Capsule URI
    Uri uri = ContentUris.withAppendedId(CapsuleContract.Capsules.CONTENT_URI, capsule.getId());

    // Build the DELETE operation
    this.mOperations.add(
        ContentProviderOperation.newDelete(uri).withYieldAllowed(withYield).build());
    // Get the capsule ID
    String capsuleId = String.valueOf(capsule.getId());
    // Build the Discovery DELETE operation
    this.mOperations.add(
        ContentProviderOperation.newDelete(CapsuleContract.Discoveries.CONTENT_URI)
            .withSelection(
                CapsuleContract.Discoveries.CAPSULE_ID + " = ?", new String[] {capsuleId})
            .withYieldAllowed(false)
            .build());
    // Build the Ownership DELETE operation
    this.mOperations.add(
        ContentProviderOperation.newDelete(CapsuleContract.Ownerships.CONTENT_URI)
            .withSelection(CapsuleContract.Ownerships.CAPSULE_ID + " = ?", new String[] {capsuleId})
            .withYieldAllowed(false)
            .build());
  }
 public void buildOwnershipSave(Capsule capsule, CapsuleContract.SyncStateAction syncAction) {
   // Determine the best IDs for the Capsule
   CapsuleOperations.Capsules.determineBestId(this.mResolver, this, capsule);
   CapsuleOperations.Ownerships.determineBestId(this.mResolver, this, (CapsuleOwnership) capsule);
   // Build the INSERT/UPDATE operations depending on if the Capsule and Ownership exist
   if (capsule.getId() > 0) {
     // Capsule UPDATE
     this.buildCapsuleUpdate(capsule, /* withYield */ true);
     // Check if the Ownership exists
     if (((CapsuleOwnership) capsule).getOwnershipId() > 0) {
       // Ownership UPDATE
       this.buildOwnershipUpdate((CapsuleOwnership) capsule, /* withYield */ false, syncAction);
     } else {
       // Ownership INSERT
       this.buildOwnershipInsert((CapsuleOwnership) capsule, /* withYield */ false, syncAction);
     }
   } else {
     // Capsule INSERT
     this.buildCapsuleInsert(capsule, /* withYield */ true);
     // Check if the Ownership exists
     if (((CapsuleOwnership) capsule).getOwnershipId() > 0) {
       // Ownership UPDATE
       this.buildOwnershipUpdate(
           (CapsuleOwnership) capsule, /* withYield */
           false,
           this.getLastOperationIndex(),
           syncAction);
     } else {
       // Ownership INSERT
       this.buildOwnershipInsert(
           (CapsuleOwnership) capsule, /* withYield */
           false,
           this.getLastOperationIndex(),
           syncAction);
     }
   }
 }
    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;
    }