View Javadoc
1   package ejava.examples.asyncmarket.bo;
2   
3   import java.io.Serializable;
4   import java.util.List;
5   import java.util.ArrayList;
6   import java.util.Date;
7   
8   import javax.persistence.*;
9   
10  @NamedQueries({
11      @NamedQuery(name="AsyncMarket_getAuctionItems",
12              query="select ai from AuctionItem ai"),
13      @NamedQuery(name="AsyncMarket_getAvailableAuctionItems",
14              query="select ai from AuctionItem ai " +
15                      "where ai.closed = false")
16  })
17  @Entity @Table(name="ASYNCMARKET_AUCTIONITEM")
18  public class AuctionItem implements Serializable {
19      private static final long serialVersionUID = 1L;
20      
21      @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
22      private long id;
23      
24      @Version
25      private long version;
26      
27      @Column(length=32)
28      private String name;
29      @Column(length=32)
30      private String productId;
31      
32      @Temporal(TemporalType.TIMESTAMP)
33      private Date startDate;
34      
35      @Temporal(TemporalType.TIMESTAMP)
36      private Date endDate;
37      
38      private double minBid;
39      
40      @ManyToOne(optional=false, fetch=FetchType.LAZY)
41      private Person owner;
42      
43      @OneToMany(mappedBy="item",
44                 fetch=FetchType.LAZY,
45                 cascade= {CascadeType.PERSIST,
46                           CascadeType.REMOVE})
47      private List<Bid> bids = new ArrayList<Bid>();
48      private boolean closed=false;
49      
50      @OneToOne(fetch=FetchType.EAGER)
51      private Bid winningBid;
52      
53      public AuctionItem() {}
54      public AuctionItem(long id) { setId(id); }
55      
56      public long getId() {
57          return id;
58      }
59      private void setId(long id) {
60          this.id = id;
61      }
62      
63      public long getVersion() {
64          return version;
65      }
66      public void setVersion(long version) {
67          this.version = version;
68      }
69      
70      public String getName() {
71          return name;
72      }
73      public void setName(String name) {
74          this.name = name;
75      }
76      
77      public String getProductId() {
78          return productId;
79      }
80      public void setProductId(String productId) {
81          this.productId = productId;
82      }
83  
84      public double getMinBid() {
85          return minBid;
86      }
87      public void setMinBid(double minBid) {
88          this.minBid = minBid;
89      }
90      
91      public Date getEndDate() {
92          return endDate;
93      }
94      public void setEndDate(Date endDate) {
95          this.endDate = endDate;
96      }
97      
98      public Date getStartDate() {
99          return startDate;
100     }
101     public void setStartDate(Date startDate) {
102         this.startDate = startDate;
103     }
104 
105     public Person getOwner() {
106         return owner;
107     }
108     public void setOwner(Person owner) {
109         this.owner = owner;
110     }
111     
112     public void closeBids() {
113         closed=true;
114         setWinningBid(getHighestBid());        
115     }
116     
117     public void setClosed(boolean closed) {
118         this.closed = closed;
119     }
120     public boolean isClosed() {
121         return closed;
122     }
123     
124     public List<Bid> getBids() {
125         return bids;
126     }
127     public void setBids(List<Bid> bids) {        
128         this.bids = bids;
129     }
130     
131     public void addBid(Bid bid) throws IllegalArgumentException {
132         Bid highest = getHighestBid();
133         if (highest == null || bid.getAmount() > highest.getAmount()) {
134             bids.add(bid);
135         }
136     }
137     
138     @Transient
139     public Bid getHighestBid() {
140         Bid highest = null;
141         for (Bid b : bids) {
142             if (highest == null || b.getAmount() > highest.getAmount()) {
143                 highest = b;
144             }
145         }
146         return highest;
147     }
148     
149     public Bid getWinningBid() {
150         return winningBid;
151     }
152     public void setWinningBid(Bid winningBid) {
153         this.winningBid = winningBid;
154     }
155     
156     public String toString() {
157         StringBuilder text = new StringBuilder();
158         text.append("id=" + id);
159         text.append(", name=" + name);
160         text.append(", version=" + version);
161         text.append(", startDate=" + startDate);
162         text.append(", endDate=" + endDate);
163         text.append(", midBid=$" + minBid);
164         text.append(", bids(" + bids.size() + ")={");
165         for(Bid b : bids) {
166             text.append(b.getBidder() + " $" + b.getAmount() + ",");
167         }
168         text.append("}");
169         text.append(", closed=" + closed);
170         text.append(", winning bid=" + (winningBid==null ? null :
171             winningBid.getAmount() + " $" + winningBid.getAmount()));
172         return text.toString();
173     }
174 }