Android Open Source - journal Journal Content Provider






From Project

Back to project page journal.

License

The source code is released under:

MIT License

If you think the Android project journal listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package cochrane343.journal.contentprovider;
/*from  ww w. jav  a  2 s.  c  om*/
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.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import cochrane343.journal.contentprovider.JournalContract.Category;
import cochrane343.journal.contentprovider.JournalContract.Expense;
import cochrane343.journal.exceptions.IllegalUriException;
 
/**
 * @author cochrane343
 * @version 1.0
 */
public class JournalContentProvider extends ContentProvider {
    private JournalDatabaseHelper databaseHelper;

    private static final int URI_EXPENSES = 1;
    private static final int URI_SPECIFIC_EXPENSE = 2;
    private static final int URI_CATEGORIES = 3;
    private static final int URI_SPECIFIC_CATEGORY = 4;
    private static final int URI_CATEGORY_TOTALS = 5;

    private static final UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
 
    static {
        URI_MATCHER.addURI(JournalContract.AUTHORITY, Expense.EXPENSES_PATH, URI_EXPENSES);
        URI_MATCHER.addURI(JournalContract.AUTHORITY, Expense.EXPENSES_PATH + "/#", URI_SPECIFIC_EXPENSE);
        URI_MATCHER.addURI(JournalContract.AUTHORITY, Category.CATEGORIES_PATH, URI_CATEGORIES);
        URI_MATCHER.addURI(JournalContract.AUTHORITY, Category.CATEGORIES_PATH + "/#", URI_SPECIFIC_CATEGORY);
        URI_MATCHER.addURI(JournalContract.AUTHORITY, Category.CATEGORY_TOTALS_PATH, URI_CATEGORY_TOTALS);
    }

    /**
     * Not yet implemented
     */
    @Override
    public String getType(final Uri uri) {
        return null;
    }
  
    @Override
    public boolean onCreate() {
        databaseHelper = new JournalDatabaseHelper(getContext());

        return true;
    }

    @Override
    public Cursor query(final Uri uri, final String[] projection, final String selection,
            final String[] selectionArgs, final String sortOrder) {
        
        final SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();

        String groupBy = null;
        Uri notificationUri;

        switch (URI_MATCHER.match(uri)){
            case URI_EXPENSES:
                queryBuilder.setTables(Expense.TABLE_NAME);
                notificationUri = Expense.EXPENSES_URI;
                break;
            case URI_SPECIFIC_EXPENSE:
                queryBuilder.setTables(Expense.TABLE_NAME);
                final String expenseId = uri.getLastPathSegment();
                queryBuilder.appendWhere(Expense._ID + " = " + expenseId);
                notificationUri = Expense.EXPENSES_URI;
                break;
            case URI_CATEGORIES:
                queryBuilder.setTables(Category.TABLE_NAME);
                notificationUri = Category.CATEGORIES_URI;
                break;
            case URI_SPECIFIC_CATEGORY:
                queryBuilder.setTables(Category.TABLE_NAME);
                final String categoryId = uri.getLastPathSegment();
                queryBuilder.appendWhere(Category._ID + " = " + categoryId);
                notificationUri = Category.CATEGORIES_URI;
                break;
            case URI_CATEGORY_TOTALS:
                queryBuilder.setTables(Category.TOTALS_VIEW_NAME);
                groupBy = Category._ID;
                notificationUri = Expense.EXPENSES_URI;
                break;
            default:
                throw new IllegalUriException(uri.toString());
        }

        final SQLiteDatabase database = databaseHelper.getReadableDatabase();
        final Cursor cursor = queryBuilder.query(database, projection, selection,
                selectionArgs, groupBy, null, sortOrder);

        cursor.setNotificationUri(getContext().getContentResolver(), notificationUri);

        return cursor;
    }

    @Override
    public Uri insert(final Uri uri, final ContentValues values) {       
        final SQLiteDatabase database = databaseHelper.getWritableDatabase();

        long id;
        Uri notificationUri;
        
        switch (URI_MATCHER.match(uri)){
            case URI_EXPENSES:
                id = database.insert(Expense.TABLE_NAME, null, values);
                notificationUri = Expense.EXPENSES_URI;
                break;
            default:
                throw new IllegalUriException(uri.toString());
        }
        
        getContext().getContentResolver().notifyChange(notificationUri, null);
        
        return ContentUris.withAppendedId(uri, id);
    }

    @Override
    public int update(final Uri uri, final ContentValues values, final String selection, final String[] selectionArgs) {
        final SQLiteDatabase database = databaseHelper.getWritableDatabase();
        
        int rowsUpdated = 0;
        
        switch (URI_MATCHER.match(uri)) {
            case URI_SPECIFIC_EXPENSE:
                final String id = uri.getLastPathSegment();
                final String whereClause = Expense._ID + "= ?";
                
                rowsUpdated = database.update(Expense.TABLE_NAME, values,
                       whereClause, new String[]{id});
                
                break;
            default:
                throw new IllegalUriException(uri.toString());
        }
        
        getContext().getContentResolver().notifyChange(uri, null);
        
        return rowsUpdated;
    }
 
    @Override
    public int delete(final Uri uri, final String selection, final String[] selectionArgs) {
        final SQLiteDatabase database = databaseHelper.getWritableDatabase();
        
        int rowsDeleted = 0;
        
        switch (URI_MATCHER.match(uri)) {
            case URI_SPECIFIC_EXPENSE:
                final String id = uri.getLastPathSegment();
                final String whereClause = Expense._ID + "= ?";
                
                rowsDeleted = database.delete(Expense.TABLE_NAME,
                        whereClause, new String[]{id});
                
                break;
            default:
                throw new IllegalUriException(uri.toString());
        }
        
        getContext().getContentResolver().notifyChange(uri, null);
        
        return rowsDeleted;
    }
}




Java Source Code List

cochrane343.journal.Constants.java
cochrane343.journal.CurrencyHelper.java
cochrane343.journal.DateTimeHelper.java
cochrane343.journal.ExpenseEditListener.java
cochrane343.journal.ExpensesListAdapter.java
cochrane343.journal.MainActivity.java
cochrane343.journal.MonthlyExpensesFragment.java
cochrane343.journal.MonthlyExpensesPagerAdapter.java
cochrane343.journal.SettingsActivity.java
cochrane343.journal.TranslationHelper.java
cochrane343.journal.contentprovider.JournalContentProvider.java
cochrane343.journal.contentprovider.JournalContract.java
cochrane343.journal.contentprovider.JournalDatabaseHelper.java
cochrane343.journal.dialogs.CategorySpinnerAdapter.java
cochrane343.journal.dialogs.ExpenseDialogFragment.java
cochrane343.journal.dialogs.ExpenseDialogListener.java
cochrane343.journal.exceptions.IllegalDisplayModeException.java
cochrane343.journal.exceptions.IllegalLoaderIdException.java
cochrane343.journal.exceptions.IllegalUriException.java
cochrane343.journal.exceptions.MissingFragmentArgumentException.java