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 ; 7 import java.util.*; 8 9 15 @Entity(access = AccessType.FIELD) 16 @Table(name = "ITEM") 17 @org.hibernate.annotations.BatchSize(size = 10) 18 public class Item implements Serializable , Comparable , Auditable { 19 20 @Id(generate = GeneratorType.AUTO) 21 @Column(name = "USER_ID") 22 private Long id = null; 23 24 @Version 25 private int version = 0; 26 27 @Column(length = 255, nullable = false, updatable = false) 28 private String 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 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 85 @Transient 86 private Collection<String > images = new ArrayList<String >(); 87 88 @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 106 Item() {} 107 108 111 public Item(String name, String description, User seller, 112 MonetaryAmount initialPrice, MonetaryAmount reservePrice, 113 Date startDate, Date endDate, 114 Set<CategorizedItem> categories, List<Bid> bids, Bid successfulBid, Collection<String > 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 132 public Item(String name, String 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 147 public Long getId() { return id; } 148 public int getVersion() { return version; } 149 150 public String getName() { return name; } 151 public User getSeller() { return seller; } 152 153 public String getDescription() { return description; } 154 public void setDescription(String 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 ("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 ("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 > getImages() { return images; } 182 public void setImages(Collection<String > 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 196 public boolean equals(Object 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 toString() { 216 return "Item ('" + getId() + "'), " + 217 "Name: '" + getName() + "' " + 218 "Initial Price: '" + getInitialPrice()+ "'"; 219 } 220 221 public int compareTo(Object o) { 222 if (o instanceof Item) { 223 return this.getCreated().compareTo( ((Item)o).getCreated() ); 224 } 225 return 0; 226 } 227 228 230 243 public Bid placeBid(User bidder, MonetaryAmount bidAmount, 244 Bid currentMaxBid, Bid currentMinBid) 245 throws BusinessException { 246 247 if (currentMaxBid != null && currentMaxBid.getAmount().compareTo(bidAmount) > 0) { 249 throw new BusinessException("Bid too low."); 250 } 251 252 if ( !state.equals(ItemState.ACTIVE) ) 254 throw new BusinessException("Auction is not active yet."); 255 256 if ( this.getEndDate().before( new Date() ) ) 258 throw new BusinessException("Can't place new bid, auction already ended."); 259 260 Bid newBid = new Bid(bidAmount, this, bidder); 262 263 this.addBid(newBid); 265 266 return newBid; 267 } 268 269 272 public void setPendingForApproval() { 273 state = ItemState.PENDING; 274 } 275 276 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 ("Item still in draft."); 289 290 state = ItemState.ACTIVE; 291 approvedBy = byUser; 292 approvalDatetime = new Date(); 293 } 294 295 } 296 | Popular Tags |