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      private long id;
21      private long version;
22      private String name;
23      private String productId;
24      private Date startDate;
25      private Date endDate;    
26      private double minBid;
27      private Person owner;
28      private List<Bid> bids = new ArrayList<Bid>();
29      private boolean closed=false;
30      private Bid winningBid;
31      
32      public AuctionItem() {}
33      public AuctionItem(long id) { setId(id); }
34      
35      @Id @GeneratedValue
36      public long getId() {
37          return id;
38      }
39      private void setId(long id) {
40          this.id = id;
41      }
42      @Version
43      public long getVersion() {
44          return version;
45      }
46      public void setVersion(long version) {
47          this.version = version;
48      }
49      
50      public String getName() {
51          return name;
52      }
53      public void setName(String name) {
54          this.name = name;
55      }
56      public String getProductId() {
57          return productId;
58      }
59      public void setProductId(String productId) {
60          this.productId = productId;
61      }
62  
63      public double getMinBid() {
64          return minBid;
65      }
66      public void setMinBid(double minBid) {
67          this.minBid = minBid;
68      }
69      
70      @Temporal(TemporalType.TIMESTAMP)
71      public Date getEndDate() {
72          return endDate;
73      }
74      public void setEndDate(Date endDate) {
75          this.endDate = endDate;
76      }
77      @Temporal(TemporalType.TIMESTAMP)
78      public Date getStartDate() {
79          return startDate;
80      }
81      public void setStartDate(Date startDate) {
82          this.startDate = startDate;
83      }
84  
85      @ManyToOne
86      public Person getOwner() {
87          return owner;
88      }
89      public void setOwner(Person owner) {
90          this.owner = owner;
91      }
92      public void closeBids() {
93          closed=true;
94          setWinningBid(getHighestBid());        
95      }
96      public void setClosed(boolean closed) {
97          this.closed = closed;
98      }
99      public boolean isClosed() {
100         return closed;
101     }
102     
103     @OneToMany(mappedBy="item")
104     public List<Bid> getBids() {
105         return bids;
106     }
107     public void setBids(List<Bid> bids) {        
108         this.bids = bids;
109     }
110     public void addBid(Bid bid) throws IllegalArgumentException {
111         Bid highest = getHighestBid();
112         if (highest == null || bid.getAmount() > highest.getAmount()) {
113             bids.add(bid);
114         }
115     }
116     
117     @Transient
118     public Bid getHighestBid() {
119         Bid highest = null;
120         for (Bid b : bids) {
121             if (highest == null || b.getAmount() > highest.getAmount()) {
122                 highest = b;
123             }
124         }
125         return highest;
126     }
127     
128     @OneToOne
129     public Bid getWinningBid() {
130         return winningBid;
131     }
132     public void setWinningBid(Bid winningBid) {
133         this.winningBid = winningBid;
134     }
135     
136     public String toString() {
137         StringBuilder text = new StringBuilder();
138         text.append("id=" + id);
139         text.append(", name=" + name);
140         text.append(", version=" + version);
141         text.append(", startDate=" + startDate);
142         text.append(", endDate=" + endDate);
143         text.append(", midBid=$" + minBid);
144         text.append(", bids(" + bids.size() + ")={");
145         for(Bid b : bids) {
146             text.append(b.getBidder() + " $" + b.getAmount() + ",");
147         }
148         text.append("}");
149         text.append(", closed=" + closed);
150         text.append(", winning bid=" + (winningBid==null ? null :
151             winningBid.getAmount() + " $" + winningBid.getAmount()));
152         return text.toString();
153     }
154 }