KickJava   Java API By Example, From Geeks To Geeks.

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


1 package edu.rice.rubis.servlets;
2
3 import java.io.IOException JavaDoc;
4 import java.net.URLEncoder JavaDoc;
5 import java.sql.Connection JavaDoc;
6 import java.sql.PreparedStatement JavaDoc;
7 import java.sql.ResultSet 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 a list of items belonging to a specific category.
14  * It must be called this way :
15  * <pre>
16  * http://..../SearchItemsByCategory?category=xx&categoryName=yy
17  * where xx is the category id
18  * and yy is the category name
19  * /<pre>
20  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
21  * @version 1.0
22  */

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

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

54   private void printError(String JavaDoc errorMsg, ServletPrinter sp)
55   {
56     sp.printHTMLheader("RUBiS ERROR: Search Items By Category");
57     sp.printHTML(
58       "<h2>We cannot process your request due to the following error :</h2><br>");
59     sp.printHTML(errorMsg);
60     sp.printHTMLfooter();
61     
62   }
63
64   private void itemList(
65     Integer JavaDoc categoryId,
66     String JavaDoc categoryName,
67     int page,
68     int nbOfItems,
69     ServletPrinter sp)
70   {
71     
72     PreparedStatement JavaDoc stmt = null;
73     Connection JavaDoc conn = null;
74     
75     String JavaDoc itemName, endDate;
76     int itemId;
77     float maxBid;
78     int nbOfBids = 0;
79     ResultSet JavaDoc rs = null;
80
81     // get the list of items
82
try
83     {
84       conn = getConnection();
85       //conn.setAutoCommit(false);
86

87       stmt =
88         conn.prepareStatement(
89           "SELECT items.name, items.id, items.end_date, items.max_bid, items.nb_of_bids, items.initial_price FROM items WHERE items.category=? AND end_date>=NOW() ORDER BY items.end_date ASC LIMIT ?,?");
90       stmt.setInt(1, categoryId.intValue());
91       stmt.setInt(2, page * nbOfItems);
92       stmt.setInt(3, nbOfItems);
93       rs = stmt.executeQuery();
94     }
95     catch (Exception JavaDoc e)
96     {
97       sp.printHTML("Failed to executeQuery for item: " + e);
98       closeConnection(stmt, conn);
99       return;
100     }
101     try
102     {
103       if (!rs.first())
104       {
105         if (page == 0)
106         {
107           sp.printHTML(
108             "<h2>Sorry, but there are no items available in this category !</h2>");
109         }
110         else
111         {
112           sp.printHTML(
113             "<h2>Sorry, but there are no more items available in this category !</h2>");
114           sp.printItemHeader();
115           sp.printItemFooter(
116             "<a HREF=\"/rubis_servlets/servlet/edu.rice.rubis.servlets.SearchItemsByCategory?category="
117               + categoryId
118               + "&categoryName="
119               + URLEncoder.encode(categoryName)
120               + "&page="
121               + (page - 1)
122               + "&nbOfItems="
123               + nbOfItems
124               + "\">Previous page</a>",
125             "");
126         }
127         closeConnection(stmt, conn);
128         return;
129       }
130
131       sp.printItemHeader();
132       do
133       {
134         itemName = rs.getString("name");
135         itemId = rs.getInt("id");
136         endDate = rs.getString("end_date");
137         maxBid = rs.getFloat("max_bid");
138         nbOfBids = rs.getInt("nb_of_bids");
139         float initialPrice = rs.getFloat("initial_price");
140         if (maxBid < initialPrice)
141           maxBid = initialPrice;
142         sp.printItem(itemName, itemId, maxBid, nbOfBids, endDate);
143       }
144       while (rs.next());
145       if (page == 0)
146       {
147         sp.printItemFooter(
148           "",
149           "<a HREF=\"/rubis_servlets/servlet/edu.rice.rubis.servlets.SearchItemsByCategory?category="
150             + categoryId
151             + "&categoryName="
152             + URLEncoder.encode(categoryName)
153             + "&page="
154             + (page + 1)
155             + "&nbOfItems="
156             + nbOfItems
157             + "\">Next page</a>");
158       }
159       else
160       {
161         sp.printItemFooter(
162           "<a HREF=\"/rubis_servlets/servlet/edu.rice.rubis.servlets.SearchItemsByCategory?category="
163             + categoryId
164             + "&categoryName="
165             + URLEncoder.encode(categoryName)
166             + "&page="
167             + (page - 1)
168             + "&nbOfItems="
169             + nbOfItems
170             + "\">Previous page</a>",
171           "<a HREF=\"/rubis_servlets/servlet/edu.rice.rubis.servlets.SearchItemsByCategory?category="
172             + categoryId
173             + "&categoryName="
174             + URLEncoder.encode(categoryName)
175             + "&page="
176             + (page + 1)
177             + "&nbOfItems="
178             + nbOfItems
179             + "\">Next page</a>");
180       }
181       //conn.commit();
182
closeConnection(stmt, conn);
183     }
184     catch (Exception JavaDoc e)
185     {
186       printError("Exception getting item list: " + e + "<br>", sp);
187       // try
188
// {
189
// conn.rollback();
190
// }
191
// catch (Exception se)
192
// {
193
// printError("Transaction rollback failed: " + e +"<br>");
194
// }
195
closeConnection(stmt, conn);
196     }
197   }
198
199   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
200     throws IOException JavaDoc, ServletException JavaDoc
201   {
202     Integer JavaDoc page;
203     Integer JavaDoc nbOfItems;
204     String JavaDoc value = request.getParameter("category");
205     ;
206     Integer JavaDoc categoryId;
207     String JavaDoc categoryName = request.getParameter("categoryName");
208
209     ServletPrinter sp = null;
210     sp = new ServletPrinter(response, "SearchItemsByCategory");
211
212     if ((value == null) || (value.equals("")))
213     {
214       printError("You must provide a category identifier!<br>", sp);
215       return;
216     }
217     else
218       categoryId = new Integer JavaDoc(value);
219
220     value = request.getParameter("page");
221     if ((value == null) || (value.equals("")))
222       page = new Integer JavaDoc(0);
223     else
224       page = new Integer JavaDoc(value);
225
226     value = request.getParameter("nbOfItems");
227     if ((value == null) || (value.equals("")))
228       nbOfItems = new Integer JavaDoc(25);
229     else
230       nbOfItems = new Integer JavaDoc(value);
231
232     if (categoryName == null)
233     {
234       sp.printHTMLheader("RUBiS: Missing category name");
235       sp.printHTML("<h2>Items in this category</h2><br><br>");
236     }
237     else
238     {
239       sp.printHTMLheader("RUBiS: Items in category " + categoryName);
240       sp.printHTML("<h2>Items in category " + categoryName + "</h2><br><br>");
241     }
242
243     itemList(categoryId, categoryName, page.intValue(), nbOfItems.intValue(), sp);
244     sp.printHTMLfooter();
245   }
246
247   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
248     throws IOException JavaDoc, ServletException JavaDoc
249   {
250     doGet(request, response);
251   }
252
253   /**
254   * Clean up the connection pool.
255   */

256   public void destroy()
257   {
258     super.destroy();
259   }
260 }
261
Popular Tags