Skip to content

Instantly share code, notes, and snippets.

@Josephaguele
Created June 4, 2017 12:58
Show Gist options
  • Save Josephaguele/54fd8903209bb93f2d13be92fcb7b829 to your computer and use it in GitHub Desktop.
Save Josephaguele/54fd8903209bb93f2d13be92fcb7b829 to your computer and use it in GitHub Desktop.
package com.example.android.pets.data;
import com.example.android.pets.data.PetContract.PetEntry;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.util.Log;
import static android.R.attr.id;
import static com.example.android.pets.data.PetContract.CONTENT_AUTHORITY;
import static com.example.android.pets.data.PetContract.PATH_PETS;
import static com.example.android.pets.data.PetContract.PetEntry.COLUMN_PET_BREED;
import static com.example.android.pets.data.PetContract.PetEntry.COLUMN_PET_GENDER;
import static com.example.android.pets.data.PetContract.PetEntry.COLUMN_PET_NAME;
import static com.example.android.pets.data.PetContract.PetEntry.COLUMN_PET_WEIGHT;
import static com.example.android.pets.data.PetContract.PetEntry.CONTENT_URI;
import static com.example.android.pets.data.PetContract.PetEntry.GENDER_MALE;
import static com.example.android.pets.data.PetContract.PetEntry.GENDER_UNKNOWN;
import static com.example.android.pets.data.PetContract.PetEntry.TABLE_NAME;
import static com.example.android.pets.data.PetContract.PetEntry._ID;
/**
* Created by AGUELE OSEKUEMEN JOE on 5/29/2017.
* ContentProvider for Pets app.
*/
public class PetProvider extends ContentProvider {
private static final String LOG_TAG = PetProvider.class.getSimpleName();
/**Database helper object*/
private PetDbHelper mDbHelper;
// URI matcher code for the content URI for teh pets table
private static final int PETS = 100;
// URI matcher code for the content URI for a single pet in the pets table
private static final int PETS_ID = 101;
/**
* UriMatcher object to match a content URI to a corresponding code.
* The input passed into the constructor represents the code to return for the root URI
* It's common to use NO_MATCH as the input for this case
*/
private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
// Static initializer. This is run the first time anything is called from this class.
static{
// The calls to addURI() go here, for all of the content URI patterns that the provider
// should recognize. All paths added to the UriMatcher have a corresponding code to return
// when a match is found.
sUriMatcher.addURI(CONTENT_AUTHORITY, PATH_PETS, PETS);
sUriMatcher.addURI(CONTENT_AUTHORITY, PATH_PETS +"/#", PETS_ID);
}
// Initialize the provider and the database helper object.
@Override
public boolean onCreate() {
mDbHelper = new PetDbHelper(getContext());
return true;
}
// Perform the query for the given URI. Use the given projection, selection, selection arguments, and sort order.
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
// Get readable database
SQLiteDatabase database = mDbHelper.getReadableDatabase();
// This cursor will hold the result of the query
Cursor cursor;
// Figure out if the URI matcher can match the URI to a specific code
int match = sUriMatcher.match(uri);
switch(match){
case PETS:
// For the PETS code, query the pets table directly wit the given
// projection, selection, selection arguments and sort order. The cursor
// could contain multiple rows of the pets table.
// TODO: Perform database query on pets table.
cursor = database.query(TABLE_NAME, projection, selection, selectionArgs,null, null, sortOrder);
break;
case PETS_ID:
//For the PET_ID code, extract out the ID from the URI.
// For an example URI such as "com.example.android.pets/pets/3",
// the selection will be "_id=?" and the selection argument will be a
// String array containing the actual ID of 3 in this case.
// For every "?" in the selection, we need to have an element in the selection
// arguments that will fill in the "?". Since we have 1 question mark in the
// selection, we have 1 String in the selection arguments String array.
selection = PetEntry._ID + "=?";
selectionArgs = new String[]{String.valueOf(ContentUris.parseId(uri))};
// This will perform a query on the pets able where the _id equals 3 to return a
// Cursor containing that row of the table.
cursor = database.query(TABLE_NAME, projection, selection, selectionArgs,
null, null, sortOrder);
break;
default:
throw new IllegalArgumentException("Cannot query unknown URI" + uri);
}
return cursor;
}
// Returns the MIME type of data for the content URI.
@Override
public String getType(Uri uri) {
return null;
}
// Insert new data into the provider with the given ContentValues.
@Override
public Uri insert(Uri uri, ContentValues contentValues) {
final int match = sUriMatcher.match(uri); // URI matcher is used to check if there is a match
switch(match){
case PETS:
return insertPet(uri, contentValues);
default:
throw new IllegalArgumentException("Insertion is not supported for " +uri);
}
}
//Insert a pet into the database with thegiven content values. Return the ne content URI
// for that specific row in the database
private Uri insertPet(Uri uri, ContentValues values){
//check that the name is not null
String name = values.getAsString(COLUMN_PET_NAME);
if (name == null){
throw new IllegalArgumentException("Pet requires a name");
}
// Check that the gender is not null
Integer gender = values.getAsInteger(COLUMN_PET_GENDER);
if (gender == null || !PetEntry.isValidGender(gender)){
throw new IllegalArgumentException("Pet requires a valid gender");
}
// If the weight is provided, check that it is greater than or equal to 0kg
/*
* If the weight is null, that's fine and we can proceed with insertion (the database will insert
* default weight 0 automatically) if the wieght is not null AND it's a negative wieght, then we
* to throw an exception with the message "Pet requires valid weight". We use the "&&" SYMBOL
* to indicate that both "weight != null" must be true and "weight < 0" must be true, in order
* for the whole test condition to be true, and for the code within the "if" statement to
* execute
* */
Integer weight = values.getAsInteger(COLUMN_PET_WEIGHT);
if( weight != null && weight < 0 ) {
throw new IllegalArgumentException("Pet weight can't be less than zero");
}
//Get writeable database
SQLiteDatabase database = mDbHelper.getWritableDatabase();
// insert the new pet with the given values
long id = database.insert(TABLE_NAME,null,values);
//If the ID is -1, then the insertion failed. Log an error and return null.
if (id == -1){
Log.e(LOG_TAG, "Failed to insert row for " + uri);
return null;
}
// return the new URI with the ID (of the newly inserted row) appended to the end of it
return ContentUris.withAppendedId(uri, id);
}
// Delete data at the given selection and selection arguments
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
return 0;
}
@Override
public int update(Uri uri, ContentValues contentValues, String selection, String[] selectionArgs) {
final int match = sUriMatcher.match(uri);
switch(match){
case PETS:
return updatePet(uri, contentValues, selection, selectionArgs);
case PETS_ID:
// For the PETS_ code, extract out the ID from the URI,
// so we know which row to update. Selection will be "_id=?" and selection
// arguments will be a String array containing the actual I.
selection = _ID + "=?";
selectionArgs = new String[] { String.valueOf(ContentUris.parseId(uri))};
return updatePet(uri, contentValues, selection, selectionArgs);
default:
throw new IllegalArgumentException("Update is not supported for " + uri);
}
}
private int updatePet(Uri uri, ContentValues values, String selection, String[]selectionArgs){
// TODO: Update the selected pets in the pets database table with the given ContentValues
// TODO: Return the number of rows that were affected
if (values.containsKey(COLUMN_PET_WEIGHT)){
// Check that the weight is greater than or equal to 0kg and also
// check that the weight is not null
Integer weight = values.getAsInteger(COLUMN_PET_WEIGHT);
if (weight == null || weight < 0){
throw new IllegalArgumentException("Pets correct weight must be inputted");
}
}
//Get writable database
SQLiteDatabase database = mDbHelper.getWritableDatabase();
selection = PetEntry._ID + "=?";
selectionArgs = new String[]{String.valueOf(ContentUris.parseId(uri))};
// insert the new pet with the given values
int row = database.update(TABLE_NAME,values,selection,selectionArgs);
return row;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment