KickJava   Java API By Example, From Geeks To Geeks.

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


1 package edu.rice.rubis.beans.servlets;
2
3 import java.io.IOException JavaDoc;
4 import java.util.GregorianCalendar JavaDoc;
5
6 import javax.jms.MapMessage JavaDoc;
7 import javax.jms.Session JavaDoc;
8 import javax.jms.TextMessage JavaDoc;
9 import javax.jms.Topic JavaDoc;
10 import javax.jms.TopicConnection JavaDoc;
11 import javax.jms.TopicConnectionFactory JavaDoc;
12 import javax.jms.TopicRequestor JavaDoc;
13 import javax.jms.TopicSession JavaDoc;
14 import javax.naming.Context JavaDoc;
15 import javax.naming.InitialContext JavaDoc;
16 import javax.servlet.ServletException JavaDoc;
17 import javax.servlet.http.HttpServlet JavaDoc;
18 import javax.servlet.http.HttpServletRequest JavaDoc;
19 import javax.servlet.http.HttpServletResponse JavaDoc;
20
21 import edu.rice.rubis.beans.TimeManagement;
22
23 /**
24  * Add a new item in the database
25  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
26  * @version 1.0
27  */

28 public class RegisterItem extends HttpServlet JavaDoc
29 {
30
31   private void printError(String JavaDoc errorMsg, ServletPrinter sp)
32   {
33     sp.printHTMLheader("RUBiS ERROR: Register user");
34     sp.printHTML("<h2>Your registration has not been processed due to the following error :</h2><br>");
35     sp.printHTML(errorMsg);
36     sp.printHTMLfooter();
37   }
38
39   /**
40    * Check the values from the html register item form and create a new item
41    * @param request a <code>HttpServletRequest</code> value
42    * @param response a <code>HttpServletResponse</code> value
43    * @exception IOException if an error occurs
44    * @exception ServletException if an error occurs
45    */

46   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc
47   {
48     ServletPrinter sp = null;
49     String JavaDoc name=null, description=null;
50     float initialPrice, buyNow, reservePrice;
51     Float JavaDoc stringToFloat;
52     int quantity, duration;
53     Integer JavaDoc categoryId, userId, stringToInt;
54     String JavaDoc startDate, endDate;
55     int itemId;
56
57     sp = new ServletPrinter(response, "RegisterItem");
58       
59     Context JavaDoc initialContext = null;
60     try
61     {
62       initialContext = new InitialContext JavaDoc();
63     }
64     catch (Exception JavaDoc e)
65     {
66       printError("Cannot get initial context for JNDI: " + e+"<br>", sp);
67       return ;
68     }
69
70     String JavaDoc value = request.getParameter("name");
71     if ((value == null) || (value.equals("")))
72     {
73       printError("You must provide a name!<br>", sp);
74       return ;
75     }
76     else
77       name = value;
78
79     value = request.getParameter("description");
80     if ((value == null) || (value.equals("")))
81     {
82       description="No description";
83     }
84     else
85       description = value;
86
87     value = request.getParameter("initialPrice");
88     if ((value == null) || (value.equals("")))
89     {
90       printError("You must provide an initial price!<br>", sp);
91       return ;
92     }
93     else
94     {
95       stringToFloat = new Float JavaDoc(value);
96       initialPrice = stringToFloat.floatValue();
97     }
98
99     value = request.getParameter("reservePrice");
100     if ((value == null) || (value.equals("")))
101     {
102       reservePrice = 0;
103     }
104     else
105     {
106       stringToFloat = new Float JavaDoc(value);
107       reservePrice = stringToFloat.floatValue();
108
109     }
110
111     value = request.getParameter("buyNow");
112     if ((value == null) || (value.equals("")))
113     {
114       buyNow = 0;
115     }
116     else
117     {
118       stringToFloat = new Float JavaDoc(value);
119       buyNow = stringToFloat.floatValue();
120     }
121  
122     value = request.getParameter("duration");
123     if ((value == null) || (value.equals("")))
124     {
125       printError("You must provide a duration!<br>", sp);
126       return ;
127     }
128     else
129     {
130       stringToInt = new Integer JavaDoc(value);
131       duration = stringToInt.intValue();
132       GregorianCalendar JavaDoc now, later;
133       now = new GregorianCalendar JavaDoc();
134       later = TimeManagement.addDays(now, duration);
135       startDate = TimeManagement.dateToString(now);
136       endDate = TimeManagement.dateToString(later);
137     }
138
139     value = request.getParameter("quantity");
140     if ((value == null) || (value.equals("")))
141     {
142       printError("You must provide a quantity!<br>", sp);
143       return ;
144     }
145     else
146     {
147       stringToInt = new Integer JavaDoc(value);
148       quantity = stringToInt.intValue();
149     }
150  
151     userId = new Integer JavaDoc(request.getParameter("userId"));
152     categoryId = new Integer JavaDoc(request.getParameter("categoryId"));
153
154     TopicConnectionFactory JavaDoc topicFactory = null;
155     TopicConnection JavaDoc connection = null;
156     TopicSession JavaDoc session = null;
157     Topic JavaDoc topic = null;
158     String JavaDoc html;
159     try
160     {
161       // lookup the connection factory
162
topicFactory = (TopicConnectionFactory JavaDoc)initialContext.lookup(Config.TopicConnectionFactoryName);
163       // create a connection to the JMS provider
164
connection = topicFactory.createTopicConnection();
165       // lookup the destination
166
topic = (Topic JavaDoc) initialContext.lookup(Config.PrefixTopicName+"topicRegisterItem");
167       // create a session
168
session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); // no transaction and auto ack
169
}
170     catch (Exception JavaDoc e)
171     {
172       sp.printHTML("Cannot connect to message bean MDB_RegisterItem : " +e+"<br>");
173       return ;
174     }
175     try
176     {
177       // create a requestor to receive the reply
178
TopicRequestor JavaDoc requestor = new TopicRequestor JavaDoc(session, topic);
179       // create a message
180
MapMessage JavaDoc message = session.createMapMessage();
181       // set parameters
182
message.setString("name", name);
183       message.setString("description", description);
184       message.setFloat("initialPrice", initialPrice);
185       message.setInt("quantity", quantity);
186       message.setFloat("reservePrice", reservePrice);
187       message.setFloat("buyNow", buyNow);
188       message.setString("startDate", startDate);
189       message.setString("endDate", endDate);
190       message.setInt("userId", userId.intValue());
191       message.setInt("categoryId", categoryId.intValue());
192       message.setJMSCorrelationID("registerItem");
193       // send the message and receive the reply
194
connection.start(); // allows message to be delivered (default is connection stopped)
195
TextMessage JavaDoc reply = (TextMessage JavaDoc)requestor.request(message);
196       connection.stop();
197       // read the reply
198
html = reply.getText();
199       // close connection and session
200
requestor.close(); // also close the session
201
connection.close();
202     }
203     catch (Exception JavaDoc e)
204     {
205       sp.printHTML("Item registration failed: " +e+"<br>");
206       return ;
207     }
208
209     sp.printHTMLheader("RUBiS: Selling "+name);
210
211     sp.printHTML("<center><h2>Your Item has been successfully registered.</h2></center><br>\n");
212     sp.printHTML("<b>RUBiS has stored the following information about your item:</b><br><p>\n");
213     sp.printHTML("<TABLE>\n");
214     sp.printHTML("<TR><TD>Name<TD>"+name+"\n");
215     sp.printHTML("<TR><TD>Description<TD>"+description+"\n");
216     sp.printHTML("<TR><TD>Initial price<TD>"+initialPrice+"\n");
217     sp.printHTML("<TR><TD>ReservePrice<TD>"+reservePrice+"\n");
218     sp.printHTML("<TR><TD>Buy Now<TD>"+buyNow+"\n");
219     sp.printHTML("<TR><TD>Quantity<TD>"+quantity+"\n");
220     sp.printHTML("<TR><TD>Duration<TD>"+duration+"\n");
221     sp.printHTML("</TABLE>\n");
222     sp.printHTML("<br><b>The following information has been automatically generated by RUBiS:</b><br>\n");
223     sp.printHTML("<TABLE>\n");
224     sp.printHTML("<TR><TD>Start date<TD>"+startDate+"\n");
225     sp.printHTML("<TR><TD>end date<TD>"+endDate+"\n");
226     sp.printHTML("<TR><TD>User id<TD>"+userId+"\n");
227     sp.printHTML("<TR><TD>Category id<TD>"+categoryId+"\n");
228     sp.printHTML(html);
229     sp.printHTML("</TABLE>\n");
230     sp.printHTMLfooter();
231    
232   }
233     
234  
235   /**
236    * Call the doGet method: check the values from the html register item form
237    * and create a new item
238    * @param request a <code>HttpServletRequest</code> value
239    * @param response a <code>HttpServletResponse</code> value
240    * @exception IOException if an error occurs
241    * @exception ServletException if an error occurs
242    */

243   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc
244   {
245     doGet(request, response);
246   }
247 }
248
Popular Tags