KickJava   Java API By Example, From Geeks To Geeks.

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


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

25
26 public class MDB_StoreComment implements MessageDrivenBean JavaDoc, MessageListener
27 {
28   // private UserTransaction utx = null;
29

30   private DataSource JavaDoc dataSource;
31   private MessageDrivenContext JavaDoc messageDrivenContext;
32   private TopicConnectionFactory connectionFactory;
33   private TopicConnection connection;
34   private Topic topic;
35   private TopicSession session;
36   private Context JavaDoc initialContext = null;
37
38
39   public MDB_StoreComment()
40   {
41
42   }
43
44   public void onMessage(Message message)
45   {
46     try
47     {
48       MapMessage request = (MapMessage)message;
49       int fromId = request.getInt("fromId");
50       int toId = request.getInt("toId");
51       int itemId = request.getInt("itemId");
52       int rating = request.getInt("rating");
53       String JavaDoc comment = request.getString("comment");
54
55        // add the new comment in the database
56
createComment(fromId, toId, itemId, rating, comment);
57     }
58     catch (Exception JavaDoc e)
59     {
60       throw new EJBException JavaDoc("Message traitment failed for MDB_StoreComment: " +e);
61     }
62   }
63
64   /**
65    * Create a new comment and update the rating of the user.
66    *
67    * @param fromId id of the user posting the comment
68    * @param toId id of the user who is the subject of the comment
69    * @param itemId id of the item related to the comment
70    * @param rating value of the rating for the user
71    * @param comment text of the comment
72    * @since 1.1
73    */

74   public void createComment(int fromId, int toId, int itemId, int rating, String JavaDoc comment) throws RemoteException JavaDoc
75   {
76     PreparedStatement JavaDoc stmt = null;
77     ResultSet JavaDoc rs = null;
78     Connection JavaDoc conn = null;
79
80       // utx = messageDrivenContext.getUserTransaction(); //bean managed transaction
81
try
82     {
83        // utx.begin();
84
try
85       {
86         // create new comment
87
String JavaDoc now = TimeManagement.currentDateToString();
88         conn = dataSource.getConnection();
89         stmt = conn.prepareStatement("INSERT INTO comments VALUES (NULL, \""+
90                                      fromId+
91                                      "\", \""+toId+"\", \""+itemId+
92                                      "\", \""+ rating+"\", \""+now+"\",\""+comment+"\")");
93
94         stmt.executeUpdate();
95         stmt.close();
96       }
97       catch (SQLException JavaDoc e)
98       {
99         try { stmt.close(); } catch (Exception JavaDoc ignore) {}
100         try { conn.close(); } catch (Exception JavaDoc ignore) {}
101        throw new RemoteException JavaDoc("Error while storing the comment (got exception: " +e+")<br>");
102       }
103       // Try to find the user corresponding to the 'to' ID
104
try
105       {
106         stmt = conn.prepareStatement("SELECT rating FROM users WHERE id=?");
107         stmt.setInt(1, toId);
108         rs = stmt.executeQuery();
109         if (rs.first())
110         {
111           int userRating = rs.getInt("rating");
112           userRating = userRating + rating;
113  
114           PreparedStatement JavaDoc userStmt = conn.prepareStatement("UPDATE users SET rating=? WHERE id=?");
115           userStmt.setInt(1, userRating);
116           userStmt.setInt(2, toId);
117           userStmt.executeUpdate();
118           userStmt.close();
119         }
120         stmt.close();
121       }
122       catch (SQLException JavaDoc e)
123       {
124         try { stmt.close(); } catch (Exception JavaDoc ignore) {}
125         try { conn.close(); } catch (Exception JavaDoc ignore) {}
126         throw new RemoteException JavaDoc("Error while updating user's rating (got exception: " +e+")<br>");
127       }
128       if (conn != null) conn.close();
129        // utx.commit();
130
}
131     catch (Exception JavaDoc e)
132     {
133       try { conn.close(); } catch (Exception JavaDoc ignore) {}
134       // try
135
// {
136
// utx.rollback();
137
throw new RemoteException JavaDoc("Error while storing the comment (got exception: " +e+")<br>");
138        // }
139
// catch (Exception se)
140
// {
141
// throw new RemoteException("Transaction rollback failed: " + e +"<br>");
142
// }
143
}
144   }
145
146   // ======================== EJB related methods ============================
147

148   /**
149    * Set the associated context. The container call this method
150    * after the instance creation.
151    * The enterprise Bean instance should store the reference to the context
152    * object in an instance variable.
153    * This method is called with no transaction context.
154    *
155    * @param MessageDrivenContext A MessageDrivenContext interface for the instance.
156    * @throws EJBException Thrown by the method to indicate a failure caused by
157    * a system-level error.
158    */

159   public void setMessageDrivenContext(MessageDrivenContext JavaDoc ctx)
160   {
161     messageDrivenContext = ctx;
162     if (dataSource == null)
163     {
164       // Finds DataSource from JNDI
165
try
166       {
167         initialContext = new InitialContext JavaDoc();
168         dataSource = (DataSource JavaDoc)initialContext.lookup("java:comp/env/jdbc/rubis");
169       }
170       catch (Exception JavaDoc e)
171       {
172         throw new EJBException JavaDoc("Cannot get JNDI InitialContext");
173       }
174     }
175   }
176
177   /**
178    * The Message driven bean must define an ejbCreate methods with no args.
179    *
180    */

181   public void ejbCreate()
182   {
183
184   }
185  
186   /**
187    * A container invokes this method before it ends the life of the message-driven object.
188    * This happens when a container decides to terminate the message-driven object.
189    *
190    * This method is called with no transaction context.
191    *
192    * @throws EJBException Thrown by the method to indicate a failure caused by
193    * a system-level error.
194    */

195   public void ejbRemove() {}
196
197  
198
199 }
200
Popular Tags