KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.GregorianCalendar JavaDoc;
9
10 import javax.servlet.ServletException JavaDoc;
11 import javax.servlet.http.HttpServletRequest JavaDoc;
12 import javax.servlet.http.HttpServletResponse JavaDoc;
13
14 /**
15  * Add a new item in the database
16  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
17  * @version 1.0
18  */

19 public class RegisterItem extends RubisHttpServlet
20 {
21   
22
23   public int getPoolSize()
24   {
25     return Config.RegisterItemPoolSize;
26   }
27
28 /**
29  * Close both statement and connection.
30  */

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

49   private void printError(String JavaDoc 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   /** Check the values from the html register item form and create a new item */
60   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
61     throws IOException JavaDoc, ServletException JavaDoc
62   {
63     String JavaDoc name = null, description = null;
64     float initialPrice, buyNow, reservePrice;
65     Float JavaDoc stringToFloat;
66     int quantity, duration;
67     Integer JavaDoc categoryId, userId, stringToInt;
68     String JavaDoc startDate, endDate;
69     int itemId;
70
71     ServletPrinter sp = null;
72     sp = new ServletPrinter(response, "RegisterItem");
73
74     String JavaDoc 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 JavaDoc(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 JavaDoc(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 JavaDoc(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 JavaDoc(value);
135       duration = stringToInt.intValue();
136       GregorianCalendar JavaDoc now, later;
137       now = new GregorianCalendar JavaDoc();
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 JavaDoc(value);
152       quantity = stringToInt.intValue();
153     }
154
155     userId = new Integer JavaDoc(request.getParameter("userId"));
156     categoryId = new Integer JavaDoc(request.getParameter("categoryId"));
157
158      PreparedStatement JavaDoc stmt = null;
159      Connection JavaDoc conn = null;
160     try
161     {
162       
163       conn = getConnection();
164       conn.setAutoCommit(false); // faster if made inside a Tx
165

166       // Try to create a new item
167
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 JavaDoc 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       // To test if the item was correctly added in the database
206
try
207       {
208         stmt = conn.prepareStatement("SELECT id FROM items WHERE name=?");
209         stmt.setString(1, name);
210         ResultSet JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc se)
261       {
262         sp.printHTML("Transaction rollback failed: " + e + "<br>");
263         closeConnection(stmt, conn);
264       }
265     }
266   }
267
268   /**
269    * Call the doGet method: check the values from the html register item form
270    * and create a new item
271    */

272   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
273     throws IOException JavaDoc, ServletException JavaDoc
274   {
275     doGet(request, response);
276   }
277
278   /**
279    * Clean up the connection pool.
280    */

281   public void destroy()
282   {
283     super.destroy();
284   }
285 }
286
Popular Tags