KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.hibernate.ce.auction.model;
2
3 import org.hibernate.ce.auction.exceptions.*;
4
5 import javax.persistence.*;
6 import java.io.Serializable JavaDoc;
7 import java.util.*;
8
9 /**
10  * An item for sale.
11  *
12  * @author Christian Bauer <christian@hibernate.org>
13  *
14  */

15 @Entity(access = AccessType.FIELD)
16 @Table(name = "ITEM")
17 @org.hibernate.annotations.BatchSize(size = 10)
18 public class Item implements Serializable JavaDoc, Comparable JavaDoc, Auditable {
19
20     @Id(generate = GeneratorType.AUTO)
21     @Column(name = "USER_ID")
22     private Long JavaDoc id = null;
23
24     @Version
25     private int version = 0;
26
27     @Column(length = 255, nullable = false, updatable = false)
28     private String JavaDoc name;
29
30     @ManyToOne
31     @JoinColumn(name="SELLER_ID", nullable = false, updatable = false)
32     private User seller;
33
34     @Column(length = 4000, nullable = false)
35     private String JavaDoc description;
36
37     @org.hibernate.annotations.Type(type = "monetary_amount_usd")
38     @org.hibernate.annotations.Columns(columns = {
39         @Column( name="INITIAL_PRICE"),
40         @Column( name="INITIAL_PRICE_CURRENCY", length = 2)
41     })
42     private MonetaryAmount initialPrice;
43
44     @org.hibernate.annotations.Type(type = "monetary_amount_usd")
45     @org.hibernate.annotations.Columns(columns = {
46         @Column( name="RESERVE_PRICE"),
47         @Column( name="RESERVE_PRICE_CURRENCY", length = 2)
48     })
49     private MonetaryAmount reservePrice;
50
51     @Column( nullable = false, updatable = false)
52     private Date startDate;
53
54     @Column( nullable = false, updatable = false)
55     private Date endDate;
56
57     @OneToMany(cascade = CascadeType.ALL, mappedBy = "item")
58     @org.hibernate.annotations.Cascade(value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
59     private Set<CategorizedItem> categorizedItems = new HashSet<CategorizedItem>();
60
61     @OneToMany(cascade = CascadeType.ALL, mappedBy = "item")
62     @org.hibernate.annotations.Cascade(value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
63     @org.hibernate.annotations.OrderBy(clause = "CREATED desc")
64     private Collection<Bid> bids = new ArrayList<Bid>();
65
66     @ManyToOne
67     @JoinColumn(name = "SUCCESSFUL_BID_ID", nullable = true)
68     private Bid successfulBid;
69
70     //TODO: Map this using idbag and collection of value types
71
/*
72     <idbag name="images"
73             table="ITEM_IMAGES">
74         <collection-id column="ITEM_IMAGE_ID" type="int">
75             <!-- For PostgreSQL, Oracle, HSQLDB, etc. -->
76             <generator class="sequence"/>
77             <!-- For MS SQL, DB2, etc.
78             <generator class="identity"/>
79             -->
80         </collection-id>
81         <key column="ITEM_ID"/>
82         <element column="FILENAME" type="string"/>
83     </idbag>
84     */

85     @Transient
86     private Collection<String JavaDoc> images = new ArrayList<String JavaDoc>();
87
88     //TODO: Test if this really works
89
@org.hibernate.annotations.Type(type = "item_state")
90     @Column(name = "ITEM_STATE", nullable = false)
91     private ItemState state;
92
93     @ManyToOne
94     @JoinColumn(name = "APPROVED_BY_USER_ID", nullable = true)
95     private User approvedBy;
96
97     @Column( nullable = true)
98     private Date approvalDatetime;
99
100     @Column( nullable = false, updatable = false)
101     private Date created = new Date();
102
103     /**
104      * No-arg constructor for JavaBean tools.
105      */

106     Item() {}
107
108     /**
109      * Full constructor.
110      */

111     public Item(String JavaDoc name, String JavaDoc description, User seller,
112                 MonetaryAmount initialPrice, MonetaryAmount reservePrice,
113                 Date startDate, Date endDate,
114                 Set<CategorizedItem> categories, List<Bid> bids, Bid successfulBid, Collection<String JavaDoc> images) {
115         this.name = name;
116         this.seller = seller;
117         this.description = description;
118         this.initialPrice = initialPrice;
119         this.reservePrice = reservePrice;
120         this.startDate = startDate;
121         this.endDate = endDate;
122         this.categorizedItems = categories;
123         this.bids = bids;
124         this.successfulBid = successfulBid;
125         this.images = images;
126         this.state = ItemState.DRAFT;
127     }
128
129     /**
130      * Simple properties only constructor.
131      */

132     public Item(String JavaDoc name, String JavaDoc description, User seller,
133                 MonetaryAmount initialPrice, MonetaryAmount reservePrice,
134                 Date startDate, Date endDate) {
135         this.name = name;
136         this.seller = seller;
137         this.description = description;
138         this.initialPrice = initialPrice;
139         this.reservePrice = reservePrice;
140         this.startDate = startDate;
141         this.endDate = endDate;
142         this.state = ItemState.DRAFT;
143     }
144
145     // ********************** Accessor Methods ********************** //
146

147     public Long JavaDoc getId() { return id; }
148     public int getVersion() { return version; }
149
150     public String JavaDoc getName() { return name; }
151     public User getSeller() { return seller; }
152
153     public String JavaDoc getDescription() { return description; }
154     public void setDescription(String JavaDoc description) { this.description = description; }
155
156     public MonetaryAmount getInitialPrice() { return initialPrice; }
157
158     public MonetaryAmount getReservePrice() { return reservePrice; }
159
160     public Date getStartDate() { return startDate; }
161
162     public Date getEndDate() { return endDate; }
163
164     public Set<CategorizedItem> getCategorizedItems() { return categorizedItems; }
165     public void addCategorizedItem(CategorizedItem catItem) {
166         if (catItem == null)
167             throw new IllegalArgumentException JavaDoc("Can't add a null CategorizedItem.");
168         this.getCategorizedItems().add(catItem);
169     }
170
171     public Collection<Bid> getBids() { return bids; }
172     public void addBid(Bid bid) {
173         if (bid == null)
174             throw new IllegalArgumentException JavaDoc("Can't add a null Bid.");
175         this.getBids().add(bid);
176     }
177
178     public Bid getSuccessfulBid() { return successfulBid; }
179     public void setSuccessfulBid(Bid successfulBid) { this.successfulBid = successfulBid; }
180
181     public Collection<String JavaDoc> getImages() { return images; }
182     public void setImages(Collection<String JavaDoc> images) { this.images = images; }
183
184     public ItemState getState() { return state; }
185
186     public User getApprovedBy() { return approvedBy; }
187     public void setApprovedBy(User approvedBy) { this.approvedBy = approvedBy; }
188
189     public Date getApprovalDatetime() { return approvalDatetime; }
190     public void setApprovalDatetime(Date approvalDatetime) { this.approvalDatetime = approvalDatetime; }
191
192     public Date getCreated() { return created; }
193
194     // ********************** Common Methods ********************** //
195

196     public boolean equals(Object JavaDoc o) {
197         if (this == o) return true;
198         if (!(o instanceof Item)) return false;
199
200         final Item item = (Item) o;
201
202         if (!created.equals(item.created)) return false;
203         if (name != null ? !name.equals(item.name) : item.name != null) return false;
204
205         return true;
206     }
207
208     public int hashCode() {
209         int result;
210         result = (name != null ? name.hashCode() : 0);
211         result = 29 * result + created.hashCode();
212         return result;
213     }
214
215     public String JavaDoc toString() {
216         return "Item ('" + getId() + "'), " +
217                 "Name: '" + getName() + "' " +
218                 "Initial Price: '" + getInitialPrice()+ "'";
219     }
220
221     public int compareTo(Object JavaDoc o) {
222         if (o instanceof Item) {
223             return this.getCreated().compareTo( ((Item)o).getCreated() );
224         }
225         return 0;
226     }
227
228     // ********************** Business Methods ********************** //
229

230     /**
231      * Places a bid while checking business constraints.
232      *
233      * This method may throw a BusinessException if one of the requirements
234      * for the bid placement wasn't met, e.g. if the auction already ended.
235      *
236      * @param bidder
237      * @param bidAmount
238      * @param currentMaxBid the most valuable bid for this item
239      * @param currentMinBid the least valuable bid for this item
240      * @return
241      * @throws BusinessException
242      */

243     public Bid placeBid(User bidder, MonetaryAmount bidAmount,
244                         Bid currentMaxBid, Bid currentMinBid)
245         throws BusinessException {
246
247         // Check highest bid (can also be a different Strategy (pattern))
248
if (currentMaxBid != null && currentMaxBid.getAmount().compareTo(bidAmount) > 0) {
249             throw new BusinessException("Bid too low.");
250         }
251
252         // Auction is active
253
if ( !state.equals(ItemState.ACTIVE) )
254             throw new BusinessException("Auction is not active yet.");
255
256         // Auction still valid
257
if ( this.getEndDate().before( new Date() ) )
258             throw new BusinessException("Can't place new bid, auction already ended.");
259
260         // Create new Bid
261
Bid newBid = new Bid(bidAmount, this, bidder);
262
263         // Place bid for this Item
264
this.addBid(newBid);
265
266         return newBid;
267     }
268
269     /**
270      * Anyone can set this item into PENDING state for approval.
271      */

272     public void setPendingForApproval() {
273         state = ItemState.PENDING;
274     }
275
276     /**
277      * Approve this item for auction and set its state to active.
278      *
279      * @param byUser
280      * @throws BusinessException
281      */

282     public void approve(User byUser) throws BusinessException {
283
284         if ( !byUser.isAdmin() )
285             throw new PermissionException("Not an administrator.");
286
287         if ( !state.equals(ItemState.PENDING) )
288             throw new IllegalStateException JavaDoc("Item still in draft.");
289
290         state = ItemState.ACTIVE;
291         approvedBy = byUser;
292         approvalDatetime = new Date();
293     }
294
295 }
296
Popular Tags