Commit 50b81ad1 authored by Hakim Ouhida's avatar Hakim Ouhida
Browse files

storing history is working

parent 7bdee226
......@@ -106,6 +106,7 @@ public class LookupHistory extends HttpServlet {
}
String username = (String)session.getAttribute("username");
History history = historyDAO.loadHistory(username, id);
if (history == null) System.out.println("loaded history is null");
histories.put(id, history);
session.setAttribute("history", histories);
//TODO response
......
......@@ -48,7 +48,7 @@ public class LookupStory extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
//try {
try {
switch (request.getParameter("code")) {
case "0":
......@@ -61,10 +61,9 @@ public class LookupStory extends HttpServlet {
default:
throw new IllegalStateException("Unexpected value: " + request.getParameter("code"));
}
//}
/*
catch (Exception e) {
} catch (Exception e) {
e.printStackTrace();
/*
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
......@@ -76,8 +75,8 @@ public class LookupStory extends HttpServlet {
out.println("<p>" + e.getStackTrace().toString() + "</p>");
out.println("</body>");
out.println("</html>");
*/
}
*/
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
......
......@@ -36,9 +36,13 @@ public class SessionListener implements HttpSessionListener {
HttpSession session = event.getSession();
if (session.getAttribute("username") != null && session.getAttribute("stories") != null) {
for (int id : ((HashSet<Integer>)session.getAttribute("stories"))) {
System.out.println("In session destruction");
String username = (String)session.getAttribute("username");
History history = ((HashMap<Integer, History>)session.getAttribute("history")).get(id);
if (history != null) new HistoryDAO(ds).saveHistory(username, id, history);
if (history != null) {
System.out.println("history not null");
new HistoryDAO(ds).saveHistory(username, id, history);
}
}
}
}
......
......@@ -34,12 +34,13 @@ public class HistoryDAO extends AbstractDAO {
st.setInt(2, id);
ResultSet rs = st.executeQuery();
while (rs.next()) {
temp.put(rs.getInt("index"), rs.getInt("id_paragraphe"));
temp.put(rs.getInt("indice"), rs.getInt("id_paragraphe"));
}
for (int i=0; i<temp.size(); i++) {
st = conn.prepareStatement("SELECT * FROM paragraph WHERE id_paragraph = ?");
st = conn.prepareStatement("SELECT * FROM paragraphe WHERE id_paragraphe = ?");
st.setInt(1, temp.get(i));
rs = st.executeQuery();
rs.next();
history.addParagraph(new Paragraph(rs.getInt("id_paragraphe"),rs.getString("texte"),rs.getString("auteur"),rs.getBoolean("isconclusion"), rs.getBoolean("valide")));
}
return history;
......@@ -50,19 +51,17 @@ public class HistoryDAO extends AbstractDAO {
public void saveHistory(String username, int story_id, History history) {
try (Connection conn = getConn()) {
String st = "INSERT INTO progression (username, id_histoire, index, id_paragraphe) VALUES";
for (int i=0; i<history.size(); i++) {
st += " (?, ?, ?, ?)";
PreparedStatement st = conn.prepareStatement("INSERT INTO progression (username, id_histoire, indice, id_paragraphe) VALUES (?, ?, ?, ?)");
st.setString(i + 1, username);
st.setInt(i + 2, story_id);
st.setInt(i + 3, i);
st.setInt(i + 4, history.getParagraph(i).getId());
st.executeQuery();
}
PreparedStatement pst = conn.prepareStatement(st);
for (int i=0; i<history.size(); i++) {
pst.setString(i + 1, username);
pst.setInt(i + 2, story_id);
pst.setInt(i + 3, i);
pst.setInt(i + 4, history.getParagraph(i).getId());
}
pst.executeQuery();
System.out.println("saveHistory suceeded");
} catch (SQLException e) {
e.printStackTrace();
throw new DAOException("save history Database access error :( : " + e.getMessage(), e);
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment