KickJava   Java API By Example, From Geeks To Geeks.

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


1 package edu.rice.rubis.beans;
2
3 import java.rmi.RemoteException JavaDoc;
4 import javax.ejb.SessionBean JavaDoc;
5 import javax.ejb.SessionContext JavaDoc;
6 import javax.ejb.FinderException JavaDoc;
7 import javax.ejb.ObjectNotFoundException JavaDoc;
8 import javax.ejb.CreateException JavaDoc;
9 import javax.ejb.RemoveException JavaDoc;
10 import javax.ejb.EJBException JavaDoc;
11 import javax.naming.Context JavaDoc;
12 import javax.naming.InitialContext JavaDoc;
13 import javax.rmi.PortableRemoteObject JavaDoc;
14 import javax.sql.DataSource JavaDoc;
15 import java.io.Serializable JavaDoc;
16 import javax.transaction.UserTransaction JavaDoc;
17
18 /**
19  * This is a stateless session bean used to create e new comment for a user.
20  *
21  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
22  * @version 1.1
23  */

24
25 public class SB_StoreCommentBean implements SessionBean JavaDoc
26 {
27   protected SessionContext JavaDoc sessionContext;
28   protected Context JavaDoc initialContext = null;
29   protected DataSource JavaDoc dataSource = null;
30   private UserTransaction JavaDoc utx = null;
31
32   /**
33    * Create a new comment and update the rating of the user.
34    *
35    * @param fromId id of the user posting the comment
36    * @param toId id of the user who is the subject of the comment
37    * @param itemId id of the item related to the comment
38    * @param rating value of the rating for the user
39    * @param comment text of the comment
40    * @since 1.1
41    */

42   public void createComment(Integer JavaDoc fromId, Integer JavaDoc toId, Integer JavaDoc itemId, int rating, String JavaDoc comment) throws RemoteException JavaDoc
43   {
44     // Try to find the user corresponding to the 'to' ID
45
User to;
46     try
47     {
48       UserHome uHome = (UserHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/User"),
49                                                     UserHome.class);
50       to = uHome.findByPrimaryKey(new UserPK(toId));
51     }
52     catch (Exception JavaDoc e)
53     {
54       throw new RemoteException JavaDoc("Cannot lookup User ("+toId+"): " +e+"<br>");
55     }
56     CommentHome cHome;
57     try
58     {
59       cHome = (CommentHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/Comment"),
60                                                        CommentHome.class);
61     }
62     catch (Exception JavaDoc e)
63     {
64       throw new RemoteException JavaDoc("Cannot lookup Comment: " +e+"<br>");
65     }
66     utx = sessionContext.getUserTransaction();
67     try
68     {
69       utx.begin();
70       Comment c = cHome.create(fromId, toId, itemId, rating, comment);
71       to.updateRating(rating);
72       utx.commit();
73     }
74     catch (Exception JavaDoc e)
75     {
76       try
77       {
78         utx.rollback();
79         throw new RemoteException JavaDoc("Error while storing the comment (got exception: " +e+")<br>");
80       }
81       catch (Exception JavaDoc se)
82       {
83         throw new RemoteException JavaDoc("Transaction rollback failed: " + e +"<br>");
84       }
85     }
86         
87   }
88
89
90
91   // ======================== EJB related methods ============================
92

93   /**
94    * This method is empty for a stateless session bean
95    */

96   public void ejbCreate() throws CreateException JavaDoc, RemoteException JavaDoc
97   {
98   }
99
100   /** This method is empty for a stateless session bean */
101   public void ejbActivate() throws RemoteException JavaDoc {}
102   /** This method is empty for a stateless session bean */
103   public void ejbPassivate() throws RemoteException JavaDoc {}
104   /** This method is empty for a stateless session bean */
105   public void ejbRemove() throws RemoteException JavaDoc {}
106
107
108   /**
109    * Sets the associated session context. The container calls this method
110    * after the instance creation. This method is called with no transaction context.
111    * We also retrieve the Home interfaces of all RUBiS's beans.
112    *
113    * @param sessionContext - A SessionContext interface for the instance.
114    * @exception RemoteException - Thrown if the instance could not perform the function
115    * requested by the container because of a system-level error.
116    */

117   public void setSessionContext(SessionContext JavaDoc sessionContext) throws RemoteException JavaDoc
118   {
119     this.sessionContext = sessionContext;
120     if (dataSource == null)
121     {
122       // Finds DataSource from JNDI
123

124       try
125       {
126         initialContext = new InitialContext JavaDoc();
127         dataSource = (DataSource JavaDoc)initialContext.lookup("java:comp/env/jdbc/rubis");
128       }
129       catch (Exception JavaDoc e)
130       {
131         throw new RemoteException JavaDoc("Cannot get JNDI InitialContext");
132       }
133     }
134   }
135
136 }
137
Popular Tags