diff --git a/src/main/java/com/mousetech/gourmetj/AdminMainBean.java b/src/main/java/com/mousetech/gourmetj/AdminMainBean.java index 4e46892..607095c 100644 --- a/src/main/java/com/mousetech/gourmetj/AdminMainBean.java +++ b/src/main/java/com/mousetech/gourmetj/AdminMainBean.java @@ -123,6 +123,7 @@ public class AdminMainBean implements Serializable { */ @PostConstruct void init() { + log.info("Constructing AdminMainBean " + this); this.setSearchText(userSession.getLastSearch()); // Clean up from any previous operations. this.userSession.setRecipe(null); @@ -191,6 +192,7 @@ public class AdminMainBean implements Serializable { /** * Get printable preptime. Database version is in seconds. + * @deprecated User {@link UserSession#formatTime(Long)} * * @return Formatted time. Called from EL on main page. */ @@ -210,17 +212,4 @@ public class AdminMainBean implements Serializable { return sb.toString(); } - public String formatCategories(Recipe r) { - StringBuffer sb = new StringBuffer(30); - boolean first = true; - for (Category cat : r.getCategories()) { - if (first) { - first = false; - } else { - sb.append(", "); - } - sb.append(cat.getCategory()); - } - return sb.toString(); - } } diff --git a/src/main/java/com/mousetech/gourmetj/RecipeDetailBean.java b/src/main/java/com/mousetech/gourmetj/RecipeDetailBean.java index 20d9b24..c2fc860 100644 --- a/src/main/java/com/mousetech/gourmetj/RecipeDetailBean.java +++ b/src/main/java/com/mousetech/gourmetj/RecipeDetailBean.java @@ -218,6 +218,8 @@ public class RecipeDetailBean implements Serializable { private void init() { this.recipe = userSession.getRecipe(); + + log.info("Using recipe: " + this.recipe ); /** * For "create new, this recipe is a blank constructed * and passed from main page. For Detail display, it's @@ -241,6 +243,7 @@ public class RecipeDetailBean implements Serializable { getIngredients().setWrappedData( buildIngredientFacade(recipe.getIngredientHash())); + log.info("Set recipe: " + this.recipe ); } /** @@ -367,8 +370,8 @@ public class RecipeDetailBean implements Serializable { * * 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. + * line separation characters, so an inputTextArea was used + * instead. * * @param event Unused */ @@ -450,7 +453,8 @@ public class RecipeDetailBean implements Serializable { } // Otherwise, try for split. - String[] lineArray = ingredientTextLines.split(RE_INGSPLIT); + String[] lineArray = + ingredientTextLines.split(RE_INGSPLIT); for (String line : lineArray) { if (line.isBlank()) { continue; // actually should discard any above @@ -538,16 +542,22 @@ public class RecipeDetailBean implements Serializable { * save ingredients. */ public String doSave() { + // if ! isDirty()... + // Update recipe object based on UI: + if (!saveIngredients()) { + JSFUtils + .addErrorMessage("Could not save Ingredients"); + return null; + } + + if (recipeService.save(this.getRecipe())) { + userSession.setRecipe(null); + return "main"; + } else { + JSFUtils.addErrorMessage("Save recipe failed"); return null; } - updateRecipeCategories(recipe, category); - // Rebuild ingredients list with groups applied - updateRecipeGroups(getWrappedIngredients()); - recipeService.save(recipe); - userSession.setRecipe(null); - setDirty(false); - return "main"; } /** @@ -587,6 +597,16 @@ public class RecipeDetailBean implements Serializable { * it. */ private boolean saveIngredients() { + if ( ! updateIngredientList()) { + return false; + } + updateRecipeCategories(recipe, category); + // Rebuild ingredients list with groups applied + updateRecipeGroups(getWrappedIngredients()); + return true; + } + + private boolean updateIngredientList() { List saveIng = getWrappedIngredients(); List iList = recipe.getIngredientHash(); iList.clear(); @@ -599,7 +619,7 @@ public class RecipeDetailBean implements Serializable { if (scat == null) { if ((ingKey != null) && !ingKey.isBlank()) { scat = new Shopcat(); - scat.setIngkey(ingKey); +// scat.setIngkey(ingKey); scat.setShopcategory(shopCatName); ing.setShopCat(scat); } else { @@ -766,7 +786,7 @@ public class RecipeDetailBean implements Serializable { this.cuisineList = cuisineList; } - //*** + // *** // Shopcat for IngredientUI private List shopcatList; @@ -780,7 +800,7 @@ public class RecipeDetailBean implements Serializable { public void ajaxShopcat(AjaxBehaviorEvent event) { log.warn("SHOPCAT "); } - + // *** public String editDescription() { @@ -889,11 +909,12 @@ public class RecipeDetailBean implements Serializable { * * @param event PrimeFaces file upload event object */ - public void ajaxUploadImage(FileUploadEvent event) { - UploadedFile foo = event.getFile(); - + public void ajaxUploadImage(FileUploadEvent event) { + UploadedFile foo = event.getFile(); + PictureController.importImage(recipe, foo.getContents()); - } + } + /** * Remove images from recipe * diff --git a/src/main/java/com/mousetech/gourmetj/UserSession.java b/src/main/java/com/mousetech/gourmetj/UserSession.java index f15e011..5fdc39b 100644 --- a/src/main/java/com/mousetech/gourmetj/UserSession.java +++ b/src/main/java/com/mousetech/gourmetj/UserSession.java @@ -5,6 +5,7 @@ import java.io.Serializable; import javax.enterprise.context.SessionScoped; import javax.inject.Named; +import com.mousetech.gourmetj.persistence.model.Category; import com.mousetech.gourmetj.persistence.model.Recipe; @Named @@ -97,6 +98,21 @@ public class UserSession implements Serializable { } // ==== + + public String formatCategories(Recipe r) { + StringBuffer sb = new StringBuffer(30); + boolean first = true; + for (Category cat : r.getCategories()) { + if (first) { + first = false; + } else { + sb.append(", "); + } + sb.append(cat.getCategory()); + } + return sb.toString(); + } + public String formatTime(Long ltime) { if (ltime == null) { return ""; 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 8ae92af..3dd338a 100644 --- a/src/main/java/com/mousetech/gourmetj/persistence/model/Ingredient.java +++ b/src/main/java/com/mousetech/gourmetj/persistence/model/Ingredient.java @@ -5,9 +5,10 @@ import javax.persistence.*; import javax.validation.constraints.NotNull; /** - * The persistent class for the "ingredients" database table. - * The display model is @see IngredientUI. Persistence - * is usually done along with the containing recipe via + * The persistent class for the "ingredients" database table. The + * display model is @see IngredientUI. Persistence is usually + * done along with the containing recipe via + * * @see RecipeService. */ @Entity @@ -16,9 +17,12 @@ import javax.validation.constraints.NotNull; public class Ingredient implements Serializable, IngredientIF { private static final long serialVersionUID = 1L; - @ManyToOne(fetch = FetchType.LAZY, cascade = { - CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH }, optional = true) - @JoinColumn(name = "ingkey",insertable = false, updatable = false, nullable = true) + @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")) Shopcat shopCat; /** 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 c9e05c7..e9e4725 100644 --- a/src/main/java/com/mousetech/gourmetj/persistence/model/Shopcat.java +++ b/src/main/java/com/mousetech/gourmetj/persistence/model/Shopcat.java @@ -1,8 +1,20 @@ package com.mousetech.gourmetj.persistence.model; import java.io.Serializable; -import javax.persistence.*; -import javax.validation.constraints.NotNull; +import java.util.ArrayList; +import java.util.List; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.ManyToOne; +import javax.persistence.NamedQueries; +import javax.persistence.NamedQuery; +import javax.persistence.OneToMany; +import javax.persistence.Table; /** * The persistent class for the "shopcats" database table. @@ -19,9 +31,6 @@ import javax.validation.constraints.NotNull; */ @Entity @Table(name = "shopcats") -@NamedQueries({ - @NamedQuery(name = "Shopcat.findAll", query = "SELECT s FROM Shopcat s"), - @NamedQuery(name = "Shopcat.findIngkey", query = "SELECT s FROM Shopcat s where s.ingkey = :ingkey") }) public class Shopcat implements Serializable { private static final long serialVersionUID = 1L; @@ -30,11 +39,23 @@ public class Shopcat implements Serializable { @Column(name = "id") private Long id; - @Column(name = "ingkey") - @NotNull - // @UniqueConstraint(name="ingkey") - private String ingkey; - // @OneToMany ingredients + //@Column(name = "ingkey", unique = true, nullable = false ) + @OneToMany(fetch = FetchType.LAZY, mappedBy = "ingkey" ) + private List ingredients; + + /** + * @return the ingredients + */ + public List getIngredients() { + return ingredients; + } + + /** + * @param ingredients the ingredients to set + */ + public void setIngredients(List ingredient) { + this.ingredients = ingredient; + } @Column(name = "position") private Integer position; @@ -53,14 +74,6 @@ public class Shopcat implements Serializable { this.id = id; } - public String getIngkey() { - return this.ingkey; - } - - public void setIngkey(String ingkey) { - this.ingkey = ingkey; - } - public Integer getPosition() { return this.position; } @@ -79,7 +92,7 @@ public class Shopcat implements Serializable { @Override public String toString() { - return "Shopcat for " + ingkey + "(" + shopcategory + return "Shopcat for " + "(" + shopcategory + ")"; } } 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 11ff51e..ed9dd19 100644 --- a/src/main/java/com/mousetech/gourmetj/persistence/service/RecipeService.java +++ b/src/main/java/com/mousetech/gourmetj/persistence/service/RecipeService.java @@ -66,9 +66,9 @@ public class RecipeService implements Serializable { return recipeRepository.FindCuisinesNative(); } - public void save(Recipe recipe) { - // TODO Auto-generated method stub - + public boolean save(Recipe recipe) { + recipeRepository.save(recipe); + return true; } public void delete(Recipe recipe) { diff --git a/src/main/java/com/mousetech/gourmetj/springweb/PictureController.java b/src/main/java/com/mousetech/gourmetj/springweb/PictureController.java index af5b29b..60c6c51 100644 --- a/src/main/java/com/mousetech/gourmetj/springweb/PictureController.java +++ b/src/main/java/com/mousetech/gourmetj/springweb/PictureController.java @@ -143,7 +143,12 @@ public class PictureController { httpHeaders.set(HttpHeaders.CONTENT_TYPE, MediaType.IMAGE_PNG_VALUE); httpHeaders.set(HttpHeaders.CACHE_CONTROL, "no-cache"); - byte[] image = editRecipe.getImage(); + byte[] image = null; + if ( editRecipe != null ) { + // Otherwise we are creating new recipe, pre-display + image = editRecipe.getImage(); + } + if (image == null) { image = BLANK_IMAGE; } diff --git a/src/main/resources/META-INF/resources/detailEdit.xhtml b/src/main/resources/META-INF/resources/detailEdit.xhtml index 52b6517..a23f222 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 -