Commit 62aa3152 authored by Abdelmoujib Megzari's avatar Abdelmoujib Megzari
Browse files

some bugs solved and publish working

parent 2a416142
......@@ -7,6 +7,9 @@ package controleur;
import dao.ChoixDAO;
import dao.ParagraphDAO;
import modele.Choix;
import modele.Paragraph;
import java.io.IOException;
import java.io.PrintWriter;
import javax.annotation.Resource;
......@@ -100,12 +103,11 @@ public class AssociateParagraphChoice extends HttpServlet {
HttpServletResponse response,
ParagraphDAO paragraphDAO)
throws ServletException, IOException {
//TODO DELETE FOLLOWER IF != NULL AND NOT VALIDE ELSE REFUSE
HttpSession session = request.getSession(false);
if (session == null || session.getAttribute("username") == null) {
return;//TODO
}
//TODO SECURITY LEAK CAN CHANGE THE PARAGRAPHE ASSOCIATED TO A CHOICE EVEN IF HE THERE IS ALREADY ONE AND HE ISN'T THE AUTHOR
//TODO verify user is associated to this story
System.out.println("choice_id: " + request.getParameter("choice_id"));
int choiceID = Integer.parseInt(request.getParameter("choice_id"));
int paragraphFollowerID;
......@@ -116,6 +118,17 @@ public class AssociateParagraphChoice extends HttpServlet {
paragraphFollowerID = (Integer) (request.getAttribute("paragraphFollower"));
}
System.out.println(paragraphFollowerID);
Paragraph oldParagraph = new ChoixDAO(ds).getChoiceParagraph(choiceID);
if(oldParagraph!=null && !oldParagraph.isValide() && oldParagraph.getAuteur() == session.getAttribute("username")){
paragraphDAO.deleteParagraph(oldParagraph.getId());
}else if(oldParagraph!=null){
response.setContentType("text/html");
try (PrintWriter out = response.getWriter()){
out.println(-1);
}
return;
}
paragraphDAO.associateParagraphChoice(choiceID, paragraphFollowerID);
}
......
......@@ -5,6 +5,7 @@
*/
package controleur;
import dao.ChoixDAO;
import dao.StoryDAO;
import dao.ParagraphDAO;
import modele.Choix;
......@@ -126,6 +127,12 @@ public class LookupParagraph extends HttpServlet {
}
choiceList = newChoiceList;
}
for (Choix choice:choiceList) {
choice.setRequired(new ChoixDAO(ds).getReadBefore(choice.getId()));
if(!allReadbefore(choice.getRequired())){
choice.setPublished(false);
}
}
boolean deletable = paragraphDAO.isDeletable(Integer.parseInt(request.getParameter("paragraph_id")));
request.setAttribute("paragraph",paragraph);
request.setAttribute("choiceList",choiceList);
......@@ -137,5 +144,13 @@ public class LookupParagraph extends HttpServlet {
else System.out.println("mode: " + request.getParameter("mode"));
}
private boolean allReadbefore(List<Integer> required) {
if(required==null){
return true;
}
//TODO return true if all elements in required are in history
return true;
}
}
......@@ -194,8 +194,8 @@ public class LookupStory extends HttpServlet {
request.setAttribute("owner", owner);
request.setAttribute("action", "1");
//this.getServletContext().getRequestDispatcher("/LookupHistory").include(request, response);
//System.out.println(((HashMap<Integer, History>)session.getAttribute("history")).get(story.getId()).size());
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"));
request.setAttribute("publicStory", publicStory);
request.setAttribute("invitedList",invitedList);
......@@ -203,8 +203,8 @@ public class LookupStory extends HttpServlet {
this.getServletContext().getRequestDispatcher("/story.jsp").include(request, response);
//if (session.getAttribute("sotries") == null) session.setAttribute("stories", new HashSet<Integer>());
//((HashSet<Integer>)session.getAttribute("stories")).add(story.getId());
if (session.getAttribute("sotries") == null) session.setAttribute("stories", new HashSet<Integer>());
((HashSet<Integer>)session.getAttribute("stories")).add(story.getId());
}
/**
......
......@@ -8,7 +8,6 @@ package controleur;
import dao.ChoixDAO;
import dao.DAOException;
import dao.ParagraphDAO;
import dao.StoryDAO;
import java.io.IOException;
import java.io.PrintWriter;
......@@ -69,6 +68,9 @@ public class ManageParagraph extends HttpServlet {
case "7":
actionDeleteParagraph(request, response, new ParagraphDAO(ds));
break;
case "8":
actionSetReadBefore(request, response, new ParagraphDAO(ds));
break;
}}catch(Exception e) {
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
......@@ -77,6 +79,7 @@ public class ManageParagraph extends HttpServlet {
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
......@@ -231,6 +234,14 @@ public class ManageParagraph extends HttpServlet {
}
}
private void actionSetReadBefore(HttpServletRequest request, HttpServletResponse response, ParagraphDAO paragraphDAO) {
if (!isLegitim(request, paragraphDAO)) {
return; //TODO
}
int id = Integer.parseInt(request.getParameter("paragraph_id"));
int before_id = Integer.parseInt(request.getParameter("before_id"));
paragraphDAO.setReadBefore(id,before_id);
}
/**
* Deletes the given paragraph. We verify if the author is connected
......
......@@ -14,6 +14,7 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Set;
/**
*
......@@ -86,30 +87,39 @@ public class ChoixDAO extends AbstractDAO {
}
}
public boolean publishChoice(int choiceId) {
public boolean publishChoice(int choiceId, Set<Integer> history) {
boolean publish = false;
Paragraph paragraph= getChoiceParagraph(choiceId);
if(paragraph==null){
Paragraph paragraph= getChoiceParagraph(choiceId);
if(paragraph==null){
return false;
}
history.add(choiceId);
List<Integer> requires= new ParagraphDAO(dataSource).getReadBefore(paragraph.getId());
for (Integer req:requires){
if(!history.contains(req)){
return false;
}
if (paragraph.getIsConclusion()) {
}
if (paragraph.getIsConclusion()) {
publish = true;
publishChoiceDAO(choiceId);
System.out.println(choiceId+"is conclusion");
}
List<Choix> choices = new ParagraphDAO(dataSource).getChoicesParagraph(paragraph.getId());
for(Choix choix: choices){
if(publishChoice(choix.getId(),history)){
publish = true;
publishChoiceDAO(choiceId);
System.out.println(choiceId+"is conclusion");
}
List<Choix> choices = new ParagraphDAO(dataSource).getChoicesParagraph(paragraph.getId());
for(Choix choix: choices){
if(publishChoice(choix.getId())){
publish = true;
publishChoiceDAO(choiceId);
System.out.println(choiceId+"is published");
}
System.out.println(choiceId+"is published");
}
}
return publish;
return publish;
}
private void publishChoiceDAO(int choiceId) {
try (Connection conn = getConn()) {
PreparedStatement st = conn.prepareStatement(" UPDATE choix SET publie=1 WHERE id_choix=?");
......@@ -120,7 +130,7 @@ public class ChoixDAO extends AbstractDAO {
}
}
private Paragraph getChoiceParagraph(int choiceId){
public Paragraph getChoiceParagraph(int choiceId){
try (Connection conn = getConn()) {
PreparedStatement st = conn.prepareStatement("SELECT * FROM choix WHERE id_choix=? AND id_paragraphe_suite IS NOT NULL");
st.setInt(1, choiceId);
......@@ -136,4 +146,17 @@ public class ChoixDAO extends AbstractDAO {
}
}
public List<Integer> getReadBefore(int id) {
Paragraph paragraph = getChoiceParagraph(id);
if(paragraph==null){
return null;
}
List<Integer> readBefore= new ParagraphDAO(dataSource).getReadBefore(paragraph.getId());
if(readBefore.isEmpty()){
return null;
}
return readBefore;
}
}
\ No newline at end of file
......@@ -12,6 +12,7 @@ import javax.sql.DataSource;
import java.sql.*;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
/**
*
......@@ -319,4 +320,31 @@ public class ParagraphDAO extends AbstractDAO {
}
}
public void setReadBefore(int paragraphId, int beforeParagraphId){
try ( Connection conn = getConn()) {
PreparedStatement st = conn.prepareStatement("INSERT INTO alireavant (id_paragraphe, id_paragraphe_precedent) VALUES (?, ?)");
st.setInt(1, paragraphId);
st.setInt(2, beforeParagraphId);
st.executeQuery();
} catch (SQLException e) {
throw new DAOException(" set read before Database access error :( : " + e.getMessage(), e);
}
}
public List<Integer> getReadBefore(int paragraphId){
try ( Connection conn = getConn(); PreparedStatement st = conn.prepareStatement("SELECT * FROM alireavant WHERE id_paragraphe=?")) {
st.setInt(1, paragraphId);
ResultSet rs = st.executeQuery();
List<Integer> ll = new LinkedList<>();
while (rs.next()) {
ll.add(rs.getInt("id_paragraphe_precedent"));
}
return ll;
} catch (Exception e) {
throw new DAOException(" get read before Database access error :( : " + e.getMessage(), e);
}
}
}
......@@ -8,6 +8,7 @@ package dao;
import javax.sql.DataSource;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import modele.Choix;
......@@ -302,7 +303,7 @@ public class StoryDAO extends AbstractDAO {
boolean publish =false;
for (Choix choice :
choices) {
if(new ChoixDAO(dataSource).publishChoice(choice.getId())){
if(new ChoixDAO(dataSource).publishChoice(choice.getId(),new HashSet<Integer>())){
publish = true;
}
}
......
......@@ -5,6 +5,9 @@
*/
package modele;
import java.util.List;
import java.util.Set;
/**
*
* @author pangpangguy
......@@ -21,6 +24,20 @@ public class Choix {
return published;
}
public void setPublished(boolean published) {
this.published = published;
}
public List<Integer> getRequired() {
return required;
}
public void setRequired(List<Integer> required) {
this.required = required;
}
private List<Integer> required = null;
private boolean published;
public int getIdParagraphe() {
......
......@@ -28,6 +28,7 @@ function showLogin() {
y.style.display = "none";
}
}
function choose(sourceid, destinationid,story_id,mode,async){
if (typeof(mode)=='undefined'){
mode = mod;
......@@ -49,17 +50,19 @@ function choose(sourceid, destinationid,story_id,mode,async){
$("#subParagraph"+sourceid).empty().append(data.responseText.substring(1)) ;
console.log(data.responseText.substring(1));
if(mod==="edit") {
switchMode();
switchMode();
switchMode(false);
switchMode(false);
}
editParagraphe(destinationid,false, true);
document.getElementById("subParagraph"+sourceid).scrollIntoView({behavior:"smooth", block:"start",inline:"nearest"});
}else {
$("#subParagraph" + sourceid).empty().append(data.responseText);
console.log(data.responseText);
if (mod === "edit") {
switchMode();
switchMode();
switchMode(false);
switchMode(false);
}
document.getElementById("subParagraph"+sourceid).scrollIntoView({behavior:"smooth", block:"start",inline:"nearest"});
}
}
......@@ -86,7 +89,7 @@ function editParagraphe(paragraph_id,isConclusion,notvalid) {
`<br><input type="text" id="link${paragraph_id}">`+
`<button onclick="associate(${paragraph_id})" type="button">associer à ce paragraph existant</button>`+
`<br><input type="text" id="require${paragraph_id}">`+
`<button onclick="associate(${paragraph_id})" type="button">paragraphe requis</button>`
`<button onclick="setReadBefore(${paragraph_id})" type="button">paragraphe requis</button>`;
}
element.innerHTML = `<form method='post' action='index.jsp' id='modif${paragraph_id}'> ` +
`<textarea class=\"edit_paragraphe\" id=\"edit_Paragraphe${paragraph_id}\" rows=\"6\" cols=\"100\" placeholder='titre du choix' >`+oldText+`</textarea>` +
......@@ -167,8 +170,11 @@ function submit_edit_paragraphe(paragraph_id, save, notvalid) {
;
}
function switchMode(){
function switchMode(stop){
if(typeof(stop)=="undefined"){
stop = true;
}
if (mod==="read") {
mod="edit";
for (const element of document.getElementsByClassName("edit")) {
......@@ -177,6 +183,9 @@ function switchMode(){
document.getElementById("switch_mode").innerHTML = "<button>read_mode</button>";
}else{
mod="read";
if(stop) {
location.reload();
}
for (const element of document.getElementsByClassName("edit")) {
element.style.display = "None";
}
......@@ -190,7 +199,7 @@ function newParagraphe(paragraphToContinueID, choice_id){
let paragraphFollowerID = createParagraph(choice_id);
console.log(paragraphFollowerID);
if (paragraphFollowerID !== -1) {
document.getElementById(`choice${choice_id}`).setAttribute("onclick",`choose(${paragraphToContinueID},${paragraphFollowerID})`)
document.getElementById(`choice${choice_id}`).setAttribute("onclick",`choose(${paragraphToContinueID},${paragraphFollowerID},${story_id})`)
choose(paragraphToContinueID,paragraphFollowerID,story_id);
}
}
......@@ -245,7 +254,7 @@ function deleteParagraphe(id){
}
function associate(id){
//TODO BUG LAST LOCK NOT WORKING AFTER UPDATING PAGE
//TODO bug detected after association the new paragraphe isn't deleted and is forever locked
let follower = parseInt(document.getElementById(`link${id}`).value);
console.log(follower);
......@@ -276,5 +285,11 @@ function publish(story_id){
return console.error(errorThrown);
});
}
//TODO APRÉS ABONDON LE CHOIX NE FONCTIONNE PLUS
function setReadBefore(actualId){
let beforeId = parseInt(document.getElementById(`require${actualId}`).value);
$.post("ManageParagraph", {action: 8,paragraph_id: actualId,before_id: beforeId}).done(function (data) {
});
}
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