KickJava   Java API By Example, From Geeks To Geeks.

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


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
8 import javax.servlet.ServletException JavaDoc;
9 import javax.servlet.http.HttpServletRequest JavaDoc;
10 import javax.servlet.http.HttpServletResponse JavaDoc;
11
12 /**
13  * Build the html page with the list of all items for given category and region.
14  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
15  * @version 1.0
16  */

17 public class SearchItemsByRegion extends RubisHttpServlet
18 {
19
20   public int getPoolSize()
21   {
22     return Config.SearchItemsByRegionPoolSize;
23   }
24
25 /**
26  * Close both statement and connection.
27  */

28   private void closeConnection(PreparedStatement JavaDoc stmt, Connection JavaDoc conn)
29   {
30     try
31     {
32       if (stmt != null)
33         stmt.close(); // close statement
34
if (conn != null)
35         releaseConnection(conn);
36     }
37     catch (Exception JavaDoc ignore)
38     {
39     }
40   }
41
42 /**
43  * Display an error message.
44  * @param errorMsg the error message value
45  */

46   private void printError(String JavaDoc errorMsg, ServletPrinter sp)
47   {
48     sp.printHTMLheader("RUBiS ERROR: SearchItemsByRegion");
49     sp.printHTML(
50       "<h2>Your request has not been processed due to the following error :</h2><br>");
51     sp.printHTML(errorMsg);
52     sp.printHTMLfooter();
53     
54   }
55
56   /** List items in the given category for the given region */
57   private void itemList(
58     Integer JavaDoc categoryId,
59     Integer JavaDoc regionId,
60     int page,
61     int nbOfItems,
62     ServletPrinter sp)
63   {
64     String JavaDoc itemName, endDate;
65     int itemId, nbOfBids = 0;
66     float maxBid;
67     ResultSet JavaDoc rs = null;
68     PreparedStatement JavaDoc stmt = null;
69     Connection JavaDoc conn = null;
70
71     // get the list of items
72
try
73     {
74       conn = getConnection();
75       stmt =
76         conn.prepareStatement(
77           "SELECT items.name, items.id, items.end_date, items.max_bid, items.nb_of_bids, items.initial_price FROM items,users WHERE items.category=? AND items.seller=users.id AND users.region=? AND end_date>=NOW() ORDER BY items.end_date ASC LIMIT ?,?");
78       stmt.setInt(1, categoryId.intValue());
79       stmt.setInt(2, regionId.intValue());
80       stmt.setInt(3, page * nbOfItems);
81       stmt.setInt(4, nbOfItems);
82       rs = stmt.executeQuery();
83     }
84     catch (Exception JavaDoc e)
85     {
86       sp.printHTML("Failed to execute Query for items in region: " + e);
87       closeConnection(stmt, conn);
88       return;
89     }
90     try
91     {
92       if (!rs.first())
93       {
94         if (page == 0)
95         {
96           sp.printHTML(
97             "<h3>Sorry, but there is no items in this category for this region.</h3><br>");
98         }
99         else
100         {
101           sp.printHTML(
102             "<h3>Sorry, but there is no more items in this category for this region.</h3><br>");
103           sp.printItemHeader();
104           sp.printItemFooter(
105             "<a HREF=\"/rubis_servlets/servlet/edu.rice.rubis.servlets.SearchItemsByRegion?category="
106               + categoryId
107               + "&region="
108               + regionId
109               + "&page="
110               + (page - 1)
111               + "&nbOfItems="
112               + nbOfItems
113               + "\">Previous page</a>",
114             "");
115         }
116         closeConnection(stmt, conn);
117         return;
118       }
119
120       sp.printItemHeader();
121       do
122       {
123         itemName = rs.getString("name");
124         itemId = rs.getInt("id");
125         endDate = rs.getString("end_date");
126         maxBid = rs.getFloat("max_bid");
127         nbOfBids = rs.getInt("nb_of_bids");
128         float initialPrice = rs.getFloat("initial_price");
129         if (maxBid < initialPrice)
130           maxBid = initialPrice;
131         sp.printItem(itemName, itemId, maxBid, nbOfBids, endDate);
132       }
133       while (rs.next());
134       if (page == 0)
135       {
136         sp.printItemFooter(
137           "",
138           "<a HREF=\"/rubis_servlets/servlet/edu.rice.rubis.servlets.SearchItemsByRegion?category="
139             + categoryId
140             + "&region="
141             + regionId
142             + "&page="
143             + (page + 1)
144             + "&nbOfItems="
145             + nbOfItems
146             + "\">Next page</a>");
147       }
148       else
149       {
150         sp.printItemFooter(
151           "<a HREF=\"/rubis_servlets/servlet/edu.rice.rubis.servlets.SearchItemsByRegion?category="
152             + categoryId
153             + "&region="
154             + regionId
155             + "&page="
156             + (page - 1)
157             + "&nbOfItems="
158             + nbOfItems
159             + "\">Previous page</a>",
160           "<a HREF=\"/rubis_servlets/servlet/edu.rice.rubis.servlets.SearchItemsByRegion?category="
161             + categoryId
162             + "&region="
163             + regionId
164             + "&page="
165             + (page + 1)
166             + "&nbOfItems="
167             + nbOfItems
168             + "\">Next page</a>");
169       }
170       closeConnection(stmt, conn);
171     }
172     catch (Exception JavaDoc e)
173     {
174       sp.printHTML("Exception getting item list: " + e + "<br>");
175       closeConnection(stmt, conn);
176     }
177   }
178
179   /* Read the parameters, lookup the remote category and region and build the web page with
180      the list of items */

181   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
182     throws IOException JavaDoc, ServletException JavaDoc
183   {
184     Integer JavaDoc categoryId, regionId;
185     Integer JavaDoc page;
186     Integer JavaDoc nbOfItems;
187
188     ServletPrinter sp = null;
189     sp = new ServletPrinter(response, "SearchItemsByRegion");
190
191     String JavaDoc value = request.getParameter("category");
192     if ((value == null) || (value.equals("")))
193     {
194       printError("You must provide a category!<br>", sp);
195       return;
196     }
197     else
198       categoryId = new Integer JavaDoc(value);
199
200     value = request.getParameter("region");
201     if ((value == null) || (value.equals("")))
202     {
203       printError("You must provide a region!<br>", sp);
204       return;
205     }
206     else
207       regionId = new Integer JavaDoc(value);
208
209     value = request.getParameter("page");
210     if ((value == null) || (value.equals("")))
211       page = new Integer JavaDoc(0);
212     else
213       page = new Integer JavaDoc(value);
214
215     value = request.getParameter("nbOfItems");
216     if ((value == null) || (value.equals("")))
217       nbOfItems = new Integer JavaDoc(25);
218     else
219       nbOfItems = new Integer JavaDoc(value);
220
221     sp.printHTMLheader("RUBiS: Search items by region");
222     itemList(categoryId, regionId, page.intValue(), nbOfItems.intValue(), sp);
223     sp.printHTMLfooter();
224   }
225
226   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
227     throws IOException JavaDoc, ServletException JavaDoc
228   {
229     doGet(request, response);
230   }
231
232   /**
233   * Clean up the connection pool.
234   */

235   public void destroy()
236   {
237     super.destroy();
238   }
239 }
240
Popular Tags