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 import java.util.GregorianCalendar ; 9 10 import javax.servlet.ServletException ; 11 import javax.servlet.http.HttpServletRequest ; 12 import javax.servlet.http.HttpServletResponse ; 13 14 19 public class RegisterItem extends RubisHttpServlet 20 { 21 22 23 public int getPoolSize() 24 { 25 return Config.RegisterItemPoolSize; 26 } 27 28 31 private void closeConnection(PreparedStatement stmt, Connection conn) 32 { 33 try 34 { 35 if (stmt != null) 36 stmt.close(); if (conn != null) 38 releaseConnection(conn); 39 } 40 catch (Exception ignore) 41 { 42 } 43 } 44 45 49 private void printError(String errorMsg, ServletPrinter sp) 50 { 51 sp.printHTMLheader("RUBiS ERROR: Register Item"); 52 sp.printHTML( 53 "<h2>Your registration has not been processed due to the following error :</h2><br>"); 54 sp.printHTML(errorMsg); 55 sp.printHTMLfooter(); 56 57 } 58 59 60 public void doGet(HttpServletRequest request, HttpServletResponse response) 61 throws IOException , ServletException 62 { 63 String name = null, description = null; 64 float initialPrice, buyNow, reservePrice; 65 Float stringToFloat; 66 int quantity, duration; 67 Integer categoryId, userId, stringToInt; 68 String startDate, endDate; 69 int itemId; 70 71 ServletPrinter sp = null; 72 sp = new ServletPrinter(response, "RegisterItem"); 73 74 String value = request.getParameter("name"); 75 if ((value == null) || (value.equals(""))) 76 { 77 printError("You must provide a name!<br>", sp); 78 return; 79 } 80 else 81 name = value; 82 83 value = request.getParameter("description"); 84 if ((value == null) || (value.equals(""))) 85 { 86 description = "No description."; 87 } 88 else 89 description = value; 90 91 value = request.getParameter("initialPrice"); 92 if ((value == null) || (value.equals(""))) 93 { 94 printError("You must provide an initial price!<br>", sp); 95 return; 96 } 97 else 98 { 99 stringToFloat = new Float (value); 100 initialPrice = stringToFloat.floatValue(); 101 } 102 103 value = request.getParameter("reservePrice"); 104 if ((value == null) || (value.equals(""))) 105 { 106 reservePrice = 0; 107 } 108 else 109 { 110 stringToFloat = new Float (value); 111 reservePrice = stringToFloat.floatValue(); 112 113 } 114 115 value = request.getParameter("buyNow"); 116 if ((value == null) || (value.equals(""))) 117 { 118 buyNow = 0; 119 } 120 else 121 { 122 stringToFloat = new Float (value); 123 buyNow = stringToFloat.floatValue(); 124 } 125 126 value = request.getParameter("duration"); 127 if ((value == null) || (value.equals(""))) 128 { 129 printError("You must provide a duration!<br>", sp); 130 return; 131 } 132 else 133 { 134 stringToInt = new Integer (value); 135 duration = stringToInt.intValue(); 136 GregorianCalendar now, later; 137 now = new GregorianCalendar (); 138 later = TimeManagement.addDays(now, duration); 139 startDate = TimeManagement.dateToString(now); 140 endDate = TimeManagement.dateToString(later); 141 } 142 143 value = request.getParameter("quantity"); 144 if ((value == null) || (value.equals(""))) 145 { 146 printError("You must provide a quantity!<br>", sp); 147 return; 148 } 149 else 150 { 151 stringToInt = new Integer (value); 152 quantity = stringToInt.intValue(); 153 } 154 155 userId = new Integer (request.getParameter("userId")); 156 categoryId = new Integer (request.getParameter("categoryId")); 157 158 PreparedStatement stmt = null; 159 Connection conn = null; 160 try 161 { 162 163 conn = getConnection(); 164 conn.setAutoCommit(false); 166 try 168 { 169 stmt = 170 conn.prepareStatement( 171 "INSERT INTO items VALUES (NULL, \"" 172 + name 173 + "\", \"" 174 + description 175 + "\", \"" 176 + initialPrice 177 + "\", \"" 178 + quantity 179 + "\", \"" 180 + reservePrice 181 + "\", \"" 182 + buyNow 183 + "\", 0, 0, \"" 184 + startDate 185 + "\", \"" 186 + endDate 187 + "\", \"" 188 + userId 189 + "\", " 190 + categoryId 191 + ")"); 192 stmt.executeUpdate(); 193 stmt.close(); 194 } 195 catch (SQLException e) 196 { 197 conn.rollback(); 198 printError( 199 "RUBiS internal error: Item registration failed (got exception: " 200 + e 201 + ")<br>", sp); 202 closeConnection(stmt, conn); 203 return; 204 } 205 try 207 { 208 stmt = conn.prepareStatement("SELECT id FROM items WHERE name=?"); 209 stmt.setString(1, name); 210 ResultSet irs = stmt.executeQuery(); 211 if (!irs.first()) 212 { 213 conn.rollback(); 214 printError("This item does not exist in the database.", sp); 215 closeConnection(stmt, conn); 216 return; 217 } 218 itemId = irs.getInt("id"); 219 220 } 221 catch (SQLException e) 222 { 223 conn.rollback(); 224 printError("Failed to execute Query for the new item: " + e, sp); 225 closeConnection(stmt, conn); 226 return; 227 } 228 229 sp.printHTMLheader("RUBiS: Item to sell " + name); 230 sp.printHTML("<h2>Your Item has been successfully registered.</h2><br>"); 231 sp.printHTML( 232 "RUBiS has stored the following information about your item:<br>"); 233 sp.printHTML("Name : " + name + "<br>"); 234 sp.printHTML("Description : " + description + "<br>"); 235 sp.printHTML("Initial price: " + initialPrice + "<br>"); 236 sp.printHTML("ReservePrice : " + reservePrice + "<br>"); 237 sp.printHTML("Buy Now : " + buyNow + "<br>"); 238 sp.printHTML("Quantity : " + quantity + "<br>"); 239 sp.printHTML("User id :" + userId + "<br>"); 240 sp.printHTML("Category id :" + categoryId + "<br>"); 241 sp.printHTML("Duration : " + duration + "<br>"); 242 sp.printHTML( 243 "<br>The following information has been automatically generated by RUBiS:<br>"); 244 sp.printHTML("Start date :" + startDate + "<br>"); 245 sp.printHTML("End date :" + endDate + "<br>"); 246 sp.printHTML("item id :" + itemId + "<br>"); 247 248 conn.commit(); 249 sp.printHTMLfooter(); 250 closeConnection(stmt, conn); 251 } 252 catch (Exception e) 253 { 254 sp.printHTML("Exception getting comment list: " + e + "<br>"); 255 try 256 { 257 conn.rollback(); 258 closeConnection(stmt, conn); 259 } 260 catch (Exception se) 261 { 262 sp.printHTML("Transaction rollback failed: " + e + "<br>"); 263 closeConnection(stmt, conn); 264 } 265 } 266 } 267 268 272 public void doPost(HttpServletRequest request, HttpServletResponse response) 273 throws IOException , ServletException 274 { 275 doGet(request, response); 276 } 277 278 281 public void destroy() 282 { 283 super.destroy(); 284 } 285 } 286 | Popular Tags |