Created
August 8, 2017 07:03
-
-
Save AnnKrepchenko/7cfa82e37f182dcef51d708d6ae8c7b8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GoodsContentProvider extends ContentProvider { | |
private DbHelper dbHelper; | |
private static final int GOODS = 1; | |
private static final int GOODS_GOOD = 2; | |
//-- | |
private static final int CATEGORIES = 7; | |
private static final int CATEGORIES_CATEGORY = 8; | |
//- | |
private static final UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH); | |
static { | |
URI_MATCHER.addURI(GoodsEntity.AUTHORITY, GoodsEntity.TABLE_NAME, GOODS); | |
URI_MATCHER.addURI(GoodsEntity.AUTHORITY, GoodsEntity.TABLE_NAME + "/#", GOODS_GOOD); | |
URI_MATCHER.addURI(GoodsEntity.AUTHORITY, CategoryEntity.TABLE_NAME, CATEGORIES); | |
URI_MATCHER.addURI(GoodsEntity.AUTHORITY, CategoryEntity.TABLE_NAME + "/#", CATEGORIES_CATEGORY); | |
} | |
@Override | |
public boolean onCreate() { | |
dbHelper = new DbHelper(getContext()); | |
return true; | |
} | |
@Override | |
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { | |
String selectionToAppend = ""; | |
String tableName; | |
String query = ""; | |
switch (URI_MATCHER.match(uri)) { | |
case GOODS: | |
tableName = GoodsEntity.TABLE_NAME; | |
break; | |
case GOODS_GOOD: | |
tableName = GoodsEntity.TABLE_NAME; | |
selectionToAppend = GoodsEntity._ID + " = " + uri.getLastPathSegment(); | |
break; | |
default: | |
throw new IllegalArgumentException("Wrong uri"); | |
} | |
SQLiteDatabase db = dbHelper.getReadableDatabase(); | |
Cursor c= db.query(tableName, projection, appendSelections(selection, selectionToAppend), selectionArgs, null, null, sortOrder); | |
c.setNotificationUri(getContext().getContentResolver(), uri); | |
return c; | |
} | |
@Override | |
public String getType(Uri uri) { | |
String result; | |
switch (URI_MATCHER.match(uri)) { | |
case GOODS: | |
result = "vnd.android.cursor.dir/com.krepchenko.yourshoppinglist.provider." + GoodsEntity.TABLE_NAME; | |
break; | |
case GOODS_GOOD: | |
result = "vnd.android.cursor.item/com.krepchenko.yourshoppinglist.provider." + GoodsEntity.TABLE_NAME; | |
break; | |
case CATEGORIES: | |
result = "vnd.android.cursor.dir/com.krepchenko.yourshoppinglist.provider." + CategoryEntity.TABLE_NAME; | |
break; | |
case CATEGORIES_CATEGORY: | |
result = "vnd.android.cursor.item/com.krepchenko.yourshoppinglist.provider." + CategoryEntity.TABLE_NAME; | |
break; | |
default: | |
throw new IllegalArgumentException(); | |
} | |
return result; | |
} | |
@Override | |
public Uri insert(Uri uri, ContentValues values) { | |
long result; | |
String tableName; | |
switch (URI_MATCHER.match(uri)) { | |
case GOODS: | |
tableName = GoodsEntity.TABLE_NAME; | |
break; | |
case CATEGORIES: | |
tableName = CategoryEntity.TABLE_NAME; | |
break; | |
default: | |
throw new IllegalArgumentException(); | |
} | |
SQLiteDatabase db = dbHelper.getWritableDatabase(); | |
result = db.insert(tableName, null, values); | |
getContext().getContentResolver().notifyChange(uri, null); | |
return Uri.withAppendedPath(uri, String.valueOf(result)); | |
} | |
@Override | |
public int delete(Uri uri, String selection, String[] selectionArgs) { | |
String tableName; | |
String selectionToAppend = ""; | |
switch (URI_MATCHER.match(uri)) { | |
case GOODS: | |
tableName = GoodsEntity.TABLE_NAME; | |
break; | |
case GOODS_GOOD: | |
tableName = GoodsEntity.TABLE_NAME; | |
selectionToAppend = GoodsEntity._ID + " = " + uri.getLastPathSegment(); | |
break; | |
case CATEGORIES: | |
tableName = CategoryEntity.TABLE_NAME; | |
break; | |
case CATEGORIES_CATEGORY: | |
tableName = CategoryEntity.TABLE_NAME; | |
selectionToAppend = CategoryEntity._ID + " = " + uri.getLastPathSegment(); | |
break; | |
default: | |
throw new IllegalArgumentException(); | |
} | |
SQLiteDatabase db = dbHelper.getWritableDatabase(); | |
int affected = db.delete(tableName, appendSelections(selection, selectionToAppend), selectionArgs); | |
if (affected > 0) { | |
getContext().getContentResolver().notifyChange(uri, null); | |
} | |
return affected; | |
} | |
@Override | |
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { | |
String tableName; | |
String selectionToAppend = ""; | |
switch (URI_MATCHER.match(uri)) { | |
case GOODS: | |
tableName = GoodsEntity.TABLE_NAME; | |
break; | |
case GOODS_GOOD: | |
tableName = GoodsEntity.TABLE_NAME; | |
selectionToAppend = GoodsEntity._ID + " = " + uri.getLastPathSegment(); | |
break; | |
case CATEGORIES: | |
tableName = CategoryEntity.TABLE_NAME; | |
break; | |
case CATEGORIES_CATEGORY: | |
tableName = CategoryEntity.TABLE_NAME; | |
selectionToAppend = CategoryEntity._ID + " = " + uri.getLastPathSegment(); | |
break; | |
default: | |
throw new IllegalArgumentException(); | |
} | |
SQLiteDatabase db = dbHelper.getWritableDatabase(); | |
int affected = db.update(tableName, values, appendSelections(selection, selectionToAppend), selectionArgs); | |
if (affected > 0) { | |
getContext().getContentResolver().notifyChange(uri, null); | |
} | |
return affected; | |
} | |
private static String appendSelections(String baseSelection, String selectionToAppend) { | |
if (!TextUtils.isEmpty(selectionToAppend)) { | |
if (!TextUtils.isEmpty(baseSelection)) { | |
baseSelection = " ( " + baseSelection + " ) AND " + selectionToAppend; | |
} else { | |
baseSelection = selectionToAppend; | |
} | |
} | |
return baseSelection; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment