From 1f357911403fb6ef301f5d211b2da8910841641e Mon Sep 17 00:00:00 2001 From: Adam Saturna Date: Mon, 11 Mar 2013 11:49:22 -0600 Subject: [PATCH 01/18] Database Added, comments will be added later I just wanted to make it available ASAP. Note: Recipe Class modified for the database support, the advanced features can be added later. --- .../cmput301/recipebot/model/DbHelper.java | 348 ++++++++++++++++++ .../com/cmput301/recipebot/model/Recipe.java | 15 + 2 files changed, 363 insertions(+) create mode 100644 app/src/main/java/com/cmput301/recipebot/model/DbHelper.java diff --git a/app/src/main/java/com/cmput301/recipebot/model/DbHelper.java b/app/src/main/java/com/cmput301/recipebot/model/DbHelper.java new file mode 100644 index 0000000..2a962f9 --- /dev/null +++ b/app/src/main/java/com/cmput301/recipebot/model/DbHelper.java @@ -0,0 +1,348 @@ +package com.cmput301.recipebot.model; +//code from: http://www.youtube.com/watch?v=v61A90qlK9s + +import android.content.ContentValues; +import android.content.Context; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; +import android.provider.BaseColumns; +import android.util.Log; + +import java.util.ArrayList; + +public class DbHelper extends SQLiteOpenHelper { + + private static final String TAG = "DbHelper"; + public static final String DB_NAME = "log.db"; + public static final int DB_VERSION = 21; + + public static final String INGREDIENTS_TABLE = "ingredients"; + public static final String I_ID = BaseColumns._ID; + public static final String REC_ID = "rec_id"; + public static final String INGREDIENT = "ingredient"; + + public static final String FOODS_TABLE = "foods"; + public static final String F_NAME = "f_name"; + + public static final String RECIPES_TABLE = "recepies"; + public static final String R_ID = BaseColumns._ID; + public static final String R_NAME = "r_name"; + public static final String WRITER = "writer"; + public static final String PREP = "prep"; + + public static final String HASIMAGES_TABLE = "has_images"; + public static final String HIT_ID = BaseColumns._ID; + public static final String RECIPE = "r_id"; + public static final String IMAGE_PATH = "image_path"; + + public DbHelper(Context context) { + super(context, DB_NAME, null, DB_VERSION); + + } + + @Override + public void onCreate(SQLiteDatabase db) { + + //FOODS_TABLE + String sql1 = String.format("create table %s (%s TEXT primary key)", + FOODS_TABLE, F_NAME); + Log.d(TAG, "creating FOODS_TABLE"); + //RECIPES_TABLE + String sql2 = String.format("create table %s (%s INTEGER primary key, %s TEXT, %s TEXT, " + + "%s TEXT)", + RECIPES_TABLE, R_ID, WRITER, R_NAME, PREP); + + //HASIMAGES_TABLE + String sql3 = String.format("create table %s (%s INTEGER primary key, %s INTEGER, %s TEXT, " + + "foreign key(%s) references %s)", + HASIMAGES_TABLE, HIT_ID, RECIPE, IMAGE_PATH, RECIPE, RECIPES_TABLE); + + //INGREDIENTS_TABLE + String sql4 = String.format("create table %s (%s INTEGER primary key, %s INTEGER, %s TEXT, " + + "foreign key(%s) references %s)", + INGREDIENTS_TABLE, I_ID, REC_ID, INGREDIENT, REC_ID, RECIPES_TABLE); + + + try { + db.execSQL(sql1); + } catch (Exception e) { + Log.e(TAG, "FAILED TO CREATE TABLE: " + FOODS_TABLE); + } + try { + db.execSQL(sql2); + } catch (Exception e) { + Log.e(TAG, "FAILED TO CREATE TABLE: " + RECIPES_TABLE); + } + try { + db.execSQL(sql3); + } catch (Exception e) { + Log.e(TAG, "FAILED TO CREATE TABLE: " + HASIMAGES_TABLE); + } + try { + db.execSQL(sql4); + } catch (Exception e) { + Log.e(TAG, "FAILED TO CREATE TABLE: " + INGREDIENTS_TABLE); + } + } + + + @Override + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + try { + db.execSQL("drop table if exists " + FOODS_TABLE); + db.execSQL("drop table if exists " + RECIPES_TABLE); + db.execSQL("drop table if exists " + HASIMAGES_TABLE); + db.execSQL("drop table if exists " + INGREDIENTS_TABLE); + } catch (Exception e) { + Log.d("dbUPGRADE", "failed to drop all tables"); + } + + Log.d(TAG, "onUpdate dropped table ALL"); + this.onCreate(db); + } + // code from: http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/ + //returns a list of log entries + + public ArrayList loadPantry() { + + ArrayList logList = new ArrayList(); + SQLiteDatabase db = this.getReadableDatabase(); + + Cursor cursor = db.rawQuery("SELECT * FROM " + FOODS_TABLE, null); + if (cursor.moveToFirst()) { + try { + do { + logList.add(cursor.getString(0)); + } while (cursor.moveToNext()); + } catch (Exception e) { + Log.e(TAG, "failed to load all Foods from: " + FOODS_TABLE); + } + } + cursor.close(); + db.close(); + return logList; + } + + public ArrayList loadRecipes() { + + ArrayList logList = new ArrayList(); + ArrayList ingredients = new ArrayList(); + ArrayList photos = new ArrayList(); + ArrayList directions = new ArrayList(); + SQLiteDatabase db = this.getReadableDatabase(); + + Cursor cursor = db.rawQuery("SELECT * FROM " + RECIPES_TABLE + " ORDER BY " + R_NAME + " ASC", null); + if (cursor.moveToFirst()) { + try { + do { + //SET RECIPE INFO + Recipe recipe = new Recipe(); + recipe.setId(cursor.getInt(0)); + recipe.setUser(cursor.getString(1)); + recipe.setName(cursor.getString(2)); + directions.add(cursor.getString(3)); + recipe.setDirections(directions); + + //SET INGREDIENTS + Cursor cursor2 = db.rawQuery("SELECT " + INGREDIENT + " FROM " + INGREDIENTS_TABLE + " WHERE " + + REC_ID + " =" + cursor.getInt(0), null); + if(cursor2.moveToFirst()) { + try{ + do { + Ingredient ingredient = new Ingredient(cursor2.getString(0), "", 0); + ingredients.add(ingredient); + } while(cursor2.moveToNext()); + } catch (Exception e) { + Log.e(TAG, "failed to load INGREDIENTS from: " + INGREDIENTS_TABLE); + } + } + cursor2.close(); + recipe.setIngredients(ingredients); + + //SET PHOTOS + Cursor cursor3 = db.rawQuery("SELECT " + IMAGE_PATH + " FROM " + HASIMAGES_TABLE + " WHERE " + + RECIPE + " =" + cursor.getInt(0), null); + if(cursor3.moveToFirst()) { + try{ + do { + photos.add(cursor3.getString(0)); + } while(cursor3.moveToNext()); + } catch (Exception e) { + Log.e(TAG, "failed to load IMAGES from: " + HASIMAGES_TABLE); + } + } + cursor3.close(); + recipe.setPhotos(photos); + + logList.add(recipe); + } while (cursor.moveToNext()); + } catch (Exception e) { + Log.e(TAG, "failed to load all Recipes from: " + RECIPES_TABLE); + } + } + cursor.close(); + db.close(); + return logList; + } + + //TODO + public void addRecipe(int id, String writer, String recipe_name, String prep, ArrayList ingr, ArrayList pics) { + SQLiteDatabase db = this.getWritableDatabase(); + + ContentValues values = new ContentValues(); + values.put(WRITER, writer); + values.put(R_NAME, recipe_name); + values.put(PREP, prep); + values.put(R_ID, id); + + // Inserting Row + try { + db.insertOrThrow(RECIPES_TABLE, null, values); + } catch (Exception e) { + Log.e(TAG, "Couldn't put entry into: " + RECIPES_TABLE); + } + + ContentValues values2 = new ContentValues(); + + for(int x = 0; x < ingr.size(); x++) { + values2.put(INGREDIENT, ingr.get(x)); + values2.put(REC_ID, id); + + try { + db.insertOrThrow(INGREDIENTS_TABLE, null, values2); + Log.d(TAG, "try: add ingredient SUCCESS: " + ingr.get(x)); + } catch (Exception e) { + Log.e(TAG, "Couldn't put entry into: " + INGREDIENTS_TABLE + ": " + ingr.get(x)); + } + values2.clear(); + } + + ContentValues values3 = new ContentValues(); + for(int y = 0; y < pics.size(); y++) { + values3.put(IMAGE_PATH, pics.get(y)); + values3.put(RECIPE, id); + + try { + db.insertOrThrow(HASIMAGES_TABLE, null, values3); + Log.d(TAG, "try: add photo SUCCESS: " + pics.get(y)); + } catch (Exception e) { + Log.e(TAG, "Couldn't put entry into: " + HASIMAGES_TABLE + ": " + pics.get(y)); + } + values3.clear(); + } + values.clear(); + db.close(); + } + + public void editRecipeName(Recipe recipe, String r_name) { + SQLiteDatabase db = this.getWritableDatabase(); + + String sql1 = String.format("UPDATE " + RECIPES_TABLE + " SET " + R_NAME + " ='%s'" + + " WHERE " + R_ID + " =%s", r_name, recipe.getId()); + try { + db.execSQL(sql1); + recipe.setName(r_name); + } catch (Exception e) { + Log.e(TAG, "FAILED TO UPDATE A RECIPE: " + recipe.getName()); + } + + db.close(); + } + + public void editRecipePrep(Recipe recipe, String prep) { + SQLiteDatabase db = this.getWritableDatabase(); + ArrayList preparation = new ArrayList(); + preparation.add(prep); + + //UPDATE RECIPE_TABLE + String sql1 = String.format("UPDATE " + RECIPES_TABLE + " SET " + PREP + " ='%s'" + + " WHERE " + R_ID + " =%s", prep, recipe.getId()); + try { + db.execSQL(sql1); + recipe.setDirections(preparation); + } catch (Exception e) { + Log.e(TAG, "FAILED TO UPDATE A RECIPE: " + prep); + } + + db.close(); + } + + public void editRecipeIngredients(Recipe recipe, ArrayList ingredients) { + SQLiteDatabase db = this.getWritableDatabase(); + ContentValues values = new ContentValues(0); + ArrayList ingr = new ArrayList(); + + db.delete(INGREDIENTS_TABLE, REC_ID + " = ?", new String[] {String.valueOf(recipe.getId())}); + + for(int x = 0; x < ingredients.size(); x++) { + + values.put(REC_ID, recipe.getId()); + values.put(INGREDIENT, ingredients.get(x)); + Ingredient ingredient = new Ingredient(ingredients.get(x), "", 0); + ingr.add(ingredient); + try { + db.insertOrThrow(INGREDIENTS_TABLE, null, values); + } catch (Exception e) { + Log.e(TAG, "FAILED TO UPDATE INGREDIENT: " + ingredients.get(x)); + } + } + recipe.setIngredients(ingr); + db.close(); + } + + public void editRecipePictures(Recipe recipe, ArrayList pictures) { + SQLiteDatabase db = this.getWritableDatabase(); + ContentValues values = new ContentValues(0); + db.delete(HASIMAGES_TABLE, RECIPE + " = ?", new String[] {String.valueOf(recipe.getId())}); + + for(int x = 0; x < pictures.size(); x++) { + + values.put(RECIPE, recipe.getId()); + values.put(IMAGE_PATH, pictures.get(x)); + try { + db.insertOrThrow(HASIMAGES_TABLE, null, values); + recipe.setPhotos(pictures); + } catch (Exception e) { + Log.e(TAG, "FAILED TO UPDATE A PICTURE: " + pictures.get(x)); + } + } + + db.close(); + } + + public void removeRecipe(int r_id) { + SQLiteDatabase db = this.getWritableDatabase(); + try { + Log.d(TAG, "removing recipe and depenencies"); + db.delete(RECIPES_TABLE, R_ID + " = ?", new String[] {String.valueOf(r_id)}); + db.delete(INGREDIENTS_TABLE, REC_ID + " = ?", new String[] {String.valueOf(r_id)}); + db.delete(HASIMAGES_TABLE, RECIPE + " = ?", new String[] {String.valueOf(r_id)}); + } catch (Exception e) { + Log.e(TAG, "Couldn't remove a recipe and all db sub trees"); + } + db.close(); + } + + public void removePantryItem(String food_name) { + SQLiteDatabase db = this.getWritableDatabase(); + try { + db.delete(FOODS_TABLE, F_NAME + " = ?", new String[] {String.valueOf(food_name)}); + } catch(Exception e) { + Log.e(TAG, "FAILED TO REMOVE FROM PANTRY: " + food_name); + } + db.close(); + } + + public void addPantryItem(String food_name) { + SQLiteDatabase db = this.getWritableDatabase(); + ContentValues value = new ContentValues(); + value.put(F_NAME, food_name); + try { + db.insertOrThrow(FOODS_TABLE, null, value); + } catch(Exception e) { + Log.d(TAG, "FAILED TO ADD A PANTRY ITEM: " + food_name + "(already exists)"); + } + db.close(); + } +} \ No newline at end of file diff --git a/app/src/main/java/com/cmput301/recipebot/model/Recipe.java b/app/src/main/java/com/cmput301/recipebot/model/Recipe.java index dac980f..7fb4433 100644 --- a/app/src/main/java/com/cmput301/recipebot/model/Recipe.java +++ b/app/src/main/java/com/cmput301/recipebot/model/Recipe.java @@ -35,6 +35,7 @@ public class Recipe { private ArrayList ingredients; private ArrayList directions; private ArrayList images; + private ArrayList photos; public Recipe(int id, String user, String name, ArrayList ingredients, ArrayList directions, ArrayList images) { @@ -46,6 +47,16 @@ public Recipe(int id, String user, String name, ArrayList ingredient this.images = images; } + + //Another constructor added for db support + public Recipe() { + this.id = 0; + this.user = ""; + this.name = ""; + this.ingredients = new ArrayList(); + this.directions = new ArrayList(); + } + /** * Get the unique id for this recipe. * @@ -125,6 +136,10 @@ public void setImage(ArrayList images) { this.images = images; } + public void setPhotos(ArrayList photos) { + this.photos = photos; + } + @Override public String toString() { return "Recipe [id=" + id + ", user=" + user + ", name=" + name + ", ingredients=" From a9c09b5ac2e3fbd050343a721522fa9ff2e52d9d Mon Sep 17 00:00:00 2001 From: Ethan Mykytiuk Date: Mon, 11 Mar 2013 13:01:57 -0600 Subject: [PATCH 02/18] Adding in code for the pantry --- app/res/layout/fragment_pantry.xml | 74 ++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 app/res/layout/fragment_pantry.xml diff --git a/app/res/layout/fragment_pantry.xml b/app/res/layout/fragment_pantry.xml new file mode 100644 index 0000000..2dc6a2b --- /dev/null +++ b/app/res/layout/fragment_pantry.xml @@ -0,0 +1,74 @@ + + + + +