package com.uwetrottmann.shopr.provider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;

import com.uwetrottmann.shopr.provider.ShoprContract.Items;
import com.uwetrottmann.shopr.provider.ShoprContract.Shops;

public class ShoprProvider extends ContentProvider {

	private static UriMatcher sUriMatcher;

	private static final int ITEMS = 100;
	private static final int ITEM_ID = 101;

	private static final int SHOPS = 200;
	private static final int SHOP_ID = 201;

	/**
	 * Build and return a {@link UriMatcher} that catches all {@link Uri}
	 * variations supported by this {@link ContentProvider}.
	 */
	private static UriMatcher buildUriMatcher(Context context) {
		final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
		final String authority = ShoprContract.CONTENT_AUTHORITY;

		// Items
		matcher.addURI(authority, ShoprContract.PATH_ITEMS, ITEMS);
		matcher.addURI(authority, ShoprContract.PATH_ITEMS + "/*", ITEM_ID);

		// Shops
		matcher.addURI(authority, ShoprContract.PATH_SHOPS, SHOPS);
		matcher.addURI(authority, ShoprContract.PATH_SHOPS + "/*", SHOP_ID);

		return matcher;
	}

	@Override
	public boolean onCreate() {
		final Context context = getContext();
		sUriMatcher = buildUriMatcher(context);
		return true;
	}

	@Override
	public String getType(Uri uri) {
		final int match = sUriMatcher.match(uri);
		switch (match) {
		case ITEMS:
			return Items.CONTENT_TYPE;
		case ITEM_ID:
			return Items.CONTENT_ITEM_TYPE;
		case SHOPS:
			return Shops.CONTENT_TYPE;
		case SHOP_ID:
			return Shops.CONTENT_ITEM_TYPE;
		default:
			throw new UnsupportedOperationException("Unknown uri: " + uri);
		}
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		// TODO Auto-generated method stub
		return 0;
	}

}

@@ -231,15 +231,87 @@
 oid.
-net.Uri
+database.sqlite.SQLiteDatabase;%0Aimport android.net.Uri;%0Aimport android.util.Log
 ;%0A%0Ai
@@ -428,16 +428,77 @@
 t.Shops;
+%0Aimport com.uwetrottmann.shopr.provider.ShoprDatabase.Tables;
 %0A%0Apublic
@@ -748,16 +748,114 @@
 = 201;%0A%0A
+%09private static final String TAG = %22ShoprProvider%22;%0A%0A%09private static final boolean LOGV = false;%0A%0A
 %09/**%0A%09 *
@@ -1482,24 +1482,61 @@
 atcher;%0A%09%7D%0A%0A
+%09private ShoprDatabase mOpenHelper;%0A%0A
 %09@Override%0A%09
@@ -1645,16 +1645,60 @@
 ntext);%0A
+%09%09mOpenHelper = new ShoprDatabase(context);%0A
 %09%09return
@@ -2344,57 +2344,698 @@
 %7B%0A%09%09
-// TODO Auto-generated method stub%0A%09%09return null;
+if (LOGV)%0A%09%09%09Log.v(TAG, %22insert(uri=%22 + uri + %22, values=%22 + values.toString()%0A%09%09%09%09%09+ %22)%22);%0A%09%09final SQLiteDatabase db = mOpenHelper.getWritableDatabase();%0A%09%09final int match = sUriMatcher.match(uri);%0A%09%09switch (match) %7B%0A%09%09case ITEMS: %7B%0A%09%09%09db.insertOrThrow(Tables.ITEMS, null, values);%0A%09%09%09getContext().getContentResolver().notifyChange(uri, null);%0A%09%09%09return Items.buildItemUri(values.getAsInteger(Items._ID));%0A%09%09%7D%0A%09%09case SHOPS: %7B%0A%09%09%09db.insertOrThrow(Tables.SHOPS, null, values);%0A%09%09%09getContext().getContentResolver().notifyChange(uri, null);%0A%09%09%09return Shops.buildShopUri(values.getAsInteger(Shops._ID));%0A%09%09%7D%0A%09%09default: %7B%0A%09%09%09throw new UnsupportedOperationException(%22Unknown uri: %22 + uri);%0A%09%09%7D%0A%09%09%7D
 %0A%09%7D%0A

Implement inserting.