KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > rubis > beans > BidBean


1 package edu.rice.rubis.beans;
2
3 import java.rmi.*;
4 import javax.ejb.*;
5 import javax.naming.Context JavaDoc;
6 import javax.naming.InitialContext JavaDoc;
7 import javax.rmi.PortableRemoteObject JavaDoc;
8
9 /**
10  * BidBean is an entity bean with "container managed persistence".
11  * The state of an instance is stored into a relational database.
12  * The following table should exist:<p>
13  * <pre>
14  * CREATE TABLE bids (
15  * id INTEGER UNSIGNED NOT NULL UNIQUE,
16  * user_id INTEGER,
17  * item_id INTEGER,
18  * qty INTEGER,
19  * bid FLOAT UNSIGNED NOT NULL,
20  * max_bid FLOAT UNSIGNED NOT NULL,
21  * date DATETIME
22  * INDEX item (item_id),
23  * INDEX user (user_id)
24  * );
25  * </pre>
26  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
27  * @version 1.0
28  */

29
30 public class BidBean implements EntityBean
31 {
32   private EntityContext entityContext;
33   private transient boolean isDirty; // used for the isModified function
34

35   /* Class member variables */
36
37   public Integer JavaDoc id;
38   public Integer JavaDoc userId;
39   public Integer JavaDoc itemId;
40   public int qty;
41   public float bid;
42   public float maxBid;
43   public String JavaDoc date;
44
45   
46   /**
47    * Get bid's id.
48    *
49    * @return bid id
50    * @exception RemoteException if an error occurs
51    */

52   public Integer JavaDoc getId() throws RemoteException
53   {
54     return id;
55   }
56
57   /**
58    * Get the user id which is the primary key in the users table.
59    *
60    * @return user id
61    * @exception RemoteException if an error occurs
62    */

63   public Integer JavaDoc getUserId() throws RemoteException
64   {
65     return userId;
66   }
67
68   /**
69    * Get the item id which is the primary key in the items table.
70    *
71    * @return item id
72    * @exception RemoteException if an error occurs
73    */

74   public Integer JavaDoc getItemId() throws RemoteException
75   {
76     return itemId;
77   }
78
79   /**
80    * Get how many of this item the user wants.
81    *
82    * @return quantity of items for this bid.
83    * @exception RemoteException if an error occurs
84    */

85   public int getQuantity() throws RemoteException
86   {
87     return qty;
88   }
89
90   /**
91    * Get the bid of the user.
92    *
93    * @return user's bid
94    * @exception RemoteException if an error occurs
95    */

96   public float getBid() throws RemoteException
97   {
98     return bid;
99   }
100
101   /**
102    * Get the maximum bid wanted by the user.
103    *
104    * @return user's maximum bid
105    * @exception RemoteException if an error occurs
106    */

107   public float getMaxBid() throws RemoteException
108   {
109     return maxBid;
110   }
111
112   /**
113    * Time of the Bid in the format 'YYYY-MM-DD hh:mm:ss'
114    *
115    * @return bid time
116    * @exception RemoteException if an error occurs
117    */

118   public String JavaDoc getDate() throws RemoteException
119   {
120     return date;
121   }
122
123   /**
124    * Give the nick name of the bidder
125    *
126    * @return bidder's nick name
127    * @exception RemoteException if an error occurs
128    */

129   public String JavaDoc getBidderNickName() throws RemoteException
130   {
131     Context JavaDoc initialContext = null;
132     try
133     {
134       initialContext = new InitialContext JavaDoc();
135     }
136     catch (Exception JavaDoc e)
137     {
138       System.err.print("Cannot get initial context for JNDI: " + e);
139       return null;
140     }
141
142     // Try to find the user nick name corresponding to the sellerId
143
UserHome uHome;
144     try
145     {
146       uHome = (UserHome)PortableRemoteObject.narrow(initialContext.lookup("UserHome"),
147                                                     UserHome.class);
148     }
149     catch (Exception JavaDoc e)
150     {
151       System.err.print("Cannot lookup User: " +e);
152       return null;
153     }
154     try
155     {
156       User u = uHome.findByPrimaryKey(new UserPK(userId));
157       return u.getNickName();
158     }
159     catch (Exception JavaDoc e)
160     {
161       System.err.print("This user does not exist (got exception: " +e+")<br>");
162       return null;
163     }
164   }
165
166
167   /**
168    * Set a new user identifier. This id must match
169    * the primary key of the users table.
170    *
171    * @param id user id
172    * @exception RemoteException if an error occurs
173    */

174   public void setUserId(Integer JavaDoc id) throws RemoteException
175   {
176     userId = id;
177     isDirty = true; // the bean content has been modified
178
}
179
180   /**
181    * Set a new item identifier. This id must match
182    * the primary key of the items table.
183    *
184    * @param id item id
185    * @exception RemoteException if an error occurs
186    */

187   public void setItemId(Integer JavaDoc id) throws RemoteException
188   {
189     itemId = id;
190     isDirty = true; // the bean content has been modified
191
}
192
193   /**
194    * Set a new quantity for this bid
195    *
196    * @param Qty quantity
197    * @exception RemoteException if an error occurs
198    */

199   public void setQuantity(int Qty) throws RemoteException
200   {
201     qty = Qty;
202     isDirty = true; // the bean content has been modified
203
}
204
205   /**
206    * Set a new bid on the item for the user.
207    * <pre>
208    * Warning! This method does not update the maxBid value in the items table
209    * </pre>
210    *
211    * @param newBid a <code>float</code> value
212    * @exception RemoteException if an error occurs
213    */

214   public void setBid(float newBid) throws RemoteException
215   {
216     bid = newBid;
217     isDirty = true; // the bean content has been modified
218
}
219
220   /**
221    * Set a new maximum bid on the item for the user
222    *
223    * @param newBid a <code>float</code> value
224    * @exception RemoteException if an error occurs
225    */

226   public void setMaxBid(float newBid) throws RemoteException
227   {
228     maxBid = newBid;
229     isDirty = true; // the bean content has been modified
230
}
231
232   /**
233    * Set a new date for this bid
234    *
235    * @param newDate bid date
236    * @exception RemoteException if an error occurs
237    */

238   public void setDate(String JavaDoc newDate) throws RemoteException
239   {
240     date = newDate;
241     isDirty = true; // the bean content has been modified
242
}
243
244
245   /**
246    * This method is used to create a new Bid Bean.
247    * The date is automatically set to the current date when the method is called.
248    *
249    * @param bidUserId user id of the bidder, must match the primary key of table users
250    * @param bidItemId item id, must match the primary key of table items
251    * @param userBid the amount of the user bid
252    * @param userMaxBid the maximum amount the user wants to bid
253    * @param quantity number of items the user wants to buy
254    *
255    * @return pk primary key set to null
256    * @exception CreateException if an error occurs
257    * @exception RemoteException if an error occurs
258    * @exception RemoveException if an error occurs
259    */

260   public BidPK ejbCreate(Integer JavaDoc bidUserId, Integer JavaDoc bidItemId, float userBid, float userMaxBid, int quantity) throws CreateException, RemoteException, RemoveException
261   {
262     Item item;
263     InitialContext JavaDoc initialContext = null;
264     // Find the item to update its maxBid and nbOfBids
265
try
266     {
267       initialContext = new InitialContext JavaDoc();
268       ItemHome iHome = (ItemHome)PortableRemoteObject.narrow(initialContext.lookup("ItemHome"), ItemHome.class);
269       item = iHome.findByPrimaryKey(new ItemPK(bidItemId));
270     }
271     catch (Exception JavaDoc e)
272     {
273       throw new CreateException("Error while getting item id "+bidItemId+" in BidBean: " + e+"<br>");
274     }
275     item.setMaxBid(userBid);
276     item.addOneBid();
277
278      // Connecting to IDManager Home interface thru JNDI
279
IDManagerHome home = null;
280       IDManager idManager = null;
281       
282       try
283       {
284         home = (IDManagerHome)PortableRemoteObject.narrow(initialContext.lookup(
285                "java:comp/env/ejb/IDManager"), IDManagerHome.class);
286       }
287       catch (Exception JavaDoc e)
288       {
289         throw new EJBException("Cannot lookup IDManager: " +e);
290       }
291      try
292       {
293         IDManagerPK idPK = new IDManagerPK();
294         idManager = home.findByPrimaryKey(idPK);
295         id = idManager.getNextBidID();
296         userId = bidUserId;
297         itemId = bidItemId;
298         bid = userBid;
299         maxBid = userMaxBid;
300         qty = quantity;
301         date = TimeManagement.currentDateToString();
302       }
303      catch (Exception JavaDoc e)
304      {
305        throw new EJBException("Cannot create bid: " +e);
306      }
307     return null;
308   }
309
310
311   /** This method just set an internal flag to
312       reload the id generated by the DB */

313   public void ejbPostCreate(Integer JavaDoc bidUserId, Integer JavaDoc bidItemId, float userBid, float userMaxBid, int quantity)
314   {
315     isDirty = true; // the id has to be reloaded from the DB
316
}
317
318   /** Persistence is managed by the container and the bean
319       becomes up to date */

320   public void ejbLoad() throws RemoteException
321   {
322     isDirty = false;
323   }
324
325   /** Persistence is managed by the container and the bean
326       becomes up to date */

327   public void ejbStore() throws RemoteException
328   {
329     isDirty = false;
330   }
331
332   /** This method is empty because persistence is managed by the container */
333   public void ejbActivate() throws RemoteException {}
334   /** This method is empty because persistence is managed by the container */
335   public void ejbPassivate() throws RemoteException {}
336   /** This method is empty because persistence is managed by the container */
337   public void ejbRemove() throws RemoteException, RemoveException {}
338
339   /**
340    * Sets the associated entity context. The container invokes this method
341    * on an instance after the instance has been created.
342    *
343    * This method is called in an unspecified transaction context.
344    *
345    * @param context An EntityContext interface for the instance. The instance should
346    * store the reference to the context in an instance variable.
347    * @exception EJBException Thrown by the method to indicate a failure
348    * caused by a system-level error.
349    * @exception RemoteException - This exception is defined in the method signature
350    * to provide backward compatibility for enterprise beans
351    * written for the EJB 1.0 specification.
352    * Enterprise beans written for the EJB 1.1 and
353    * higher specification should throw the javax.ejb.EJBException
354    * instead of this exception.
355    */

356   public void setEntityContext(EntityContext context) throws RemoteException
357   {
358     entityContext = context;
359   }
360
361   /**
362    * Unsets the associated entity context. The container calls this method
363    * before removing the instance. This is the last method that the container
364    * invokes on the instance. The Java garbage collector will eventually invoke
365    * the finalize() method on the instance.
366    *
367    * This method is called in an unspecified transaction context.
368    *
369    * @exception EJBException Thrown by the method to indicate a failure
370    * caused by a system-level error.
371    * @exception RemoteException - This exception is defined in the method signature
372    * to provide backward compatibility for enterprise beans
373    * written for the EJB 1.0 specification.
374    * Enterprise beans written for the EJB 1.1 and
375    * higher specification should throw the javax.ejb.EJBException
376    * instead of this exception.
377    */

378   public void unsetEntityContext() throws RemoteException
379   {
380     entityContext = null;
381   }
382
383
384   /**
385    * Returns true if the beans has been modified.
386    * It prevents the EJB server from reloading a bean
387    * that has not been modified.
388    *
389    * @return a <code>boolean</code> value
390    */

391   public boolean isModified()
392   {
393     return isDirty;
394   }
395
396
397   /**
398    * Display bid history information as an HTML table row
399    *
400    * @return a <code>String</code> containing HTML code
401    * @exception RemoteException if an error occurs
402    * @since 1.0
403    */

404   public String JavaDoc printBidHistory() throws RemoteException
405   {
406     return "<TR><TD><a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.ViewUserInfo?userId="+userId+
407       "\">"+getBidderNickName()+"<TD>"+bid+"<TD>"+date+"\n";
408   }
409 }
410
Popular Tags