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 import java.sql.SQLException ; 8 9 import javax.servlet.ServletException ; 10 import javax.servlet.http.HttpServletRequest ; 11 import javax.servlet.http.HttpServletResponse ; 12 13 29 30 public class StoreComment extends RubisHttpServlet 31 { 32 33 34 public int getPoolSize() 35 { 36 return Config.StoreCommentPoolSize; 37 } 38 39 42 private void closeConnection(PreparedStatement stmt, Connection conn) 43 { 44 try 45 { 46 if (stmt != null) 47 stmt.close(); if (conn != null) 49 releaseConnection(conn); 50 } 51 catch (Exception ignore) 52 { 53 } 54 } 55 56 60 private void printError(String errorMsg, ServletPrinter sp) 61 { 62 sp.printHTMLheader("RUBiS ERROR: StoreComment"); 63 sp.printHTML( 64 "<h2>Your request has not been processed due to the following error :</h2><br>"); 65 sp.printHTML(errorMsg); 66 sp.printHTMLfooter(); 67 68 } 69 70 public void doGet(HttpServletRequest request, HttpServletResponse response) 71 throws IOException , ServletException 72 { 73 doPost(request, response); 74 } 75 76 public void doPost(HttpServletRequest request, HttpServletResponse response) 77 throws IOException , ServletException 78 { 79 Integer toId; Integer fromId; Integer itemId; String comment; Integer rating; ServletPrinter sp = null; 85 PreparedStatement stmt = null; 86 Connection conn = null; 87 88 sp = new ServletPrinter(response, "StoreComment"); 89 90 91 92 String value = request.getParameter("to"); 93 if ((value == null) || (value.equals(""))) 94 { 95 printError("<h3>You must provide a 'to user' identifier !<br></h3>", sp); 96 return; 97 } 98 else 99 toId = new Integer (value); 100 101 value = request.getParameter("from"); 102 if ((value == null) || (value.equals(""))) 103 { 104 printError("<h3>You must provide a 'from user' identifier !<br></h3>", sp); 105 return; 106 } 107 else 108 fromId = new Integer (value); 109 110 value = request.getParameter("itemId"); 111 if ((value == null) || (value.equals(""))) 112 { 113 printError("<h3>You must provide an item identifier !<br></h3>", sp); 114 return; 115 } 116 else 117 itemId = new Integer (value); 118 119 value = request.getParameter("rating"); 120 if ((value == null) || (value.equals(""))) 121 { 122 printError("<h3>You must provide a rating !<br></h3>", sp); 123 return; 124 } 125 else 126 rating = new Integer (value); 127 128 comment = request.getParameter("comment"); 129 if ((comment == null) || (comment.equals(""))) 130 { 131 printError("<h3>You must provide a comment !<br></h3>", sp); 132 return; 133 } 134 135 try 136 { 137 conn = getConnection(); 138 conn.setAutoCommit(false); try 141 { 142 String now = TimeManagement.currentDateToString(); 143 stmt = 144 conn.prepareStatement( 145 "INSERT INTO comments VALUES (NULL, \"" 146 + fromId 147 + "\", \"" 148 + toId 149 + "\", \"" 150 + itemId 151 + "\", \"" 152 + rating 153 + "\", \"" 154 + now 155 + "\",\"" 156 + comment 157 + "\")"); 158 159 stmt.executeUpdate(); 160 stmt.close(); 161 } 162 catch (SQLException e) 163 { 164 conn.rollback(); 165 printError( 166 "Error while storing the comment (got exception: " + e + ")<br>", sp); 167 closeConnection(stmt, conn); 168 return; 169 } 170 try 172 { 173 ResultSet urs; 174 stmt = conn.prepareStatement("SELECT rating FROM users WHERE id=?"); 175 stmt.setInt(1, toId.intValue()); 176 urs = stmt.executeQuery(); 177 if (urs.first()) 178 { 179 int userRating = urs.getInt("rating"); 180 userRating = userRating + rating.intValue(); 181 182 stmt = conn.prepareStatement("UPDATE users SET rating=? WHERE id=?"); 183 stmt.setInt(1, userRating); 184 stmt.setInt(2, toId.intValue()); 185 stmt.executeUpdate(); 186 } 187 } 188 catch (SQLException e) 189 { 190 conn.rollback(); 191 printError( 192 "Error while updating user's rating (got exception: " + e + ")<br>", sp); 193 closeConnection(stmt, conn); 194 return; 195 } 196 sp.printHTMLheader("RUBiS: Comment posting"); 197 sp.printHTML( 198 "<center><h2>Your comment has been successfully posted.</h2></center>"); 199 200 sp.printHTMLfooter(); 201 conn.commit(); 202 closeConnection(stmt, conn); 203 } 204 catch (Exception e) 205 { 206 sp.printHTML("Exception getting comment list: " + e + "<br>"); 207 try 208 { 209 conn.rollback(); 210 closeConnection(stmt, conn); 211 } 212 catch (Exception se) 213 { 214 sp.printHTML("Transaction rollback failed: " + e + "<br>"); 215 closeConnection(stmt, conn); 216 } 217 } 218 } 219 220 223 public void destroy() 224 { 225 super.destroy(); 226 } 227 } 228 | Popular Tags |