KickJava   Java API By Example, From Geeks To Geeks.

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


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 /** Builds the html page with the list of all categories and provides links to browse all
14     items in a category or items in a category for a given region */

15 public class BrowseCategories extends RubisHttpServlet
16 {
17   
18
19
20   public int getPoolSize()
21   {
22     return Config.BrowseCategoriesPoolSize;
23   }
24   
25   /**
26    * Close the connection and statement.
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   /** List all the categories in the database */
43   private boolean categoryList(int regionId, int userId, PreparedStatement JavaDoc stmt, Connection JavaDoc conn, ServletPrinter sp)
44   {
45     String JavaDoc categoryName;
46     int categoryId;
47     ResultSet JavaDoc rs = null;
48
49     // get the list of categories
50
try
51     {
52       stmt = conn.prepareStatement("SELECT name, id FROM categories");
53       rs = stmt.executeQuery();
54     }
55     catch (Exception JavaDoc e)
56     {
57       sp.printHTML("Failed to execute Query for categories list: " + e);
58       closeConnection(stmt, conn);
59       return false;
60     }
61     try
62     {
63       if (!rs.first())
64       {
65         sp.printHTML(
66           "<h2>Sorry, but there is no category available at this time. Database table is empty</h2><br>");
67         closeConnection(stmt, conn);
68         return false;
69       }
70       else
71         sp.printHTML("<h2>Currently available categories</h2><br>");
72
73       do
74       {
75         categoryName = rs.getString("name");
76         categoryId = rs.getInt("id");
77
78         if (regionId != -1)
79         {
80           sp.printCategoryByRegion(categoryName, categoryId, regionId);
81         }
82         else
83         {
84           if (userId != -1)
85             sp.printCategoryToSellItem(categoryName, categoryId, userId);
86           else
87             sp.printCategory(categoryName, categoryId);
88         }
89       }
90       while (rs.next());
91     }
92     catch (Exception JavaDoc e)
93     {
94       sp.printHTML("Exception getting categories list: " + e + "<br>");
95       closeConnection(stmt, conn);
96       return false;
97     }
98     return true;
99   }
100
101   /** Build the html page for the response */
102   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
103     throws IOException JavaDoc, ServletException JavaDoc
104   {
105     ServletPrinter sp = null;
106     PreparedStatement JavaDoc stmt = null;
107     Connection JavaDoc conn = null;
108     int regionId = -1, userId = -1;
109     String JavaDoc username = null, password = null;
110
111     sp = new ServletPrinter(response, "BrowseCategories");
112     sp.printHTMLheader("RUBiS available categories");
113
114     username = request.getParameter("nickname");
115     password = request.getParameter("password");
116
117     conn = getConnection();
118
119     // Authenticate the user who want to sell items
120
if ((username != null && username != "")
121       || (password != null && password != ""))
122     {
123       Auth auth = new Auth(conn, sp);
124       userId = auth.authenticate(username, password);
125       if (userId == -1)
126       {
127         sp.printHTML(
128           " You don't have an account on RUBiS!<br>You have to register first.<br>");
129         sp.printHTMLfooter();
130         closeConnection(stmt, conn);
131         return;
132       }
133     }
134
135     String JavaDoc value = request.getParameter("region");
136     if ((value != null) && (!value.equals("")))
137     {
138       // get the region ID
139
try
140       {
141         stmt = conn.prepareStatement("SELECT id FROM regions WHERE name=?");
142         stmt.setString(1, value);
143         ResultSet JavaDoc rs = stmt.executeQuery();
144         if (!rs.first())
145         {
146           sp.printHTML(
147             " Region " + value + " does not exist in the database!<br>");
148           closeConnection(stmt, conn);
149           return;
150         }
151         regionId = rs.getInt("id");
152         stmt.close();
153       }
154       catch (SQLException JavaDoc e)
155       {
156         sp.printHTML("Failed to execute Query for region: " + e);
157         closeConnection(stmt, conn);
158         return;
159       }
160     }
161
162     boolean connAlive = categoryList(regionId, userId, stmt, conn, sp);
163     if (connAlive) {
164         closeConnection(stmt, conn);
165     }
166     sp.printHTMLfooter();
167
168   }
169
170   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
171     throws IOException JavaDoc, ServletException JavaDoc
172   {
173     doGet(request, response);
174   }
175
176   /**
177    * Clean up the connection pool.
178    */

179   public void destroy()
180   {
181     super.destroy();
182   }
183
184 }
185
Popular Tags