public static int SafeDeleteJournal(ContentResolver resolver, String journalName) { int ret = 0; Cursor c = resolver.query( JournalEntry.CONTENT_URI, new String[] {JournalEntry.COLUMN_ID}, JournalEntry.COLUMN_NAME + " = ?", new String[] {journalName}, null); if (c.moveToFirst()) { int journalID = c.getInt(0); String journalIDString = Integer.toString(journalID); c.close(); ret = resolver.delete( EntryEntry.CONTENT_URI.buildUpon().appendPath(journalIDString).build(), EntryEntry.COLUMN_JOURNAL_ID + " = ?", new String[] {journalIDString}); ret += resolver.delete( JournalEntry.CONTENT_URI, JournalEntry.COLUMN_ID + " = ?", new String[] {journalIDString}); } return ret; }
@Override public Uri insert(Uri uri, ContentValues values) { final SQLiteDatabase db = new TravelDbHelper(getContext()).getWritableDatabase(); Uri ret = null; long dbID; switch (matcher.match(uri)) { case JOURNAL: dbID = db.insertOrThrow(JournalEntry.TABLE_NAME, null, values); if (dbID > 0) ret = EntryEntry.CONTENT_URI.buildUpon().appendPath(Long.toString(dbID)).build(); else throw new android.database.SQLException("Failed to insert row into uri: " + uri); break; case ENTRY: int value = Integer.parseInt(uri.getPathSegments().get(1)); values.put(EntryEntry.COLUMN_JOURNAL_ID, value); values.put(EntryEntry.COLUMN_DATE, Calendar.getInstance().getTime().getTime()); dbID = db.insert(EntryEntry.TABLE_NAME, null, values); if (dbID > 0) { values.put(EntryEntry.COLUMN_ID, dbID); ret = EntryEntry.CONTENT_URI .buildUpon() .appendPath(Long.toString(value)) .appendPath(Long.toString(dbID)) .build(); } else throw new android.database.SQLException("Failed to insert row into uri: " + uri); break; case ENTRY_BY_JOURNAL: Cursor cursor = db.query( JournalEntry.TABLE_NAME, new String[] {JournalEntry.COLUMN_ID}, JournalEntry.COLUMN_NAME + " = ?", new String[] {uri.getPathSegments().get(1)}, null, null, null); if (cursor.moveToFirst()) { values.put(EntryEntry.COLUMN_JOURNAL_ID, cursor.getLong(0)); values.put(EntryEntry.COLUMN_DATE, Calendar.getInstance().getTime().getTime()); dbID = db.insert(EntryEntry.TABLE_NAME, null, values); if (dbID > 0) { String journalName = uri.getPathSegments().get(1); ret = EntryEntry.CONTENT_URI .buildUpon() .appendPath(journalName) .appendPath(Long.toString(dbID)) .build(); } else throw new android.database.SQLException("Failed to insert row into uri: " + uri); cursor.close(); } else { throw new UnsupportedOperationException( "Entry attempted to access an uncreated journal: " + uri); } break; default: throw new UnsupportedOperationException("Unknown URI detected: " + uri); } getContext().getContentResolver().notifyChange(uri, null); return ret; }