From 95641cf00bebe110e6791087b0f13781b264a23b Mon Sep 17 00:00:00 2001 From: Tim Holloway Date: Sun, 16 Jan 2022 17:33:20 -0500 Subject: [PATCH] Updateable pantry status --- .../mousetech/gourmetj/ShoppingListBean.java | 53 +++++++++++++++++-- .../gourmetj/persistence/model/Pantry.java | 8 ++- .../gourmetj/utils/YamlShoppingList.java | 4 ++ .../META-INF/resources/shoppingList.xhtml | 24 ++++++++- 4 files changed, 83 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/mousetech/gourmetj/ShoppingListBean.java b/src/main/java/com/mousetech/gourmetj/ShoppingListBean.java index 6995106..ea1da3e 100644 --- a/src/main/java/com/mousetech/gourmetj/ShoppingListBean.java +++ b/src/main/java/com/mousetech/gourmetj/ShoppingListBean.java @@ -16,16 +16,19 @@ import org.primefaces.model.StreamedContent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.mousetech.gourmetj.persistence.dao.PantryRepository; import com.mousetech.gourmetj.persistence.dao.ShopcatRepository; import com.mousetech.gourmetj.persistence.model.Ingredient; import com.mousetech.gourmetj.persistence.model.Pantry; import com.mousetech.gourmetj.persistence.model.Recipe; import com.mousetech.gourmetj.persistence.model.Shopcat; +import com.mousetech.gourmetj.persistence.service.RecipeService; import com.mousetech.gourmetj.utils.YamlShoppingList; -/** Backing bean for the Shopping List tab of the "More..." - * page. +/** + * Backing bean for the Shopping List tab of the "More..." page. * _TestedBy ShoppingListBeanTest + * * @author timh * @since Jan 16, 2022 */ @@ -104,6 +107,11 @@ public class ShoppingListBean implements Serializable { buildMaps(); } + public void clearRecipeList() { + this.userSession.getShoppingList().clear(); + getRecipeList().clear(); + } + public List getRecipeList() { if (this.recipeList == null) { this.recipeList = loadRecipeList(); @@ -111,14 +119,29 @@ public class ShoppingListBean implements Serializable { return this.recipeList; } + @Inject + RecipeService recipeService; + private List loadRecipeList() { List list = userSession.getShoppingList().stream() - .map(r -> new RecipeReference(r)) + .map(r -> new RecipeReference(fetchRecipe(r))) .collect(Collectors.toList()); return list; } + /** + * Fetch the current version of a recipe. The reference + * version may be out of date if the recipe was edited or + * the Pantry status of one or more Ingredients changed. + * @param r reference recipe + * @return current version of reference recipe + */ + private Recipe fetchRecipe(Recipe r) { + Recipe rnew = this.recipeService.findDetails(r.getId()); + return rnew; + } + public List getIngredientList() { return this.siList; } @@ -165,7 +188,7 @@ public class ShoppingListBean implements Serializable { } boolean inPantry = false; Pantry pantry = ing.getPantry(); - if ( (pantry != null) && pantry.getPantry()) { + if ((pantry != null) && pantry.getPantry()) { inPantry = true; } sing = new ShopIngredient(amt, ing.getUnit(), @@ -242,6 +265,28 @@ public class ShoppingListBean implements Serializable { .createDownload(getIngredientList()); } + + @Inject + PantryRepository pantryRepository; + + /** + * Listener for when Pantry checkbox is checked/unchecked + * @param ing Ingredient to update + */ + public void updatePantry(ShopIngredient ing) { + String ingKey = ing.getIngkey(); + boolean checked = ing.isInPantry(); + Pantry pantry = pantryRepository.findPantryByIngkey(ingKey); + if ( pantry == null ) { + pantry = new Pantry(ingKey, checked); + } else { + pantry.setPantry(checked); + } + pantryRepository.save(pantry); + // Reset recipes to cover ingredient change + this.recipeList = null; + } + // ============================================= private List shopcatList; diff --git a/src/main/java/com/mousetech/gourmetj/persistence/model/Pantry.java b/src/main/java/com/mousetech/gourmetj/persistence/model/Pantry.java index 8b7e331..7c2abfe 100644 --- a/src/main/java/com/mousetech/gourmetj/persistence/model/Pantry.java +++ b/src/main/java/com/mousetech/gourmetj/persistence/model/Pantry.java @@ -13,10 +13,11 @@ public class Pantry implements Serializable { private static final long serialVersionUID = 1L; @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id") private Long id; - @Column(name = "ingkey", nullable=false) + @Column(name = "ingkey", nullable = false) private String ingkey; @Column(name = "pantry") @@ -28,6 +29,11 @@ public class Pantry implements Serializable { public Pantry() { } + public Pantry(String ingKey, boolean checked) { + this.ingkey = ingKey; + this.pantry = checked; + } + public Long getId() { return this.id; } diff --git a/src/main/java/com/mousetech/gourmetj/utils/YamlShoppingList.java b/src/main/java/com/mousetech/gourmetj/utils/YamlShoppingList.java index 61c23e9..129fed4 100644 --- a/src/main/java/com/mousetech/gourmetj/utils/YamlShoppingList.java +++ b/src/main/java/com/mousetech/gourmetj/utils/YamlShoppingList.java @@ -49,6 +49,10 @@ public class YamlShoppingList { if (StringUtils.isBlank(newShopcat)) { newShopcat = "Unassigned"; } + if ( ing.isInPantry()) { + // Don't put pantry items in list. + continue; + } if (!StringUtils.equals(newShopcat, oldShopcat)) { wtr.println(newShopcat + ":"); oldShopcat = newShopcat; diff --git a/src/main/resources/META-INF/resources/shoppingList.xhtml b/src/main/resources/META-INF/resources/shoppingList.xhtml index a4486fd..0434e97 100644 --- a/src/main/resources/META-INF/resources/shoppingList.xhtml +++ b/src/main/resources/META-INF/resources/shoppingList.xhtml @@ -47,6 +47,12 @@ > + - + + + + + + + + @@ -131,6 +149,10 @@ /> + + + +