KickJava   Java API By Example, From Geeks To Geeks.

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


1 package edu.rice.rubis.beans.servlets;
2
3 import java.io.IOException JavaDoc;
4
5 import javax.jms.MapMessage JavaDoc;
6 import javax.jms.Session JavaDoc;
7 import javax.jms.Topic JavaDoc;
8 import javax.jms.TopicConnection JavaDoc;
9 import javax.jms.TopicConnectionFactory JavaDoc;
10 import javax.jms.TopicPublisher JavaDoc;
11 import javax.jms.TopicSession JavaDoc;
12 import javax.naming.Context JavaDoc;
13 import javax.naming.InitialContext JavaDoc;
14 import javax.servlet.ServletException JavaDoc;
15 import javax.servlet.http.HttpServlet JavaDoc;
16 import javax.servlet.http.HttpServletRequest JavaDoc;
17 import javax.servlet.http.HttpServletResponse JavaDoc;
18
19 /** This servlet records a bid in the database and display
20  * the result of the transaction.
21  * It must be called this way :
22  * <pre>
23  * http://..../StoreBid?itemId=aa&userId=bb&minBid=cc&maxQty=dd&bid=ee&maxBid=ff&qty=gg
24  * where: aa is the item id
25  * bb is the user id
26  * cc is the minimum acceptable bid for this item
27  * dd is the maximum quantity available for this item
28  * ee is the user bid
29  * ff is the maximum bid the user wants
30  * gg is the quantity asked by the user
31  * </pre>
32  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
33  * @version 1.0
34  */

35
36 public class StoreBid extends HttpServlet JavaDoc
37 {
38
39   private void printError(String JavaDoc errorMsg, ServletPrinter sp)
40   {
41     sp.printHTMLheader("RUBiS ERROR: StoreBid");
42     sp.printHTML("<h2>Your request has not been processed due to the following error :</h2><br>");
43     sp.printHTML(errorMsg);
44     sp.printHTMLfooter();
45   }
46
47
48   /**
49    * Call the <code>doPost</code> method.
50    *
51    * @param request a <code>HttpServletRequest</code> value
52    * @param response a <code>HttpServletResponse</code> value
53    * @exception IOException if an error occurs
54    * @exception ServletException if an error occurs
55    */

56   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc
57   {
58     doPost(request, response);
59   }
60
61   /**
62    * Store the bid to the database and display resulting message.
63    *
64    * @param request a <code>HttpServletRequest</code> value
65    * @param response a <code>HttpServletResponse</code> value
66    * @exception IOException if an error occurs
67    * @exception ServletException if an error occurs
68    */

69   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc
70   {
71     ServletPrinter sp = null;
72     Context JavaDoc initialContext = null;
73     Integer JavaDoc userId; // item id
74
Integer JavaDoc itemId; // user id
75
float minBid; // minimum acceptable bid for this item
76
float bid; // user bid
77
float maxBid; // maximum bid the user wants
78
int maxQty; // maximum quantity available for this item
79
int qty; // quantity asked by the user
80

81     sp = new ServletPrinter(response, "StoreBid");
82
83     /* Get and check all parameters */
84
85     String JavaDoc value = request.getParameter("userId");
86     if ((value == null) || (value.equals("")))
87     {
88       printError("<h3>You must provide a user identifier !<br></h3>", sp);
89       return ;
90     }
91     else
92       userId = new Integer JavaDoc(value);
93
94     value = request.getParameter("itemId");
95     if ((value == null) || (value.equals("")))
96     {
97       printError("<h3>You must provide an item identifier !<br></h3>", sp);
98       return ;
99     }
100     else
101       itemId = new Integer JavaDoc(value);
102
103     value = request.getParameter("minBid");
104     if ((value == null) || (value.equals("")))
105     {
106       printError("<h3>You must provide a minimum bid !<br></h3>", sp);
107       return ;
108     }
109     else
110     {
111       Float JavaDoc foo = new Float JavaDoc(value);
112       minBid = foo.floatValue();
113     }
114
115     value = request.getParameter("bid");
116     if ((value == null) || (value.equals("")))
117     {
118       printError("<h3>You must provide a bid !<br></h3>", sp);
119       return ;
120     }
121     else
122     {
123       Float JavaDoc foo = new Float JavaDoc(value);
124       bid = foo.floatValue();
125     }
126
127     value = request.getParameter("maxBid");
128     if ((value == null) || (value.equals("")))
129     {
130       printError("<h3>You must provide a maximum bid !<br></h3>", sp);
131       return ;
132     }
133     else
134     {
135       Float JavaDoc foo = new Float JavaDoc(value);
136       maxBid = foo.floatValue();
137     }
138
139     value = request.getParameter("maxQty");
140     if ((value == null) || (value.equals("")))
141     {
142       printError("<h3>You must provide a maximum quantity !<br></h3>", sp);
143       return ;
144     }
145     else
146     {
147       Integer JavaDoc foo = new Integer JavaDoc(value);
148       maxQty = foo.intValue();
149     }
150
151     value = request.getParameter("qty");
152     if ((value == null) || (value.equals("")))
153     {
154       printError("<h3>You must provide a quantity !<br></h3>", sp);
155       return ;
156     }
157     else
158     {
159       Integer JavaDoc foo = new Integer JavaDoc(value);
160       qty = foo.intValue();
161     }
162
163     /* Check for invalid values */
164
165     if (qty > maxQty)
166     {
167       printError("<h3>You cannot request "+qty+" items because only "+maxQty+" are proposed !<br></h3>", sp);
168       return ;
169     }
170     if (bid < minBid)
171     {
172       printError("<h3>Your bid of $"+bid+" is not acceptable because it is below the $"+minBid+" minimum bid !<br></h3>", sp);
173       return ;
174     }
175     if (maxBid < minBid)
176     {
177       printError("<h3>Your maximum bid of $"+maxBid+" is not acceptable because it is below the $"+minBid+" minimum bid !<br></h3>", sp);
178       return ;
179     }
180     if (maxBid < bid)
181     {
182       printError("<h3>Your maximum bid of $"+maxBid+" is not acceptable because it is below your current bid of $"+bid+" !<br></h3>", sp);
183       return ;
184     }
185
186     try
187     {
188       initialContext = new InitialContext JavaDoc();
189     }
190     catch (Exception JavaDoc e)
191     {
192       printError("Cannot get initial context for JNDI: " + e+"<br>", sp);
193       return ;
194     }
195
196     TopicConnectionFactory JavaDoc topicFactory = null;
197     TopicConnection JavaDoc connection = null;
198     TopicSession JavaDoc session = null;
199     Topic JavaDoc topic = null;
200     try
201     {
202       // lookup the connection factory
203
topicFactory = (TopicConnectionFactory JavaDoc)initialContext.lookup(Config.TopicConnectionFactoryName);
204       // create a connection to the JMS provider
205
connection = topicFactory.createTopicConnection();
206       // lookup the destination
207
topic = (Topic JavaDoc) initialContext.lookup(Config.PrefixTopicName+"topicStoreBid");
208       // create a session
209
session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); // no transaction and auto ack
210
}
211     catch (Exception JavaDoc e)
212     {
213       sp.printHTML("Cannot connect to message bean MDB_StoreBid : " +e+"<br>");
214       return ;
215     }
216     try
217     {
218       sp.printHTMLheader("RUBiS: Bidding result");
219       // create a sender
220
TopicPublisher JavaDoc sender = session.createPublisher(topic); // identified publisher
221
// create a message
222
MapMessage JavaDoc message = session.createMapMessage();
223       // set parameters
224
message.setInt("userId", userId.intValue());
225       message.setInt("itemId", itemId.intValue());
226       message.setFloat("bid", bid);
227       message.setFloat("maxBid", maxBid);
228       message.setInt("quantity", qty);
229       // send the message
230
connection.start(); // allows message to be delivered (default is connection stopped)
231
sender.publish(message);
232       connection.stop();
233       // close connection and session
234
sender.close();
235       session.close();
236       connection.close();
237     }
238     catch (Exception JavaDoc e)
239     {
240       printError("Error while storing the bid (got exception: " +e+")<br>", sp);
241       return ;
242     }
243     sp.printHTML("<center><h2>Your bid has been successfully processed.</h2></center>\n");
244     sp.printHTMLfooter();
245   }
246
247 }
248
Popular Tags