public List<CookingRecipe> findRecipesWithExactIngredients(List<String> ingredients) throws Exception { System.out.println("Database: findRecipesWithExactIngredients(" + ingredients + ")"); List<CookingRecipe> allCookingRecipes = getAllCookingRecipes(); List<CookingRecipe> vyhovujuce = new ArrayList<>(); boolean obsahuje = false; for (CookingRecipe cr : allCookingRecipes) { // iterujeme vsetky ingrediencie receptu // prechadzame ingrediencie a zistujeme ci sa dana ingrediencia nachadza for (String ci2 : ingredients) { obsahuje = false; // predpokladame ze danu ingredienciu neobsahuje for (CookingIngredient ci : cr.getIngredients()) { if (ci.getIngredient().equalsIgnoreCase(ci2)) { obsahuje = true; break; } } if (!obsahuje) { break; } } // ak najdeme vsetky ingrediencie v recepte, tak vyhovuje if (obsahuje) { vyhovujuce.add(cr); } } System.out.println("Database: vyhovujucich receptov: " + vyhovujuce.size()); return vyhovujuce; }
public synchronized CookingRecipe insertCookingRecipe(CookingRecipe recipe) throws Exception { System.out.println("Database: insertCookingRecipe(" + recipe + ")"); SimpleJdbcInsert insert = new SimpleJdbcInsert(jdbcTemplate); insert.withTableName("recipes"); insert.setGeneratedKeyName("id"); Map<String, Object> mapa = new HashMap<String, Object>(); mapa.put("recipe_name", recipe.getName()); mapa.put("author", recipe.getAuthor()); mapa.put("guide", recipe.getGuide()); // mapa.put("incident_ID", incidentID); Number insertedRecipeId = insert.executeAndReturnKey(mapa); recipe.setId(insertedRecipeId.longValue()); insertIngredients(recipe.getIngredients(), insertedRecipeId.longValue()); for (CookingIngredient ci : recipe.getIngredients()) { ci.setRecipe_id(insertedRecipeId.longValue()); } return recipe; }
public List<CookingRecipe> getAllCookingRecipes() throws Exception { System.out.println("Database: getAllCookingRecipes()"); // load recepty bez ingrediencii RowMapper<CookingRecipe> rowMapper = new CookingRecipeRowMapper(); String sql = "SELECT * FROM recipes order by id asc"; List<CookingRecipe> recipes = jdbcTemplate.query(sql, rowMapper); // load ingrediencie k receptom RowMapper<CookingIngredient> mapper2 = new CookingIngredientRowMapper(); sql = "SELECT * FROM ingredients order by recipe_id,ingredient,quantity asc"; List<CookingIngredient> ingredients = jdbcTemplate.query(sql, mapper2); // priradime ingrediencie k svojim receptom for (CookingRecipe cr : recipes) { for (CookingIngredient ci : ingredients) { if (ci.getRecipe_id().equals(cr.getId())) { cr.getIngredients().add(ci); } } } return recipes; }
public List<CookingRecipe> findRecipesWithIngredients(List<String> ingredients) throws Exception { System.out.println("Database: findRecipesWithIngredients(" + ingredients + ")"); List<CookingRecipe> allCookingRecipes = getAllCookingRecipes(); List<CookingRecipe> vyhovujuce = new ArrayList<>(); boolean naslo = false; for (CookingRecipe cr : allCookingRecipes) { // iterujeme vsetky ingrediencie receptu for (CookingIngredient ci : cr.getIngredients()) { naslo = false; for (String ci2 : ingredients) { if (ci.getIngredient().equalsIgnoreCase(ci2)) { vyhovujuce.add(cr); naslo = true; break; } } if (naslo) { break; } } } System.out.println("Database: vyhovujucich receptov: " + vyhovujuce.size()); return vyhovujuce; }
private void insertIngredients(List<CookingIngredient> ingredients, long recipe_id) { System.out.println("Database: insertIngredients(" + ingredients + ", " + recipe_id + ")"); // vytvor register log pre dany blocek StringBuilder sql = new StringBuilder("insert into ingredients(recipe_id,ingredient,quantity) values\n"); if (ingredients.size() == 0) { throw new RuntimeException("ingredients size must not be 0"); } for (int i = 0; i < ingredients.size() - 1; i++) { CookingIngredient ing = ingredients.get(i); sql.append( "(" + recipe_id + " ,'" + ing.getIngredient() + "','" + ing.getQuantity() + "' ),\n"); } CookingIngredient ing = ingredients.get(ingredients.size() - 1); sql.append("(" + recipe_id + " ,'" + ing.getIngredient() + "','" + ing.getQuantity() + "' );"); jdbcTemplate.execute(sql.toString()); }