| 1 package edu.rice.rubis.beans.servlets; 2 3 import java.io.IOException ; 4 import java.util.GregorianCalendar ; 5 6 import javax.jms.MapMessage ; 7 import javax.jms.Session ; 8 import javax.jms.TextMessage ; 9 import javax.jms.Topic ; 10 import javax.jms.TopicConnection ; 11 import javax.jms.TopicConnectionFactory ; 12 import javax.jms.TopicRequestor ; 13 import javax.jms.TopicSession ; 14 import javax.naming.Context ; 15 import javax.naming.InitialContext ; 16 import javax.servlet.ServletException ; 17 import javax.servlet.http.HttpServlet ; 18 import javax.servlet.http.HttpServletRequest ; 19 import javax.servlet.http.HttpServletResponse ; 20 21 import edu.rice.rubis.beans.TimeManagement; 22 23 28 public class RegisterItem extends HttpServlet  29 { 30 31 private void printError(String 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 46 public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException  47 { 48 ServletPrinter sp = null; 49 String name=null, description=null; 50 float initialPrice, buyNow, reservePrice; 51 Float stringToFloat; 52 int quantity, duration; 53 Integer categoryId, userId, stringToInt; 54 String startDate, endDate; 55 int itemId; 56 57 sp = new ServletPrinter(response, "RegisterItem"); 58 59 Context initialContext = null; 60 try 61 { 62 initialContext = new InitialContext (); 63 } 64 catch (Exception e) 65 { 66 printError("Cannot get initial context for JNDI: " + e+"<br>", sp); 67 return ; 68 } 69 70 String 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 (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 (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 (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 (value); 131 duration = stringToInt.intValue(); 132 GregorianCalendar now, later; 133 now = new GregorianCalendar (); 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 (value); 148 quantity = stringToInt.intValue(); 149 } 150 151 userId = new Integer (request.getParameter("userId")); 152 categoryId = new Integer (request.getParameter("categoryId")); 153 154 TopicConnectionFactory topicFactory = null; 155 TopicConnection connection = null; 156 TopicSession session = null; 157 Topic topic = null; 158 String html; 159 try 160 { 161 topicFactory = (TopicConnectionFactory )initialContext.lookup(Config.TopicConnectionFactoryName); 163 connection = topicFactory.createTopicConnection(); 165 topic = (Topic ) initialContext.lookup(Config.PrefixTopicName+"topicRegisterItem"); 167 session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); } 170 catch (Exception e) 171 { 172 sp.printHTML("Cannot connect to message bean MDB_RegisterItem : " +e+"<br>"); 173 return ; 174 } 175 try 176 { 177 TopicRequestor requestor = new TopicRequestor (session, topic); 179 MapMessage message = session.createMapMessage(); 181 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 connection.start(); TextMessage reply = (TextMessage )requestor.request(message); 196 connection.stop(); 197 html = reply.getText(); 199 requestor.close(); connection.close(); 202 } 203 catch (Exception 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 243 public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException  244 { 245 doGet(request, response); 246 } 247 } 248 | Popular Tags |