From 75a8487cfb2d6a403e1b0d7a1d4391e74dda88ea Mon Sep 17 00:00:00 2001 From: Tim Holloway Date: Fri, 14 Jan 2022 17:27:26 -0500 Subject: [PATCH] Tab for editing shopping categories completed. --- .../mousetech/gourmetj/ShoppingListBean.java | 134 ++++++++++-- .../persistence/dao/ShopcatRepository.java | 21 +- .../resources/WEB-INF/layout/layout.xhtml | 14 +- .../WEB-INF/layout/misctabs/ingshopkey.xhtml | 199 ++++++++---------- .../META-INF/resources/css/style.css | 6 + .../META-INF/resources/shoppingList.xhtml | 118 +++++++---- 6 files changed, 305 insertions(+), 187 deletions(-) diff --git a/src/main/java/com/mousetech/gourmetj/ShoppingListBean.java b/src/main/java/com/mousetech/gourmetj/ShoppingListBean.java index fc57512..0d73dc3 100644 --- a/src/main/java/com/mousetech/gourmetj/ShoppingListBean.java +++ b/src/main/java/com/mousetech/gourmetj/ShoppingListBean.java @@ -12,7 +12,6 @@ import javax.inject.Inject; import javax.inject.Named; import org.apache.commons.lang3.StringUtils; -import org.primefaces.event.ReorderEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -151,9 +150,9 @@ public class ShoppingListBean implements Serializable { } } - private List shopcatList; + private List shopcatList; - public List getShopcatList() { + public List getShopcatList() { if (shopcatList == null) { shopcatList = loadShopcatList(); } @@ -163,9 +162,9 @@ public class ShoppingListBean implements Serializable { @Inject ShopcatRepository shopcatRepository; - private List loadShopcatList() { - return shopcatRepository - .findAllByOrderByShopcategoryAsc(); + private List loadShopcatList() { + return shopcatRepository.findDistinctCategoryNative(); +// .findAllByOrderByShopcategoryAsc(); } private Shopcat xeditShopcat = new Shopcat(); @@ -180,19 +179,19 @@ public class ShoppingListBean implements Serializable { } public void doEditShopcat(int scId) { - xeditShopcat = null; - final List scl = getShopcatList(); - for (Shopcat sc : scl) { - if (sc.getId() == scId) { - xeditShopcat = sc; - this.oldShopcategoryName = sc.getShopcategory(); - if (sc.getPosition() == null) { - sc.setPosition(0); - } - return; - } - } - log.error("SHOPCAT " + scId + " NOT FOUND"); +// xeditShopcat = null; +// final List scl = getShopcatList(); +// for (Shopcat sc : scl) { +// if (sc.getId() == scId) { +// xeditShopcat = sc; +// this.oldShopcategoryName = sc.getShopcategory(); +// if (sc.getPosition() == null) { +// sc.setPosition(0); +// } +// return; +// } +// } +// log.error("SHOPCAT " + scId + " NOT FOUND"); } public void ajaxOnClickShopcatIngkey() { @@ -202,12 +201,103 @@ public class ShoppingListBean implements Serializable { } /** - * Updates all ingredient keys for shopcat name-change. - * Note that once done, this cannot be undone! + * Updates all ingredient keys for shopcat name-change. Note + * that once done, this cannot be undone! */ public void ajaxOnClickShopcat() { this.shopcatRepository.UpdateShopcats( - this.oldShopcategoryName, xeditShopcat.getShopcategory()); + this.oldShopcategoryName, + xeditShopcat.getShopcategory()); this.shopcatList = null; } + + // === + private String selectedShopcat; + + /** + * @return the selectedShopcat + */ + public String getSelectedShopcat() { + return selectedShopcat; + } + + /** + * @param selectedShopcat the selectedShopcat to set + */ + public void setSelectedShopcat(String selectedShopcat) { + this.selectedShopcat = selectedShopcat; + this.ingkeyList = null; + } + + private List selectedIngkey; + + /** + * @return the selectedIngkey + */ + public List getSelectedIngkey() { + return selectedIngkey; + } + + /** + * @param selectedIngkey the selectedIngkey to set + */ + public void setSelectedIngkey(List selectedIngkey) { + this.selectedIngkey = selectedIngkey; + } + + private List ingkeyList; + + /** + * @return the ingkeyList + */ + public List getIngkeyList() { + if (ingkeyList == null) { + ingkeyList = loadIngkeyListFor(selectedShopcat); + } + return ingkeyList; + } + + private List loadIngkeyListFor( + String selectedShopcat2) { + List list = this.shopcatRepository + .findByIngkeySorted(selectedShopcat2); + return list; + } + + private String newShopcat; + + /** + * @return the newShopcat + */ + public String getNewShopcat() { + return newShopcat; + } + + /** + * @param newShopcat the newShopcat to set + */ + public void setNewShopcat(String newShopcat) { + this.newShopcat = newShopcat; + } + + public List suggestShopcat(String query) { + return this.shopcatList; + } + + public void doChangeShopcat() { + String oldCat = this.getSelectedShopcat(); + String newCat = this.getNewShopcat(); + if (oldCat.equals(newCat)) { + return; // effective NO-OP + } + newCat = newCat.trim(); + if ( StringUtils.isBlank(newCat)) { + this.shopcatRepository + .deleteShopcatFor(this.getSelectedIngkey()); + } else { + this.shopcatRepository + .updateShopcatFor(newCat, this.getSelectedIngkey()); + } + } + } 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 15c7886..576a784 100644 --- a/src/main/java/com/mousetech/gourmetj/persistence/dao/ShopcatRepository.java +++ b/src/main/java/com/mousetech/gourmetj/persistence/dao/ShopcatRepository.java @@ -12,8 +12,8 @@ import org.springframework.stereotype.Repository; import com.mousetech.gourmetj.persistence.model.Shopcat; /** - * JpaRepository for Shopping Categories, which relate ManyToOne to Ingredient. - * Service method is @see RecipeService + * JpaRepository for Shopping Categories, which relate ManyToOne + * to Ingredient. Service method is @see RecipeService * * @author timh * @since Dec 28, 2021 @@ -34,11 +34,24 @@ public interface ShopcatRepository @Transactional @Query(value = "UPDATE Shopcat set shopcategory = :newCatname WHERE shopcategory = :oldCatname") @Modifying - public int UpdateShopcats(String oldCatname, String newCatname); - + public int UpdateShopcats(String oldCatname, + String newCatname); + public Shopcat findShopcatByIngkey(String ingkey); public void deleteByIngkey(String key); public List findAllByOrderByShopcategoryAsc(); + + @Query(value = "SELECT s.ingkey FROM Shopcat s WHERE s.shopcategory = :shopcat ORDER BY s.ingkey") + public List findByIngkeySorted(String shopcat); + + @Transactional + @Query(value = "UPDATE Shopcat set shopcategory = :newCat WHERE ingkey IN :selectedIngkey") + @Modifying + public void updateShopcatFor(String newCat, List selectedIngkey); + + @Transactional + @Query(value = "DELETE Shopcat WHERE ingkey IN :selectedIngkey") + @Modifying public void deleteShopcatFor(List selectedIngkey); } diff --git a/src/main/resources/META-INF/resources/WEB-INF/layout/layout.xhtml b/src/main/resources/META-INF/resources/WEB-INF/layout/layout.xhtml index fc588df..800c7d5 100644 --- a/src/main/resources/META-INF/resources/WEB-INF/layout/layout.xhtml +++ b/src/main/resources/META-INF/resources/WEB-INF/layout/layout.xhtml @@ -25,11 +25,15 @@ - - (C) 2021 Tim Holloway, Licensed under the Apache License, Version 2.0. -

Based on Gourmet Recipe Manager by T. Hinkle

+ + + - - - - - - - - - - - - - - - + + + + - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/META-INF/resources/css/style.css b/src/main/resources/META-INF/resources/css/style.css index 52fdb11..af4381a 100644 --- a/src/main/resources/META-INF/resources/css/style.css +++ b/src/main/resources/META-INF/resources/css/style.css @@ -21,3 +21,9 @@ textarea { font-family: 'latoregular', 'Trebuchet MS,Arial,Helvetica,sans-serif'; font-size: 1em } + +#footer { + position: absolute; + bottom: 90px; + width: 100%; +} \ No newline at end of file diff --git a/src/main/resources/META-INF/resources/shoppingList.xhtml b/src/main/resources/META-INF/resources/shoppingList.xhtml index 7be6e37..5cdc360 100644 --- a/src/main/resources/META-INF/resources/shoppingList.xhtml +++ b/src/main/resources/META-INF/resources/shoppingList.xhtml @@ -25,57 +25,87 @@ } - - - - - - - - - - - - - - - + + + + + + + + + - - - - - + + - - - - - - - + + + + + + + + + + + + + + + + + + + + + + - - + + + + + + + + - - \ No newline at end of file