KickJava   Java API By Example, From Geeks To Geeks.

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


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  * BuyNowBean 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 buy_now (
15  * id INTEGER UNSIGNED NOT NULL UNIQUE,
16  * buyer_id INTEGER UNSIGNED NOT NULL,
17  * item_id INTEGER UNSIGNED NOT NULL,
18  * qty INTEGER,
19  * date DATETIME,
20  * PRIMARY KEY(id),
21  * INDEX buyer (buyer_id),
22  * INDEX item (item_id)
23  * );
24  * </pre>
25  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
26  * @version 1.0
27  */

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

34   /* Class member variables */
35
36   public Integer JavaDoc id;
37   public Integer JavaDoc buyerId;
38   public Integer JavaDoc itemId;
39   public int qty;
40   public String JavaDoc date;
41
42
43   /**
44    * Get BuyNow id.
45    *
46    * @return BuyNow id
47    * @exception RemoteException if an error occurs
48    */

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

60   public Integer JavaDoc getBuyerId() throws RemoteException
61   {
62     return buyerId;
63   }
64
65   /**
66    * Get the item id which is the primary key in the items table.
67    *
68    * @return item id
69    * @exception RemoteException if an error occurs
70    */

71   public Integer JavaDoc getItemId() throws RemoteException
72   {
73     return itemId;
74   }
75
76   /**
77    * Get how many of this item the user has bought.
78    *
79    * @return quantity of items for this BuyNow.
80    * @exception RemoteException if an error occurs
81    */

82   public int getQuantity() throws RemoteException
83   {
84     return qty;
85   }
86
87   /**
88    * Time of the BuyNow in the format 'YYYY-MM-DD hh:mm:ss'
89    *
90    * @return BuyNow time
91    * @exception RemoteException if an error occurs
92    */

93   public String JavaDoc getDate() throws RemoteException
94   {
95     return date;
96   }
97
98   /**
99    * Set a new buyer identifier. This id must match
100    * the primary key of the users table.
101    *
102    * @param id buyer id
103    * @exception RemoteException if an error occurs
104    */

105   public void setBuyerId(Integer JavaDoc id) throws RemoteException
106   {
107     buyerId = id;
108     isDirty = true; // the bean content has been modified
109
}
110
111   /**
112    * Set a new item identifier. This id must match
113    * the primary key of the items table.
114    *
115    * @param id item id
116    * @exception RemoteException if an error occurs
117    */

118   public void setItemId(Integer JavaDoc id) throws RemoteException
119   {
120     itemId = id;
121     isDirty = true; // the bean content has been modified
122
}
123
124   /**
125    * Set a new quantity for this BuyNow
126    *
127    * @param Qty quantity
128    * @exception RemoteException if an error occurs
129    */

130   public void setQuantity(int Qty) throws RemoteException
131   {
132     qty = Qty;
133     isDirty = true; // the bean content has been modified
134
}
135
136   /**
137    * Set a new date for this BuyNow
138    *
139    * @param newDate BuyNow date
140    * @exception RemoteException if an error occurs
141    */

142   public void setDate(String JavaDoc newDate) throws RemoteException
143   {
144     date = newDate;
145     isDirty = true; // the bean content has been modified
146
}
147
148
149   /**
150    * This method is used to create a new BuyNow Bean.
151    * The date is automatically set to the current date when the method is called.
152    *
153    * @param BuyNowUserId user id of the buyer, must match the primary key of table users
154    * @param BuyNowItemId item id, must match the primary key of table items
155    * @param quantity number of items the user wants to buy
156    *
157    * @return pk primary key set to null
158    * @exception CreateException if an error occurs
159    * @exception RemoteException if an error occurs
160    * @exception RemoveException if an error occurs
161    */

162   public BuyNowPK ejbCreate(Integer JavaDoc BuyNowUserId, Integer JavaDoc BuyNowItemId, int quantity) throws CreateException, RemoteException, RemoveException
163   {
164      // Connecting to IDManager Home interface thru JNDI
165
IDManagerHome home = null;
166       IDManager idManager = null;
167       
168       try
169       {
170         InitialContext JavaDoc initialContext = new InitialContext JavaDoc();
171         home = (IDManagerHome)PortableRemoteObject.narrow(initialContext.lookup(
172                "java:comp/env/ejb/IDManager"), IDManagerHome.class);
173       }
174       catch (Exception JavaDoc e)
175       {
176         throw new EJBException("Cannot lookup IDManager: " +e);
177       }
178      try
179       {
180         IDManagerPK idPK = new IDManagerPK();
181         idManager = home.findByPrimaryKey(idPK);
182         id = idManager.getNextBuyNowID();
183         buyerId = BuyNowUserId;
184         itemId = BuyNowItemId;
185         qty = quantity;
186         date = TimeManagement.currentDateToString();
187       }
188      catch (Exception JavaDoc e)
189      {
190         throw new EJBException("Cannot create buyNow: " +e);
191       }
192     return null;
193   }
194
195   /** This method just set an internal flag to
196       reload the id generated by the DB */

197   public void ejbPostCreate(Integer JavaDoc BuyNowUserId, Integer JavaDoc BuyNowItemId, int quantity)
198   {
199     isDirty = true; // the id has to be reloaded from the DB
200
}
201
202   /** Persistence is managed by the container and the bean
203       becomes up to date */

204   public void ejbLoad() throws RemoteException
205   {
206     isDirty = false;
207   }
208
209   /** Persistence is managed by the container and the bean
210       becomes up to date */

211   public void ejbStore() throws RemoteException
212   {
213     isDirty = false;
214   }
215
216   /** This method is empty because persistence is managed by the container */
217   public void ejbActivate() throws RemoteException {}
218   /** This method is empty because persistence is managed by the container */
219   public void ejbPassivate() throws RemoteException {}
220   /** This method is empty because persistence is managed by the container */
221   public void ejbRemove() throws RemoteException, RemoveException {}
222
223   /**
224    * Sets the associated entity context. The container invokes this method
225    * on an instance after the instance has been created.
226    *
227    * This method is called in an unspecified transaction context.
228    *
229    * @param context An EntityContext interface for the instance. The instance should
230    * store the reference to the context in an instance variable.
231    * @exception EJBException Thrown by the method to indicate a failure
232    * caused by a system-level error.
233    * @exception RemoteException - This exception is defined in the method signature
234    * to provide backward compatibility for enterprise beans
235    * written for the EJB 1.0 specification.
236    * Enterprise beans written for the EJB 1.1 and
237    * higher specification should throw the javax.ejb.EJBException
238    * instead of this exception.
239    */

240   public void setEntityContext(EntityContext context) throws RemoteException
241   {
242     entityContext = context;
243   }
244
245   /**
246    * Unsets the associated entity context. The container calls this method
247    * before removing the instance. This is the last method that the container
248    * invokes on the instance. The Java garbage collector will eventually invoke
249    * the finalize() method on the instance.
250    *
251    * This method is called in an unspecified transaction context.
252    *
253    * @exception EJBException Thrown by the method to indicate a failure
254    * caused by a system-level error.
255    * @exception RemoteException - This exception is defined in the method signature
256    * to provide backward compatibility for enterprise beans
257    * written for the EJB 1.0 specification.
258    * Enterprise beans written for the EJB 1.1 and
259    * higher specification should throw the javax.ejb.EJBException
260    * instead of this exception.
261    */

262   public void unsetEntityContext() throws RemoteException
263   {
264     entityContext = null;
265   }
266
267   /**
268    * Returns true if the beans has been modified.
269    * It prevents the EJB server from reloading a bean
270    * that has not been modified.
271    *
272    * @return a <code>boolean</code> value
273    */

274   public boolean isModified()
275   {
276     return isDirty;
277   }
278 }
279
Popular Tags