KickJava   Java API By Example, From Geeks To Geeks.

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


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.TextMessage JavaDoc;
8 import javax.jms.Topic JavaDoc;
9 import javax.jms.TopicConnection JavaDoc;
10 import javax.jms.TopicConnectionFactory JavaDoc;
11 import javax.jms.TopicRequestor JavaDoc;
12 import javax.jms.TopicSession JavaDoc;
13 import javax.naming.Context JavaDoc;
14 import javax.naming.InitialContext JavaDoc;
15 import javax.servlet.ServletException JavaDoc;
16 import javax.servlet.http.HttpServlet JavaDoc;
17 import javax.servlet.http.HttpServletRequest JavaDoc;
18 import javax.servlet.http.HttpServletResponse JavaDoc;
19
20 /**
21  * Build the html page with the list of all items for given category and region.
22  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
23  * @version 1.0
24  */

25 public class SearchItemsByRegion extends HttpServlet JavaDoc
26 {
27  
28   private void printError(String JavaDoc errorMsg, ServletPrinter sp)
29   {
30     sp.printHTMLheader("RUBiS ERROR: SearchItemsByRegion");
31     sp.printHTML("<h2>Your request has not been processed due to the following error :</h2><br>");
32     sp.printHTML(errorMsg);
33     sp.printHTMLfooter();
34   }
35
36
37   /** List items in the given category for the given region */
38   private void itemList(Integer JavaDoc categoryId, Integer JavaDoc regionId, int page, int nbOfItems, ServletPrinter sp, Context JavaDoc initialContext)
39   {
40     TopicConnectionFactory JavaDoc topicFactory = null;
41     TopicConnection JavaDoc connection = null;
42     TopicSession JavaDoc session = null;
43     Topic JavaDoc topic = null;
44     String JavaDoc list;
45     try
46     {
47       // lookup the connection factory
48
topicFactory = (TopicConnectionFactory JavaDoc)initialContext.lookup(Config.TopicConnectionFactoryName);
49       // create a connection to the JMS provider
50
connection = topicFactory.createTopicConnection();
51       // lookup the destination
52
topic = (Topic JavaDoc) initialContext.lookup(Config.PrefixTopicName+"topicSearchItemsByRegion");
53       // create a session
54
session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); // no transaction and auto ack
55
}
56     catch (Exception JavaDoc e)
57     {
58       sp.printHTML("Cannot connect to message bean MDB_SearchItemsByRegion : " +e+"<br>");
59       return ;
60     }
61     try
62     {
63       // create a requestor to receive the reply
64
TopicRequestor JavaDoc requestor = new TopicRequestor JavaDoc(session, topic);
65       // create a message
66
MapMessage JavaDoc message = session.createMapMessage();
67       // set parameters
68
message.setInt("categoryId", categoryId.intValue());
69       message.setInt("regionId", regionId.intValue());
70       message.setInt("page", page);
71       message.setInt("nbItems", nbOfItems);
72       message.setJMSCorrelationID("searchByRegion");
73       // send the message and receive the reply
74
connection.start(); // allows message to be delivered (default is connection stopped)
75
TextMessage JavaDoc reply = (TextMessage JavaDoc)requestor.request(message);
76       connection.stop();
77       // read the reply
78
list = reply.getText();
79       // close connection and session
80
requestor.close(); // also close the session
81
connection.close();
82     }
83     catch (Exception JavaDoc e)
84     {
85       sp.printHTML("Cannot get the list of items: " +e+"<br>");
86       return ;
87     }
88     try
89     {
90       if ((list != null) && (!list.equals("")))
91       {
92         sp.printItemHeader();
93         sp.printHTML(list);
94         sp.printItemFooter();
95       }
96       else
97       {
98         if (page == 0)
99           sp.printHTML("<h3>Sorry, but there is no items in this category for this region.</h3><br>");
100         else
101         {
102           sp.printHTML("<h3>Sorry, but there is no more items in this category for this region.</h3><br>");
103           sp.printItemHeader();
104           sp.printItemFooter("<a HREF=\""+Config.context+"/servlet/edu.rice.rubis.beans.servlets.SearchItemsByRegion?category="+categoryId+
105                              "&region="+regionId+"&page="+(page-1)+"&nbOfItems="+nbOfItems+"\">Previous page</a>", "");
106         }
107         return ;
108       }
109       if (page == 0)
110         sp.printItemFooter("", "<a HREF=\""+Config.context+"/servlet/edu.rice.rubis.beans.servlets.SearchItemsByRegion?category="+categoryId+
111                            "&region="+regionId+"&page="+(page+1)+"&nbOfItems="+nbOfItems+"\">Next page</a>");
112       else
113         sp.printItemFooter("<a HREF=\""+Config.context+"/servlet/edu.rice.rubis.beans.servlets.SearchItemsByRegion?category="+categoryId+
114                            "&region="+regionId+"&page="+(page-1)+"&nbOfItems="+nbOfItems+"\">Previous page</a>",
115                            "<a HREF=\"/servlet/edu.rice.rubis.beans.servlets.SearchItemsByRegion?category="+categoryId+
116                            "&region="+regionId+"&page="+(page+1)+"&nbOfItems="+nbOfItems+"\">Next page</a>");
117     }
118     catch (Exception JavaDoc e)
119     {
120       sp.printHTML("Exception getting item list: " + e +"<br>");
121     }
122   }
123
124   /**
125    * Read the parameters, lookup the remote category and region
126    * and build the web page with the list of items
127    *
128    * @param request a <code>HttpServletRequest</code> value
129    * @param response a <code>HttpServletResponse</code> value
130    * @exception IOException if an error occurs
131    * @exception ServletException if an error occurs
132    */

133   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc
134   {
135     ServletPrinter sp = null;
136     Context JavaDoc initialContext = null;
137
138     Integer JavaDoc categoryId, regionId;
139     Integer JavaDoc page;
140     Integer JavaDoc nbOfItems;
141
142     sp = new ServletPrinter(response, "SearchItemsByRegion");
143
144     try
145     {
146       initialContext = new InitialContext JavaDoc();
147     }
148     catch (Exception JavaDoc e)
149     {
150       printError("Cannot get initial context for JNDI: " + e+"<br>", sp);
151       return ;
152     }
153
154     String JavaDoc value = request.getParameter("category");
155     if ((value == null) || (value.equals("")))
156     {
157       printError("You must provide a category!<br>", sp);
158       return ;
159     }
160     else
161       categoryId = new Integer JavaDoc(value);
162
163     value = request.getParameter("region");
164     if ((value == null) || (value.equals("")))
165     {
166       printError("You must provide a region!<br>", sp);
167       return ;
168     }
169     else
170       regionId = new Integer JavaDoc(value);
171
172     value = request.getParameter("page");
173     if ((value == null) || (value.equals("")))
174       page = new Integer JavaDoc(0);
175     else
176       page = new Integer JavaDoc(value);
177
178     value = request.getParameter("nbOfItems");
179     if ((value == null) || (value.equals("")))
180       nbOfItems = new Integer JavaDoc(25);
181     else
182       nbOfItems = new Integer JavaDoc(value);
183
184     sp.printHTMLheader("RUBiS: Search items by region");
185     sp.printHTML("<h2>Items in this region</h2><br><br>");
186      
187     itemList(categoryId, regionId, page.intValue(), nbOfItems.intValue(), sp, initialContext);
188         
189     sp.printHTMLfooter();
190   }
191
192
193   /**
194    * Call the <code>doGet</code> method.
195    *
196    * @param request a <code>HttpServletRequest</code> value
197    * @param response a <code>HttpServletResponse</code> value
198    * @exception IOException if an error occurs
199    * @exception ServletException if an error occurs
200    */

201   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc
202   {
203     doGet(request, response);
204   }
205 }
206
Popular Tags