KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > rubis > servlets > PutBid


1 package edu.rice.rubis.servlets;
2
3 import java.io.IOException JavaDoc;
4 import java.sql.Connection JavaDoc;
5 import java.sql.PreparedStatement JavaDoc;
6 import java.sql.ResultSet JavaDoc;
7 import java.sql.SQLException JavaDoc;
8
9 import javax.servlet.ServletException JavaDoc;
10 import javax.servlet.http.HttpServletRequest JavaDoc;
11 import javax.servlet.http.HttpServletResponse JavaDoc;
12
13 /** This servlets display the page allowing a user to put a bid
14  * on an item.
15  * It must be called this way :
16  * <pre>
17  * http://..../PutBid?itemId=xx&nickname=yy&password=zz
18  * where xx is the id of the item
19  * yy is the nick name of the user
20  * zz is the user password
21  * /<pre>
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.0
24  */

25
26 public class PutBid extends RubisHttpServlet
27 {
28  
29
30   public int getPoolSize()
31   {
32     return Config.PutBidPoolSize;
33   }
34
35 /**
36  * Close both statement and connection.
37  */

38   private void closeConnection(PreparedStatement JavaDoc stmt, Connection JavaDoc conn)
39   {
40     try
41     {
42       if (stmt != null)
43         stmt.close(); // close statement
44
if (conn != null)
45         releaseConnection(conn);
46
47     }
48     catch (Exception JavaDoc ignore)
49     {
50     }
51   }
52
53 /**
54  * Display an error message.
55  * @param errorMsg the error message value
56  */

57   private void printError(String JavaDoc errorMsg, ServletPrinter sp)
58   {
59     sp.printHTMLheader("RUBiS ERROR: PutBid");
60     sp.printHTML(
61       "<h2>Your request has not been processed due to the following error :</h2><br>");
62     sp.printHTML(errorMsg);
63     sp.printHTMLfooter();
64     
65   }
66
67   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
68     throws IOException JavaDoc, ServletException JavaDoc
69   {
70      ServletPrinter sp = null;
71      
72     String JavaDoc itemStr = request.getParameter("itemId");
73     String JavaDoc name = request.getParameter("nickname");
74     String JavaDoc pass = request.getParameter("password");
75     sp = new ServletPrinter(response, "PubBid");
76
77     if ((itemStr == null)
78       || (itemStr.equals(""))
79       || (name == null)
80       || (name.equals(""))
81       || (pass == null)
82       || (pass.equals("")))
83     {
84       printError("Item id, name and password are required - Cannot process the request<br>", sp);
85       return;
86     }
87     Integer JavaDoc itemId = new Integer JavaDoc(itemStr);
88     
89     PreparedStatement JavaDoc stmt = null;
90     Connection JavaDoc conn = null;
91     conn = getConnection();
92     // Authenticate the user who want to bid
93
Auth auth = new Auth(conn, sp);
94     int userId = auth.authenticate(name, pass);
95     if (userId == -1)
96     {
97       printError(" You don't have an account on RUBiS!<br>You have to register first.<br>", sp);
98       closeConnection(stmt, conn);
99       return;
100     }
101
102     // Try to find the Item corresponding to the Item ID
103
String JavaDoc itemName, endDate, startDate, description, sellerName;
104     float maxBid, initialPrice, buyNow, reservePrice;
105     int quantity, sellerId, nbOfBids = 0;
106     ResultSet JavaDoc rs = null;
107     try
108     {
109       stmt = conn.prepareStatement("SELECT * FROM items WHERE id=?");
110       stmt.setInt(1, itemId.intValue());
111       rs = stmt.executeQuery();
112     }
113     catch (Exception JavaDoc e)
114     {
115       printError("Failed to execute Query for item: " + e, sp);
116       closeConnection(stmt, conn);
117       return;
118     }
119     try
120     {
121       if (!rs.first())
122       {
123         printError("<h2>This item does not exist!</h2>", sp);
124         closeConnection(stmt, conn);
125         return;
126       }
127       itemName = rs.getString("name");
128       description = rs.getString("description");
129       endDate = rs.getString("end_date");
130       startDate = rs.getString("start_date");
131       initialPrice = rs.getFloat("initial_price");
132       reservePrice = rs.getFloat("reserve_price");
133       buyNow = rs.getFloat("buy_now");
134       quantity = rs.getInt("quantity");
135       sellerId = rs.getInt("seller");
136       
137       PreparedStatement JavaDoc sellerStmt = null;
138       try
139       {
140         sellerStmt =
141           conn.prepareStatement("SELECT nickname FROM users WHERE id=?");
142         sellerStmt.setInt(1, sellerId);
143         ResultSet JavaDoc sellerResult = sellerStmt.executeQuery();
144         // Get the seller's name
145
if (sellerResult.first())
146           sellerName = sellerResult.getString("nickname");
147         else
148         {
149           printError("Unknown seller", sp);
150           sellerStmt.close();
151           closeConnection(stmt, conn);
152           return;
153         }
154         sellerStmt.close();
155
156       }
157       catch (SQLException JavaDoc e)
158       {
159         printError("Failed to executeQuery for seller: " + e, sp);
160         sellerStmt.close();
161         closeConnection(stmt, conn);
162         return;
163       }
164       PreparedStatement JavaDoc maxBidStmt = null;
165       try
166       {
167         maxBidStmt =
168           conn.prepareStatement(
169             "SELECT MAX(bid) AS bid FROM bids WHERE item_id=?");
170         maxBidStmt.setInt(1, itemId.intValue());
171         ResultSet JavaDoc maxBidResult = maxBidStmt.executeQuery();
172         // Get the current price (max bid)
173
if (maxBidResult.first())
174           maxBid = maxBidResult.getFloat("bid");
175         else
176           maxBid = initialPrice;
177         maxBidStmt.close();
178       }
179       catch (SQLException JavaDoc e)
180       {
181         printError("Failed to executeQuery for max bid: " + e, sp);
182         maxBidStmt.close();
183         closeConnection(stmt, conn);
184         return;
185       }
186       PreparedStatement JavaDoc nbStmt = null;
187       try
188       {
189         nbStmt =
190           conn.prepareStatement(
191             "SELECT COUNT(*) AS bid FROM bids WHERE item_id=?");
192         nbStmt.setInt(1, itemId.intValue());
193         ResultSet JavaDoc nbResult = nbStmt.executeQuery();
194         // Get the number of bids for this item
195
if (nbResult.first())
196           nbOfBids = nbResult.getInt("bid");
197         nbStmt.close();
198       }
199       catch (SQLException JavaDoc e)
200       {
201         printError("Failed to executeQuery for number of bids: " + e, sp);
202         nbStmt.close();
203         closeConnection(stmt, conn);
204         return;
205       }
206       sp.printItemDescription(
207         itemId.intValue(),
208         itemName,
209         description,
210         initialPrice,
211         reservePrice,
212         buyNow,
213         quantity,
214         maxBid,
215         nbOfBids,
216         sellerName,
217         sellerId,
218         startDate,
219         endDate,
220         userId,
221         conn);
222     }
223     catch (Exception JavaDoc e)
224     {
225       printError("Exception getting item list: " + e + "<br>", sp);
226       closeConnection(stmt, conn);
227     }
228     closeConnection(stmt, conn);
229     sp.printHTMLfooter();
230   }
231
232   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
233     throws IOException JavaDoc, ServletException JavaDoc
234   {
235     doGet(request, response);
236   }
237
238   /**
239   * Clean up the connection pool.
240   */

241   public void destroy()
242   {
243     super.destroy();
244   }
245 }
246
Popular Tags