KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

36   /* Class member variables */
37
38   public Integer JavaDoc id;
39   public Integer JavaDoc fromUserId;
40   public Integer JavaDoc toUserId;
41   public Integer JavaDoc itemId;
42   public int rating;
43   public String JavaDoc date;
44   public String JavaDoc comment;
45
46
47   /**
48    * Get comment's id.
49    *
50    * @return comment id
51    * @exception RemoteException if an error occurs
52    */

53   public Integer JavaDoc getId() throws RemoteException
54   {
55     return id;
56   }
57
58   /**
59    * Get the user id of the author of the comment
60    *
61    * @return author user id
62    * @exception RemoteException if an error occurs
63    */

64   public Integer JavaDoc getFromUserId() throws RemoteException
65   {
66     return fromUserId;
67   }
68
69   /**
70    * Get the user id of the user this comment is about.
71    *
72    * @return user id this comment is about
73    * @exception RemoteException if an error occurs
74    */

75   public Integer JavaDoc getToUserId() throws RemoteException
76   {
77     return toUserId;
78   }
79
80   /**
81    * Get the item id which is the primary key in the items table.
82    *
83    * @return item id
84    * @exception RemoteException if an error occurs
85    */

86   public Integer JavaDoc getItemId() throws RemoteException
87   {
88     return itemId;
89   }
90
91   /**
92    * Get the rating associated to this comment.
93    *
94    * @return rating
95    * @exception RemoteException if an error occurs
96    */

97   public float getRating() throws RemoteException
98   {
99     return rating;
100   }
101
102   /**
103    * Time of the Comment in the format 'YYYY-MM-DD hh:mm:ss'
104    *
105    * @return comment time
106    * @exception RemoteException if an error occurs
107    */

108   public String JavaDoc getDate() throws RemoteException
109   {
110     return date;
111   }
112   
113   /**
114    * Get the comment text.
115    *
116    * @return comment text
117    * @exception RemoteException if an error occurs
118    */

119   public String JavaDoc getComment() throws RemoteException
120   {
121     return comment;
122   }
123
124
125   /**
126    * Set a new user identifier for the author of the comment.
127    * This id must match the primary key of the users table.
128    *
129    * @param id author user id
130    * @exception RemoteException if an error occurs
131    */

132   public void setFromUserId(Integer JavaDoc id) throws RemoteException
133   {
134     fromUserId = id;
135     isDirty = true; // the bean content has been modified
136
}
137
138   /**
139    * Set a new user identifier for the user this comment is about.
140    * This id must match the primary key of the users table.
141    *
142    * @param id user id comment is about
143    * @exception RemoteException if an error occurs
144    */

145   public void setToUserId(Integer JavaDoc id) throws RemoteException
146   {
147     toUserId = id;
148     isDirty = true; // the bean content has been modified
149
}
150
151   /**
152    * Set a new item identifier. This id must match
153    * the primary key of the items table.
154    *
155    * @param id item id
156    * @exception RemoteException if an error occurs
157    */

158   public void setItemId(Integer JavaDoc id) throws RemoteException
159   {
160     itemId = id;
161     isDirty = true; // the bean content has been modified
162
}
163
164   /**
165    * Set a new rating for the ToUserId.
166    *
167    * @param Rating an <code>int</code> value
168    * @exception RemoteException if an error occurs
169    */

170   public void setRating(int Rating) throws RemoteException
171   {
172     rating = Rating;
173     isDirty = true; // the bean content has been modified
174
}
175
176   /**
177    * Set a new date for this comment
178    *
179    * @param newDate comment date
180    * @exception RemoteException if an error occurs
181    */

182   public void setDate(String JavaDoc newDate) throws RemoteException
183   {
184     date = newDate;
185     isDirty = true; // the bean content has been modified
186
}
187
188   /**
189    * Set a new comment for ToUserId from FromUserId.
190    *
191    * @param newComment Comment
192    * @exception RemoteException if an error occurs
193    */

194   public void setComment(String JavaDoc newComment) throws RemoteException
195   {
196     comment = newComment;
197     isDirty = true; // the bean content has been modified
198
}
199
200
201   /**
202    * This method is used to create a new Comment Bean.
203    * The date is automatically set to the current date when the method is called.
204    *
205    * @param FromUserId user id of the comment author, must match the primary key of table users
206    * @param ToUserId user id of the user this comment is about, must match the primary key of table users
207    * @param ItemId item id, must match the primary key of table items
208    * @param Rating user (ToUserId) rating given by the author (FromUserId)
209    * @param Comment comment text
210    *
211    * @return pk primary key set to null
212    * @exception CreateException if an error occurs
213    * @exception RemoteException if an error occurs
214    * @exception RemoveException if an error occurs
215    */

216   public CommentPK ejbCreate(Integer JavaDoc FromUserId, Integer JavaDoc ToUserId, Integer JavaDoc ItemId, int Rating, String JavaDoc Comment) throws CreateException, RemoteException, RemoveException
217   {
218       // Connecting to IDManager Home interface thru JNDI
219
IDManagerHome home = null;
220       IDManager idManager = null;
221       
222       try
223       {
224         InitialContext JavaDoc initialContext = new InitialContext JavaDoc();
225         home = (IDManagerHome)PortableRemoteObject.narrow(initialContext.lookup(
226                "java:comp/env/ejb/IDManager"), IDManagerHome.class);
227       }
228       catch (Exception JavaDoc e)
229       {
230         throw new EJBException("Cannot lookup IDManager: " +e);
231       }
232      try
233       {
234         IDManagerPK idPK = new IDManagerPK();
235         idManager = home.findByPrimaryKey(idPK);
236         id = idManager.getNextCommentID();
237         fromUserId = FromUserId;
238         toUserId = ToUserId;
239         itemId = ItemId;
240         rating = Rating;
241         date = TimeManagement.currentDateToString();
242         comment = Comment;
243       }
244       catch (Exception JavaDoc e)
245       {
246         throw new EJBException("Cannot create comment: " +e);
247       }
248     return null;
249   }
250
251   /** This method just set an internal flag to
252       reload the id generated by the DB */

253   public void ejbPostCreate(Integer JavaDoc FromUserId, Integer JavaDoc ToUserId, Integer JavaDoc ItemId, int Rating, String JavaDoc Comment)
254   {
255     isDirty = true; // the id has to be reloaded from the DB
256
}
257
258   /** Persistence is managed by the container and the bean
259       becomes up to date */

260   public void ejbLoad() throws RemoteException
261   {
262     isDirty = false;
263   }
264
265   /** Persistence is managed by the container and the bean
266       becomes up to date */

267   public void ejbStore() throws RemoteException
268   {
269     isDirty = false;
270   }
271
272   /** This method is empty because persistence is managed by the container */
273   public void ejbActivate() throws RemoteException {}
274   /** This method is empty because persistence is managed by the container */
275   public void ejbPassivate() throws RemoteException {}
276   /** This method is empty because persistence is managed by the container */
277   public void ejbRemove() throws RemoteException, RemoveException {}
278
279   /**
280    * Sets the associated entity context. The container invokes this method
281    * on an instance after the instance has been created.
282    *
283    * This method is called in an unspecified transaction context.
284    *
285    * @param context An EntityContext interface for the instance. The instance should
286    * store the reference to the context in an instance variable.
287    * @exception EJBException Thrown by the method to indicate a failure
288    * caused by a system-level error.
289    * @exception RemoteException - This exception is defined in the method signature
290    * to provide backward compatibility for enterprise beans
291    * written for the EJB 1.0 specification.
292    * Enterprise beans written for the EJB 1.1 and
293    * higher specification should throw the javax.ejb.EJBException
294    * instead of this exception.
295    */

296   public void setEntityContext(EntityContext context) throws RemoteException
297   {
298     entityContext = context;
299   }
300
301   /**
302    * Unsets the associated entity context. The container calls this method
303    * before removing the instance. This is the last method that the container
304    * invokes on the instance. The Java garbage collector will eventually invoke
305    * the finalize() method on the instance.
306    *
307    * This method is called in an unspecified transaction context.
308    *
309    * @exception EJBException Thrown by the method to indicate a failure
310    * caused by a system-level error.
311    * @exception RemoteException - This exception is defined in the method signature
312    * to provide backward compatibility for enterprise beans
313    * written for the EJB 1.0 specification.
314    * Enterprise beans written for the EJB 1.1 and
315    * higher specification should throw the javax.ejb.EJBException
316    * instead of this exception.
317    */

318   public void unsetEntityContext() throws RemoteException
319   {
320     entityContext = null;
321   }
322
323   /**
324    * Returns true if the beans has been modified.
325    * It prevents the EJB server from reloading a bean
326    * that has not been modified.
327    *
328    * @return a <code>boolean</code> value
329    */

330   public boolean isModified()
331   {
332     return isDirty;
333   }
334
335
336   /**
337    * Display comment information as an HTML table row
338    *
339    * @return a <code>String</code> containing HTML code
340    * @exception RemoteException if an error occurs
341    * @since 1.0
342    */

343   public String JavaDoc printComment(String JavaDoc userName) throws RemoteException
344   {
345     return "<DT><b><BIG><a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.ViewUserInfo?userId="+fromUserId+"\">"+userName+"</a></BIG></b>"+
346       " wrote the "+date+"<DD><i>"+comment+"</i><p>\n";
347   }
348 }
349
Popular Tags