Example #1
0
  /**
   * retreives information from the nutritionix api
   *
   * @param curRecipe
   */
  public void getNutritionixIngredientInfo(Recipe curRecipe) {

    ArrayList<NutritionixInfo> nutritionixIngredientList = new ArrayList<NutritionixInfo>();
    List<RecipeIngredient> ingredients = this.recipe.getIngredients();

    try {

      // Use the Nutritionix API to lookup nutrition facts info for each one of our ingredients
      for (RecipeIngredient ingredient : ingredients) {
        String ingredientName = ingredient.getIngredient().getIngredientName();
        nutritionixIngredientList.add(nutritionixBean.searchForIngredient(ingredientName, 0, 1));
      }

      // Calculate and set the nutrition attributes for this class
      for (RecipeIngredient ingredient : ingredients) {
        int servings = Math.round(ingredient.getIngredientAmount());
        for (NutritionixInfo ni : nutritionixIngredientList) {
          this.setCalories(servings * (this.getCalories() + ni.getNfCalories()));
          this.setCaloriesFromFat(
              servings * (this.getCaloriesFromFat() + ni.getNfCaloriesFromFat()));
          this.setCholesterol(servings * (this.getCholesterol() + ni.getNfCholesterol()));
          this.setDietaryFiber(servings * (this.getDietaryFiber() + ni.getNfDietaryFiber()));
          this.setProtein(servings * (this.getProtein() + ni.getNfProtein()));
          this.setSaturatedFat(servings * (this.getSaturatedFat() + ni.getNfSaturatedFat()));
          this.setSodium(servings * (this.getSodium() + ni.getNfSodium()));
          this.setSugars(servings * (this.getSugars() + ni.getNfSugars()));
          this.setTotalCarbohydrates(
              servings * (this.getTotalCarbohydrates() + ni.getNfTotalCarbohydrate()));
          this.setTotalFat(servings * (this.getTotalFat() + ni.getNfTotalFat()));
          this.setTransFat(servings * (this.getTransFat() + ni.getNfTransFat()));

          this.setCalciumPercentage(servings * (this.getCalciumPercentage() + ni.getNfCalciumDv()));
          this.setIronPercentage(servings * (this.getIronPercentage() + ni.getNfIronDv()));
          this.setVitaminAPercentage(
              servings * (this.getVitaminAPercentage() + ni.getNfVitaminADv()));
          this.setVitaminCPercentage(
              servings * (this.getVitaminCPercentage() + ni.getNfVitaminCDv()));
        }
      }

    } catch (MalformedURLException ex) {
      Logger.getLogger(ViewRecipeBean.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
      Logger.getLogger(ViewRecipeBean.class.getName()).log(Level.SEVERE, null, ex);
    } catch (JSONException ex) {
      Logger.getLogger(ViewRecipeBean.class.getName()).log(Level.SEVERE, null, ex);
    }
  }
Example #2
0
 public String getReadableIngredientsList() {
   if (recipe != null) {
     String ingredientsListString = "";
     List<RecipeIngredient> ingredients = this.recipe.getIngredients();
     int ingredientsCount = ingredients.size();
     int i = 1;
     for (RecipeIngredient ingredient : ingredients) {
       if (i != ingredientsCount) {
         ingredientsListString += ingredient.getIngredient().getIngredientName() + ", ";
       } else {
         ingredientsListString += ingredient.getIngredient().getIngredientName();
       }
       i++;
     }
     return ingredientsListString;
   } else {
     return "";
   }
 }