Commit 7dade4cc authored by Hakim Ouhida's avatar Hakim Ouhida
Browse files

resolved a bug in add choice

parent b6b00a96
......@@ -14,6 +14,7 @@ import modele.Paragraph;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.ServletException;
......@@ -23,6 +24,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;
import modele.History;
/**
*
......@@ -102,6 +104,7 @@ public class LookupParagraph extends HttpServlet {
Paragraph paragraph = paragraphDAO.getParagraph(Integer.parseInt(request.getParameter("paragraph_id")));
if(!paragraph.isValide() ){
if( !session.getAttribute("username").equals(paragraph.getAuteur())){
System.out.println("you are " + (String)session.getAttribute("username") + " author is " + paragraph.getAuteur());
try (PrintWriter out = response.getWriter()){
response.setContentType("text/html");
out.println("-1");
......@@ -129,7 +132,7 @@ public class LookupParagraph extends HttpServlet {
}
for (Choix choice:choiceList) {
choice.setRequired(new ChoixDAO(ds).getReadBefore(choice.getId()));
if(!allReadbefore(choice.getRequired())){
if(!allReadbefore(request, storyid, choice.getRequired())){
choice.setPublished(false);
}
}
......@@ -145,12 +148,18 @@ public class LookupParagraph extends HttpServlet {
}
private boolean allReadbefore(List<Integer> required) {
private boolean allReadbefore(HttpServletRequest request, int storyid, List<Integer> required) {
if(required==null){
return true;
}
//TODO return true if all elements in required are in history
return true;
HttpSession session = request.getSession(true);
HashMap<Integer, History> histories = (HashMap<Integer, History>) session.getAttribute("history");
if (histories == null) return false;
History history = histories.get(storyid);
if (history == null) return false;
return history.contains(required);
}
}
......@@ -68,7 +68,7 @@ public class LookupStory extends HttpServlet {
break;
case "4":
actionGetAllPublicStories(request, response,new StoryDAO(ds));
break;
default:
throw new IllegalStateException("Unexpected value: " + request.getParameter("code"));
}
......
......@@ -69,6 +69,7 @@ public class ChoixDAO extends AbstractDAO {
public boolean canLock(String username, int choiceID) {
try (Connection conn = getConn()) {
System.out.println(choiceID);
boolean can = true;
PreparedStatement st = conn.prepareStatement("SELECT * FROM choix WHERE id_choix=? AND NOT id_paragraphe_suite=NULL");
st.setInt(1, choiceID);
......
......@@ -122,10 +122,11 @@ public class StoryDAO extends AbstractDAO {
st.setString(1, username);
ResultSet rs = st.executeQuery();
while (rs.next()) {
stories.add(new Story(rs.getInt("id_histoire"), rs.getString("titre_histoire"),
stories.add(new Story(rs.getInt("id_histoire"), rs.getString("titre"),
rs.getInt("id_premier_paragraphe"), rs.getBoolean("publiee"), rs.getBoolean("publique")));
}
} catch (SQLException e) {
e.printStackTrace();
throw new DAOException("get user stories Database access error :( : " + e.getMessage(), e);
}
return stories;
......
......@@ -8,6 +8,7 @@ package modele;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
/**
*
......@@ -81,6 +82,18 @@ public class History implements Iterable<Paragraph> {
public Iterator iterator() {
return headParagraph;
}
public boolean contains(List<Integer> required) {
for (int pr_id : required) {
if (!contains(pr_id)) return false;
}
return true;
}
private boolean contains(int required) {
for (Paragraph p : paragraphs) {
if (p.getId() == required) return true;
}
return false;
}
}
......@@ -123,7 +123,7 @@ function submit_add_choice(paragraph_id, story_id) {
$.post("AddChoice", {paragraph_id: paragraph_id, title: text,story_id:story_id}).done(function(data){
console.log("worked")
$(`#add_choice${paragraph_id}`).empty().append(`<span class = "add_choice edit" style = "display:block;"onClick = "addChoice(${paragraph_id},${story_id})" id = "add_choice${paragraph_id}" > Add choice </span>`);
$(`#choices${paragraph_id}`).append(`<span class=\"choice\" onClick=\"newParagraphe(${paragraph_id}, ${data})\">`
$(`#choices${paragraph_id}`).append(`<span class=\"choice\" onClick=\"newParagraphe(${paragraph_id}, ${data})\" id=choice${data}>`
+`${text}</span>`);
}).fail(function (jqXHR, textStatus, errorThrown) {
return console.error(errorThrown)
......
<%@ page pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
......
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<div class="paragraph_contents" id="paragraph_contents${paragraph.id}">
<c:if test="${paragraph.auteur==sessionScope.username}" var="maVariable" scope="request">
<div class="modify edit" id="modifyParagraph${paragraph.id}" onclick="editParagraphe(${paragraph.id},${paragraph.isConclusion?'true':'false'},false)"><button>modifiyer</button></div>
<div class="modify edit" id="modifyParagraph${paragraph.id}" onclick="editParagraphe(${paragraph.id},${paragraph.isConclusion?'true':'false'},false)"><button>modify</button></div>
<c:if test="${deletable}" var="maVariable" scope="request">
<div class="delete edit" id="deleteParagraph${paragraph.id}" onclick="deleteParagraphe(${paragraph.id},${paragraph.isConclusion?'true':'false'})"><button>suprimer</button></div>
<div class="delete edit" id="deleteParagraph${paragraph.id}" onclick="deleteParagraphe(${paragraph.id},${paragraph.isConclusion?'true':'false'})"><button>delete</button></div>
</c:if>
</c:if>
<div class="edit">${paragraph.id}</div>
......
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