KickJava   Java API By Example, From Geeks To Geeks.

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


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 buy an item
14  * It must be called this way :
15  * <pre>
16  * http://..../BuyNow?itemId=xx&nickname=yy&password=zz
17  * where xx is the id of the item
18  * yy is the nick name of the user
19  * zz is the user password
20  * </pre>
21  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
22  * @version 1.0
23  */

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

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

55   private void printError(String JavaDoc errorMsg, ServletPrinter sp)
56   {
57     sp.printHTMLheader("RUBiS ERROR: Buy now");
58     sp.printHTML(
59       "<h2>Your request has not been processed due to the following error :</h2><br>");
60     sp.printHTML(errorMsg);
61     sp.printHTMLfooter();
62   }
63
64   /**
65    * Authenticate the user and end the display a buy now form
66    *
67    * @param request a <code>HttpServletRequest</code> value
68    * @param response a <code>HttpServletResponse</code> value
69    * @exception IOException if an error occurs
70    * @exception ServletException if an error occurs
71    */

72   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
73     throws IOException JavaDoc, ServletException JavaDoc
74   {
75     ServletPrinter sp = null;
76     
77     String JavaDoc itemStr = request.getParameter("itemId");
78     String JavaDoc name = request.getParameter("nickname");
79     String JavaDoc pass = request.getParameter("password");
80     sp = new ServletPrinter(response, "BuyNow");
81
82     if ((itemStr == null)
83       || (itemStr.equals(""))
84       || (name == null)
85       || (name.equals(""))
86       || (pass == null)
87       || (pass.equals("")))
88     {
89       printError("Item id, name and password are required - Cannot process the request<br>", sp);
90       return;
91     }
92     PreparedStatement JavaDoc stmt = null;
93     Connection JavaDoc conn = null;
94     // Authenticate the user who want to bid
95
conn = getConnection();
96     Auth auth = new Auth(conn, sp);
97     int userId = auth.authenticate(name, pass);
98     if (userId == -1)
99     {
100       sp.printHTML("name: " + name + "<br>");
101       sp.printHTML("pwd: " + pass + "<br>");
102       printError(" You don't have an account on RUBiS!<br>You have to register first.<br>", sp);
103       closeConnection(stmt, conn);
104       return;
105     }
106     Integer JavaDoc itemId = new Integer JavaDoc(itemStr);
107     // Try to find the Item corresponding to the Item ID
108
try
109     {
110       stmt = conn.prepareStatement("SELECT * FROM items WHERE id=?");
111       stmt.setInt(1, itemId.intValue());
112       ResultSet JavaDoc irs = stmt.executeQuery();
113       if (!irs.first())
114       {
115         printError("This item does not exist in the database.", sp);
116         closeConnection(stmt, conn);
117         return;
118       }
119
120       String JavaDoc itemName = irs.getString("name");
121       String JavaDoc description = irs.getString("description");
122       String JavaDoc startDate = irs.getString("start_date");
123       String JavaDoc endDate = irs.getString("end_date");
124       float buyNow = irs.getFloat("buy_now");
125       int quantity = irs.getInt("quantity");
126       int sellerId = irs.getInt("seller");
127       stmt.close();
128       String JavaDoc sellerName = null;
129       try
130       {
131         stmt = conn.prepareStatement("SELECT nickname FROM users WHERE id=?");
132         stmt.setInt(1, sellerId);
133         ResultSet JavaDoc srs = stmt.executeQuery();
134         if (!srs.first())
135         {
136           printError("This user does not exist in the database.", sp);
137           closeConnection(stmt, conn);
138           return;
139         }
140         sellerName = srs.getString("nickname");
141       }
142       catch (SQLException JavaDoc s)
143       {
144         printError("Failed to execute Query for seller: " + s, sp);
145         closeConnection(stmt, conn);
146         return;
147       }
148       // Display the form for buying the item
149
sp.printItemDescriptionToBuyNow(
150         itemId.intValue(),
151         itemName,
152         description,
153         buyNow,
154         quantity,
155         sellerId,
156         sellerName,
157         startDate,
158         endDate,
159         userId);
160
161     }
162     catch (SQLException JavaDoc e)
163     {
164       printError("Failed to execute Query for item: " + e, sp);
165       closeConnection(stmt, conn);
166       return;
167     }
168     sp.printHTMLfooter();
169     closeConnection(stmt, conn);
170
171     // try
172
// {
173
// stmt = conn.prepareStatement("UPDATE items SET end_date=? WHERE id=?");
174
// stmt.setInt(1, endDate);
175
// stmt.setInt(2, itemId.intValue());
176
// stmt.executeUpdate();
177
// }
178
// catch (SQLException e)
179
// {
180
// printError("Failed to update the auction end date: " +e);
181
// return;
182
// }
183
// sp.printHTMLheader("Buy now");
184
// sp.printHTML("<h2>You bought: "+item.getName()+" for $"+item.getBuyNow()+
185
// ".</h2><br>");
186

187   }
188
189   /**
190    * Call the doGet method
191    *
192    * @param request a <code>HttpServletRequest</code> value
193    * @param response a <code>HttpServletResponse</code> value
194    * @exception IOException if an error occurs
195    * @exception ServletException if an error occurs
196    */

197   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
198     throws IOException JavaDoc, ServletException JavaDoc
199   {
200     doGet(request, response);
201   }
202
203   /**
204    * Clean up the connection pool.
205    */

206   public void destroy()
207   {
208     super.destroy();
209   }
210 }
211
Popular Tags