Commit 1c295ad0 authored by Hakim Ouhida's avatar Hakim Ouhida
Browse files

overriding history and changing choice is working now

parent a96abfa4
......@@ -90,7 +90,7 @@ public class AddToHistory extends HttpServlet {
HttpSession session = request.getSession(true);
int pos = (request.getAttribute("position") != null)? ((Integer)request.getAttribute("position")).intValue() : -1;
int pos = (request.getParameter("position") != null)? (Integer.parseInt(request.getParameter("position"))) : -1;
Paragraph p = (Paragraph) request.getAttribute("paragraph");
System.out.println("history" + ((Integer)request.getAttribute("storyid")).intValue());
HashMap<Integer, History> history = (HashMap<Integer, History>)session.getAttribute("history");
......
......@@ -181,7 +181,7 @@ public class LookupStory extends HttpServlet {
request.setAttribute("edit", edit);
request.setAttribute("owner", owner);
request.setAttribute("action", "1");
System.out.println("looking up history...");
this.getServletContext().getRequestDispatcher("/LookupHistory").include(request, response);
System.out.println(((HashMap<Integer, History>)session.getAttribute("history")).get(story.getId()).size());
request.setAttribute("history", session.getAttribute("history"));
......
......@@ -36,28 +36,36 @@ public class HistoryDAO extends AbstractDAO {
while (rs.next()) {
temp.put(rs.getInt("indice"), rs.getInt("id_paragraphe"));
}
System.out.println("in loadHistory..");
int previous = -1;
for (int i=0; i<temp.size(); i++) {
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")));
history.addParagraph(previous, new Paragraph(rs.getInt("id_paragraphe"),rs.getString("texte"),rs.getString("auteur"),rs.getBoolean("isconclusion"), rs.getBoolean("valide")));
previous = rs.getInt("id_paragraphe");
}
System.out.println("loading history succeeded");
return history;
} catch (SQLException e) {
e.printStackTrace();
throw new DAOException("load history Database access error :( : " + e.getMessage(), e);
}
}
public void saveHistory(String username, int story_id, History history) {
cleanHistory(username, story_id);
try (Connection conn = getConn()) {
for (int i=0; i<history.size(); i++) {
int i = 0;
for (Paragraph p : history.getParagraphs()) {
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.setString(1, username);
st.setInt(2, story_id);
st.setInt(3, i);
st.setInt(4, p.getId());
st.executeQuery();
i++;
}
System.out.println("saveHistory suceeded");
} catch (SQLException e) {
......@@ -65,5 +73,17 @@ public class HistoryDAO extends AbstractDAO {
throw new DAOException("save history Database access error :( : " + e.getMessage(), e);
}
}
public void cleanHistory(String username, int story_id) {
try (Connection conn = getConn()) {
PreparedStatement st = conn.prepareStatement("DELETE FROM progression WHERE username = ? AND id_histoire = ?");
st.setString(1, username);
st.setInt(2, story_id);
st.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
throw new DAOException("save history Database access error :( : " + e.getMessage(), e);
}
}
}
......@@ -6,26 +6,20 @@
package modele;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
/**
*
* @author hakim
*/
public class History {
public class History implements Iterable<Paragraph> {
private boolean upToDate = true;
private Paragraph headParagraph;
private ArrayList<Paragraph> paragraphs = new ArrayList<>();
public void addParagraph(Paragraph p) {
paragraphs.add(p);
upToDate = false;
}
public Paragraph getParagraph(int index) {
return paragraphs.get(index);
}
public ArrayList<Paragraph> getParagraphs() {
return paragraphs;//TODO new copy ?
return paragraphs;
}
public boolean isUpToDate() {
......@@ -36,20 +30,57 @@ public class History {
upToDate = true;
}
public void addParagraph(int pos, Paragraph p) {
if (pos == -1 || pos >= paragraphs.size()) {
paragraphs.add(p);
} else {
ArrayList<Paragraph> newList = new ArrayList<>();
for (int i=0; i<pos; i++) {
newList.add(paragraphs.get(i));
public void addParagraph(int position, Paragraph paragraph) {
System.out.println("position: " + position);
if (headParagraph == null || position == -1) {
headParagraph = paragraph;
return;
}
Paragraph current = headParagraph;
int i = 0;
while (current != null) {
if (current.getId() == position) {
current.setNext(paragraph);
break;
}
paragraphs.add(p);
System.out.println("current: " + current.getId());
current = current.getNext();
i++;
if (i > 500) break;
}
resetParagraphs();
}
private void resetParagraphs() {
int i = 0;
paragraphs = new ArrayList<>();
Paragraph current = headParagraph;
while (current != null) {
i++;
paragraphs.add(current);
current = current.getNext();
System.out.println(i);
if (i > 500) break;
}
upToDate = false;
}
public int size() {
return paragraphs.size();
int size = 0;
Paragraph current = headParagraph;
while (current != null) {
size++;
current = current.getNext();
System.out.println(size);
if (size > 500) break;
}
return size;
}
@Override
public Iterator iterator() {
return headParagraph;
}
}
......@@ -5,17 +5,21 @@
*/
package modele;
import java.util.Iterator;
/**
*
* @author pangpangguy
*/
public class Paragraph {
public class Paragraph implements Iterator {
private int id;
private String texte;
private String auteur;
private boolean isConclusion;
private boolean valide;
private Paragraph next;
private Paragraph prev;
public boolean isValide() {
return valide;
......@@ -27,6 +31,8 @@ public class Paragraph {
this.auteur = auteur;
this.isConclusion = isConclusion;
this.valide= valide;
this.next = null;
this.prev = null;
}
public int getId() {
......@@ -44,4 +50,31 @@ public class Paragraph {
public boolean getIsConclusion() {
return isConclusion;
}
@Override
public boolean equals(Object o) {
return ((o instanceof Paragraph) && (((Paragraph)o).id == this.id));
}
public void setNext(Paragraph p) {
next = p;
}
public void setPrev(Paragraph p) {
prev = p;
}
public Paragraph getNext() {
return next;
}
@Override
public boolean hasNext() {
return next != null;
}
@Override
public Object next() {
return next;
}
}
......@@ -39,7 +39,7 @@ function choose(sourceid, destinationid,story_id,mode,async){
url:"LookupParagraph",
type: "POST",
async: async,
data:({ paragraph_id: destinationid,storyid: story_id, mode: mode}),
data:({position:sourceid, paragraph_id: destinationid,storyid: story_id, mode: mode}),
dataType:"text",
complete:function( data ) {
console.log(data.responseText);
......
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