KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.GregorianCalendar JavaDoc;
9
10 /**
11  * OldItemBean is an entity bean with "container managed persistence".
12  * The state of an instance is stored into a relational database.
13  * Items are considered old when the end auction date is over. A daemon
14  * moves at specific times items from the items table to the old_items table.
15  * The following table should exist:<p>
16  * <pre>
17  * CREATE TABLE old_items (
18  * id INTEGER UNSIGNED NOT NULL UNIQUE,
19  * name VARCHAR(100),
20  * description TEXT,
21  * initial_price FLOAT UNSIGNED NOT NULL,
22  * quantity INTEGER UNSIGNED NOT NULL,
23  * reserve_price FLOAT UNSIGNED DEFAULT 0,
24  * buy_now FLOAT UNSIGNED DEFAULT 0,
25  * nb_of_bids INTEGER UNSIGNED DEFAULT 0,
26  * max_bid FLOAT UNSIGNED DEFAULT 0,
27  * start_date DATETIME,
28  * end_date DATETIME,
29  * seller INTEGER,
30  * category INTEGER,
31  * PRIMARY KEY(id),
32  * INDEX seller_id (seller),
33  * INDEX category_id (category)
34  * );
35  * </pre>
36  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
37  * @version 1.1
38  */

39
40 public class OldItemBean implements EntityBean
41 {
42   private EntityContext entityContext;
43   private transient boolean isDirty; // used for the isModified function
44

45   /* Class member variables */
46
47   public Integer JavaDoc id;
48   public String JavaDoc name;
49   public String JavaDoc description;
50   public float initialPrice;
51   public int quantity;
52   public float reservePrice;
53   public float buyNow;
54   public int nbOfBids;
55   public float maxBid;
56   public String JavaDoc startDate;
57   public String JavaDoc endDate;
58   public Integer JavaDoc sellerId;
59   public Integer JavaDoc categoryId;
60
61   /**
62    * Get item id.
63    *
64    * @return item id
65    * @exception RemoteException if an error occurs
66    * @since 1.0
67    */

68   public Integer JavaDoc getId() throws RemoteException
69   {
70     return id;
71   }
72
73   /**
74    * Get item name. This description is usually a short description of the item.
75    *
76    * @return item name
77    * @exception RemoteException if an error occurs
78    * @since 1.0
79    */

80   public String JavaDoc getName() throws RemoteException
81   {
82     return name;
83   }
84
85   /**
86    * Get item description . This is usually an HTML file describing the item.
87    *
88    * @return item description
89    * @exception RemoteException if an error occurs
90    * @since 1.0
91    */

92   public String JavaDoc getDescription() throws RemoteException
93   {
94     return description;
95   }
96
97   /**
98    * Get item initial price set by the seller.
99    *
100    * @return item initial price
101    * @exception RemoteException if an error occurs
102    * @since 1.0
103    */

104   public float getInitialPrice() throws RemoteException
105   {
106     return initialPrice;
107   }
108
109   /**
110    * Get how many of this item are to be sold.
111    *
112    * @return item quantity
113    * @exception RemoteException if an error occurs
114    * @since 1.0
115    */

116   public int getQuantity() throws RemoteException
117   {
118     return quantity;
119   }
120
121   /**
122    * Get item reserve price set by the seller. The seller can refuse to sell if reserve price is not reached.
123    *
124    * @return item reserve price
125    * @exception RemoteException if an error occurs
126    * @since 1.0
127    */

128   public float getReservePrice() throws RemoteException
129   {
130     return reservePrice;
131   }
132
133   /**
134    * Get item Buy Now price set by the seller. A user can directly by the item at this price (no auction).
135    *
136    * @return item Buy Now price
137    * @exception RemoteException if an error occurs
138    * @since 1.0
139    */

140   public float getBuyNow() throws RemoteException
141   {
142     return buyNow;
143   }
144
145   /**
146    * Get item maximum bid (if any) for this item. This value should be the same as doing <pre>SELECT MAX(bid) FROM bids WHERE item_id=?</pre>
147    *
148    * @return current maximum bid or 0 if no bid
149    * @exception RemoteException if an error occurs
150    * @since 1.1
151    */

152   public float getMaxBid() throws RemoteException
153   {
154     return maxBid;
155   }
156
157   /**
158    * Get number of bids for this item. This value should be the same as doing <pre>SELECT COUNT(*) FROM bids WHERE item_id=?</pre>
159    *
160    * @return number of bids
161    * @exception RemoteException if an error occurs
162    * @since 1.1
163    */

164   public int getNbOfBids() throws RemoteException
165   {
166     return nbOfBids;
167   }
168
169   /**
170    * Start date of the auction in the format 'YYYY-MM-DD hh:mm:ss'
171    *
172    * @return start date of the auction
173    * @exception RemoteException if an error occurs
174    * @since 1.0
175    */

176   public String JavaDoc getStartDate() throws RemoteException
177   {
178     return startDate;
179   }
180
181   /**
182    * End date of the auction in the format 'YYYY-MM-DD hh:mm:ss'
183    *
184    * @return end date of the auction
185    * @exception RemoteException if an error occurs
186    * @since 1.0
187    */

188   public String JavaDoc getEndDate() throws RemoteException
189   {
190     return endDate;
191   }
192
193   /**
194    * Give the user id of the seller
195    *
196    * @return seller's user id
197    * @since 1.0
198    * @exception RemoteException if an error occurs
199    */

200   public Integer JavaDoc getSellerId() throws RemoteException
201   {
202     return sellerId;
203   }
204
205
206   /**
207    * Give the category id of the item
208    *
209    * @return item's category id
210    * @exception RemoteException if an error occurs
211    * @since 1.0
212    */

213   public Integer JavaDoc getCategoryId() throws RemoteException
214   {
215     return categoryId;
216   }
217
218   
219   /**
220    * Get the seller's nickname by finding the Bean corresponding
221    * to the user.
222    *
223    * @return nickname
224    * @exception RemoteException if an error occurs
225    * @since 1.0
226    */

227   public String JavaDoc getSellerNickname() throws RemoteException
228   {
229     Context JavaDoc initialContext = null;
230     try
231     {
232       initialContext = new InitialContext JavaDoc();
233     }
234     catch (Exception JavaDoc e)
235     {
236       System.err.print("Cannot get initial context for JNDI: " + e);
237       return null;
238     }
239
240     // Try to find the user nick name corresponding to the sellerId
241
UserHome uHome;
242     try
243     {
244       uHome = (UserHome)PortableRemoteObject.narrow(initialContext.lookup("UserHome"),
245                                                     UserHome.class);
246     }
247     catch (Exception JavaDoc e)
248     {
249       System.err.print("Cannot lookup User: " +e);
250       return null;
251     }
252     try
253     {
254       User u = uHome.findByPrimaryKey(new UserPK(sellerId));
255       return u.getNickName();
256     }
257     catch (Exception JavaDoc e)
258     {
259       System.err.print("This user does not exist (got exception: " +e+")<br>");
260       return null;
261     }
262   }
263   
264
265   /**
266    * Get the category name by finding the Bean corresponding to the category Id.
267    *
268    * @return category name
269    * @exception RemoteException if an error occurs
270    * @since 1.0
271    */

272   public String JavaDoc getCategoryName() throws RemoteException
273   {
274     Context JavaDoc initialContext = null;
275     try
276     {
277       initialContext = new InitialContext JavaDoc();
278     }
279     catch (Exception JavaDoc e)
280     {
281       System.err.print("Cannot get initial context for JNDI: " + e);
282       return null;
283     }
284
285     // Try to find the CategoryName corresponding to the categoryId
286
CategoryHome cHome;
287     try
288     {
289       cHome = (CategoryHome)PortableRemoteObject.narrow(initialContext.lookup("CategoryHome"),
290                                                         CategoryHome.class);
291     }
292     catch (Exception JavaDoc e)
293     {
294       System.err.print("Cannot lookup Category: " +e);
295       return null;
296     }
297     try
298     {
299       Category c = cHome.findByPrimaryKey(new CategoryPK(id));
300       return c.getName();
301     }
302     catch (Exception JavaDoc e)
303     {
304       System.err.print("This category does not exist (got exception: " +e+")<br>");
305       return null;
306     }
307   }
308
309
310   /**
311    * Set a new item identifier
312    *
313    * @param newId item identifier
314    * @exception RemoteException if an error occurs
315    * @since 1.0
316    */

317   public void setId(Integer JavaDoc newId) throws RemoteException
318   {
319     id = newId;
320     isDirty = true; // the bean content has been modified
321
}
322
323   /**
324    * Set a new item name
325    *
326    * @param newName item name
327    * @exception RemoteException if an error occurs
328    * @since 1.0
329    */

330   public void setName(String JavaDoc newName) throws RemoteException
331   {
332     name = newName;
333     isDirty = true; // the bean content has been modified
334
}
335
336   /**
337    * Set a new item description
338    *
339    * @param newDescription item description
340    * @exception RemoteException if an error occurs
341    * @since 1.0
342    */

343   public void setDescription(String JavaDoc newDescription) throws RemoteException
344   {
345     description = newDescription;
346     isDirty = true; // the bean content has been modified
347
}
348
349   /**
350    * Set a new initial price for the item
351    *
352    * @param newInitialPrice item initial price
353    * @exception RemoteException if an error occurs
354    * @since 1.0
355    */

356   public void setInitialPrice(float newInitialPrice) throws RemoteException
357   {
358     initialPrice = newInitialPrice;
359     isDirty = true; // the bean content has been modified
360
}
361
362   /**
363    * Set a new item quantity
364    *
365    * @param qty item quantity
366    * @exception RemoteException if an error occurs
367    * @since 1.0
368    */

369   public void setQuantity(int qty) throws RemoteException
370   {
371     quantity = qty;
372     isDirty = true; // the bean content has been modified
373
}
374
375   /**
376    * Set a new reserve price for the item
377    *
378    * @param newReservePrice item reserve price
379    * @exception RemoteException if an error occurs
380    * @since 1.0
381    */

382   public void setReservePrice(float newReservePrice) throws RemoteException
383   {
384     reservePrice = newReservePrice;
385     isDirty = true; // the bean content has been modified
386
}
387
388   /**
389    * Set a new Buy Now price for the item
390    *
391    * @param newBuyNow item Buy Now price
392    * @exception RemoteException if an error occurs
393    * @since 1.0
394    */

395   public void setBuyNow(float newBuyNow) throws RemoteException
396   {
397     buyNow = newBuyNow;
398     isDirty = true; // the bean content has been modified
399
}
400
401   /**
402    * Set item maximum bid. This function checks if newMaxBid is greater
403    * than current maxBid and only updates the value in this case.
404    *
405    * @param newMaxBid new maximum bid
406    * @exception RemoteException if an error occurs
407    * @since 1.1
408    */

409   public void setMaxBid(float newMaxBid) throws RemoteException
410   {
411     if (newMaxBid > maxBid)
412       maxBid = newMaxBid;
413     isDirty = true; // the bean content has been modified
414
}
415
416   /**
417    * Set the number of bids for this item
418    *
419    * @param newNbOfBids new number of bids
420    * @exception RemoteException if an error occurs
421    * @since 1.1
422    */

423   public void setNbOfBids(int newNbOfBids) throws RemoteException
424   {
425     nbOfBids = newNbOfBids;
426     isDirty = true; // the bean content has been modified
427
}
428
429   /**
430    * Add one bid for this item
431    *
432    * @exception RemoteException if an error occurs
433    * @since 1.1
434    */

435   public void addOneBid() throws RemoteException
436   {
437     nbOfBids++;
438     isDirty = true; // the bean content has been modified
439
}
440
441   /**
442    * Set a new beginning date for the auction
443    *
444    * @param newDate auction new beginning date
445    * @exception RemoteException if an error occurs
446    * @since 1.0
447    */

448   public void setStartDate(String JavaDoc newDate) throws RemoteException
449   {
450     startDate = newDate;
451     isDirty = true; // the bean content has been modified
452
}
453
454   /**
455    * Set a new ending date for the auction
456    *
457    * @param newDate auction new ending date
458    * @exception RemoteException if an error occurs
459    * @since 1.0
460    */

461   public void setEndDate(String JavaDoc newDate) throws RemoteException
462   {
463     endDate = newDate;
464     isDirty = true; // the bean content has been modified
465
}
466
467   /**
468    * Set a new seller identifier. This id must match
469    * the primary key of the users table.
470    *
471    * @param id seller id
472    * @exception RemoteException if an error occurs
473    * @since 1.0
474    */

475   public void setSellerId(Integer JavaDoc id) throws RemoteException
476   {
477     sellerId = id;
478     isDirty = true; // the bean content has been modified
479
}
480
481   /**
482    * Set a new category identifier. This id must match
483    * the primary key of the category table.
484    *
485    * @param id category id
486    * @exception RemoteException if an error occurs
487    * @since 1.0
488    */

489   public void setCategoryId(Integer JavaDoc id) throws RemoteException
490   {
491     categoryId = id;
492     isDirty = true; // the bean content has been modified
493
}
494
495
496   /**
497    * This method is used to create a new OldItem Bean. Note that the item id
498    * is automatically generated by the database (AUTO_INCREMENT) on the
499    * primary key.
500    *
501    * @param itemId item identifier
502    * @param itemName short item designation
503    * @param itemDescription long item description, usually an HTML file
504    * @param itemInitialPrice initial price fixed by the seller
505    * @param itemQuantity number to sell (of this item)
506    * @param itemReservePrice reserve price (minimum price the seller really wants to sell)
507    * @param itemBuyNow price if a user wants to buy the item immediatly
508    * @param duration duration of the auction in days (start date is when the method is called and end date is computed according to the duration)
509    * @param itemSellerId seller id, must match the primary key of table users
510    * @param itemCategoryId category id, must match the primary key of table categories
511    *
512    * @return pk primary key set to null
513    *
514    * @exception CreateException if an error occurs
515    * @exception RemoteException if an error occurs
516    * @exception RemoveException if an error occurs
517    * @since 1.0
518    */

519   public OldItemPK ejbCreate(Integer JavaDoc itemId, String JavaDoc itemName, String JavaDoc itemDescription, float itemInitialPrice,
520                           int itemQuantity, float itemReservePrice, float itemBuyNow, int duration,
521                           Integer JavaDoc itemSellerId, Integer JavaDoc itemCategoryId) throws CreateException, RemoteException, RemoveException
522   {
523     GregorianCalendar JavaDoc start = new GregorianCalendar JavaDoc();
524
525     id = itemId;
526     name = itemName;
527     description = itemDescription;
528     initialPrice = itemInitialPrice;
529     quantity = itemQuantity;
530     reservePrice = itemReservePrice;
531     buyNow = itemBuyNow;
532     sellerId = itemSellerId;
533     categoryId = itemCategoryId;
534     nbOfBids = 0;
535     maxBid = 0;
536     startDate = TimeManagement.dateToString(start);
537     endDate = TimeManagement.dateToString(TimeManagement.addDays(start, duration));
538     return null;
539   }
540
541   /** This method does currently nothing */
542   public void ejbPostCreate(Integer JavaDoc itemId, String JavaDoc itemName, String JavaDoc itemDescription, float itemInitialPrice,
543                 int itemQuantity, float itemReservePrice, float itemBuyNow, int duration,
544                 Integer JavaDoc itemSellerId, Integer JavaDoc itemCategoryId) {}
545
546   /** Persistence is managed by the container and the bean
547       becomes up to date */

548   public void ejbLoad() throws RemoteException
549   {
550     isDirty = false;
551   }
552
553   /** Persistence is managed by the container and the bean
554       becomes up to date */

555   public void ejbStore() throws RemoteException
556   {
557     isDirty = false;
558   }
559
560   /** This method is empty because persistence is managed by the container */
561   public void ejbActivate() throws RemoteException {}
562   /** This method is empty because persistence is managed by the container */
563   public void ejbPassivate() throws RemoteException {}
564   /** This method is empty because persistence is managed by the container */
565   public void ejbRemove() throws RemoteException, RemoveException {}
566
567   /**
568    * Sets the associated entity context. The container invokes this method
569    * on an instance after the instance has been created.
570    *
571    * This method is called in an unspecified transaction context.
572    *
573    * @param context An EntityContext interface for the instance. The instance should
574    * store the reference to the context in an instance variable.
575    * @exception EJBException Thrown by the method to indicate a failure
576    * caused by a system-level error.
577    * @exception RemoteException - This exception is defined in the method signature
578    * to provide backward compatibility for enterprise beans
579    * written for the EJB 1.0 specification.
580    * Enterprise beans written for the EJB 1.1 and
581    * higher specification should throw the javax.ejb.EJBException
582    * instead of this exception.
583    */

584   public void setEntityContext(EntityContext context) throws RemoteException
585   {
586     entityContext = context;
587   }
588
589   /**
590    * Unsets the associated entity context. The container calls this method
591    * before removing the instance. This is the last method that the container
592    * invokes on the instance. The Java garbage collector will eventually invoke
593    * the finalize() method on the instance.
594    *
595    * This method is called in an unspecified transaction context.
596    *
597    * @exception EJBException Thrown by the method to indicate a failure
598    * caused by a system-level error.
599    * @exception RemoteException - This exception is defined in the method signature
600    * to provide backward compatibility for enterprise beans
601    * written for the EJB 1.0 specification.
602    * Enterprise beans written for the EJB 1.1 and
603    * higher specification should throw the javax.ejb.EJBException
604    * instead of this exception.
605    */

606   public void unsetEntityContext() throws RemoteException
607   {
608     entityContext = null;
609   }
610
611
612   /**
613    * Returns true if the beans has been modified.
614    * It prevents the EJB server from reloading a bean
615    * that has not been modified.
616    *
617    * @return a <code>boolean</code> value
618    */

619   public boolean isModified()
620   {
621     return isDirty;
622   }
623 }
624
Popular Tags