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 18 24 25 public class MDB_BuyNow implements MessageDrivenBean , MessageListener 26 { 27 private DataSource dataSource; 28 private MessageDrivenContext messageDrivenContext; 29 private TopicConnectionFactory connectionFactory; 30 private TopicConnection connection; 31 private Topic topic; 32 private TopicSession session; 33 private TopicPublisher replier; 34 private Context initialContext = null; 35 36 37 public MDB_BuyNow() 38 { 39 40 } 41 42 public void onMessage(Message message) 43 { 44 try 45 { 46 MapMessage request = (MapMessage)message; 47 String correlationID = request.getJMSCorrelationID(); 48 int itemId = request.getInt("itemId"); 49 String username = request.getString("username"); 50 String password = request.getString("password"); 51 52 connectionFactory = (TopicConnectionFactory) initialContext.lookup(BeanConfig.TopicConnectionFactoryName); 54 55 String html = getBuyNowForm(itemId, username, password); 57 58 TemporaryTopic temporaryTopic = (TemporaryTopic) request.getJMSReplyTo(); 60 if (temporaryTopic != null) 61 { 62 connection = connectionFactory.createTopicConnection(); 64 session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); 66 TextMessage reply = session.createTextMessage(); 67 reply.setJMSCorrelationID(correlationID); 68 reply.setText(html); 69 replier = session.createPublisher(null); connection.start(); 71 replier.publish(temporaryTopic, reply); 72 replier.close(); 73 session.close(); 74 connection.stop(); 75 connection.close(); 76 } 77 } 78 catch (Exception e) 79 { 80 throw new EJBException ("Message traitment failed for MDB_BuyNow: " +e); 81 } 82 } 83 84 90 public String getBuyNowForm(int itemId, String username, String password) throws RemoteException 91 { 92 int userId = -1; 93 StringBuffer html = new StringBuffer (); 94 PreparedStatement stmt = null; 95 ResultSet rs = null; 96 Connection conn = null; 97 98 if ((username != null && !username.equals("")) || (password != null && !password.equals(""))) 100 { 101 TopicConnection authConnection; 102 TopicSession authSession; 103 Topic authTopic; 104 try 105 { 106 authConnection = connectionFactory.createTopicConnection(); 108 authTopic = (Topic) initialContext.lookup(BeanConfig.PrefixTopicName+"topicAuth"); 110 authSession = authConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); } 113 catch (Exception e) 114 { 115 throw new EJBException ("Cannot connect to message bean MDB_Auth : " +e+"<br>"); 116 } 117 try 118 { 119 TopicRequestor requestor = new TopicRequestor(authSession, authTopic); 121 MapMessage m = authSession.createMapMessage(); 123 m.setStringProperty("nickname", username); 125 m.setStringProperty("password", password); 126 m.setJMSCorrelationID("auth"); 127 authConnection.start(); MapMessage authReply = (MapMessage)requestor.request(m); 130 authConnection.stop(); 131 userId = authReply.getInt("userId"); 133 requestor.close(); authConnection.close(); 136 } 137 catch (Exception e) 138 { 139 throw new EJBException ("user authentication failed: " +e+"<br>"); 140 } 141 if (userId == -1) 142 { 143 html.append("You don't have an account on RUBiS!<br>You have to register first.<br>"); 144 return html.toString(); 145 } 146 } 147 String itemName = null, description = null; 149 String startDate = null, endDate = null, sellerName = null; 150 int quantity = 0, sellerId = -1; 151 float buyNow = 0; 152 try 153 { 154 conn = dataSource.getConnection(); 155 stmt = conn.prepareStatement("SELECT * FROM items WHERE id=?"); 156 stmt.setInt(1, itemId); 157 rs = stmt.executeQuery(); 158 if (rs.first()) 159 { 160 itemName = rs.getString("name"); 161 description = rs.getString("description"); 162 startDate = rs.getString("start_date"); 163 endDate = rs.getString("end_date"); 164 buyNow = rs.getFloat("buy_now"); 165 quantity = rs.getInt("quantity"); 166 sellerId = rs.getInt("seller"); 167 } 168 stmt.close(); 169 } 170 catch (Exception e) 171 { 172 try 173 { 174 if (stmt != null) stmt.close(); 175 if (conn != null) conn.close(); 176 } 177 catch (Exception ignore) 178 { 179 } 180 throw new RemoteException ("Failed to execute Query for item: " +e); 181 } 182 183 try 184 { 185 stmt = conn.prepareStatement("SELECT nickname FROM users WHERE id=?"); 186 stmt.setInt(1, sellerId); 187 ResultSet srs = stmt.executeQuery(); 188 if (srs.first()) 189 { 190 sellerName = srs.getString("nickname"); 191 } 192 stmt.close(); 193 conn.close(); 194 } 195 catch (SQLException s) 196 { 197 try 198 { 199 if (stmt != null) stmt.close(); 200 if (conn != null) conn.close(); 201 } 202 catch (Exception ignore) 203 { 204 } 205 throw new RemoteException ("Failed to execute Query for seller: " +s); 206 } 207 208 html.append("<TABLE width=\"100%\" bgcolor=\"#CCCCFF\">\n<TR><TD align=\"center\" width=\"100%\"><FONT size=\"4\" color=\"#000000\"><B>You are ready to buy this item: "+itemName+"</B></FONT></TD></TR>\n</TABLE><p>\n"); 210 try 211 { 212 html.append(printItemDescriptionToBuyNow(itemId, itemName, description, buyNow, quantity, sellerId, sellerName, startDate, endDate, userId)); 213 } 214 catch (Exception e) 215 { 216 throw new RemoteException ("Unable to print Item description: " +e); 217 } 218 219 return html.toString(); 220 } 221 222 229 public String printItemDescriptionToBuyNow(int itemId, String itemName, String description, float buyNow, int quantity, int sellerId, String sellerName, String startDate, String endDate, int userId) throws RemoteException 230 { 231 StringBuffer result = new StringBuffer ("<TABLE>\n"+"<TR><TD>Quantity<TD><b><BIG>"+quantity+"</BIG></b>\n"); 232 result.append("<TR><TD>Seller<TD><a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.ViewUserInfo?userId="+sellerId+"\">"+ 233 sellerName+"</a> (<a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.PutCommentAuth?to="+sellerId+"&itemId="+itemId+"\">Leave a comment on this user</a>)\n"+ 234 "<TR><TD>Started<TD>"+startDate+"\n"+"<TR><TD>Ends<TD>"+endDate+"\n"+ 235 "</TABLE>"+ 236 "<TABLE width=\"100%\" bgcolor=\"#CCCCFF\">\n"+ 237 "<TR><TD align=\"center\" width=\"100%\"><FONT size=\"4\" color=\"#000000\"><B>Item description</B></FONT></TD></TR>\n"+ 238 "</TABLE><p>\n"+description+"<br><p>\n"+ 239 "<TABLE width=\"100%\" bgcolor=\"#CCCCFF\">\n"+ 240 "<TR><TD align=\"center\" width=\"100%\"><FONT size=\"4\" color=\"#000000\"><B>Buy Now</B></FONT></TD></TR>\n"+ 241 "</TABLE><p>\n"+ 242 "<form action=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.StoreBuyNow\" method=POST>\n"+ 243 "<input type=hidden name=userId value="+userId+">\n"+ 244 "<input type=hidden name=itemId value="+itemId+">\n"+ 245 "<input type=hidden name=maxQty value="+quantity+">\n"); 246 if (quantity > 1) 247 result.append("<center><table><tr><td>Quantity:</td>\n"+ 248 "<td><input type=text size=5 name=qty></td></tr></table></center>\n"); 249 else 250 result.append("<input type=hidden name=qty value=1>\n"); 251 result.append("<p><input type=submit value=\"Buy now!\"></center><p>\n"); 252 return result.toString(); 253 } 254 255 257 268 public void setMessageDrivenContext(MessageDrivenContext ctx) 269 { 270 messageDrivenContext = ctx; 271 if (dataSource == null) 272 { 273 try 275 { 276 initialContext = new InitialContext (); 277 dataSource = (DataSource )initialContext.lookup("java:comp/env/jdbc/rubis"); 278 } 279 catch (Exception e) 280 { 281 throw new EJBException ("Cannot get JNDI InitialContext"); 282 } 283 } 284 } 285 286 290 public void ejbCreate() 291 { 292 293 } 294 295 304 public void ejbRemove() {} 305 306 307 308 } 309 | Popular Tags |