KickJava   Java API By Example, From Geeks To Geeks.

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


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 servlet records a BuyNow in the database and display
14  * the result of the transaction.
15  * It must be called this way :
16  * <pre>
17  * http://..../StoreBuyNow?itemId=aa&userId=bb&minBuyNow=cc&maxQty=dd&BuyNow=ee&maxBuyNow=ff&qty=gg
18  * where: aa is the item id
19  * bb is the user id
20  * cc is the minimum acceptable BuyNow for this item
21  * dd is the maximum quantity available for this item
22  * ee is the user BuyNow
23  * ff is the maximum BuyNow the user wants
24  * gg is the quantity asked by the user
25  * </pre>
26  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
27  * @version 1.0
28  */

29
30 public class StoreBuyNow extends RubisHttpServlet
31 {
32
33   public int getPoolSize()
34   {
35     return Config.StoreBuyNowPoolSize;
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: StoreBuyNow");
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   /**
70    * Call the <code>doPost</code> method.
71    *
72    * @param request a <code>HttpServletRequest</code> value
73    * @param response a <code>HttpServletResponse</code> value
74    * @exception IOException if an error occurs
75    * @exception ServletException if an error occurs
76    */

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

91   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
92     throws IOException JavaDoc, ServletException JavaDoc
93   {
94     Integer JavaDoc userId; // item id
95
Integer JavaDoc itemId; // user id
96
// float minBuyNow; // minimum acceptable BuyNow for this item
97
// float BuyNow; // user BuyNow
98
// float maxBuyNow; // maximum BuyNow the user wants
99
int maxQty; // maximum quantity available for this item
100
int qty; // quantity asked by the user
101
ServletPrinter sp = null;
102     PreparedStatement JavaDoc stmt = null;
103     Connection JavaDoc conn = null;
104
105     sp = new ServletPrinter(response, "StoreBuyNow");
106
107     /* Get and check all parameters */
108
109     String JavaDoc value = request.getParameter("userId");
110     if ((value == null) || (value.equals("")))
111     {
112       printError("<h3>You must provide a user identifier !<br></h3>", sp);
113       return;
114     }
115     else
116       userId = new Integer JavaDoc(value);
117
118     value = request.getParameter("itemId");
119     if ((value == null) || (value.equals("")))
120     {
121       printError("<h3>You must provide an item identifier !<br></h3>", sp);
122       return;
123     }
124     else
125       itemId = new Integer JavaDoc(value);
126
127     value = request.getParameter("maxQty");
128     if ((value == null) || (value.equals("")))
129     {
130       printError("<h3>You must provide a maximum quantity !<br></h3>", sp);
131       return;
132     }
133     else
134     {
135       Integer JavaDoc foo = new Integer JavaDoc(value);
136       maxQty = foo.intValue();
137     }
138
139     value = request.getParameter("qty");
140     if ((value == null) || (value.equals("")))
141     {
142       printError("<h3>You must provide a quantity !<br></h3>", sp);
143       return;
144     }
145     else
146     {
147       Integer JavaDoc foo = new Integer JavaDoc(value);
148       qty = foo.intValue();
149     }
150
151     /* Check for invalid values */
152     if (qty > maxQty)
153     {
154       printError(
155         "<h3>You cannot request "
156           + qty
157           + " items because only "
158           + maxQty
159           + " are proposed !<br></h3>", sp);
160       return;
161     }
162     String JavaDoc now = TimeManagement.currentDateToString();
163     // Try to find the Item corresponding to the Item ID
164
try
165     {
166       int quantity;
167       conn = getConnection();
168       conn.setAutoCommit(false);
169       stmt =
170         conn.prepareStatement(
171           "SELECT quantity, end_date FROM items WHERE id=?");
172       stmt.setInt(1, itemId.intValue());
173       ResultSet JavaDoc irs = stmt.executeQuery();
174       if (!irs.first())
175       {
176         conn.rollback();
177         printError("This item does not exist in the database.", sp);
178         closeConnection(stmt, conn);
179         return;
180       }
181       quantity = irs.getInt("quantity");
182       quantity = quantity - qty;
183       stmt.close();
184       if (quantity == 0)
185       {
186         stmt =
187           conn.prepareStatement(
188             "UPDATE items SET end_date=?, quantity=? WHERE id=?");
189         stmt.setString(1, now);
190         stmt.setInt(2, quantity);
191         stmt.setInt(3, itemId.intValue());
192         stmt.executeUpdate();
193         stmt.close();
194       }
195       else
196       {
197         stmt = conn.prepareStatement("UPDATE items SET quantity=? WHERE id=?");
198         stmt.setInt(1, quantity);
199         stmt.setInt(2, itemId.intValue());
200         stmt.executeUpdate();
201         stmt.close();
202       }
203     }
204     catch (SQLException JavaDoc e)
205     {
206       sp.printHTML("Failed to execute Query for the item: " + e + "<br>");
207       try
208       {
209         conn.rollback();
210         closeConnection(stmt, conn);
211       }
212       catch (Exception JavaDoc se)
213       {
214         printError("Transaction rollback failed: " + e, sp);
215         closeConnection(stmt, conn);
216       }
217       return;
218     }
219     try
220     {
221       stmt =
222         conn.prepareStatement(
223           "INSERT INTO buy_now VALUES (NULL, \""
224             + userId
225             + "\", \""
226             + itemId
227             + "\", \""
228             + qty
229             + "\", \""
230             + now
231             + "\")");
232       stmt.executeUpdate();
233       
234       conn.commit();
235       sp.printHTMLheader("RUBiS: BuyNow result");
236       if (qty == 1)
237         sp.printHTML(
238           "<center><h2>Your have successfully bought this item.</h2></center>\n");
239       else
240         sp.printHTML(
241           "<center><h2>Your have successfully bought these items.</h2></center>\n");
242     }
243     catch (Exception JavaDoc e)
244     {
245       sp.printHTML(
246         "Error while storing the BuyNow (got exception: " + e + ")<br>");
247       try
248       {
249         conn.rollback();
250         closeConnection(stmt, conn);
251       }
252       catch (Exception JavaDoc se)
253       {
254         printError("Transaction rollback failed: " + e + "<br>", sp);
255       }
256       return;
257     }
258     closeConnection(stmt, conn);
259     sp.printHTMLfooter();
260   }
261
262   /**
263   * Clean up the connection pool.
264   */

265   public void destroy()
266   {
267     super.destroy();
268   }
269
270 }
271
Popular Tags