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_SearchItemsByRegion 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_SearchItemsByRegion() 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 Integer categoryId = new Integer (request.getInt("categoryId")); 49 Integer regionId = new Integer (request.getInt("regionId")); 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, regionId, 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_SearchItemsByRegion: " +e); 82 } 83 } 84 85 91 public String getItems(Integer categoryId, Integer regionId, int page, int nbOfItems) throws RemoteException 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 stmt = conn.prepareStatement("SELECT items.name, items.id, items.end_date, items.max_bid, items.nb_of_bids, items.initial_price FROM items,users WHERE items.category=? AND items.seller=users.id AND users.region=? AND end_date>=NOW() LIMIT ?,?"); 107 stmt.setInt(1, categoryId.intValue()); 108 stmt.setInt(2, regionId.intValue()); 109 stmt.setInt(3, page*nbOfItems); 110 stmt.setInt(4, nbOfItems); 111 rs = stmt.executeQuery(); 112 } 113 catch (SQLException e) 114 { 115 116 throw new RemoteException ("Failed to get the items: " +e); 117 } 118 try 119 { 120 while (rs.next()) 121 { 122 itemName = rs.getString("name"); 123 itemId = rs.getInt("id"); 124 endDate = rs.getString("end_date"); 125 maxBid = rs.getFloat("max_bid"); 126 nbOfBids = rs.getInt("nb_of_bids"); 127 initialPrice = rs.getFloat("initial_price"); 128 if (maxBid <initialPrice) 129 maxBid = initialPrice; 130 html.append(printItem(itemName, itemId, maxBid, nbOfBids, endDate)); 131 } 132 stmt.close(); 133 conn.close(); 134 } 135 catch (Exception e) 136 { 137 try 138 { 139 if (stmt != null) stmt.close(); 140 if (conn != null) conn.close(); 141 } 142 catch (Exception ignore) 143 { 144 } 145 throw new RemoteException ("Cannot get items list: " +e); 146 } 147 return html.toString(); 148 } 149 150 151 158 public String printItem(String name, int id, float maxBid, int nbOfBids, String endDate) throws RemoteException 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 182 public void setMessageDrivenContext(MessageDrivenContext ctx) 183 { 184 messageDrivenContext = ctx; 185 if (dataSource == null) 186 { 187 try 189 { 190 initialContext = new InitialContext (); 191 dataSource = (DataSource )initialContext.lookup("java:comp/env/jdbc/rubis"); 192 } 193 catch (Exception e) 194 { 195 throw new EJBException ("Cannot get JNDI InitialContext"); 196 } 197 } 198 } 199 200 204 public void ejbCreate() 205 { 206 207 } 208 209 218 public void ejbRemove() {} 219 220 221 } 222 | Popular Tags |