KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > ce > auction > model > User


1 package org.hibernate.ce.auction.model;
2
3 import org.hibernate.ce.auction.exceptions.BusinessException;
4
5 import javax.persistence.*;
6 import java.io.Serializable JavaDoc;
7 import java.util.*;
8
9 /**
10  * A user of the CaveatEmptor auction application.
11  * <p>
12  * This class represents the user entity of CaveatEmptor business.
13  * The associations are: a <tt>Set</tt> of <tt>Item</tt>s the user
14  * is selling, a <tt>Set</tt> of <tt>Bid</tt>s the user has made,
15  * and an <tt>Address</tt> component. Also a <tt>Set</tt> of
16  * <tt>BuyNow</tt>s, that is, immediate buys made for an item.
17  * <p>
18  * The <tt>billingDetails</tt> are used to calculate and bill the
19  * user for his activities on our system. The <tt>username</tt>
20  * and <tt>password</tt> are used as login credentials. The
21  * <tt>ranking</tt> is a number that is increased by each successful
22  * transaction, but may also be manually increased (or decreased) by
23  * the system administrators.
24  * <p>
25  * <b>Persistence:</b>
26  * <p>
27  * A User is a versioned entity, with some special properties.
28  * One is the username, it is immutable and unique. The
29  * defaultBillingDetails property points to one of the
30  * BillingDetails in the collection of all BillingDetails.
31  *
32  * @author Christian Bauer <christian@hibernate.org>
33  */

34 @Entity(access = AccessType.FIELD)
35 @Table(name = "USERS")
36 public class User implements Serializable JavaDoc, Comparable JavaDoc {
37
38     @Id(generate = GeneratorType.AUTO)
39     @Column(name = "USER_ID")
40     private Long JavaDoc id = null;
41
42     @Version
43     private int version = 0;
44
45     @Column(length = 255, nullable = false)
46     private String JavaDoc firstname;
47
48     @Column(length = 255, nullable = false)
49     private String JavaDoc lastname;
50
51     @Column(length = 16, nullable = false, unique = true, updatable = false)
52     private String JavaDoc username;
53
54     @Column(name = "'PASSWORD'", length = 12, nullable = false)
55     private String JavaDoc password;
56
57     @Column(length = 255, nullable = false)
58     private String JavaDoc email;
59
60     @Column(nullable = false)
61     private int ranking = 0;
62
63     @Column(name = "IS_ADMIN", nullable = false)
64     private boolean admin = false;
65
66     @Column(nullable = false)
67     private Date created = new Date();
68
69     @Embedded
70     private Address address;
71
72     @OneToMany(mappedBy = "seller")
73     private Set<Item> items = new HashSet<Item>();
74
75     @OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
76     private Set<BillingDetails> billingDetails = new HashSet<BillingDetails>();
77
78     @ManyToOne(fetch = FetchType.LAZY)
79     @JoinColumn(name="DEFAULT_BILLINGDETAILS_ID", nullable = true)
80     private BillingDetails defaultBillingDetails;
81
82     /**
83      * No-arg constructor for JavaBean tools.
84      */

85     User() {}
86
87     /**
88      * Full constructor.
89      */

90     public User(String JavaDoc firstname, String JavaDoc lastname, String JavaDoc username,
91                 String JavaDoc password, String JavaDoc email,
92                 Address address, Set<Item> items,
93                 Set<BillingDetails> billingDetails) {
94         this.firstname = firstname;
95         this.lastname = lastname;
96         this.username = username;
97         this.password = password;
98         this.email = email;
99         this.address = address;
100         this.items = items;
101         this.billingDetails = billingDetails;
102     }
103
104     /**
105      * Simple constructor.
106      */

107     public User(String JavaDoc firstname, String JavaDoc lastname,
108                 String JavaDoc username, String JavaDoc password, String JavaDoc email) {
109         this.firstname = firstname;
110         this.lastname = lastname;
111         this.username = username;
112         this.password = password;
113         this.email = email;
114     }
115
116     // ********************** Accessor Methods ********************** //
117

118     public Long JavaDoc getId() { return id; }
119     public int getVersion() { return version; }
120
121     public String JavaDoc getFirstname() { return firstname; }
122     public void setFirstname(String JavaDoc firstname) { this.firstname = firstname; }
123
124     public String JavaDoc getLastname() { return lastname; }
125     public void setLastname(String JavaDoc lastname) { this.lastname = lastname; }
126
127     public String JavaDoc getUsername() { return username; }
128
129     public String JavaDoc getPassword() { return password; }
130     public void setPassword(String JavaDoc password) { this.password = password; }
131
132     public String JavaDoc getEmail() { return email; }
133     public void setEmail(String JavaDoc email) { this.email = email; }
134
135     public int getRanking() { return ranking; }
136     public void setRanking(int ranking) { this.ranking = ranking; }
137
138     public Address getAddress() { return address; }
139     public void setAddress(Address address) { this.address = address; }
140
141     public Set getItems() { return items; }
142     public void addItem(Item item) {
143         if (item == null)
144             throw new IllegalArgumentException JavaDoc("Can't add a null Item.");
145         this.getItems().add(item);
146     }
147
148     @org.hibernate.annotations.Cascade(value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
149     public Set getBillingDetails() { return billingDetails; }
150     /**
151       * Adds a <tt>BillingDetails</tt> to the set.
152       * <p>
153       * This method checks if there is only one billing method
154       * in the set, then makes this the default.
155       *
156       * @param billingDetails
157       */

158      public void addBillingDetails(BillingDetails billingDetails) {
159          if (billingDetails == null)
160              throw new IllegalArgumentException JavaDoc("Can't add a null BillingDetails.");
161          this.getBillingDetails().add(billingDetails);
162
163          if (getBillingDetails().size() == 1) {
164              setDefaultBillingDetails(billingDetails);
165          }
166     }
167     /**
168      * Removes a <tt>BillingDetails</tt> from the set.
169      * <p>
170      * This method checks if the removed is the default element,
171      * and will throw a BusinessException if there is more than one
172      * left to chose from. This might actually not be the best way
173      * to handle this situation.
174      *
175      * @param billingDetails
176      * @throws BusinessException
177      */

178     public void removeBillingDetails(BillingDetails billingDetails)
179         throws BusinessException {
180         if (billingDetails == null)
181             throw new IllegalArgumentException JavaDoc("Can't add a null BillingDetails.");
182
183         if (getBillingDetails().size() >= 2) {
184             getBillingDetails().remove(billingDetails);
185             setDefaultBillingDetails((BillingDetails)getBillingDetails().iterator().next());
186         } else {
187             throw new BusinessException("Please set new default BillingDetails first");
188         }
189     }
190
191     public BillingDetails getDefaultBillingDetails() { return defaultBillingDetails; }
192     public void setDefaultBillingDetails(BillingDetails defaultBillingDetails) {
193         this.defaultBillingDetails = defaultBillingDetails;
194     }
195
196     public Date getCreated() { return created; }
197
198     public boolean isAdmin() { return admin; }
199     public void setAdmin(boolean admin) { this.admin = admin; }
200
201     // ********************** Common Methods ********************** //
202

203     public boolean equals(Object JavaDoc o) {
204         if (this == o) return true;
205         if (!(o instanceof User)) return false;
206         final User user = (User) o;
207         if (!username.equals(user.username)) return false;
208         return true;
209     }
210
211     public int hashCode() {
212         return username.hashCode();
213     }
214
215     public String JavaDoc toString() {
216         return "User ('" + getId() + "'), " +
217                 "Username: '" + getUsername() + "'";
218     }
219
220     public int compareTo(Object JavaDoc o) {
221         if (o instanceof User)
222             return this.getCreated().compareTo( ((User)o).getCreated() );
223         return 0;
224     }
225
226     // ********************** Business Methods ********************** //
227

228     public void increaseRanking() {
229         setRanking(getRanking() + 1);
230     }
231
232 }
233
Popular Tags