KickJava   Java API By Example, From Geeks To Geeks.

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


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 list of bids regarding an item.
14  * It must be called this way :
15  * <pre>
16  * http://..../ViewUserInfo?itemId=xx where xx is the id of the item
17  * /<pre>
18  */

19
20 public class ViewBidHistory extends RubisHttpServlet
21 {
22
23
24   public int getPoolSize()
25   {
26     return Config.ViewBidHistoryPoolSize;
27   }
28
29 /**
30  * Close both statement and connection to the database.
31  */

32   private void closeConnection(PreparedStatement JavaDoc stmt, Connection JavaDoc conn)
33   {
34     try
35     {
36       if (stmt != null)
37         stmt.close(); // close statement
38
if (conn != null)
39         releaseConnection(conn);
40     }
41     catch (Exception JavaDoc ignore)
42     {
43     }
44   }
45
46   /** List the bids corresponding to an item */
47   private boolean listBids(Integer JavaDoc itemId, PreparedStatement JavaDoc stmt, Connection JavaDoc conn, ServletPrinter sp)
48   {
49     float bid;
50     int userId;
51     String JavaDoc bidderName, date;
52     ResultSet JavaDoc rs = null;
53
54     // Get the list of the user's last bids
55
try
56     {
57       stmt =
58         conn.prepareStatement(
59           "SELECT * FROM bids WHERE item_id=? ORDER BY date DESC");
60       stmt.setInt(1, itemId.intValue());
61       rs = stmt.executeQuery();
62       if (!rs.first())
63       {
64         sp.printHTML(
65           "<h3>There is no bid corresponding to this item.</h3><br>");
66         closeConnection(stmt, conn);
67         return false;
68       }
69     }
70     catch (SQLException JavaDoc e)
71     {
72       sp.printHTML("Exception getting bids list: " + e + "<br>");
73       closeConnection(stmt, conn);
74       return false;
75     }
76
77     sp.printBidHistoryHeader();
78     try
79     {
80       do
81       {
82         // Get the bids
83
date = rs.getString("date");
84         bid = rs.getFloat("bid");
85         userId = rs.getInt("user_id");
86
87         ResultSet JavaDoc urs = null;
88         try
89         {
90           stmt = conn.prepareStatement("SELECT nickname FROM users WHERE id=?");
91           stmt.setInt(1, userId);
92           urs = stmt.executeQuery();
93           if (!urs.first())
94           {
95             sp.printHTML("This user does not exist in the database.<br>");
96             closeConnection(stmt, conn);
97             return false;
98           }
99           bidderName = urs.getString("nickname");
100         }
101         catch (SQLException JavaDoc e)
102         {
103           sp.printHTML("Couldn't get bidder name: " + e + "<br>");
104           closeConnection(stmt, conn);
105           return false;
106         }
107         sp.printBidHistory(userId, bidderName, bid, date);
108       }
109       while (rs.next());
110     }
111     catch (SQLException JavaDoc e)
112     {
113       sp.printHTML("Exception getting bid: " + e + "<br>");
114       closeConnection(stmt, conn);
115       return false;
116     }
117     sp.printBidHistoryFooter();
118     return true;
119   }
120
121   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
122     throws IOException JavaDoc, ServletException JavaDoc
123   {
124     doPost(request, response);
125   }
126
127   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
128     throws IOException JavaDoc, ServletException JavaDoc
129   {
130     String JavaDoc value = request.getParameter("itemId");
131     Integer JavaDoc itemId;
132     String JavaDoc itemName;
133     ResultSet JavaDoc rs = null;
134     ServletPrinter sp = null;
135     PreparedStatement JavaDoc stmt = null;
136     Connection JavaDoc conn = null;
137
138     sp = new ServletPrinter(response, "ViewBidHistory");
139
140     if ((value == null) || (value.equals("")))
141     {
142       sp.printHTMLheader("RUBiS ERROR: View bids history");
143       sp.printHTML("<h3>You must provide an item identifier !<br></h3>");
144       sp.printHTMLfooter();
145       return;
146     }
147     else
148       itemId = new Integer JavaDoc(value);
149     if (itemId.intValue() == -1)
150       sp.printHTML("ItemId is -1: this item does not exist.<br>");
151
152     sp.printHTMLheader("RUBiS: Bid history");
153
154     // get the item
155
try
156     {
157       conn = getConnection();
158       stmt = conn.prepareStatement("SELECT name FROM items WHERE id=?");
159       stmt.setInt(1, itemId.intValue());
160       rs = stmt.executeQuery();
161     }
162     catch (Exception JavaDoc e)
163     {
164       sp.printHTML("Failed to execute Query for item in table items: " + e);
165       closeConnection(stmt, conn);
166       return;
167     }
168     /**
169     try
170     {
171       if (!rs.first())
172       {
173         stmt.close();
174         stmt = conn.prepareStatement("SELECT name FROM old_items WHERE id=?");
175         stmt.setInt(1, itemId.intValue());
176         rs = stmt.executeQuery();
177       }
178     }
179     catch (Exception e)
180     {
181       sp.printHTML("Failed to execute Query for item in table old_items: " + e);
182       closeConnection(stmt, conn);
183       return;
184     }
185     */

186     try
187     {
188       if (!rs.first())
189       {
190         sp.printHTML("<h2>This item does not exist!</h2>");
191         closeConnection(stmt, conn);
192         return;
193       }
194       itemName = rs.getString("name");
195       sp.printHTML(
196         "<center><h3>Bid History for " + itemName + "<br></h3></center>");
197     }
198     catch (Exception JavaDoc e)
199     {
200       sp.printHTML("This item does not exist (got exception: " + e + ")<br>");
201       sp.printHTMLfooter();
202       closeConnection(stmt, conn);
203       return;
204     }
205
206     boolean connAlive = listBids(itemId, stmt, conn, sp);
207     // connAlive means we must close it. Otherwise we must NOT do a
208
// double free
209
if (connAlive) {
210         closeConnection(stmt, conn);
211     }
212     sp.printHTMLfooter();
213   }
214
215   /**
216   * Clean up the connection pool.
217   */

218   public void destroy()
219   {
220     super.destroy();
221   }
222
223 }
224
Popular Tags