Feed on
Posts
Comments
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
package org.ningning.eatsmart.entity;
 
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
 
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
 
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import org.hibernate.validator.Past;
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: User.java 141 2007-07-20 18:14:06Z ningning $
 */
@Entity
@Name("user")
@Scope(SESSION)
@Table(name = "users")
public class User implements Serializable {
  @Logger
  private static Log log;
 
  private static final long serialVersionUID = 3815749209935672737L;
 
  private Integer id;
 
  private String name;
 
  private String password;
 
  private Date birthday;
 
  private Character gender;
 
  private String role;
 
  private Collection<UserOpinion4Recipe> opinions4Recipe = new ArrayList<UserOpinion4Recipe>();
 
  private Collection<UserOpinion4Food> opinions4Food = new ArrayList<UserOpinion4Food>();
 
  private Collection<UserAllergy> allergies = new ArrayList<UserAllergy>();
 
  //private Set<Recipe> ratedRecipes;
  private Map<Recipe, UserOpinion4Recipe> recipeOpinionLookUpTable;
  //private Set<Food> ratedFoods;
  private Map<Food, UserOpinion4Food> foodOpinionLookUpTable;
  private Collection<Allergy> myAllergies;
 
  public User() {}
 
  @OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
  @Cascade(value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
  @LazyCollection(LazyCollectionOption.EXTRA)
  public Collection<UserAllergy> getAllergies() {
    return allergies;
  }
 
  public void setAllergies(Collection<UserAllergy> allergies) {
    this.allergies = allergies;
  }
 
  /**
   * @return
   */
  @Column(name = "birthday", nullable = false)
  @Past
  public Date getBirthday() {
    return birthday;
  }
 
  public void setBirthday(Date birthday) {
    this.birthday = birthday;
  }
 
  /**
   * @return
   */
  @Column(name = "gender", nullable = false)
  public Character getGender() {
    return gender;
  }
 
  public void setGender(Character gender) {
    this.gender = gender;
  }
 
  /**
   * @return
   */
  @Id
  @Column(name = "id")
  @GeneratedValue(strategy = GenerationType.AUTO)
  public Integer getId() {
    return id;
  }
 
  public void setId(Integer id) {
    this.id = id;
  }
 
  /**
   * @return the username of the user
   */
  @Column(name = "name", length = 30, unique = true, nullable = false)
  public String getName() {
    return name;
  }
 
  public void setName(String name) {
    this.name = name;
  }
 
  @OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
  @Cascade(value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
  @LazyCollection(LazyCollectionOption.EXTRA)
  public Collection<UserOpinion4Food> getOpinions4Food() {
    return opinions4Food;
  }
 
  public void setOpinions4Food(
      Collection<UserOpinion4Food> opinions4Food) {
    this.opinions4Food = opinions4Food;
  }
 
  /**
   * @return this user's opinion for recipes.
   */
  @OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
  @Cascade(value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
  @LazyCollection(LazyCollectionOption.EXTRA)
  public Collection<UserOpinion4Recipe> getOpinions4Recipe() {
    return opinions4Recipe;
  }
 
  public void setOpinions4Recipe(
      Collection<UserOpinion4Recipe> opinions4Recipe) {
    this.opinions4Recipe = opinions4Recipe;
  }
 
  /**
   * @return
   */
  @Column(name = "password", length = 30, nullable = false)
  public String getPassword() {
    return password;
  }
 
  public void setPassword(String password) {
    this.password = password;
  }
 
  /**
   * @return the role
   */
  @Column(name = "role", length = 30, nullable = false)
  public String getRole() {
    return role;
  }
 
  /**
   * @param role
   *          the role to set
   */
  public void setRole(String role) {
    this.role = role;
  }
 
  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    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 User)) return false;
    final User other = (User) obj;
    if (name == null) {
      if (other.getName() != null) return false;
    } else if (!name.equals(other.getName())) return false;
    return true;
  }
 
  // ---------------------- business logic methods ----------------------
 
  public Set<Recipe> findAllRatedRecipes() {
    if (this.recipeOpinionLookUpTable == null) {
      this.recipeOpinionLookUpTable = new HashMap<Recipe, UserOpinion4Recipe>();
      for (UserOpinion4Recipe uo4r : this.opinions4Recipe) {
        if (uo4r.getRate() != null) {
          this.recipeOpinionLookUpTable.put(uo4r.getRecipe(), uo4r);
        }
      }
    }
    return this.recipeOpinionLookUpTable.keySet();
  }
 
  public void addOrUpdateOpinion4Recipe(Recipe rcp, Short rate, String comment) {
    if (this.findAllRatedRecipes().contains(rcp)) {
      UserOpinion4Recipe uo4r = this.recipeOpinionLookUpTable.get(rcp);
      uo4r.setRate(rate);
      if (comment != null && comment.trim() != "")
        uo4r.setComment(comment);
    } else {
      UserOpinion4Recipe u = new UserOpinion4Recipe(this, rcp);
      u.setRate(rate);
      if (comment != null && comment.trim() != "")
        u.setComment(comment);
      this.recipeOpinionLookUpTable.put(rcp, u);
    }
  }
 
  public Set<Food> findAllRatedFoods() {
    if (this.foodOpinionLookUpTable == null){
      this.foodOpinionLookUpTable = new HashMap<Food, UserOpinion4Food>();
      for(UserOpinion4Food uo4f : this.opinions4Food){
        if (uo4f.getRate() != null) {
          this.foodOpinionLookUpTable.put(uo4f.getFood(), uo4f);
        }
      }
    }
    return this.foodOpinionLookUpTable.keySet();
  }
 
  public void addOrUpdateOpinion4Food(Food food, Short rate, String comment){
    if(this.findAllRatedFoods().contains(food)){
      UserOpinion4Food uo4f = this.foodOpinionLookUpTable.get(food);
      uo4f.setRate(rate);
      if(comment != null && comment.trim() != "") uo4f.setComment(comment);
    } else {
      UserOpinion4Food uf = new UserOpinion4Food(this, food);
      uf.setRate(rate);
      if (comment != null && comment.trim() != "")
        uf.setComment(comment);
      this.foodOpinionLookUpTable.put(food, uf);
    }
  }
 
  public Double doRecipeFondnessScore(Recipe recipe) {
    double score = 0.0;
 
    // if the user has rated this recipe, use her rate
    if (this.findAllRatedRecipes().contains(recipe)){
      score = this.recipeOpinionLookUpTable.get(recipe).getRate();
    } else {    
    // if the user has not rated this recipe, the fondness score will be based on her rate upon the ingredients
      for(Ingredient ingre : recipe.getIngredients()){
        Food food = ingre.getFood();
        Double weightPercent = ingre.toGramWeight() / recipe.getIngredientTotalWeight().doubleValue();
        if(this.findAllRatedFoods().contains(food)){
          score += this.foodOpinionLookUpTable.get(food).getRate() * weightPercent;
        } else {
          score += 5.0 * weightPercent;
        }
      }      
    }    
    return score;    
  }
}

One Response to “Entity Centred Application Design – User.java version 1”

Leave a Reply