Entity Centred Application Design - Recipe.java version 1
Jul 20th, 2007 by ellen
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 | /** * Created on 28 May 2007, 00:01:23 Copyright Ning Zhao. All rights * reserved. */ package org.ningning.eatsmart.entity; import java.io.Serializable; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Collection; import java.util.List; import javax.persistence.CascadeType; 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.OneToMany; import javax.persistence.OrderBy; import javax.persistence.Table; import org.hibernate.annotations.Cascade; import org.hibernate.annotations.LazyCollection; import org.hibernate.annotations.LazyCollectionOption; import org.jboss.seam.annotations.Logger; import org.jboss.seam.annotations.Name; import org.jboss.seam.annotations.Scope; import org.jboss.seam.log.Log; import static org.jboss.seam.ScopeType.*; /** * @author ningning * @version $Id: Recipe.java 141 2007-07-20 18:14:06Z ningning $ */ @Entity @Name("recipe") @Scope(SESSION) @Table(name = "recipes") public class Recipe implements Serializable { private static final long serialVersionUID = -1482691873171646032L; public static final Character CREATING = 'a'; public static final Character CREATED = 'b'; public static final Character UPDATING = 'c'; public static final Character UPDATED = 'd'; private Integer id; private String name; private String descEn; private Short cookTime; // in minute private Short prepTime; // in minute private Double heatPower; // in watt private BigDecimal ingredientTotalWeight; // in gram private Character status; /** * Bags have the most efficient performance characteristics of all the * collections we can use for a bidirectional one-to-many entity * assionciation. Because a bag does not have to maintain the index of * its elements (like a list) or check for duplicate elements (like a * set), new elements can be added to the bag without trigering the * loading. This is an important feature if the collection of entity * references is large. To map a bidirectional one-to-many association * as a bag, we must use the Collection interface and the ArrayList * implementation. */ private Collection<Ingredient> ingredients = new ArrayList<Ingredient>(); private Collection<RecipeNutrient> nutrients = new ArrayList<RecipeNutrient>(); private List<CookStep> cookSteps = new ArrayList<CookStep>(); private Collection<RecipeNote> notes = new ArrayList<RecipeNote>(); private Collection<RecipeXTag> tags = new ArrayList<RecipeXTag>(); private Collection<UserOpinion4Recipe> userOpinions = new ArrayList<UserOpinion4Recipe>(); @Logger private Log log; public Recipe() {} @OneToMany(mappedBy = "recipe", cascade = CascadeType.ALL, fetch = FetchType.LAZY) @Cascade(value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN) @OrderBy("seq") public List<CookStep> getCookSteps() { return cookSteps; } public void setCookSteps(List<CookStep> cookSteps) { this.cookSteps = cookSteps; } public void addACookStep(CookStep cookStep) { cookStep.setRecipe(this); this.cookSteps.add(cookStep); } /** * @return the cooking time in minutes */ @Column(name = "cook_time", nullable = true) public Short getCookTime() { return cookTime; } public void setCookTime(Short cookTime) { this.cookTime = cookTime; } /** * @return The detailed English description of the recipe. */ @Column(name = "desc_en", nullable = true, length = 2047) public String getDescEn() { return descEn; } /** * @param descEn Should not be longer than 2047 bytes. */ public void setDescEn(String desc) { this.descEn = desc; } /** * @return the heatPower */ @Column(name = "heat_power", nullable = true) public Double getHeatPower() { return heatPower; } /** * @param heatPower the heatPower to set */ public void setHeatPower(Double heatPower) { this.heatPower = heatPower; } /** * @return */ @Id @Column(name = "id") @GeneratedValue(strategy = GenerationType.AUTO) public Integer getId() { return id; } /** * should not be called. The id of recipe is automatically generated * by the underlying DB. * * @param id */ public void setId(Integer id) { this.id = id; } /** * @return the set of ingredients of the recipe */ @OneToMany(mappedBy = "recipe", cascade = CascadeType.ALL) @Cascade(value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN) public Collection<Ingredient> getIngredients() { return ingredients; } public void setIngredients(Collection<Ingredient> ingredients) { this.ingredients = ingredients; } /** * @return the ingredientTotalWeight */ @Column(name = "ingredient_total_weight", scale = 2, precision = 7, nullable = false) public BigDecimal getIngredientTotalWeight() { return ingredientTotalWeight; } /** * @param ingredientTotalWeight * the ingredientTotalWeight to set */ public void setIngredientTotalWeight(BigDecimal newWeight) { this.ingredientTotalWeight = newWeight; } /** * @return */ @Column(name = "name_en", length = 255, nullable = false) public String getName() { return name; } public void setName(String name) { this.name = name; } @OneToMany(mappedBy = "recipe", cascade = CascadeType.ALL, fetch = FetchType.LAZY) @Cascade(value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN) public Collection<RecipeNote> getNotes() { return notes; } public void setNotes(Collection<RecipeNote> notes) { this.notes = notes; } public void addANote(RecipeNote recipeNote) { recipeNote.setRecipe(this); this.getNotes().add(recipeNote); } /** * @return the nutrientNormalizeFactor */ public Double normalizedNutrientFactor() { return (this.ingredientTotalWeight.doubleValue() / 100); } @OneToMany(mappedBy = "recipe", cascade = CascadeType.ALL, fetch = FetchType.LAZY) @Cascade(value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN) public Collection<RecipeNutrient> getNutrients() { return nutrients; } public void setNutrients(Collection<RecipeNutrient> nutrients) { this.nutrients = nutrients; } /** * @return time for preparation for the recipe in minutes */ @Column(name = "prep_time", nullable = true) public Short getPrepTime() { return prepTime; } public void setPrepTime(Short prepTime) { this.prepTime = prepTime; } /** * @return the status of this recipe */ @Column(name = "status", nullable = false) public Character getStatus() { return status; } /** * @param status * the status to set */ public void setStatus(Character status) { this.status = status; } @OneToMany(mappedBy = "recipe", cascade = CascadeType.ALL, fetch = FetchType.LAZY) @Cascade(value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN) public Collection<RecipeXTag> getTags() { return tags; } public void setTags(Collection<RecipeXTag> tags) { this.tags = tags; } /** * @return users' opinions for this recipe. */ @OneToMany(mappedBy = "recipe", cascade = CascadeType.ALL) @Cascade(value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN) public Collection<UserOpinion4Recipe> getUserOpinions() { return userOpinions; } public void setUserOpinions( Collection<UserOpinion4Recipe> userOpinions) { this.userOpinions = userOpinions; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((descEn == null) ? 0 : descEn.hashCode()); result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (!(obj instanceof Recipe)) return false; final Recipe other = (Recipe) obj; if (descEn == null) { if (other.getDescEn() != null) return false; } else if (!descEn.equals(other.getDescEn())) return false; if (name == null) { if (other.getName() != null) return false; } else if (!name.equals(other.getName())) return false; return true; } // ----------------------business logic methods----------------------- public Double doPrice(Double wantedWeight) { double price = 0.0d; for (Ingredient ingre : this.ingredients) { double weightPercent = ingre.toGramWeight() / this.getIngredientTotalWeight().doubleValue(); double actualAmount = wantedWeight * weightPercent; double delta = ingre.getFood().getPricePer100g() * (actualAmount / 100); price = price + delta; } return price; } public Double doTotalTime(Double wantedWeight, Double heatPower) { double time = this.prepTime + this.cookTime; double weightScale = wantedWeight / this.ingredientTotalWeight.doubleValue(); double heatScale = heatPower / this.heatPower; time = time * weightScale / heatScale; return time; } } |
