KickJava   Java API By Example, From Geeks To Geeks.

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


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
8 import javax.servlet.ServletException JavaDoc;
9 import javax.servlet.http.HttpServletRequest JavaDoc;
10 import javax.servlet.http.HttpServletResponse JavaDoc;
11
12 /** This servlet records a bid in the database and display
13  * the result of the transaction.
14  * It must be called this way :
15  * <pre>
16  * http://..../StoreBid?itemId=aa&userId=bb&minBid=cc&maxQty=dd&bid=ee&maxBid=ff&qty=gg
17  * where: aa is the item id
18  * bb is the user id
19  * cc is the minimum acceptable bid for this item
20  * dd is the maximum quantity available for this item
21  * ee is the user bid
22  * ff is the maximum bid the user wants
23  * gg is the quantity asked by the user
24  * </pre>
25  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
26  * @version 1.0
27  */

28
29 public class StoreBid extends RubisHttpServlet
30 {
31
32
33   public int getPoolSize()
34   {
35     return Config.StoreBidPoolSize;
36   }
37
38 /**
39  * Close both statement and connection.
40  */

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

59   private void printError(String JavaDoc errorMsg, ServletPrinter sp)
60   {
61     sp.printHTMLheader("RUBiS ERROR: StoreBid");
62     sp.printHTML(
63       "<h2>Your request has not been processed due to the following error :</h2><br>");
64     sp.printHTML(errorMsg);
65     sp.printHTMLfooter();
66   }
67
68   /**
69    * Call the <code>doPost</code> method.
70    *
71    * @param request a <code>HttpServletRequest</code> value
72    * @param response a <code>HttpServletResponse</code> value
73    * @exception IOException if an error occurs
74    * @exception ServletException if an error occurs
75    */

76   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
77     throws IOException JavaDoc, ServletException JavaDoc
78   {
79     doPost(request, response);
80   }
81
82   /**
83    * Store the bid to the database and display resulting message.
84    *
85    * @param request a <code>HttpServletRequest</code> value
86    * @param response a <code>HttpServletResponse</code> value
87    * @exception IOException if an error occurs
88    * @exception ServletException if an error occurs
89    */

90   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
91     throws IOException JavaDoc, ServletException JavaDoc
92   {
93     Integer JavaDoc userId; // item id
94
Integer JavaDoc itemId; // user id
95
float minBid; // minimum acceptable bid for this item
96
float bid; // user bid
97
float maxBid; // maximum bid the user wants
98
int maxQty; // maximum quantity available for this item
99
int qty; // quantity asked by the user
100
ServletPrinter sp = null;
101     PreparedStatement JavaDoc stmt = null;
102     Connection JavaDoc conn = null;
103
104     sp = new ServletPrinter(response, "StoreBid");
105
106     /* Get and check all parameters */
107
108     String JavaDoc value = request.getParameter("userId");
109     if ((value == null) || (value.equals("")))
110     {
111       printError("<h3>You must provide a user identifier !<br></h3>", sp);
112       return;
113     }
114     else
115       userId = new Integer JavaDoc(value);
116
117     value = request.getParameter("itemId");
118     if ((value == null) || (value.equals("")))
119     {
120       printError("<h3>You must provide an item identifier !<br></h3>", sp);
121       return;
122     }
123     else
124       itemId = new Integer JavaDoc(value);
125
126     value = request.getParameter("minBid");
127     if ((value == null) || (value.equals("")))
128     {
129       printError("<h3>You must provide a minimum bid !<br></h3>", sp);
130       return;
131     }
132     else
133     {
134       Float JavaDoc foo = new Float JavaDoc(value);
135       minBid = foo.floatValue();
136     }
137
138     value = request.getParameter("bid");
139     if ((value == null) || (value.equals("")))
140     {
141       printError("<h3>You must provide a bid !<br></h3>", sp);
142       return;
143     }
144     else
145     {
146       Float JavaDoc foo = new Float JavaDoc(value);
147       bid = foo.floatValue();
148     }
149
150     value = request.getParameter("maxBid");
151     if ((value == null) || (value.equals("")))
152     {
153       printError("<h3>You must provide a maximum bid !<br></h3>", sp);
154       return;
155     }
156     else
157     {
158       Float JavaDoc foo = new Float JavaDoc(value);
159       maxBid = foo.floatValue();
160     }
161
162     value = request.getParameter("maxQty");
163     if ((value == null) || (value.equals("")))
164     {
165       printError("<h3>You must provide a maximum quantity !<br></h3>", sp);
166       return;
167     }
168     else
169     {
170       Integer JavaDoc foo = new Integer JavaDoc(value);
171       maxQty = foo.intValue();
172     }
173
174     value = request.getParameter("qty");
175     if ((value == null) || (value.equals("")))
176     {
177       printError("<h3>You must provide a quantity !<br></h3>", sp);
178       return;
179     }
180     else
181     {
182       Integer JavaDoc foo = new Integer JavaDoc(value);
183       qty = foo.intValue();
184     }
185
186     /* Check for invalid values */
187
188     if (qty > maxQty)
189     {
190       printError(
191         "<h3>You cannot request "
192           + qty
193           + " items because only "
194           + maxQty
195           + " are proposed !<br></h3>", sp);
196       return;
197     }
198     if (bid < minBid)
199     {
200       printError(
201         "<h3>Your bid of $"
202           + bid
203           + " is not acceptable because it is below the $"
204           + minBid
205           + " minimum bid !<br></h3>", sp);
206       return;
207     }
208     if (maxBid < minBid)
209     {
210       printError(
211         "<h3>Your maximum bid of $"
212           + maxBid
213           + " is not acceptable because it is below the $"
214           + minBid
215           + " minimum bid !<br></h3>", sp);
216       return;
217     }
218     if (maxBid < bid)
219     {
220       printError(
221         "<h3>Your maximum bid of $"
222           + maxBid
223           + " is not acceptable because it is below your current bid of $"
224           + bid
225           + " !<br></h3>", sp);
226       return;
227     }
228     try
229     {
230       conn = getConnection();
231       conn.setAutoCommit(false);
232       String JavaDoc now = TimeManagement.currentDateToString();
233       stmt =
234         conn.prepareStatement(
235           "INSERT INTO bids VALUES (NULL, \""
236             + userId
237             + "\", \""
238             + itemId
239             + "\", \""
240             + qty
241             + "\", \""
242             + bid
243             + "\", \""
244             + maxBid
245             + "\", \""
246             + now
247             + "\")");
248       stmt.executeUpdate();
249       stmt.close();
250       // update the number of bids and the max bid for the item
251
PreparedStatement JavaDoc update = null;
252       try
253       {
254         stmt =
255           conn.prepareStatement(
256             "SELECT nb_of_bids, max_bid FROM items WHERE id=?");
257         stmt.setInt(1, itemId.intValue());
258         ResultSet JavaDoc rs = stmt.executeQuery();
259         if (rs.first())
260         {
261           
262           int nbOfBids = rs.getInt("nb_of_bids");
263           nbOfBids++;
264           float oldMaxBid = rs.getFloat("max_bid");
265           if (bid > oldMaxBid)
266           {
267             oldMaxBid = bid;
268             update =
269               conn.prepareStatement(
270                 "UPDATE items SET max_bid=?, nb_of_bids=? WHERE id=?");
271             update.setFloat(1, maxBid);
272             update.setInt(2, nbOfBids);
273             update.setInt(3, itemId.intValue());
274             update.executeUpdate();
275             update.close();
276           }
277           else
278           {
279             update =
280               conn.prepareStatement("UPDATE items SET nb_of_bids=? WHERE id=?");
281             update.setInt(1, nbOfBids);
282             update.setInt(2, itemId.intValue());
283             update.executeUpdate();
284             update.close();
285           }
286
287         }
288         else
289         {
290           conn.rollback();
291           printError("Couldn't find the item.", sp);
292           closeConnection(stmt, conn);
293           return;
294         }
295       }
296       catch (Exception JavaDoc ex)
297       {
298         conn.rollback();
299         printError("Failed to update nb of bids and max bid: " + ex, sp);
300         if (update != null)
301           update.close();
302         closeConnection(stmt, conn);
303         return;
304       }
305       sp.printHTMLheader("RUBiS: Bidding result");
306       sp.printHTML(
307         "<center><h2>Your bid has been successfully processed.</h2></center>\n");
308       conn.commit();
309       closeConnection(stmt, conn);
310     }
311     catch (Exception JavaDoc e)
312     {
313       sp.printHTML(
314         "Error while storing the bid (got exception: " + e + ")<br>");
315       try
316       {
317         conn.rollback();
318         closeConnection(stmt, conn);
319       }
320       catch (Exception JavaDoc se)
321       {
322         printError("Transaction rollback failed: " + e, sp);
323       }
324       return;
325     }
326     sp.printHTMLfooter();
327   }
328
329   /**
330   * Clean up the connection pool.
331   */

332   public void destroy()
333   {
334     super.destroy();
335   }
336
337 }
338
Popular Tags