KickJava   Java API By Example, From Geeks To Geeks.

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


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 displays the full description of a given item
14  * and allows the user to bid on this item.
15  * It must be called this way :
16  * <pre>
17  * http://..../ViewItem?itemId=xx where xx is the id of the item
18  * /<pre>
19  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
20  * @version 1.0
21  */

22
23 public class ViewItem extends RubisHttpServlet
24 {
25
26
27   public int getPoolSize()
28   {
29     return Config.ViewItemPoolSize;
30   }
31
32 /**
33  * Close both statement and connection to the database.
34  */

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

53   private void printError(String JavaDoc errorMsg, ServletPrinter sp)
54   {
55     sp.printHTMLheader("RUBiS ERROR: View item");
56     sp.printHTML(
57       "<h2>We cannot process your request due to the following error :</h2><br>");
58     sp.printHTML(errorMsg);
59     sp.printHTMLfooter();
60     
61   }
62
63   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
64     throws IOException JavaDoc, ServletException JavaDoc
65   {
66     ServletPrinter sp = null;
67     PreparedStatement JavaDoc stmt = null;
68     Connection JavaDoc conn = null;
69     
70     sp = new ServletPrinter(response, "ViewItem");
71     ResultSet JavaDoc rs = null;
72
73     String JavaDoc value = request.getParameter("itemId");
74     if ((value == null) || (value.equals("")))
75     {
76       printError("No item identifier received - Cannot process the request<br>", sp);
77       return;
78     }
79     Integer JavaDoc itemId = new Integer JavaDoc(value);
80     // get the item
81
try
82     {
83       conn = getConnection();
84       stmt = conn.prepareStatement("SELECT * FROM items WHERE id=?");
85       stmt.setInt(1, itemId.intValue());
86       rs = stmt.executeQuery();
87     }
88     catch (Exception JavaDoc e)
89     {
90       sp.printHTML("Failed to execute Query for item: " + e);
91       closeConnection(stmt, conn);
92       return;
93     }
94     /**
95     try
96     {
97       if (!rs.first())
98       {
99         stmt.close();
100         stmt = conn.prepareStatement("SELECT * FROM old_items WHERE id=?");
101         stmt.setInt(1, itemId.intValue());
102         rs = stmt.executeQuery();
103       }
104     }
105     catch (Exception e)
106     {
107       sp.printHTML("Failed to execute Query for item in table old_items: " + e);
108       closeConnection(stmt, conn);
109       return;
110     }
111     */

112     try
113     {
114       if (!rs.first())
115       {
116         sp.printHTML("<h2>This item does not exist!</h2>");
117         closeConnection(stmt, conn);
118         return;
119       }
120       String JavaDoc itemName, endDate, startDate, description, sellerName;
121       float maxBid, initialPrice, buyNow, reservePrice;
122       int quantity, sellerId, nbOfBids = 0;
123       itemName = rs.getString("name");
124       description = rs.getString("description");
125       endDate = rs.getString("end_date");
126       startDate = rs.getString("start_date");
127       initialPrice = rs.getFloat("initial_price");
128       reservePrice = rs.getFloat("reserve_price");
129       buyNow = rs.getFloat("buy_now");
130       quantity = rs.getInt("quantity");
131       sellerId = rs.getInt("seller");
132
133       maxBid = rs.getFloat("max_bid");
134       nbOfBids = rs.getInt("nb_of_bids");
135       if (maxBid < initialPrice)
136         maxBid = initialPrice;
137
138       PreparedStatement JavaDoc sellerStmt = null;
139       try
140       {
141         sellerStmt =
142           conn.prepareStatement("SELECT nickname FROM users WHERE id=?");
143         sellerStmt.setInt(1, sellerId);
144         ResultSet JavaDoc sellerResult = sellerStmt.executeQuery();
145         // Get the seller's name
146
if (sellerResult.first())
147           sellerName = sellerResult.getString("nickname");
148         else
149         {
150           sp.printHTML("Unknown seller");
151           sellerStmt.close();
152           closeConnection(stmt, conn);
153           return;
154         }
155         sellerStmt.close();
156
157       }
158       catch (SQLException JavaDoc e)
159       {
160         sp.printHTML("Failed to executeQuery for seller: " + e);
161         sellerStmt.close();
162         closeConnection(stmt, conn);
163         return;
164       }
165       sp.printItemDescription(
166         itemId.intValue(),
167         itemName,
168         description,
169         initialPrice,
170         reservePrice,
171         buyNow,
172         quantity,
173         maxBid,
174         nbOfBids,
175         sellerName,
176         sellerId,
177         startDate,
178         endDate,
179         -1,
180         conn);
181     }
182     catch (Exception JavaDoc e)
183     {
184       printError("Exception getting item list: " + e + "<br>", sp);
185     }
186     closeConnection(stmt, conn);
187     sp.printHTMLfooter();
188   }
189
190   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
191     throws IOException JavaDoc, ServletException JavaDoc
192   {
193     doGet(request, response);
194   }
195
196   /**
197   * Clean up the connection pool.
198   */

199   public void destroy()
200   {
201     super.destroy();
202   }
203 }
204
Popular Tags