KickJava   Java API By Example, From Geeks To Geeks.

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


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 BuyNow in the database and display
20  * the result of the transaction.
21  * It must be called this way :
22  * <pre>
23  * http://..../StoreBuyNow?itemId=aa&userId=bb&minBuyNow=cc&maxQty=dd&BuyNow=ee&maxBuyNow=ff&qty=gg
24  * where: aa is the item id
25  * bb is the user id
26  * cc is the minimum acceptable BuyNow for this item
27  * dd is the maximum quantity available for this item
28  * ee is the user BuyNow
29  * ff is the maximum BuyNow 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 StoreBuyNow extends HttpServlet JavaDoc
37 {
38
39   private void printError(String JavaDoc errorMsg, ServletPrinter sp)
40   {
41     sp.printHTMLheader("RUBiS ERROR: StoreBuyNow");
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 BuyNow 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
74     Integer JavaDoc userId; // item id
75
Integer JavaDoc itemId; // user id
76
float minBuyNow; // minimum acceptable BuyNow for this item
77
float BuyNow; // user BuyNow
78
float maxBuyNow; // maximum BuyNow the user wants
79
int maxQty; // maximum quantity available for this item
80
int qty; // quantity asked by the user
81

82     sp = new ServletPrinter(response, "StoreBuyNow");
83
84     /* Get and check all parameters */
85
86     String JavaDoc value = request.getParameter("userId");
87     if ((value == null) || (value.equals("")))
88     {
89       printError("<h3>You must provide a user identifier !<br></h3>", sp);
90       return ;
91     }
92     else
93       userId = new Integer JavaDoc(value);
94
95     value = request.getParameter("itemId");
96     if ((value == null) || (value.equals("")))
97     {
98       printError("<h3>You must provide an item identifier !<br></h3>", sp);
99       return ;
100     }
101     else
102       itemId = new Integer JavaDoc(value);
103
104
105     value = request.getParameter("maxQty");
106     if ((value == null) || (value.equals("")))
107     {
108       printError("<h3>You must provide a maximum quantity !<br></h3>", sp);
109       return ;
110     }
111     else
112     {
113       Integer JavaDoc foo = new Integer JavaDoc(value);
114       maxQty = foo.intValue();
115     }
116
117     value = request.getParameter("qty");
118     if ((value == null) || (value.equals("")))
119     {
120       printError("<h3>You must provide a quantity !<br></h3>", sp);
121       return ;
122     }
123     else
124     {
125       Integer JavaDoc foo = new Integer JavaDoc(value);
126       qty = foo.intValue();
127     }
128
129     /* Check for invalid values */
130
131     if (qty > maxQty)
132     {
133       printError("<h3>You cannot request "+qty+" items because only "+maxQty+" are proposed !<br></h3>", sp);
134       return ;
135     }
136
137     try
138     {
139       initialContext = new InitialContext JavaDoc();
140     }
141     catch (Exception JavaDoc e)
142     {
143       printError("Cannot get initial context for JNDI: " + e+"<br>", sp);
144       return ;
145     }
146
147     TopicConnectionFactory JavaDoc topicFactory = null;
148     TopicConnection JavaDoc connection = null;
149     TopicSession JavaDoc session = null;
150     Topic JavaDoc topic = null;
151     try
152     {
153       // lookup the connection factory
154
topicFactory = (TopicConnectionFactory JavaDoc)initialContext.lookup(Config.TopicConnectionFactoryName);
155       // create a connection to the JMS provider
156
connection = topicFactory.createTopicConnection();
157       // lookup the destination
158
topic = (Topic JavaDoc) initialContext.lookup(Config.PrefixTopicName+"topicStoreBuyNow");
159       // create a session
160
session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); // no transaction and auto ack
161
}
162     catch (Exception JavaDoc e)
163     {
164       sp.printHTML("Cannot connect to message bean MDB_StoreBuyNow : " +e+"<br>");
165       return ;
166     }
167     try
168     {
169       sp.printHTMLheader("RUBiS: BuyNow result");
170       // create a sender
171
TopicPublisher JavaDoc sender = session.createPublisher(topic); // identified publisher
172
// create a message
173
MapMessage JavaDoc message = session.createMapMessage();
174       // set parameters
175
message.setInt("itemId", itemId.intValue());
176       message.setInt("userId", userId.intValue());
177       message.setInt("quantity", qty);
178       // send the message
179
connection.start(); // allows message to be delivered (default is connection stopped)
180
sender.publish(message);
181       connection.stop();
182       // close connection and session
183
sender.close();
184       session.close();
185       connection.close();
186     }
187     catch (Exception JavaDoc e)
188     {
189       printError("Error while storing the buyNow (got exception: " +e+")<br>", sp);
190       return ;
191     }
192     if (qty == 1)
193       sp.printHTML("<center><h2>Your have successfully bought this item.</h2></center>\n");
194     else
195       sp.printHTML("<center><h2>Your have successfully bought these items.</h2></center>\n");
196     sp.printHTMLfooter();
197   }
198
199 }
200
Popular Tags