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 java.net.URLEncoder ; 18 19 25 26 public class MDB_SearchItemsByCategory implements MessageDrivenBean , MessageListener 27 { 28 private DataSource dataSource; 29 private MessageDrivenContext messageDrivenContext; 30 private TopicConnectionFactory connectionFactory; 31 private TopicConnection connection; 32 private Topic topic; 33 private TopicSession session; 34 private TopicPublisher replier; 35 private Context initialContext = null; 36 37 public MDB_SearchItemsByCategory() 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 id = request.getInt("categoryId"); 49 Integer categoryId = new Integer (id); 50 int page = request.getInt("page"); 51 int nbOfItems = request.getInt("nbItems"); 52 53 connectionFactory = (TopicConnectionFactory) initialContext.lookup(BeanConfig.TopicConnectionFactoryName); 55 56 String html = getItems(categoryId, page, nbOfItems); 58 59 TemporaryTopic temporaryTopic = (TemporaryTopic) request.getJMSReplyTo(); 61 if (temporaryTopic != null) 62 { 63 connection = connectionFactory.createTopicConnection(); 65 session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); 67 TextMessage reply = session.createTextMessage(); 68 reply.setJMSCorrelationID(correlationID); 69 reply.setText(html); 70 replier = session.createPublisher(null); connection.start(); 72 replier.publish(temporaryTopic, reply); 73 replier.close(); 74 session.close(); 75 connection.stop(); 76 connection.close(); 77 } 78 } 79 catch (Exception e) 80 { 81 throw new EJBException ("Message traitment failed for MDB_SearchItemsByCategory: " +e); 82 } 83 } 84 85 91 public String getItems(Integer categoryId, int page, int nbOfItems) 92 { 93 Connection conn = null; 94 PreparedStatement stmt = null; 95 ResultSet rs = null; 96 String itemName, endDate; 97 int itemId; 98 float maxBid, initialPrice; 99 int nbOfBids=0; 100 StringBuffer html = new StringBuffer (); 101 102 try 104 { 105 conn = dataSource.getConnection(); 106 107 stmt = conn.prepareStatement("SELECT items.name, items.id, items.end_date, items.max_bid, items.nb_of_bids, items.initial_price FROM items WHERE items.category=? AND end_date>=NOW() LIMIT ?,?"); 108 stmt.setInt(1, categoryId.intValue()); 109 stmt.setInt(2, page*nbOfItems); 110 stmt.setInt(3, nbOfItems); 111 rs = stmt.executeQuery(); 112 113 } 114 catch (SQLException e) 115 { 116 117 throw new EJBException ("Failed to get the items: " +e); 118 } 119 try 120 { 121 while (rs.next()) 122 { 123 itemName = rs.getString("name"); 124 itemId = rs.getInt("id"); 125 endDate = rs.getString("end_date"); 126 maxBid = rs.getFloat("max_bid"); 127 nbOfBids = rs.getInt("nb_of_bids"); 128 initialPrice = rs.getFloat("initial_price"); 129 if (maxBid <initialPrice) 130 maxBid = initialPrice; 131 html.append(printItem(itemName, itemId, maxBid, nbOfBids, endDate)); 132 } 133 stmt.close(); 134 conn.close(); 135 } 136 catch (Exception e) 137 { 138 try 139 { 140 if (stmt != null) stmt.close(); 141 if (conn != null) conn.close(); 142 } 143 catch (Exception ignore) 144 { 145 } 146 throw new EJBException ("Cannot get items list: " +e); 147 } 148 return html.toString(); 149 } 150 151 152 158 public String printItem(String name, int id, float maxBid, int nbOfBids, String endDate) 159 { 160 return "<TR><TD><a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.ViewItem?itemId="+id+"\">"+name+ 161 "<TD>"+maxBid+ 162 "<TD>"+nbOfBids+ 163 "<TD>"+endDate+ 164 "<TD><a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.PutBidAuth?itemId="+id+"\"><IMG SRC=\""+BeanConfig.context+"/bid_now.jpg\" height=22 width=90></a>\n"; 165 } 166 167 168 169 170 172 183 public void setMessageDrivenContext(MessageDrivenContext ctx) 184 { 185 messageDrivenContext = ctx; 186 if (dataSource == null) 187 { 188 try 190 { 191 initialContext = new InitialContext (); 192 dataSource = (DataSource )initialContext.lookup("java:comp/env/jdbc/rubis"); 193 } 194 catch (Exception e) 195 { 196 throw new EJBException ("Cannot get JNDI InitialContext"); 197 } 198 } 199 } 200 201 205 public void ejbCreate() 206 { 207 208 } 209 210 219 public void ejbRemove() {} 220 221 222 } 223 | Popular Tags |