Android Open Source - anluwage Annotation Based Provider






From Project

Back to project page anluwage.

License

The source code is released under:

GNU General Public License

If you think the Android project anluwage 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 com.sudocode.content.provider.api;
/*from   w  w w.  ja v a  2s .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.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.provider.BaseColumns;
import com.sudocode.content.provider.api.annotation.Provider;
import com.sudocode.content.provider.api.annotation.Table;
import com.sudocode.content.provider.api.exception.AnnotationBasedProviderRuntimeException;

import java.lang.annotation.Annotation;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by RM on 1/26/14.
 */
public abstract class AnnotationBasedProvider extends ContentProvider implements ProviderModel {

    private static final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

    private static final int WILDCARD_OFFSET = 10000;
    private static Map<Integer, String> tableNameMap = new HashMap<Integer, String>();

    private Provider providerAnnotation;
    private SQLiteOpenHelper databaseHelper;

    @Override
    public boolean onCreate() {
        loadUriMatchers(this);
        databaseHelper = new AnnotationBasedProviderDatabaseHelper(getContext(), this);
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String where, String[] whereClause, String sortOrder) {
        SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
        int tableMapKey = uriMatcher.match(uri);
        String tableName = getTableName(tableMapKey);
        queryBuilder.setTables(tableName);
        if (tableMapKey > WILDCARD_OFFSET) {
            queryBuilder.appendWhere("_id=" + uri.getLastPathSegment());
        }
        Cursor cursor = queryBuilder.query(databaseHelper.getReadableDatabase(),
                projection,
                where,
                whereClause,
                null,
                null,
                sortOrder);
        cursor.setNotificationUri(getContext().getContentResolver(), uri);
        return cursor;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues contentValues) {
        long newRecordId = databaseHelper.getWritableDatabase().insert(getTableName(uri), null, contentValues);
        if (newRecordId > 0) {
            getContext().getContentResolver().notifyChange(uri, null);
            return ContentUris.withAppendedId(uri, newRecordId);
        }
        return null;
    }

    @Override
    public int delete(Uri uri, String where, String[] whereClause) {
        int tableMapKey = uriMatcher.match(uri);
        if (tableMapKey > WILDCARD_OFFSET) {
            if (where == null) {
                where = BaseColumns._ID + "=" + uri.getLastPathSegment();
            } else {
                where += " AND " + BaseColumns._ID + "=" + uri.getLastPathSegment();
            }
        }
        int affectedRowsCount = databaseHelper.getWritableDatabase().delete(getTableName(tableMapKey), where, whereClause);
        getContext().getContentResolver().notifyChange(uri, null);
        return affectedRowsCount;
    }

    @Override
    public int update(Uri uri, ContentValues contentValues, String where, String[] whereClause) {
        int tableMapKey = uriMatcher.match(uri);
        if (tableMapKey > WILDCARD_OFFSET) {
            if (where == null) {
                where = BaseColumns._ID + "=" + uri.getLastPathSegment();
            } else {
                where += " AND " + BaseColumns._ID + "=" + uri.getLastPathSegment();
            }
        }
        int affectedRowsCount = databaseHelper.getWritableDatabase()
                .update(getTableName(tableMapKey), contentValues, where, whereClause);
        getContext().getContentResolver().notifyChange(uri, null);
        return affectedRowsCount;
    }

    private static void loadUriMatchers(ProviderModel providerModel) {
        Class<?>[] tableModelClasses = providerModel.getTableModelClasses();
        if (tableModelClasses != null) {
            for (int i = 0; i < tableModelClasses.length; i++) {
                Class<?> tableModelClass = tableModelClasses[i];
                Table table = checkIfHasTableAnnotation(tableModelClass);
                uriMatcher.addURI(providerModel.getAuthority(), table.value(), i + 1);
                tableNameMap.put(i + 1, table.value());
                uriMatcher.addURI(providerModel.getAuthority(), table.value() + "/#", i + 1 + WILDCARD_OFFSET);
                tableNameMap.put(i + 1 + WILDCARD_OFFSET, table.value());
            }
        }
    }

    private static Table checkIfHasTableAnnotation(Class<?> tableModelClass) {
        Table tableAnnotation = tableModelClass.getAnnotation(Table.class);
        if (tableAnnotation == null || !(tableAnnotation instanceof Table)) {
            throw new AnnotationBasedProviderRuntimeException("No Table annotation found!");
        }
        return tableAnnotation;
    }

    @Override
    public String getAuthority() {
        return getProviderInfo().authority();
    }

    @Override
    public Class<?>[] getTableModelClasses() {
        return getProviderInfo().tableModelClasses();
    }

    @Override
    public SQLiteDatabase.CursorFactory getCursorFactory() {
        return null;
    }

    @Override
    public int getVersion() {
        return getProviderInfo().databaseVersion();
    }

    @Override
    public String getDatabaseName() {
        return getProviderInfo().databaseName();
    }

    private String getTableName(int tableMapKey) {
        return tableNameMap.get(tableMapKey);
    }

    private String getTableName(Uri uri) {
        return getTableName(uriMatcher.match(uri));
    }

    private Provider getProviderInfo() {
        if (providerAnnotation == null) {
            Provider annotation = getClass().getAnnotation(Provider.class);
            if (annotation == null || !(annotation instanceof Provider)) {
                throw new AnnotationBasedProviderRuntimeException("Unable to find provider annotation");
            }
            providerAnnotation = annotation;
        }
        return providerAnnotation;
    }

}




Java Source Code List

com.sudocode.android.commons.helper.ExceptionMessageExtractor.java
com.sudocode.content.provider.api.AnnotationBasedProviderDatabaseHelper.java
com.sudocode.content.provider.api.AnnotationBasedProvider.java
com.sudocode.content.provider.api.ProviderModel.java
com.sudocode.content.provider.api.TableModelConstants.java
com.sudocode.content.provider.api.annotation.Column.java
com.sudocode.content.provider.api.annotation.ForeignField.java
com.sudocode.content.provider.api.annotation.Id.java
com.sudocode.content.provider.api.annotation.InitScripts.java
com.sudocode.content.provider.api.annotation.Provider.java
com.sudocode.content.provider.api.annotation.Table.java
com.sudocode.content.provider.api.dao.AnnotationBasedDaoSupport.java
com.sudocode.content.provider.api.dao.AnnotationBasedDao.java
com.sudocode.content.provider.api.dao.GenericAnnotationBasedDao.java
com.sudocode.content.provider.api.dao.exception.AnnotationBasedDaoException.java
com.sudocode.content.provider.api.dao.exception.UnableToSaveRecordException.java
com.sudocode.content.provider.api.exception.AnnotationBasedProviderException.java
com.sudocode.content.provider.api.exception.AnnotationBasedProviderRuntimeException.java
com.sudocode.content.provider.api.support.application.AnnotationBasedDaoInitializer.java
com.sudocode.content.provider.api.support.application.GenericAnnotationBasedDaoInitializer.java
com.sudocode.mrsq.pricer.utils.ProviderMetaDataUtils.java