1 24 25 package edu.rice.rubbos.servlets; 26 27 import java.io.IOException ; 28 import java.sql.Connection ; 29 import java.sql.PreparedStatement ; 30 import java.sql.ResultSet ; 31 32 import javax.servlet.ServletException ; 33 import javax.servlet.http.HttpServletRequest ; 34 import javax.servlet.http.HttpServletResponse ; 35 36 public class AcceptStory extends RubbosHttpServlet 37 { 38 39 public int getPoolSize() 40 { 41 return Config.BrowseCategoriesPoolSize; 42 } 43 44 private void closeConnection(PreparedStatement stmt, Connection conn) 45 { 46 try 47 { 48 if (stmt != null) 49 stmt.close(); } 51 catch (Exception ignore) 52 { 53 } 54 55 try 56 { 57 if (conn != null) 58 releaseConnection(conn); 59 } 60 catch (Exception ignore) 61 { 62 } 63 64 } 65 66 67 public void doGet(HttpServletRequest request, HttpServletResponse response) 68 throws IOException , ServletException 69 { 70 71 ServletPrinter sp = null; 72 Connection conn = null; 73 PreparedStatement stmt = null, stmtdel = null; 74 75 sp = new ServletPrinter(response, "AcceptStory"); 76 sp.printHTMLheader("RUBBoS: Story submission result"); 77 sp.printHTML("<center><h2>Story submission result:</h2></center><p>\n"); 78 79 conn = getConnection(); 80 81 int storyId = (Integer.valueOf(request.getParameter("storyId"))).intValue(); 82 83 if (storyId == 0) 84 { 85 sp.printHTML("<h3>You must provide a story identifier !<br></h3>"); 86 return; 87 } 88 89 ResultSet rs = null; 90 int updateResult; 91 92 try 93 { 94 stmt = conn 95 .prepareStatement("SELECT * FROM submissions WHERE id= storyId"); 96 rs = stmt.executeQuery(); 97 } 98 catch (Exception e) 99 { 100 sp.printHTML(" Failed to execute Query for AcceptStory: " + e); 101 closeConnection(stmt, conn); 102 return; 103 } 104 try 105 { 106 if (!rs.first()) 107 { 108 sp 109 .printHTML("<h3>ERROR: Sorry, but this story does not exist.</h3><br>"); 110 closeConnection(stmt, conn); 111 return; 112 } 113 114 String categoryTitle = rs.getString("title"); 116 int categoryDate = rs.getInt("date"); 117 String categoryBody = rs.getString("body"); 118 String categoryWriter = rs.getString("writer"); 119 String category = rs.getString("category"); 120 121 stmt = conn.prepareStatement("INSERT INTO stories VALUES (NULL, \"" 122 + categoryTitle + "\", \"" + categoryBody + "\", '" + categoryDate 123 + "', " + categoryWriter + ", " + category + ")"); 124 stmtdel = conn 125 .prepareStatement("DELETE FROM submissions WHERE id=storyId"); 126 127 updateResult = stmt.executeUpdate(); 128 updateResult = stmtdel.executeUpdate(); 129 } 130 catch (Exception e) 131 { 132 sp.printHTML("Exception accepting stories: " + e + "<br>"); 133 closeConnection(stmt, conn); 134 return; 135 } 136 137 closeConnection(stmt, conn); 138 139 sp 140 .printHTML("The story has been successfully moved from the submission to the stories database table<br>\n"); 141 sp.printHTMLfooter(); 142 143 } 144 145 public void doPost(HttpServletRequest request, HttpServletResponse response) 146 throws IOException , ServletException 147 { 148 doGet(request, response); 149 } 150 151 } 152 | Popular Tags |