1 package edu.rice.rubis.servlets; 2 3 import java.io.IOException ; 4 import java.sql.Connection ; 5 import java.sql.PreparedStatement ; 6 import java.sql.ResultSet ; 7 import java.sql.SQLException ; 8 9 import javax.servlet.ServletException ; 10 import javax.servlet.http.HttpServletRequest ; 11 import javax.servlet.http.HttpServletResponse ; 12 13 15 public class BrowseCategories extends RubisHttpServlet 16 { 17 18 19 20 public int getPoolSize() 21 { 22 return Config.BrowseCategoriesPoolSize; 23 } 24 25 28 private void closeConnection(PreparedStatement stmt, Connection conn) 29 { 30 try 31 { 32 if (stmt != null) 33 stmt.close(); if (conn != null) 35 releaseConnection(conn); 36 } 37 catch (Exception ignore) 38 { 39 } 40 } 41 42 43 private boolean categoryList(int regionId, int userId, PreparedStatement stmt, Connection conn, ServletPrinter sp) 44 { 45 String categoryName; 46 int categoryId; 47 ResultSet rs = null; 48 49 try 51 { 52 stmt = conn.prepareStatement("SELECT name, id FROM categories"); 53 rs = stmt.executeQuery(); 54 } 55 catch (Exception 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 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 102 public void doGet(HttpServletRequest request, HttpServletResponse response) 103 throws IOException , ServletException 104 { 105 ServletPrinter sp = null; 106 PreparedStatement stmt = null; 107 Connection conn = null; 108 int regionId = -1, userId = -1; 109 String 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 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 value = request.getParameter("region"); 136 if ((value != null) && (!value.equals(""))) 137 { 138 try 140 { 141 stmt = conn.prepareStatement("SELECT id FROM regions WHERE name=?"); 142 stmt.setString(1, value); 143 ResultSet 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 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 request, HttpServletResponse response) 171 throws IOException , ServletException 172 { 173 doGet(request, response); 174 } 175 176 179 public void destroy() 180 { 181 super.destroy(); 182 } 183 184 } 185 | Popular Tags |