1 package edu.rice.rubis.beans; 2 3 import java.rmi.RemoteException ; 4 import javax.ejb.MessageDrivenBean ; 5 import javax.ejb.MessageDrivenContext ; 6 import javax.ejb.EJBException ; 7 import javax.jms.*; 8 import javax.naming.Context ; 9 import javax.naming.InitialContext ; 10 import javax.rmi.PortableRemoteObject ; 11 import javax.sql.DataSource ; 12 import java.sql.Connection ; 13 import java.sql.PreparedStatement ; 14 import java.sql.ResultSet ; 15 import java.sql.SQLException ; 16 import java.io.Serializable ; 17 import javax.transaction.UserTransaction ; 18 19 25 26 public class MDB_StoreComment implements MessageDrivenBean , MessageListener 27 { 28 30 private DataSource dataSource; 31 private MessageDrivenContext messageDrivenContext; 32 private TopicConnectionFactory connectionFactory; 33 private TopicConnection connection; 34 private Topic topic; 35 private TopicSession session; 36 private Context 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 comment = request.getString("comment"); 54 55 createComment(fromId, toId, itemId, rating, comment); 57 } 58 catch (Exception e) 59 { 60 throw new EJBException ("Message traitment failed for MDB_StoreComment: " +e); 61 } 62 } 63 64 74 public void createComment(int fromId, int toId, int itemId, int rating, String comment) throws RemoteException 75 { 76 PreparedStatement stmt = null; 77 ResultSet rs = null; 78 Connection conn = null; 79 80 try 82 { 83 try 85 { 86 String 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 e) 98 { 99 try { stmt.close(); } catch (Exception ignore) {} 100 try { conn.close(); } catch (Exception ignore) {} 101 throw new RemoteException ("Error while storing the comment (got exception: " +e+")<br>"); 102 } 103 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 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 e) 123 { 124 try { stmt.close(); } catch (Exception ignore) {} 125 try { conn.close(); } catch (Exception ignore) {} 126 throw new RemoteException ("Error while updating user's rating (got exception: " +e+")<br>"); 127 } 128 if (conn != null) conn.close(); 129 } 131 catch (Exception e) 132 { 133 try { conn.close(); } catch (Exception ignore) {} 134 throw new RemoteException ("Error while storing the comment (got exception: " +e+")<br>"); 138 } 144 } 145 146 148 159 public void setMessageDrivenContext(MessageDrivenContext ctx) 160 { 161 messageDrivenContext = ctx; 162 if (dataSource == null) 163 { 164 try 166 { 167 initialContext = new InitialContext (); 168 dataSource = (DataSource )initialContext.lookup("java:comp/env/jdbc/rubis"); 169 } 170 catch (Exception e) 171 { 172 throw new EJBException ("Cannot get JNDI InitialContext"); 173 } 174 } 175 } 176 177 181 public void ejbCreate() 182 { 183 184 } 185 186 195 public void ejbRemove() {} 196 197 198 199 } 200 | Popular Tags |