From 735bf814cf50b1652f27e6a00d01a8bbce93dd3c Mon Sep 17 00:00:00 2001 From: Tim Holloway Date: Mon, 3 Jan 2022 09:06:42 -0500 Subject: [PATCH] shopcat, ENTER key for ingredients --- pom.xml | 33 ++-- .../gourmetj/IngredientDigester.java | 20 ++- .../mousetech/gourmetj/RecipeDetailBean.java | 41 +++-- .../gourmetj/SpringPrimeFacesApplication.java | 18 +-- .../persistence/dao/CategoryRepository.java | 2 +- .../persistence/dao/ShopcatRepository.java | 30 ++++ .../persistence/service/RecipeService.java | 7 +- .../META-INF/resources/detailEdit.xhtml | 143 ++++++++++-------- 8 files changed, 169 insertions(+), 125 deletions(-) create mode 100644 src/main/java/com/mousetech/gourmetj/persistence/dao/ShopcatRepository.java diff --git a/pom.xml b/pom.xml index 502bc34..cbfbfb3 100644 --- a/pom.xml +++ b/pom.xml @@ -82,31 +82,20 @@ spring-boot-starter-web - - javax.servlet.jsp - javax.servlet.jsp-api - 2.3.3 - provided - - - org.apache.tomcat.embed - tomcat-embed-jasper - provided - - - org.glassfish - javax.el - - - commons-fileupload - commons-fileupload - 1.3 + javax.servlet.jsp + javax.servlet.jsp-api + 2.3.3 + provided + + + org.apache.tomcat.embed + tomcat-embed-jasper + provided - commons-io - commons-io - 2.2 + org.glassfish + javax.el diff --git a/src/main/java/com/mousetech/gourmetj/IngredientDigester.java b/src/main/java/com/mousetech/gourmetj/IngredientDigester.java index a3ac20a..076029d 100644 --- a/src/main/java/com/mousetech/gourmetj/IngredientDigester.java +++ b/src/main/java/com/mousetech/gourmetj/IngredientDigester.java @@ -65,11 +65,11 @@ public class IngredientDigester { switch (unext.length) { case 1: ing.setUnit(null); - ing.setItem(unext[0]); + ing.setItem(nonoise(unext[0])); break; case 2: ing.setUnit(unext[0]); - ing.setItem(unext[1]); + ing.setItem(nonoise(unext[1])); break; } } @@ -84,6 +84,22 @@ public class IngredientDigester { return ing; } + /** + * Remove "noise" from ingredient title. For best results, + * should be adaptable for incoming language. + * + * @param string Item string with noise + * @return Item string with noise removed + */ + private static String nonoise(String string) { + String xstring = string; + if ( xstring.startsWith("of ")) { + xstring = xstring.substring(3); + } + // Todo: remote "optional" from string, if present. + return xstring; + } + /** * Break down ingredients line into 2 parts. First part is * numeric text representing amount and optional range, diff --git a/src/main/java/com/mousetech/gourmetj/RecipeDetailBean.java b/src/main/java/com/mousetech/gourmetj/RecipeDetailBean.java index b62a874..20d9b24 100644 --- a/src/main/java/com/mousetech/gourmetj/RecipeDetailBean.java +++ b/src/main/java/com/mousetech/gourmetj/RecipeDetailBean.java @@ -51,6 +51,9 @@ public class RecipeDetailBean implements Serializable { private static final Logger log = LoggerFactory.getLogger(RecipeDetailBean.class); + // Split lines at 2 or more spaces OR at line terminators. + private static final String RE_INGSPLIT = "\\s\\s+|\\r?+\\n"; + /** * Default Constructor. */ @@ -359,10 +362,15 @@ public class RecipeDetailBean implements Serializable { // ===== /** - * Handle entry of a single ingredient line into the input - * form. + * Handle entry of ingredient line(s) into text control on + * the input form. + * + * Note: In the original Tobago port of this app, the input + * was an inputText. In PrimeFaces, this did not preserve + * line separation characters, so an inputTextArea was + * used instead. * - * @param event Unused??? + * @param event Unused */ public void ajaxAddIngredient(AjaxBehaviorEvent event) { doAddIngredient(); @@ -442,7 +450,7 @@ public class RecipeDetailBean implements Serializable { } // Otherwise, try for split. - String[] lineArray = ingredientTextLines.split(" "); + String[] lineArray = ingredientTextLines.split(RE_INGSPLIT); for (String line : lineArray) { if (line.isBlank()) { continue; // actually should discard any above @@ -758,32 +766,21 @@ public class RecipeDetailBean implements Serializable { this.cuisineList = cuisineList; } - // *** - private String shopcatPartial; - - /** - * @return the shopcatPartial - */ - public String getShopcatPartial() { - return shopcatPartial; - } - - /** - * @param shopcatPartial the shopcatPartial to set - */ - public void setShopcatPartial(String shopcatPartial) { - this.shopcatPartial = shopcatPartial; - } - + //*** + // Shopcat for IngredientUI private List shopcatList; - public List getShopcatList() { + public List shopcatList(String query) { if (shopcatList == null) { shopcatList = recipeService.findShoppingCategories(); } return shopcatList; } + public void ajaxShopcat(AjaxBehaviorEvent event) { + log.warn("SHOPCAT "); + } + // *** public String editDescription() { diff --git a/src/main/java/com/mousetech/gourmetj/SpringPrimeFacesApplication.java b/src/main/java/com/mousetech/gourmetj/SpringPrimeFacesApplication.java index a14a572..484fb21 100644 --- a/src/main/java/com/mousetech/gourmetj/SpringPrimeFacesApplication.java +++ b/src/main/java/com/mousetech/gourmetj/SpringPrimeFacesApplication.java @@ -27,7 +27,7 @@ public class SpringPrimeFacesApplication { public void onStartup(ServletContext servletContext) throws ServletException { servletContext.setInitParameter( - "primefaces.THEME", "bluesky"); + "primefaces.THEME", "afternoon"); servletContext.setInitParameter( "javax.faces.FACELETS_SKIP_COMMENTS", "true"); @@ -39,20 +39,4 @@ public class SpringPrimeFacesApplication { } }; } - -// @Bean -// public FilterRegistrationBean FileUploadFilter() { -// FilterRegistrationBean registration = new FilterRegistrationBean(); -// registration.setFilter(new org.primefaces.webapp.filter.FileUploadFilter()); -// registration.setName("PrimeFaces FileUpload Filter"); -// return registration; -// } - -// @Bean -// public FilterRegistrationBean hiddenHttpMethodFilterDisabled( -// HiddenHttpMethodFilter filter) { -// FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(filter); -// filterRegistrationBean.setEnabled(false); -// return filterRegistrationBean; -// } } diff --git a/src/main/java/com/mousetech/gourmetj/persistence/dao/CategoryRepository.java b/src/main/java/com/mousetech/gourmetj/persistence/dao/CategoryRepository.java index f3bf97f..9fb103c 100644 --- a/src/main/java/com/mousetech/gourmetj/persistence/dao/CategoryRepository.java +++ b/src/main/java/com/mousetech/gourmetj/persistence/dao/CategoryRepository.java @@ -9,7 +9,7 @@ import org.springframework.stereotype.Repository; import com.mousetech.gourmetj.persistence.model.Category; /** - * JpaRepositort for Categories, which relate ManyToOne to Recipe. + * JpaRepository for Categories, which relate ManyToOne to Recipe. * Service method is @see CategoryService * * @author timh diff --git a/src/main/java/com/mousetech/gourmetj/persistence/dao/ShopcatRepository.java b/src/main/java/com/mousetech/gourmetj/persistence/dao/ShopcatRepository.java new file mode 100644 index 0000000..8ff64e8 --- /dev/null +++ b/src/main/java/com/mousetech/gourmetj/persistence/dao/ShopcatRepository.java @@ -0,0 +1,30 @@ +package com.mousetech.gourmetj.persistence.dao; + +import java.util.List; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.stereotype.Repository; + +import com.mousetech.gourmetj.persistence.model.Shopcat; + +/** + * JpaRepository for Shoppind Categories, which relate ManyToOne to Ingredient. + * Service method is @see RecipeService + * + * @author timh + * @since Dec 28, 2021 + */ + +@Repository +public interface ShopcatRepository + extends JpaRepository { + + final static String SQL_FIND_CATEGORIES = + "SELECT DISTINCT shopcategory from shopcats" + + " where shopcategory is not null and shopcategory <> ''" + + " ORDER BY shopcategory ASC"; + + @Query(value = SQL_FIND_CATEGORIES, nativeQuery = true) + public List findDistinctCategoryNative(); +} 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 0cda6d2..11ff51e 100644 --- a/src/main/java/com/mousetech/gourmetj/persistence/service/RecipeService.java +++ b/src/main/java/com/mousetech/gourmetj/persistence/service/RecipeService.java @@ -18,6 +18,7 @@ import org.springframework.transaction.annotation.Transactional; import com.mousetech.gourmetj.RecipeDetailBean; import com.mousetech.gourmetj.persistence.dao.CategoryRepository; 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; @@ -75,9 +76,11 @@ public class RecipeService implements Serializable { } + @Inject + ShopcatRepository shopcatRepository; + public List findShoppingCategories() { - // TODO Auto-generated method stub - return null; + return shopcatRepository.findDistinctCategoryNative(); } @Inject diff --git a/src/main/resources/META-INF/resources/detailEdit.xhtml b/src/main/resources/META-INF/resources/detailEdit.xhtml index e133832..52b6517 100644 --- a/src/main/resources/META-INF/resources/detailEdit.xhtml +++ b/src/main/resources/META-INF/resources/detailEdit.xhtml @@ -8,7 +8,7 @@ > Gourmet Recipe Manager -