1 package edu.rice.rubis.servlets; 2 3 import java.io.IOException ; 4 import java.sql.Connection ; 5 import java.sql.PreparedStatement ; 6 import java.sql.ResultSet ; 7 8 import javax.servlet.ServletException ; 9 import javax.servlet.http.HttpServletRequest ; 10 import javax.servlet.http.HttpServletResponse ; 11 12 23 24 public class PutComment extends RubisHttpServlet 25 { 26 27 28 public int getPoolSize() 29 { 30 return Config.PutCommentPoolSize; 31 } 32 33 36 private void closeConnection(PreparedStatement stmt, Connection conn) 37 { 38 try 39 { 40 if (stmt != null) 41 stmt.close(); if (conn != null) 43 releaseConnection(conn); 44 } 45 catch (Exception ignore) 46 { 47 } 48 } 49 50 54 private void printError(String errorMsg, ServletPrinter sp) 55 { 56 sp.printHTMLheader("RUBiS ERROR: PutComment"); 57 sp.printHTML( 58 "<h2>Your request has not been processed due to the following error :</h2><br>"); 59 sp.printHTML(errorMsg); 60 sp.printHTMLfooter(); 61 62 } 63 64 public void doGet(HttpServletRequest request, HttpServletResponse response) 65 throws IOException , ServletException 66 { 67 ServletPrinter sp = null; 68 69 String toStr = request.getParameter("to"); 70 String itemStr = request.getParameter("itemId"); 71 String name = request.getParameter("nickname"); 72 String pass = request.getParameter("password"); 73 sp = new ServletPrinter(response, "PubComment"); 74 75 if ((toStr == null) 76 || (toStr.equals("")) 77 || (itemStr == null) 78 || (itemStr.equals("")) 79 || (name == null) 80 || (name.equals("")) 81 || (pass == null) 82 || (pass.equals(""))) 83 { 84 printError("User id, name and password are required - Cannot process the request<br>", sp); 85 return; 86 } 87 88 PreparedStatement stmt = null; 89 Connection conn = null; 90 conn = getConnection(); 92 Auth auth = new Auth(conn, sp); 93 int userId = auth.authenticate(name, pass); 94 if (userId == -1) 95 { 96 printError("You don't have an account on RUBiS!<br>You have to register first.<br>", sp); 97 closeConnection(stmt, conn); 98 return; 99 } 100 101 103 try 104 { 105 Integer toId = new Integer (toStr); 106 Integer itemId = new Integer (itemStr); 107 ResultSet urs, irs; 108 String toName = null, itemName = null; 109 try 110 { 111 stmt = conn.prepareStatement("SELECT nickname FROM users WHERE id=?"); 112 stmt.setInt(1, toId.intValue()); 113 urs = stmt.executeQuery(); 114 if (urs.first()) 115 toName = urs.getString("nickname"); 116 stmt.close(); 117 } 118 catch (Exception e) 119 { 120 printError("Failed to execute Query for user: " + e, sp); 121 closeConnection(stmt, conn); 122 return; 123 } 124 try 125 { 126 stmt = conn.prepareStatement("SELECT name FROM items WHERE id=?"); 127 stmt.setInt(1, itemId.intValue()); 128 irs = stmt.executeQuery(); 129 if (irs.first()) 130 itemName = irs.getString("name"); 131 stmt.close(); 132 } 133 catch (Exception e) 134 { 135 printError("Failed to execute Query for item: " + e, sp); 136 closeConnection(stmt, conn); 137 return; 138 } 139 140 sp.printHTMLheader("RUBiS: Comment service"); 142 sp.printHTML( 143 "<center><h2>Give feedback about your experience with " 144 + toName 145 + "</h2><br>"); 146 sp.printHTML( 147 "<form action=\"/rubis_servlets/servlet/edu.rice.rubis.servlets.StoreComment\" method=POST>" 148 + "<input type=hidden name=to value=" 149 + toStr 150 + ">" 151 + "<input type=hidden name=from value=" 152 + userId 153 + ">" 154 + "<input type=hidden name=itemId value=" 155 + itemId 156 + ">" 157 + "<center><table>" 158 + "<tr><td><b>From</b><td>" 159 + name 160 + "<tr><td><b>To</b><td>" 161 + toName 162 + "<tr><td><b>About item</b><td>" 163 + itemName 164 + "<tr><td><b>Rating</b>" 165 + "<td><SELECT name=rating>" 166 + "<OPTION value=\"5\">Excellent</OPTION>" 167 + "<OPTION value=\"3\">Average</OPTION>" 168 + "<OPTION selected value=\"0\">Neutral</OPTION>" 169 + "<OPTION value=\"-3\">Below average</OPTION>" 170 + "<OPTION value=\"-5\">Bad</OPTION>" 171 + "</SELECT></table><p><br>" 172 + "<TEXTAREA rows=\"20\" cols=\"80\" name=\"comment\">Write your comment here</TEXTAREA><br><p>" 173 + "<input type=submit value=\"Post this comment now!\"></center><p>"); 174 } 175 catch (Exception e) 176 { 177 printError("This item does not exist (got exception: " + e + ")<br>", sp); 178 closeConnection(stmt, conn); 179 return; 180 } 181 closeConnection(stmt, conn); 182 sp.printHTMLfooter(); 183 } 184 185 public void doPost(HttpServletRequest request, HttpServletResponse response) 186 throws IOException , ServletException 187 { 188 doGet(request, response); 189 } 190 191 194 public void destroy() 195 { 196 super.destroy(); 197 } 198 } 199 | Popular Tags |