Updateable pantry status

This commit is contained in:
Tim Holloway 2022-01-16 17:33:20 -05:00
parent ec823ff784
commit 95641cf00b
4 changed files with 83 additions and 6 deletions

View File

@ -16,16 +16,19 @@ import org.primefaces.model.StreamedContent;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.mousetech.gourmetj.persistence.dao.PantryRepository;
import com.mousetech.gourmetj.persistence.dao.ShopcatRepository; import com.mousetech.gourmetj.persistence.dao.ShopcatRepository;
import com.mousetech.gourmetj.persistence.model.Ingredient; import com.mousetech.gourmetj.persistence.model.Ingredient;
import com.mousetech.gourmetj.persistence.model.Pantry; import com.mousetech.gourmetj.persistence.model.Pantry;
import com.mousetech.gourmetj.persistence.model.Recipe; import com.mousetech.gourmetj.persistence.model.Recipe;
import com.mousetech.gourmetj.persistence.model.Shopcat; import com.mousetech.gourmetj.persistence.model.Shopcat;
import com.mousetech.gourmetj.persistence.service.RecipeService;
import com.mousetech.gourmetj.utils.YamlShoppingList; 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 * _TestedBy ShoppingListBeanTest
*
* @author timh * @author timh
* @since Jan 16, 2022 * @since Jan 16, 2022
*/ */
@ -104,6 +107,11 @@ public class ShoppingListBean implements Serializable {
buildMaps(); buildMaps();
} }
public void clearRecipeList() {
this.userSession.getShoppingList().clear();
getRecipeList().clear();
}
public List<RecipeReference> getRecipeList() { public List<RecipeReference> getRecipeList() {
if (this.recipeList == null) { if (this.recipeList == null) {
this.recipeList = loadRecipeList(); this.recipeList = loadRecipeList();
@ -111,14 +119,29 @@ public class ShoppingListBean implements Serializable {
return this.recipeList; return this.recipeList;
} }
@Inject
RecipeService recipeService;
private List<RecipeReference> loadRecipeList() { private List<RecipeReference> loadRecipeList() {
List<RecipeReference> list = List<RecipeReference> list =
userSession.getShoppingList().stream() userSession.getShoppingList().stream()
.map(r -> new RecipeReference(r)) .map(r -> new RecipeReference(fetchRecipe(r)))
.collect(Collectors.toList()); .collect(Collectors.toList());
return list; 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<ShopIngredient> getIngredientList() { public List<ShopIngredient> getIngredientList() {
return this.siList; return this.siList;
} }
@ -165,7 +188,7 @@ public class ShoppingListBean implements Serializable {
} }
boolean inPantry = false; boolean inPantry = false;
Pantry pantry = ing.getPantry(); Pantry pantry = ing.getPantry();
if ( (pantry != null) && pantry.getPantry()) { if ((pantry != null) && pantry.getPantry()) {
inPantry = true; inPantry = true;
} }
sing = new ShopIngredient(amt, ing.getUnit(), sing = new ShopIngredient(amt, ing.getUnit(),
@ -242,6 +265,28 @@ public class ShoppingListBean implements Serializable {
.createDownload(getIngredientList()); .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<String> shopcatList; private List<String> shopcatList;

View File

@ -13,10 +13,11 @@ public class Pantry implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id") @Column(name = "id")
private Long id; private Long id;
@Column(name = "ingkey", nullable=false) @Column(name = "ingkey", nullable = false)
private String ingkey; private String ingkey;
@Column(name = "pantry") @Column(name = "pantry")
@ -28,6 +29,11 @@ public class Pantry implements Serializable {
public Pantry() { public Pantry() {
} }
public Pantry(String ingKey, boolean checked) {
this.ingkey = ingKey;
this.pantry = checked;
}
public Long getId() { public Long getId() {
return this.id; return this.id;
} }

View File

@ -49,6 +49,10 @@ public class YamlShoppingList {
if (StringUtils.isBlank(newShopcat)) { if (StringUtils.isBlank(newShopcat)) {
newShopcat = "Unassigned"; newShopcat = "Unassigned";
} }
if ( ing.isInPantry()) {
// Don't put pantry items in list.
continue;
}
if (!StringUtils.equals(newShopcat, oldShopcat)) { if (!StringUtils.equals(newShopcat, oldShopcat)) {
wtr.println(newShopcat + ":"); wtr.println(newShopcat + ":");
oldShopcat = newShopcat; oldShopcat = newShopcat;

View File

@ -47,6 +47,12 @@
> >
<f:facet name="header"> <f:facet name="header">
<h:outputText value="Recipes" /> <h:outputText value="Recipes" />
<p:commandButton
update="@parent:tblRecipes"
value="Clear Recipes"
immediate="true"
action="#{shoppingListBean.clearRecipeList}"
/>
</f:facet> </f:facet>
<p:column style="width: 4em"> <p:column style="width: 4em">
<p:spinner required="true" min="0" <p:spinner required="true" min="0"
@ -113,11 +119,23 @@
<p:column label="Item" <p:column label="Item"
style="width: 20em" style="width: 20em"
> >
<h:outputText <h:outputText id="outItem"
value="#{item.item}" value="#{item.item}"
styleClass="#{(item.inPantry) ? 'noRecipe' :'plusRecipe' }" styleClass="#{(item.inPantry) ? 'noRecipe' :'plusRecipe' }"
/> />
</p:column> </p:column>
<p:column style="width: 2em">
<f:facet name="header">
<h:outputText value="Pantry" />
</f:facet>
<p:selectBooleanCheckbox
value="#{item.inPantry}"
>
<p:ajax update="outItem"
listener="#{shoppingListBean.updatePantry(item)}"
/>
</p:selectBooleanCheckbox>
</p:column>
</p:dataTable> </p:dataTable>
</p:column> </p:column>
</h:form> </h:form>
@ -131,6 +149,10 @@
/> />
</p:tab> </p:tab>
<!-- --> <!-- -->
<p:tab id="tabPantry" title="Pantry">
<h:outputText value="For future implementation" />
</p:tab>
<!-- -->
<p:tab id="tabImportExport" title="Import/Export"> <p:tab id="tabImportExport" title="Import/Export">
<h:outputText value="For future implementation" /> <h:outputText value="For future implementation" />
</p:tab> </p:tab>