Class RecipeDB


  • public class RecipeDB
    extends java.lang.Object
    RecipeDB is a class that handles all the database operations for Recipe objects.
    • Constructor Summary

      Constructors 
      Constructor Description
      RecipeDB​(DBConnection connection)
      Create a RecipeDB object
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addRecipe​(Recipe recipe, OnCompleteListener<Recipe> listener)
      Adds a recipe to the Recipe collection in db and any ingredients not already in Recipe Ingredients.
      void addRecipeHelper​(java.util.HashMap<java.lang.String,​java.lang.Object> recipeMap, java.util.ArrayList<java.util.HashMap<java.lang.String,​java.lang.Object>> ingredients, Recipe recipe, OnCompleteListener<Recipe> listener)
      Helper function that adds the Recipe document to db once all transactions are finished.
      void addRecipeIncrementCounter​(java.util.HashMap<java.lang.String,​java.lang.Object> recipeMap, java.util.ArrayList<java.util.HashMap<java.lang.String,​java.lang.Object>> ingredients, Recipe recipe, OnCompleteListener<Recipe> listener)
      addRecipeIncrementCounter is a helper method for addRecipe.
      void delRecipe​(Recipe recipe, OnCompleteListener<Recipe> listener)
      Deletes a recipe document with the id from the collection of recipes
      void delRecipeDecrementCounter​(Recipe recipe, com.google.firebase.firestore.DocumentReference recipeRef, com.google.firebase.firestore.DocumentSnapshot recipeDoc, OnCompleteListener<Recipe> listener)
      Helper function for decrementing counter of every ingredient in the deleted Recipe object.
      void delRecipeHelper​(Recipe recipe, com.google.firebase.firestore.DocumentReference recipeRef, OnCompleteListener<Recipe> listener)
      Helper function for deleting the recipe document.
      com.google.firebase.firestore.DocumentReference getDocumentReference​(java.lang.String id)
      Get the DocumentReference from Recipes collection for the given id
      com.google.firebase.firestore.Query getQuery()
      Gets a query for Recipes in the db.
      void getRecipe​(java.lang.String id, OnCompleteListener<Recipe> listener)
      Gets the Recipe object with the given id from the db.
      com.google.firebase.firestore.Query getSearchQuery​(java.lang.String field)
      Query for the search functionality.
      com.google.firebase.firestore.Query getSortQuery​(java.lang.String field)
      Gets query from the db with Recipes sorted by given field.
      void updateRecipe​(Recipe recipe, OnCompleteListener<Recipe> listener)
      Updates the Recipe object in the DB
      void updateRecipeDecrementCounter​(Recipe recipe, Recipe foundRecipe, java.util.ArrayList<java.util.HashMap<java.lang.String,​java.lang.Object>> recipeIngredients, OnCompleteListener<Recipe> listener)
      Helper method that decrements counter for each ingredient in the old Recipe.
      void updateRecipeFindOldRecipe​(Recipe recipe, java.util.ArrayList<java.util.HashMap<java.lang.String,​java.lang.Object>> recipeIngredients, OnCompleteListener<Recipe> listener)
      A helper method for updateRecipe() that finds the old recipe in the db.
      void updateRecipeHelper​(Recipe recipe, java.util.ArrayList<java.util.HashMap<java.lang.String,​java.lang.Object>> recipeIngredients, OnCompleteListener<Recipe> listener)
      Given the Recipe object, updates it in the db.
      void updateRecipeIncrementCounter​(Recipe recipe, Recipe foundRecipe, java.util.ArrayList<java.util.HashMap<java.lang.String,​java.lang.Object>> recipeIngredients, OnCompleteListener<Recipe> listener)
      Helper method that increments counter for each ingredient in the updated Recipe.
      void updateReferenceCount​(Recipe recipe, int delta, OnCompleteListener<Recipe> listener)
      Updates the counter for a Recipe object in the db.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • RecipeDB

        public RecipeDB​(DBConnection connection)
        Create a RecipeDB object
        Parameters:
        connection - : the DBConnection object to connect to the database
    • Method Detail

      • addRecipe

        public void addRecipe​(Recipe recipe,
                              OnCompleteListener<Recipe> listener)
        Adds a recipe to the Recipe collection in db and any ingredients not already in Recipe Ingredients. This method will set the Recipe id to the corresponding document id in the collection
        Parameters:
        recipe - A Recipe object we want to add to the collection of Recipes and whose id we want to set
        listener - : the listener to call when the operation is complete
      • addRecipeIncrementCounter

        public void addRecipeIncrementCounter​(java.util.HashMap<java.lang.String,​java.lang.Object> recipeMap,
                                              java.util.ArrayList<java.util.HashMap<java.lang.String,​java.lang.Object>> ingredients,
                                              Recipe recipe,
                                              OnCompleteListener<Recipe> listener)
        addRecipeIncrementCounter is a helper method for addRecipe. It increments the count of ingredients in the database.
        Parameters:
        recipeMap - A HashMap of the recipe to be added.
        ingredients - A list of ingredients to be added.
        recipe - The recipe to be added.
        listener - The listener to be called when the recipe is added.
      • addRecipeHelper

        public void addRecipeHelper​(java.util.HashMap<java.lang.String,​java.lang.Object> recipeMap,
                                    java.util.ArrayList<java.util.HashMap<java.lang.String,​java.lang.Object>> ingredients,
                                    Recipe recipe,
                                    OnCompleteListener<Recipe> listener)
        Helper function that adds the Recipe document to db once all transactions are finished.
        Parameters:
        recipeMap - A HashMap of the recipe to be added.
        ingredients - A list of ingredients to be added.
        recipe - The recipe to be added.
        listener - The listener to be called when the recipe is added.
      • getRecipe

        public void getRecipe​(java.lang.String id,
                              OnCompleteListener<Recipe> listener)
        Gets the Recipe object with the given id from the db.
        Parameters:
        id - The id of the Recipe to be retrieved.
        listener - The listener to be called when the recipe is retrieved.
      • updateRecipe

        public void updateRecipe​(Recipe recipe,
                                 OnCompleteListener<Recipe> listener)
        Updates the Recipe object in the DB
        Parameters:
        recipe - the Recipe object to the updated
        listener - the OnUpdateRecipeListener object to handle the result
      • updateRecipeFindOldRecipe

        public void updateRecipeFindOldRecipe​(Recipe recipe,
                                              java.util.ArrayList<java.util.HashMap<java.lang.String,​java.lang.Object>> recipeIngredients,
                                              OnCompleteListener<Recipe> listener)
        A helper method for updateRecipe() that finds the old recipe in the db.
        Parameters:
        recipe - The new recipe to be updated.
        recipeIngredients - The ingredients of the new recipe.
        listener - The listener to be called when the recipe is updated.
      • updateRecipeIncrementCounter

        public void updateRecipeIncrementCounter​(Recipe recipe,
                                                 Recipe foundRecipe,
                                                 java.util.ArrayList<java.util.HashMap<java.lang.String,​java.lang.Object>> recipeIngredients,
                                                 OnCompleteListener<Recipe> listener)
        Helper method that increments counter for each ingredient in the updated Recipe.
        Parameters:
        recipe - The new recipe to be updated.
        foundRecipe - The old recipe to be updated.
        recipeIngredients - The ingredients of the new recipe.
        listener - The listener to be called when the recipe is updated.
      • updateRecipeDecrementCounter

        public void updateRecipeDecrementCounter​(Recipe recipe,
                                                 Recipe foundRecipe,
                                                 java.util.ArrayList<java.util.HashMap<java.lang.String,​java.lang.Object>> recipeIngredients,
                                                 OnCompleteListener<Recipe> listener)
        Helper method that decrements counter for each ingredient in the old Recipe.
        Parameters:
        recipe - The new recipe to be updated.
        foundRecipe - The old recipe to be updated.
        recipeIngredients - The ingredients of the new recipe.
        listener - The listener to be called when the recipe is updated.
      • updateRecipeHelper

        public void updateRecipeHelper​(Recipe recipe,
                                       java.util.ArrayList<java.util.HashMap<java.lang.String,​java.lang.Object>> recipeIngredients,
                                       OnCompleteListener<Recipe> listener)
        Given the Recipe object, updates it in the db.
        Parameters:
        recipe - the Recipe object to be updated
        recipeIngredients - the ArrayList of HashMaps of the ingredients
        listener - the OnUpdateRecipeListener object to handle the result
      • delRecipe

        public void delRecipe​(Recipe recipe,
                              OnCompleteListener<Recipe> listener)
        Deletes a recipe document with the id from the collection of recipes
        Parameters:
        recipe - The recipe to delete.
        listener - The listener to be called when the recipe is deleted.
      • delRecipeDecrementCounter

        public void delRecipeDecrementCounter​(Recipe recipe,
                                              com.google.firebase.firestore.DocumentReference recipeRef,
                                              com.google.firebase.firestore.DocumentSnapshot recipeDoc,
                                              OnCompleteListener<Recipe> listener)
        Helper function for decrementing counter of every ingredient in the deleted Recipe object.
        Parameters:
        recipe - The recipe to delete.
        recipeRef - The reference to the recipe to delete.
        recipeDoc - The document of the recipe to delete.
        listener - The listener to be called when the recipe is deleted.
      • delRecipeHelper

        public void delRecipeHelper​(Recipe recipe,
                                    com.google.firebase.firestore.DocumentReference recipeRef,
                                    OnCompleteListener<Recipe> listener)
        Helper function for deleting the recipe document.
        Parameters:
        recipe - The recipe to delete.
        recipeRef - The reference to the recipe document.
        listener - The listener to be called when the recipe is deleted.
      • getDocumentReference

        public com.google.firebase.firestore.DocumentReference getDocumentReference​(java.lang.String id)
        Get the DocumentReference from Recipes collection for the given id
        Parameters:
        id - The String of the document in Recipes collection we want
        Returns:
        DocumentReference of the Recipe
      • getQuery

        public com.google.firebase.firestore.Query getQuery()
        Gets a query for Recipes in the db.
        Returns:
        the query for Recipes in the db.
      • getSortQuery

        public com.google.firebase.firestore.Query getSortQuery​(java.lang.String field)
        Gets query from the db with Recipes sorted by given field.
        Parameters:
        field - The field to sort by.
        Returns:
        the query for Recipes in the db.
      • updateReferenceCount

        public void updateReferenceCount​(Recipe recipe,
                                         int delta,
                                         OnCompleteListener<Recipe> listener)
        Updates the counter for a Recipe object in the db.
        Parameters:
        recipe - The Recipe object to update.
        delta - The amount to increment the counter by.
        listener - The listener to be called when the counter is updated.
      • getSearchQuery

        public com.google.firebase.firestore.Query getSearchQuery​(java.lang.String field)
        Query for the search functionality.
        Parameters:
        field - The keyword to search for.
        Returns:
        Results matching the keyword.