KickJava   Java API By Example, From Geeks To Geeks.

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


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  * UserBean 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 users (
14  * id INTEGER UNSIGNED NOT NULL UNIQUE,
15  * firstname VARCHAR(20),
16  * lastname VARCHAR(20),
17  * nickname VARCHAR(20) NOT NULL UNIQUE,
18  * password VARCHAR(20) NOT NULL,
19  * email VARCHAR(50) NOT NULL,
20  * rating INTEGER,
21  * balance FLOAT,
22  * creation_date DATETIME,
23  * region INTEGER,
24  * PRIMARY KEY(id),
25  * INDEX auth (nickname,password),
26  * INDEX region_id (region)
27  * );
28  * </pre>
29  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
30  * @version 1.0
31  */

32
33 public class UserBean implements EntityBean
34 {
35   private EntityContext entityContext;
36   private transient boolean isDirty; // used for the isModified function
37

38   /* Class member variables */
39
40   public Integer JavaDoc id;
41   public String JavaDoc firstName;
42   public String JavaDoc lastName;
43   public String JavaDoc nickName;
44   public String JavaDoc password;
45   public String JavaDoc email;
46   public int rating;
47   public float balance;
48   public String JavaDoc creationDate;
49   public Integer JavaDoc regionId;
50
51
52   /**
53    * Get user's id.
54    *
55    * @return user id
56    * @exception RemoteException if an error occurs
57    */

58   public Integer JavaDoc getId() throws RemoteException
59   {
60     return id;
61   }
62
63   /**
64    * Get user first name.
65    *
66    * @return user first name
67    * @exception RemoteException if an error occurs
68    */

69   public String JavaDoc getFirstName() throws RemoteException
70   {
71     return firstName;
72   }
73
74   /**
75    * Get user last name.
76    *
77    * @return user last name
78    * @exception RemoteException if an error occurs
79    */

80   public String JavaDoc getLastName() throws RemoteException
81   {
82     return lastName;
83   }
84
85   /**
86    * Get user nick name. This name is unique for each user and is used for login.
87    *
88    * @return user nick name
89    * @exception RemoteException if an error occurs
90    */

91   public String JavaDoc getNickName() throws RemoteException
92   {
93     return nickName;
94   }
95
96   /**
97    * Get user password.
98    *
99    * @return user password
100    * @exception RemoteException if an error occurs
101    */

102   public String JavaDoc getPassword() throws RemoteException
103   {
104     return password;
105   }
106
107   /**
108    * Get user email address.
109    *
110    * @return user email address
111    * @exception RemoteException if an error occurs
112    */

113   public String JavaDoc getEmail() throws RemoteException
114   {
115     return email;
116   }
117
118   /**
119    * Get user rating. The higher the rating is, the most reliable the user is.
120    *
121    * @return user rating
122    * @exception RemoteException if an error occurs
123    */

124   public int getRating() throws RemoteException
125   {
126     return rating;
127   }
128   
129   /**
130    * Get user's account current balance. This account is used when a user want to sell items.
131    * There is a charge for each item to sell.
132    *
133    * @return user's account current balance
134    * @exception RemoteException if an error occurs
135    */

136   public float getBalance() throws RemoteException
137   {
138     return balance;
139   }
140
141   /**
142    * Get user creation date.
143    *
144    * @return user creation date
145    * @exception RemoteException if an error occurs
146    */

147   public String JavaDoc getCreationDate() throws RemoteException
148   {
149     return creationDate;
150   }
151
152   /**
153    * Get region identifier of user's region.
154    *
155    * @return region id of the user
156    * @exception RemoteException if an error occurs
157    */

158   public Integer JavaDoc getRegionId() throws RemoteException
159   {
160     return regionId;
161   }
162
163
164   /**
165    * Set user's first name
166    *
167    * @param newName user first name
168    * @exception RemoteException if an error occurs
169    */

170   public void setFirstName(String JavaDoc newName) throws RemoteException
171   {
172     firstName = newName;
173     isDirty = true; // the bean content has been modified
174
}
175
176   /**
177    * Set user's last name
178    *
179    * @param newName user last name
180    * @exception RemoteException if an error occurs
181    */

182   public void setLastName(String JavaDoc newName) throws RemoteException
183   {
184     lastName = newName;
185     isDirty = true; // the bean content has been modified
186
}
187
188   /**
189    * Set user's nick name
190    *
191    * @param newName user nick name
192    * @exception RemoteException if an error occurs
193    */

194   public void setNickName(String JavaDoc newName) throws RemoteException
195   {
196     nickName = newName;
197     isDirty = true; // the bean content has been modified
198
}
199
200   /**
201    * Set user's password
202    *
203    * @param newPassword a <code>String</code> value
204    * @exception RemoteException if an error occurs
205    */

206   public void setPassword(String JavaDoc newPassword) throws RemoteException
207   {
208     password = newPassword;
209     isDirty = true; // the bean content has been modified
210
}
211
212   /**
213    * Set user's email address
214    *
215    * @param newEmail a <code>String</code> value
216    * @exception RemoteException if an error occurs
217    */

218   public void setEmail(String JavaDoc newEmail) throws RemoteException
219   {
220     email = newEmail;
221     isDirty = true; // the bean content has been modified
222
}
223
224   /**
225    * Set a new creation date for this user account
226    *
227    * @param newCreationDate a <code>String</code> value
228    * @exception RemoteException if an error occurs
229    */

230   public void setCreationDate(String JavaDoc newCreationDate) throws RemoteException
231   {
232     creationDate = newCreationDate;
233     isDirty = true; // the bean content has been modified
234
}
235
236   /**
237    * Set a new region identifier. This id must match
238    * the primary key of the region table.
239    *
240    * @param id region id
241    * @exception RemoteException if an error occurs
242    */

243   public void setRegionId(Integer JavaDoc id) throws RemoteException
244   {
245     regionId = id;
246     isDirty = true; // the bean content has been modified
247
}
248
249   /**
250    * Set user rating. The higher the rating is, the most reliable the user is.
251    *
252    * @param newRating new user rating
253    * @exception RemoteException if an error occurs
254    */

255   public void setRating(int newRating) throws RemoteException
256   {
257     rating = newRating;
258     isDirty = true; // the bean content has been modified
259
}
260
261   /**
262    * Update the current rating by adding a new value to it. This value can
263    * be negative if someone wants to decrease the user rating.
264    *
265    * @param diff value to add to the rating
266    * @exception RemoteException if an error occurs
267    */

268   public void updateRating(int diff) throws RemoteException
269   {
270     rating += diff;
271     isDirty = true; // the bean content has been modified
272
}
273   
274   /**
275    * Set user's account current balance. This account is used when a user want to sell items.
276    * There is a charge for each sold item.
277    *
278    * @param newBalance set user's account current balance
279    * @exception RemoteException if an error occurs
280    */

281   public void setBalance(float newBalance) throws RemoteException
282   {
283     balance = newBalance;
284     isDirty = true; // the bean content has been modified
285
}
286
287
288   /**
289    * Returns a string displaying general information about the user.
290    * The string contains HTML tags.
291    *
292    * @return string containing general user information
293    * @exception RemoteException if an error occurs
294    */

295   public String JavaDoc getHTMLGeneralUserInformation() throws RemoteException
296   {
297     String JavaDoc result = new String JavaDoc();
298
299     result = result+"<h2>Information about "+nickName+"<br></h2>";
300     result = result+"Real life name : "+firstName+" "+lastName+"<br>";
301     result = result+"Email address : "+email+"<br>";
302     result = result+"User since : "+creationDate+"<br>";
303     result = result+"Current rating : <b>"+rating+"</b><br>";
304     return result;
305   }
306
307
308   // =============================== EJB methods ===================================
309

310   /**
311    * This method is used to create a new User Bean. The user id and the creationDate
312    * are automatically set by the system.
313    *
314    * @param userFirstName user's first name
315    * @param userLastName user's last name
316    * @param userNickName user's nick name
317    * @param userEmail email address of the user
318    * @param userPassword user's password
319    * @param userRegionId region id where the user lives
320    *
321    * @return pk primary key set to null
322    *
323    * @exception CreateException if an error occurs
324    * @exception RemoteException if an error occurs
325    * @exception RemoveException if an error occurs
326    */

327   public UserPK ejbCreate(String JavaDoc userFirstName, String JavaDoc userLastName, String JavaDoc userNickName, String JavaDoc userEmail,
328                           String JavaDoc userPassword, Integer JavaDoc userRegionId) throws CreateException, RemoteException, RemoveException
329   {
330      // Connecting to IDManager Home interface thru JNDI
331
IDManagerHome home = null;
332       IDManager idManager = null;
333       
334       try
335       {
336         InitialContext JavaDoc initialContext = new InitialContext JavaDoc();
337         home = (IDManagerHome)PortableRemoteObject.narrow(initialContext.lookup(
338                "java:comp/env/ejb/IDManager"), IDManagerHome.class);
339       }
340       catch (Exception JavaDoc e)
341       {
342         throw new EJBException("Cannot lookup IDManager: " +e);
343       }
344      try
345      {
346        IDManagerPK idPK = new IDManagerPK();
347        idManager = home.findByPrimaryKey(idPK);
348        id = idManager.getNextUserID();
349        firstName = userFirstName;
350        lastName = userLastName;
351        nickName = userNickName;
352        password = userPassword;
353        email = userEmail;
354        regionId = userRegionId;
355        creationDate = TimeManagement.currentDateToString();
356      }
357       catch (Exception JavaDoc e)
358       {
359         throw new EJBException("Cannot create user: " +e);
360       }
361     return null;
362   }
363
364
365   /** This method just set an internal flag to
366       reload the id generated by the DB */

367   public void ejbPostCreate(String JavaDoc userFirstName, String JavaDoc userLastName, String JavaDoc userNickName, String JavaDoc userEmail,
368                             String JavaDoc userPassword, Integer JavaDoc userRegionId)
369   {
370     isDirty = true; // the id has to be reloaded from the DB
371
}
372
373   /** Persistence is managed by the container and the bean
374       becomes up to date */

375   public void ejbLoad() throws RemoteException
376   {
377     isDirty = false;
378   }
379
380   /** Persistence is managed by the container and the bean
381       becomes up to date */

382   public void ejbStore() throws RemoteException
383   {
384     isDirty = false;
385   }
386
387   /** This method is empty because persistence is managed by the container */
388   public void ejbActivate() throws RemoteException {}
389   /** This method is empty because persistence is managed by the container */
390   public void ejbPassivate() throws RemoteException {}
391   /** This method is empty because persistence is managed by the container */
392   public void ejbRemove() throws RemoteException, RemoveException {}
393
394   /**
395    * Sets the associated entity context. The container invokes this method
396    * on an instance after the instance has been created.
397    *
398    * This method is called in an unspecified transaction context.
399    *
400    * @param context An EntityContext interface for the instance. The instance should
401    * store the reference to the context in an instance variable.
402    * @exception EJBException Thrown by the method to indicate a failure
403    * caused by a system-level error.
404    * @exception RemoteException - This exception is defined in the method signature
405    * to provide backward compatibility for enterprise beans
406    * written for the EJB 1.0 specification.
407    * Enterprise beans written for the EJB 1.1 and
408    * higher specification should throw the javax.ejb.EJBException
409    * instead of this exception.
410    */

411   public void setEntityContext(EntityContext context) throws RemoteException
412   {
413     entityContext = context;
414   }
415
416   /**
417    * Unsets the associated entity context. The container calls this method
418    * before removing the instance. This is the last method that the container
419    * invokes on the instance. The Java garbage collector will eventually invoke
420    * the finalize() method on the instance.
421    *
422    * This method is called in an unspecified transaction context.
423    *
424    * @exception EJBException Thrown by the method to indicate a failure
425    * caused by a system-level error.
426    * @exception RemoteException - This exception is defined in the method signature
427    * to provide backward compatibility for enterprise beans
428    * written for the EJB 1.0 specification.
429    * Enterprise beans written for the EJB 1.1 and
430    * higher specification should throw the javax.ejb.EJBException
431    * instead of this exception.
432    */

433   public void unsetEntityContext() throws RemoteException
434   {
435     entityContext = null;
436   }
437
438
439   /**
440    * Returns true if the beans has been modified.
441    * It prevents the EJB server from reloading a bean
442    * that has not been modified.
443    *
444    * @return a <code>boolean</code> value
445    */

446   public boolean isModified()
447   {
448     return isDirty;
449   }
450
451 }
452
Popular Tags