From 9a88b81b7a63192e137869538e4ae79b4a32c9c6 Mon Sep 17 00:00:00 2001 From: Tim Holloway Date: Tue, 4 Jan 2022 10:17:43 -0500 Subject: [PATCH] Midway ingkey/shopcat --- .../com/mousetech/gourmetj/IngredientUI.java | 18 +-- .../mousetech/gourmetj/RecipeDetailBean.java | 108 ++++++++++-------- .../persistence/dao/ShopcatRepository.java | 4 +- .../persistence/model/Ingredient.java | 6 +- .../gourmetj/persistence/model/Shopcat.java | 19 ++- .../persistence/service/RecipeService.java | 40 ++++--- .../META-INF/resources/detailEdit.xhtml | 24 ++-- 7 files changed, 126 insertions(+), 93 deletions(-) diff --git a/src/main/java/com/mousetech/gourmetj/IngredientUI.java b/src/main/java/com/mousetech/gourmetj/IngredientUI.java index c2e881e..99bd3bf 100644 --- a/src/main/java/com/mousetech/gourmetj/IngredientUI.java +++ b/src/main/java/com/mousetech/gourmetj/IngredientUI.java @@ -3,6 +3,10 @@ package com.mousetech.gourmetj; import com.mousetech.gourmetj.persistence.model.Ingredient; import com.mousetech.gourmetj.persistence.model.IngredientIF; import com.mousetech.gourmetj.persistence.model.Recipe; +import com.mousetech.gourmetj.persistence.model.Shopcat; + +import net.bytebuddy.asm.Advice.This; + import com.mousetech.gourmetj.IngredientDigester; import com.mousetech.gourmetj.IngredientDigester.IngredientAmountFormat; @@ -355,22 +359,22 @@ public class IngredientUI implements IngredientIF { ingredient.setUnit(unit); } - // This goes to the shopCats table via ManyToOne at save - // time. - private String shopCat; - /** * @return the shopCat */ public String getShopCat() { - return shopCat; + Shopcat scat = this.ingredient.getShopCat(); + if ( scat == null ) { + return null; + } + return scat.getShopcategory(); } /** * @param shopCat the shopCat to set */ - public void setShopCat(String shopCat) { - this.shopCat = shopCat; + public void setShopCat(Shopcat shopCat) { + this.ingredient.setShopCat(shopCat); } /** diff --git a/src/main/java/com/mousetech/gourmetj/RecipeDetailBean.java b/src/main/java/com/mousetech/gourmetj/RecipeDetailBean.java index c2fc860..559f76d 100644 --- a/src/main/java/com/mousetech/gourmetj/RecipeDetailBean.java +++ b/src/main/java/com/mousetech/gourmetj/RecipeDetailBean.java @@ -62,7 +62,7 @@ public class RecipeDetailBean implements Serializable { } /** - * Persistency service for Recipes + * Persistency service for Recipes, Shopcats and Categories */ @Inject @@ -257,7 +257,7 @@ public class RecipeDetailBean implements Serializable { if (recipe == null) { return null; } - + Set cList = recipe.getCategories(); StringBuffer sb = new StringBuffer(35); boolean first = true; @@ -308,7 +308,7 @@ public class RecipeDetailBean implements Serializable { // Shopcat is an eager fetch on Ingredient Shopcat shopCat = ing.getShopCat(); if (shopCat != null) { - ingUi.setShopCat(shopCat.getShopcategory()); + ingUi.setShopCat(shopCat); } } return list; @@ -510,11 +510,13 @@ public class RecipeDetailBean implements Serializable { } public boolean isMoveUpAble() { - if (isSelectionActive()) { - List rows = getWrappedIngredients(); - return !rows.get(0).isSelected(); - } - return false; + return true; + // TODO: +// if (isSelectionActive()) { +// List rows = getWrappedIngredients(); +// return !rows.get(0).isSelected(); +// } +// return false; } public boolean isMoveDownAble() { @@ -572,24 +574,6 @@ public class RecipeDetailBean implements Serializable { return "main"; } - /** - * Apply ingredient group IDs (optional) to individual - * ingredients. - * - * @param wrappedIngredients The wrapped ingredient facade. - */ - private void updateRecipeGroups( - List wrappedIngredients) { - String ingGroup = null; - for (IngredientUI ingUI : wrappedIngredients) { - if (ingUI.isIngGroup()) { - ingGroup = ingUI.getItem(); - } else { - ingUI.getIngredient().setInggroup(ingGroup); - } - } - } - /** * Update ingredients and shopcat from UI model * @@ -612,34 +596,58 @@ public class RecipeDetailBean implements Serializable { iList.clear(); for (IngredientUI iui : saveIng) { Ingredient ing = iui.getIngredient(); - ing.setRecipe(recipe); - String ingKey = iui.getIngkey(); - String shopCatName = iui.getShopCat(); - Shopcat scat = ing.getShopCat(); - if (scat == null) { - if ((ingKey != null) && !ingKey.isBlank()) { - scat = new Shopcat(); -// scat.setIngkey(ingKey); - scat.setShopcategory(shopCatName); - ing.setShopCat(scat); - } else { - JSFUtils.addErrorMessage( - "Shopping Category requires an Ingredient Key"); - return false; - } - } else { - if ((ingKey == null) || ingKey.isBlank()) { - ing.setShopCat(null); - } else { - ing.getShopCat() - .setShopcategory(shopCatName); - } + ing.setRecipe(recipe); + if ( ! updateShopcat(ing) ) { + return false; } iList.add(ing); } return true; } + /** + * Apply ingredient group IDs (optional) to individual + * ingredients. + * + * @param wrappedIngredients The wrapped ingredient facade. + */ + private void updateRecipeGroups( + List wrappedIngredients) { + String ingGroup = null; + for (IngredientUI ingUI : wrappedIngredients) { + if (ingUI.isIngGroup()) { + ingGroup = ingUI.getItem(); + } else { + ingUI.getIngredient().setInggroup(ingGroup); + } + } + } + + + /** + * Update shopcat for Ingredient. + * + * @param ing Ingredient to update + * @return true if update succeeded. + */ + private boolean updateShopcat(Ingredient ing) { + final String ingKey = ing.getIngkey(); + if ( (ingKey == null) || (ingKey.isBlank())) { + ing.setIngkey(null); + ing.setShopCat(null); + return true; + } + Shopcat scat = recipeService.findShopcatForIngredientKey(ingKey); + if ( scat == null ) { + JSFUtils.addErrorMessage( + "No Shopping Category is defined for Ingredient Key "+ ingKey); + return false; + + } + ing.setShopCat(scat); + return true; + } + /** * Parse out the comma-separated category text control and * post the results as children of the recipe @@ -797,8 +805,10 @@ public class RecipeDetailBean implements Serializable { return shopcatList; } - public void ajaxShopcat(AjaxBehaviorEvent event) { + public void ajaxUpdateShopcat(AjaxBehaviorEvent event) { log.warn("SHOPCAT "); +// Shopcat scat = recipeService.findShopcatForIngredientKey(ssss); +// ing.setShopcat(scat); } // *** diff --git a/src/main/java/com/mousetech/gourmetj/persistence/dao/ShopcatRepository.java b/src/main/java/com/mousetech/gourmetj/persistence/dao/ShopcatRepository.java index 8ff64e8..40c3c85 100644 --- a/src/main/java/com/mousetech/gourmetj/persistence/dao/ShopcatRepository.java +++ b/src/main/java/com/mousetech/gourmetj/persistence/dao/ShopcatRepository.java @@ -9,7 +9,7 @@ import org.springframework.stereotype.Repository; import com.mousetech.gourmetj.persistence.model.Shopcat; /** - * JpaRepository for Shoppind Categories, which relate ManyToOne to Ingredient. + * JpaRepository for Shopping Categories, which relate ManyToOne to Ingredient. * Service method is @see RecipeService * * @author timh @@ -27,4 +27,6 @@ public interface ShopcatRepository @Query(value = SQL_FIND_CATEGORIES, nativeQuery = true) public List findDistinctCategoryNative(); + + public Shopcat findShopcatByIngkey(String ingkey); } diff --git a/src/main/java/com/mousetech/gourmetj/persistence/model/Ingredient.java b/src/main/java/com/mousetech/gourmetj/persistence/model/Ingredient.java index 3dd338a..0c5aecb 100644 --- a/src/main/java/com/mousetech/gourmetj/persistence/model/Ingredient.java +++ b/src/main/java/com/mousetech/gourmetj/persistence/model/Ingredient.java @@ -20,9 +20,7 @@ public class Ingredient implements Serializable, IngredientIF { @ManyToOne(fetch = FetchType.EAGER, cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH }, optional = true) - @JoinColumn(name = "ingkey", insertable = false, - updatable = false, nullable = true, - foreignKey = @ForeignKey(name = "ingkey")) + @JoinColumn(name = "ingkey", referencedColumnName = "ingkey") Shopcat shopCat; /** @@ -53,7 +51,7 @@ public class Ingredient implements Serializable, IngredientIF { @Column(name = "inggroup") private String inggroup; - @Column(name = "ingkey") + @Column(name = "ingkey",insertable = false, updatable = false) private String ingkey; @Column(name = "item") diff --git a/src/main/java/com/mousetech/gourmetj/persistence/model/Shopcat.java b/src/main/java/com/mousetech/gourmetj/persistence/model/Shopcat.java index e9e4725..d8a4709 100644 --- a/src/main/java/com/mousetech/gourmetj/persistence/model/Shopcat.java +++ b/src/main/java/com/mousetech/gourmetj/persistence/model/Shopcat.java @@ -39,7 +39,10 @@ public class Shopcat implements Serializable { @Column(name = "id") private Long id; - //@Column(name = "ingkey", unique = true, nullable = false ) + @Column(name = "ingkey", unique = true, nullable = false ) + private String ingkey; + + @OneToMany(fetch = FetchType.LAZY, mappedBy = "ingkey" ) private List ingredients; @@ -73,7 +76,21 @@ public class Shopcat implements Serializable { public void setId(Long id) { this.id = id; } + + /** + * @return the ingkey + */ + public String getIngkey() { + return ingkey; + } + /** + * @param ingkey the ingkey to set + */ + public void setIngkey(String ingkey) { + this.ingkey = ingkey; + } + public Integer getPosition() { return this.position; } diff --git a/src/main/java/com/mousetech/gourmetj/persistence/service/RecipeService.java b/src/main/java/com/mousetech/gourmetj/persistence/service/RecipeService.java index ed9dd19..c2f7529 100644 --- a/src/main/java/com/mousetech/gourmetj/persistence/service/RecipeService.java +++ b/src/main/java/com/mousetech/gourmetj/persistence/service/RecipeService.java @@ -7,19 +7,15 @@ import java.util.Optional; import javax.enterprise.context.ApplicationScoped; import javax.inject.Inject; import javax.inject.Named; -import javax.persistence.NamedEntityGraph; -import javax.persistence.NamedNativeQuery; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.data.jpa.repository.EntityGraph; import org.springframework.transaction.annotation.Transactional; -import com.mousetech.gourmetj.RecipeDetailBean; import com.mousetech.gourmetj.persistence.dao.CategoryRepository; +import com.mousetech.gourmetj.persistence.dao.IngredientRepository; import com.mousetech.gourmetj.persistence.dao.RecipeRepository; import com.mousetech.gourmetj.persistence.dao.ShopcatRepository; -import com.mousetech.gourmetj.persistence.model.Category; import com.mousetech.gourmetj.persistence.model.Ingredient; import com.mousetech.gourmetj.persistence.model.Recipe; import com.mousetech.gourmetj.persistence.model.Shopcat; @@ -27,6 +23,17 @@ import com.mousetech.gourmetj.persistence.model.Shopcat; @Named @ApplicationScoped @Transactional +/** + * Persistence service class for Recipes. + * + * While normally, I would have separate service classes for + * different Entity Working Sets, Recipes are so central to this + * application, this class contains additional functions that + * would ordinarily have their own Service classes. + * + * @author timh + * @since Jan 4, 2022 + */ public class RecipeService implements Serializable { private static final long serialVersionUID = 1L; @@ -54,14 +61,6 @@ public class RecipeService implements Serializable { return recipeRepository.findDetailsById(recipeId); } - /** - * final String sql = "SELECT DISTINCT cuisine from recipe" + - * " where cuisine is not null and cuisine <> ''" + " ORDER - * BY cuisine ASC"; - * - * @return - */ - public List findCuisines() { return recipeRepository.FindCuisinesNative(); } @@ -72,10 +71,13 @@ public class RecipeService implements Serializable { } public void delete(Recipe recipe) { - // TODO Auto-generated method stub - + recipeRepository.delete(recipe); } + /** + * ShopcatService as a sub-function of RecipeService + */ + @Inject ShopcatRepository shopcatRepository; @@ -83,6 +85,14 @@ public class RecipeService implements Serializable { return shopcatRepository.findDistinctCategoryNative(); } + public Shopcat findShopcatForIngredientKey(String ingkey) { + return shopcatRepository.findShopcatByIngkey(ingkey); + } + + /** + * CategoryService as a sub-function of RecipeService + */ + @Inject CategoryRepository categoryRepository; diff --git a/src/main/resources/META-INF/resources/detailEdit.xhtml b/src/main/resources/META-INF/resources/detailEdit.xhtml index a23f222..59d09d3 100644 --- a/src/main/resources/META-INF/resources/detailEdit.xhtml +++ b/src/main/resources/META-INF/resources/detailEdit.xhtml @@ -135,6 +135,7 @@ Ingredients - + Shop. Cat. - - - + />